note 107919 modified in function.crc32 by sp

From: Date: Thu, 15 Mar 2012 10:22:24 +0000
Subject: note 107919 modified in function.crc32 by sp
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-186681@lists.php.net to get a copy of this message
On two different servers I found that crc32() relates to hash(\&#039;crc32b\&#039;,) This may be good to know if you are writing a crc32_file function based on hash_file. (The example does not compensate for negative crc32 results) &lt;?php $val = \&#039;hello\&#039;; var_dump(crc32($val) == ( \&#039;0x\&#039; . hash(\&#039;crc32b\&#039;, $val) ) ); // bool(true) var_dump(crc32($val) == ( \&#039;0x\&#039; . hash(\&#039;crc32\&#039;, $val) ) ); // bool(false) ?&gt; Also if you are looking for a way to reduce collisions and still keep the hash result small (smaller than say md5) you could get a nice database friendly 64 bit value by using hash/crc32 and hash/crc32b, which is slower than a single md5 but the result may be more suitable for certain tasks. &lt;?php $val = \&#039;hello\&#039;; $crc64 = ( \&#039;0x\&#039; . hash(\&#039;crc32\&#039;, $val) . hash(\&#039;crc32b\&#039;, $val) ); var_dump($crc64); // string(18) \&quot;0x3d6531193610a686\&quot; var_dump($crc64 + 0); // int(4423996193312384646) ?&gt; note: (\&#039;0x\&#039; . $hex . + 0) is faster than base_convert($hex,16,10) --was-- On two different servers I found that crc32() relates to hash('crc32b',) This may be good to know if you are writing a crc32_file function based on hash_file. (The example does not compensate for negative crc32 results) <?php $val = 'hello'; var_dump(crc32($val) == ( '0x' . hash('crc32b', $val) ) ); // bool(true) var_dump(crc32($val) == ( '0x' . hash('crc32', $val) ) ); // bool(false) ?> Also if you are looking for a way to reduce collisions and still keep the hash result small (smaller than say md5) you could get a nice database friendly 64 bit value by using hash/crc32 and hash/crc32b, which is slower than a single md5 but the result may be more suitable for certain tasks. <?php $val = 'hello'; $crc64 = ( '0x' . hash('crc32', $val) . hash('crc32b', $val) ) ); var_dump($crc64); // string(18) "0x3d6531193610a686" var_dump($crc64 + 0); // int(4423996193312384646) ?> note: ('0x' . $hex . + 0) is faster than base_convert($hex,16,10) http://php.net/manual/en/function.crc32.php

« previous php.notes (#186681) next »