Home > Backend Development > PHP Tutorial > PHP crc32 pitfall solution

PHP crc32 pitfall solution

*文
Release: 2023-03-18 15:24:01
Original
4848 people have browsed it

What pitfalls will php's crc32 encounter? How to solve it? This article mainly introduces the issues that need to be paid attention to when using PHP's crc32 function. I hope it will be helpful to everyone using crc32.

I wrote a table splitting program a few days ago. The hash algorithm used is crc32. The table splitting function is as follows:

function _getHash($username)
    {
        $hash = crc32($username) % 512;
        return $hash;
    }

    function _getTable($username)
    {
        $hash = self::_getHash($username);
        return 'user_' . $hash;
    }
Copy after login



First of all, Generate the data on the local 32-bit window machine and insert it into the corresponding table. But when I transferred the program and data to the server (64 for Linux), I found that the data could not be found. After investigation, it was found that the crc32 result on the server was different from the local one. After checking the PHP manual again, I found out that the crc32 interface is related to the machine.
php manual description:

Because PHP's integer type is signed many crc32 checksums will result in negative integers on 32bit platforms. On 64bit installations all crc32() results will be positive integers though.
Copy after login


The result returned by crc32 will overflow on a 32-bit machine, so the result may be a negative number. On a 64-bit machine, there is no overflow, so it is always positive.

The CRC algorithm is calculated based on the number of bits in the word length.

The crc32 function will calculate PHP_INT_SIZE and PHP_INT_MAX according to the two constant references in php
The definition of these two constants:
The word length of the integer number is platform-dependent, although the maximum value is usually approximately Two billion (32-bit signed). PHP does not support unsigned integers. The word length of an Integer value can be represented by the constant PHP_INT_SIZE. Since PHP 4.4.0 and PHP 5.0.5, the maximum value can be represented by the constant PHP_INT_MAX.
Output PHP_INT_SIZE: 4, PHP_INT_MAX: 2147483647 in the next 32 bits
PHP_INT_SIZE: 8, PHP_INT_MAX: 9223372036854775807 in the next 64 bits output


Related recommendations :

php function crc32() to calculate the 32-bit CRC of a string

##CRC32, It is easy to conflict. How to deal with it?

##How to use the crc32 function in PHP to verify data

The above is the detailed content of PHP crc32 pitfall solution. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template