note 83691 deleted from function.crc32 by danbrown
| From: | danbrown@php.net | Date: | Thu, 06 Oct 2011 21:25:37 +0000 |
| Subject: | note 83691 deleted from function.crc32 by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183681@lists.php.net to get a copy of this message | ||
Note Submitter: kobuki
----
I tried to use this function to calculate CRC32 values that are compatible with the CRCs in the well
known SFV files. Apparently, this doesn't work out of the box, since the crc32 hash algo
calculates a different value than found in SFV files (probably a different poly is used). There is
another hash algo supported by PHP though, crc32b, which is almost right, but needs some
transformation. The transformation involves a signed-unsigned conversion and a byte order (or
endian) swap. Below is my simple function for the task.
<?
function crc32_file($fileName)
{
$crc = hash_file("crc32b", $fileName);
$crc = sprintf("%08x", 0x100000000 + hexdec($crc));
return substr($crc, 6, 2) . substr($crc, 4, 2) . substr($crc, 2, 2) . substr($crc, 0, 2);
}
?>
As a side note:
<?
crc32(file_get_contents("some_filename.dat"));
?>
produces correct output without additional transformation. I wonder if this is a bug of PHP5 i
should try to report.