我需要使用PHP获得文件的CRC64校验和。
使用下面的代码
file_put_contents('example.txt', 'just an example');
echo hash_file('crc32', 'example.txt');我得到CRC32校验和"c8c429fe";
但是我需要使用CRC64算法来获得校验和(

)
我从这里接过它:http://en.wikipedia.org/wiki/Cyclic_redundancy_check
如何在PHP中实现这种散列算法?
发布于 2013-04-05 18:40:18
在php 64位上实现crc64()
https://www.php.net/manual/en/function.crc32.php#111699
<?php
/**
* @return array
*/
function crc64Table()
{
$crc64tab = [];
// ECMA polynomial
$poly64rev = (0xC96C5795 << 32) | 0xD7870F42;
// ISO polynomial
// $poly64rev = (0xD8 << 56);
for ($i = 0; $i < 256; $i++)
{
for ($part = $i, $bit = 0; $bit < 8; $bit++) {
if ($part & 1) {
$part = (($part >> 1) & ~(0x8 << 60)) ^ $poly64rev;
} else {
$part = ($part >> 1) & ~(0x8 << 60);
}
}
$crc64tab[$i] = $part;
}
return $crc64tab;
}
/**
* @param string $string
* @param string $format
* @return mixed
*
* Formats:
* crc64('php'); // afe4e823e7cef190
* crc64('php', '0x%x'); // 0xafe4e823e7cef190
* crc64('php', '0x%X'); // 0xAFE4E823E7CEF190
* crc64('php', '%d'); // -5772233581471534704 signed int
* crc64('php', '%u'); // 12674510492238016912 unsigned int
*/
function crc64($string, $format = '%x')
{
static $crc64tab;
if ($crc64tab === null) {
$crc64tab = crc64Table();
}
$crc = 0;
for ($i = 0; $i < strlen($string); $i++) {
$crc = $crc64tab[($crc ^ ord($string[$i])) & 0xff] ^ (($crc >> 8) & ~(0xff << 56));
}
return sprintf($format, $crc);
}发布于 2012-04-20 19:52:33
hash_file只是一个简单的包装器,它将file_get_contents($file)的结果放到包装器中,所以你可以使用任何函数来代替'crc32‘。
你必须使用crc64吗?如果您只想对文件进行散列处理,那么您可以使用md5和sha,它们可以像
$hash = hash_file("sha1", $file);否则,只需创建您自己的crc64实现并
function crc64($string){
// your code here
}
$hash = hash_file("crc64", $file);https://stackoverflow.com/questions/10245575
复制相似问题