What kind of pits does PHP crc32 encounter? How do I fix it? This paper mainly introduces the problems needing attention when using PHP's CRC32 function. Hope to use CRC32 to help you.
A few days ago wrote a sub-table program, using the hash algorithm is CRC32. The functions of the table are as follows:
function _gethash ($username) { $hash = CRC32 ($username)%; return $hash; } function _gettable ($username) { $hash = Self::_gethash ($username); Return ' User_ '. $hash; }
First generate the good data on the local 32-bit window machine and insert the corresponding table. But then the program and data to the server (64 for Linux), found no data. After the investigation, found that the original server CRC32 results and local differences. Check the PHP manual again to know, CRC32 interface was originally related to the machine.
Description of the PHP manual:
Because PHP ' s integer type is signed many CRC32 checksums would result in negative integers on 32bit platforms. On 64bit installations all CRC32 () results'll be positive integers though.
The result returned by CRC32 will overflow on a 32-bit machine, so the result may be negative. The 64-bit machine does not overflow, so it is always positive.
The CRC algorithm is calculated by bit-length bits.
The CRC32 function is calculated as a reference to the two constants in PHP Php_int_size,php_int_max
The definitions of these two constants:
The word length of an integer is related to the platform, although the usual maximum value is approximately 2 billion (32-bit signed). PHP does not support unsigned integers. The length of the integer value can be expressed as a constant php_int_size, since PHP 4.4.0 and PHP 5.0.5, the maximum value can be expressed as a constant php_int_max.
Output under 32 bits in php_int_size:4,php_int_max:2147483647
Output under 64 bits in php_int_size:8,php_int_max:9223372036854775807
Related recommendations:
PHP computes a string of 32-bit CRC function CRC32 ()
CRC32, it's easy to clash.
How does the CRC32 function in PHP validate data