Hướng dẫn dùng crc32 calculator 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) 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) crc32 — Calculates the crc32 polynomial of a string Descriptioncrc32(string 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(). Parametersstring The data. Return Values Returns the crc32 checksum of ExamplesExample #1 Displaying a crc32 checksum This example shows how to print a converted checksum with the printf() function:
See Also
jian at theorchard dot com ¶ 12 years ago
JS at JavsSys dot Org ¶ 9 years ago
i at morfi dot ru ¶ 9 years ago
$crc64tab; Bulk at bulksplace dot com ¶ 17 years ago
sprintf("%u", $crc);
Clifford dot ct at gmail dot com ¶ 10 years ago
slimshady451 ¶ 15 years ago
same ¶ 18 years ago
dave at jufer dot info ¶ 14 years ago
alban dot lopez+php [ at ] gmail dot com ¶ 10 years ago
genCRC (&$ptr) arris at zsolttech dot com ¶ 10 years ago
$CRCTable = array(); quix at free dot fr ¶ 19 years ago
__crc32_init_table() { // Builds lookup table array spectrumizer at cycos dot net ¶ 19 years ago
Ren ¶ 14 years ago
arachnid at notdot dot net ¶ 18 years ago
sukitsupaluk at hotmail dot com ¶ 11 years ago
?>
roberto at spadim dot com dot br ¶ 16 years ago
chernyshevsky at hotmail dot com ¶ 11 years ago
$vec) {
berna (at) gensis (dot) com (dot) br ¶ 12 years ago
mail at tristansmis dot nl ¶ 15 years ago
= abs(crc32($string)); toggio at writeme dot com ¶ 6 years ago
gabri dot ns at gmail dot com ¶ 11 years ago
$CRC32Table, $Reflect8Table; dotg at mail dot ru ¶ 5 years ago
waldomonster at netjukebox dot demon dot nl ¶ 18 years ago
|