Hướng dẫn dùng crc hash trong PHP

Ví dụ và cách sử dụng hàm crc32 trong PHP để tính CRC có độ dài 32-bit của một chuỗi.

in Hàm xử lý chuỗi trong PHP

[PHP 4 >= 4.0.1, PHP 5]
crc32 – Tính CRC 32-bit của một chuỗi

Miêu tả hàm crc32

CRC32 [cyclic redundancy check 32-bit] là một loại hàm băm, được dùng để sinh ra giá trị kiểm thử có độ dài 32-bit của một chuỗi. Hàm này thông thường được dùng để xác nhận tính toàn vẹn của dữ liệu được truyền đi.

Cảnh báo: Kiểu số nguyên của PHP trên nền tảng 32-bit có thể dẫn đến kết quả là số nguyên âm khi sử dụng hàm crc32. Trên nền tảng 64bit hàm crc32[] sẽ cho kết quả là số nguyên dương.

Vì vậy bạn cần sử dụng định dạng “%u”  khi dùng hàm sprintf[] hoặc printf[] để in ra kết quả crc32[] là dương và có định dạng thập phân.

Để hiển thị kết quả dưới dạng thập lục phân [hệ 16] bạn có thể sử dụng định dạng “%x” khi dùng hàm sprintf[] hoặc printf[] nhưng cũng có thể dùng hàm dechex[], cả 2 cách đều có tác dụng hiển thị kết quả của hàm crc32[] thành số nguyên dương.

Trên nền tảng 64bit cũng có thể trả lại kết quả là số nguyên âm với các giá trị kết quả cao, phá vỡ quá trình chuyển đổi sang hệ 16 với phần âm thêm hậu tố 0xFFFFFFFF########. Với hệ 16 dường như hầu hết các trường hợp phổ biến nhất chúng tôi quyết định không phá vỡ quy trình này ngay cả khi nó phá vỡ phép so sánh nhị phân trực tiếp trong khoảng 50% số trường hợp khi chuyển từ 32 sang 64bits. Trong quá khứ, hàm trả về một số nguyên có thể không phải là ý tưởng tốt nhất và việc trả về một chuỗi hệ 6 [hex] ngay lập tức [ví dụ hàm md5[]] sẽ có thể là một ý tưởng tốt hơn. Với một giải pháp linh động hơn bạn cũng có thể xem tới hàm hash[]. hash[“crc32b”, $str] sẽ trả về chuỗi tương tự như dechex[crc32[$str]].

Tham số

string

Dữ liệu cần tính.

Giá trị trả về

Trả về chuỗi kiểm tra crc32 là một số nguyên.

Ví dụ

Ví dụ 1 về hàm crc32[]

Ví dụ dưới đây sẽ cho thấy cách in ra kết quả kiểm tra bằng hàm printf[] :

[PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8]

crc32Calculates the crc32 polynomial of a string

Description

crc32[string $string]: int

Warning

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.

So you need to use the "%u" formatter of sprintf[] or printf[] to get the string representation of the unsigned crc32[] checksum in decimal format.

For a hexadecimal representation of the checksum you can either use the "%x" formatter of sprintf[] or printf[] or the dechex[] conversion functions, both of these also take care of converting the crc32[] result to an unsigned integer.

Having 64bit installations also return negative integers for higher result values was considered but would break the hexadecimal conversion as negatives would get an extra 0xFFFFFFFF######## offset then. As hexadecimal representation seems to be the most common use case we decided to not break this even if it breaks direct decimal comparisons in about 50% of the cases when moving from 32 to 64bits.

In retrospect having the function return an integer maybe wasn't the best idea and returning a hex string representation right away [as e.g. md5[] does] might have been a better plan to begin with.

For a more portable solution you may also consider the generic hash[]. hash["crc32b", $str] will return the same string as str_pad[dechex[crc32[$str]], 8, '0', STR_PAD_LEFT].

Parameters

string

The data.

Return Values

Returns the crc32 checksum of string as an integer.

Examples

Example #1 Displaying a crc32 checksum

This example shows how to print a converted checksum with the printf[] function:

See Also

  • hash[] - Generate a hash value [message digest]
  • md5[] - Calculate the md5 hash of a string
  • sha1[] - Calculate the sha1 hash of a string

jian at theorchard dot com

12 years ago

This function returns an unsigned integer from a 64-bit Linux platform.  It does return the signed integer from other 32-bit platforms even a 64-bit Windows one.

The reason is because the two constants PHP_INT_SIZE and PHP_INT_MAX have different values on the 64-bit Linux platform.

I've created a work-around function to handle this situation.



Hope this helps.

JS at JavsSys dot Org

9 years ago

The khash[] function by sukitsupaluk has two problems, it does not use all 62 characters from the $map set and when corrected it then produces different results on 64-bit compared to 32-bit PHP systems.

Here is my modified version :

Bài Viết Liên Quan

Chủ Đề