Skip to content

Commit

Permalink
Merge pull request #406 from siad007/FileHashImprovemends
Browse files Browse the repository at this point in the history
Added optional algorithm attribute to FileHashTask
  • Loading branch information
mrook committed Feb 16, 2015
2 parents 3d9c5e9 + dbeab3b commit 517b2b2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
45 changes: 32 additions & 13 deletions classes/phing/tasks/ext/FileHashTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class FileHashTask extends Task
*/
private $hashtype = 0;

private $algorithm = '';

/**
* Specify if MD5 or SHA1 hash should be used
* @param integer $type 0=MD5, 1=SHA1
Expand All @@ -62,6 +64,11 @@ public function setHashtype($type)
$this->hashtype = $type;
}

public function setAlgorithm($type)
{
$this->algorithm = strtolower($type);
}

/**
* Which file to calculate the hash value of
* @param PhingFile $file
Expand Down Expand Up @@ -93,24 +100,35 @@ public function main()
$this->checkPropertyName();

// read file
if ((int) $this->hashtype === 0) {
if ($this->algorithm !== '' && in_array($this->algorithm, hash_algos())) {
$this->log("Calculating $this->algorithm hash from: " . $this->file);
$hashValue = hash_file($this->algorithm, $this->file);
} elseif ((int) $this->hashtype === 0) {
$this->log("Calculating MD5 hash from: " . $this->file);
$hashValue = md5_file($this->file, false);
} elseif ((int) $this->hashtype === 1) {
$this->log("Calculating SHA1 hash from: " . $this->file);
$hashValue = sha1_file($this->file, false);
} else {
throw new BuildException(
sprintf(
'[FileHash] Unknown hashtype specified %d. Must be either 0 (=MD5) or 1 (=SHA1).',
$this->hashtype
));

if ($this->algorithm !== '') {
throw new BuildException(
sprintf(
'[FileHash] Unknown algorithm specified %d. Must be one of %s',
$this->algorithm,
implode(', ', hash_algos())
)
);
} else {
throw new BuildException(
sprintf(
'[FileHash] Unknown hashtype specified %d. Must be either 0 (=MD5) or 1 (=SHA1)',
$this->hashtype
));
}
}

// publish hash value
$this->project->setProperty($this->propertyName, $hashValue);

}

/**
Expand All @@ -128,12 +146,13 @@ private function checkFile()
}

if (!is_readable($this->file)) {
throw new BuildException(sprintf(
'[FileHash] Input file does not exist or is not readable: %s',
$this->file
));
throw new BuildException(
sprintf(
'[FileHash] Input file does not exist or is not readable: %s',
$this->file
)
);
}

}

/**
Expand Down
11 changes: 11 additions & 0 deletions docs/docbook5/en/source/appendixes/optionaltasks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,8 @@
<title>FileHashTask</title>
<para>Calculates either MD5 or SHA1 hash value of a file and stores the value as a hex
string in a property.</para>
<para>Other popular <link xlink:href="http://php.net/manual/en/function.hash-algos.php">algorithms</link>
like "crc32" or "sha512" may be used with help of the <literal>algorithm</literal> attribute.</para>
<table>
<title>Attributes</title>
<tgroup cols="5">
Expand Down Expand Up @@ -939,6 +941,15 @@
<entry>0</entry>
<entry>No</entry>
</row>
<row>
<entry><literal>algorithm</literal></entry>
<entry><literal role="type">String</literal></entry>
<entry>Specifies what hash algorithm to use. Supported
<link xlink:href="http://php.net/manual/en/function.hash-algos.php">algorithms</link>.
</entry>
<entry>n/a</entry>
<entry>No</entry>
</row>
<row>
<entry><literal>propertyname</literal></entry>
<entry><literal role="type">String</literal></entry>
Expand Down
5 changes: 5 additions & 0 deletions test/classes/phing/tasks/ext/FileHashTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public function testSHA1()
{
$this->expectLog("testSHA1", "dadd0aafb79d9fb8299a928efb23c112874bbda3");
}

public function testCRC32()
{
$this->expectLog("testCRC32", "d34c2e86");
}
}
8 changes: 7 additions & 1 deletion test/etc/tasks/ext/filehash.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
hashtype="1"/>
<echo>${filehash.sha1}</echo>
</target>

<target name="testCRC32">
<filehash file="filehash.bin" propertyName="filehash.crc32"
algorithm="crc32"/>
<echo>${filehash.crc32}</echo>
</target>

<target name="build" depends="testMD5,testSHA1"/>
<target name="build" depends="testMD5,testSHA1,testCRC32"/>
</project>

0 comments on commit 517b2b2

Please sign in to comment.