5

I need to get CRC64 checksum of files using PHP.

With this code

file_put_contents('example.txt', 'just an example');

echo hash_file('crc32', 'example.txt');

I get CRC32 checksum "c8c429fe";

But I need to get a checksum using CRC64 algorithm (

enter image description here)

I took it from here: http://en.wikipedia.org/wiki/Cyclic_redundancy_check

How can I implement this hashing algorithm in PHP?

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
artask
  • 429
  • 7
  • 18

3 Answers3

7

Implementation crc64() on php 64bit

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);
}
Savvas Radevic
  • 543
  • 1
  • 8
  • 23
Morfi
  • 101
  • 1
  • 3
0

hash_file is simply a wrapper that takes result from file_get_contents($file) to a wrapper, so you can use any function instead of the 'crc32'.

Do you have to use crc64? If you just want to hash files, you'll have md5 and sha at your disposal, which can be used as easily as

$hash = hash_file("sha1", $file);

otherwise, just make your own crc64 implementation and

function crc64($string){
    // your code here
}

$hash = hash_file("crc64", $file);
nyson
  • 1,055
  • 6
  • 20
  • Thank you, @nyson! Yes, I have to use CRC64, but I can't find any implementations of this algorythm in PHP. – artask Apr 20 '12 at 12:06
0

True crc64 is not possible with PHP because PHP's int is internally defined as a signed long, meaning it is only capable of values between -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807, where crc64 returns an unsigned long. The answers here are somewhat misleading because of this. See http://php.net/crc32 suggested by @deceze in his comment, but understand that crc64 on 64 bit PHP builds suffers the same problem as crc32 does on 32 bit PHP builds.

Because of this problem, PHP will overflow during the first part of the algortithim (x64), potentially setting up the rest to overflow as well, when for example x4 normally wouldn't.

JSON
  • 1,819
  • 20
  • 27