crc32
crc32(string $string): int
The CRC-32 checksum (IEEE 802.3 polynomial 0xEDB88320) — checksums detect transmission corruption, not tampering, a different tool from the md5/sha cryptographic hash family; languages without a built-in implement the classic table-driven algorithm.
C C23
#include <stdint.h>
#include <stdio.h>
#include <string.h>
/* classic table-driven CRC-32 (IEEE 802.3, polynomial 0xEDB88320) */
static uint32_t table[256];
static void make_table(void) {
for (uint32_t i = 0; i < 256; i++) {
uint32_t crc = i;
for (int k = 0; k < 8; k++)
crc = (crc & 1) ? 0xEDB88320u ^ (crc >> 1) : crc >> 1;
table[i] = crc;
}
}
static uint32_t crc32_of(const unsigned char *s, size_t n) {
uint32_t crc = 0xFFFFFFFFu;
for (size_t i = 0; i < n; i++)
crc = (crc >> 8) ^ table[(crc ^ s[i]) & 0xFFu];
return crc ^ 0xFFFFFFFFu;
}
int main(void) {
make_table();
const char *fox = "The quick brown fox jumps over the lazy dog";
uint32_t c1 = crc32_of((const unsigned char *)fox, strlen(fox));
uint32_t c2 = crc32_of((const unsigned char *)"hello", 5);
printf("%08x %u\n", c1, (unsigned)c1);
printf("%08x %u\n", c2, (unsigned)c2);
return 0;
}
414fa339 1095738169 3610a686 907060870
C++ C++26
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>
// classic table-driven CRC-32 (IEEE 802.3, polynomial 0xEDB88320)
static std::vector<uint32_t> make_table() {
std::vector<uint32_t> table(256);
for (uint32_t i = 0; i < 256; i++) {
uint32_t crc = i;
for (int k = 0; k < 8; k++)
crc = (crc & 1) ? 0xEDB88320u ^ (crc >> 1) : crc >> 1;
table[i] = crc;
}
return table;
}
int main() {
const auto table = make_table();
auto crc32_of = [&table](const std::string &s) {
uint32_t crc = 0xFFFFFFFFu;
for (unsigned char b : s)
crc = (crc >> 8) ^ table[(crc ^ b) & 0xFFu];
return crc ^ 0xFFFFFFFFu;
};
uint32_t c1 = crc32_of("The quick brown fox jumps over the lazy dog");
uint32_t c2 = crc32_of("hello");
std::cout << std::hex << c1 << " " << std::dec << c1 << "\n";
std::cout << std::hex << c2 << " " << std::dec << c2 << "\n";
return 0;
}
414fa339 1095738169 3610a686 907060870
C# v11.0.100
// hand-rolled table-driven CRC-32 (System.IO.Hashing offers Crc32 via NuGet)
static uint[] MakeTable() {
var table = new uint[256];
for (uint i = 0; i < 256; i++) {
uint crc = i;
for (int k = 0; k < 8; k++)
crc = (crc & 1) != 0 ? 0xEDB88320u ^ (crc >> 1) : crc >> 1;
table[i] = crc;
}
return table;
}
static uint Crc32(byte[] data) {
var table = MakeTable();
uint crc = 0xFFFFFFFFu;
foreach (byte b in data)
crc = (crc >> 8) ^ table[(crc ^ b) & 0xFFu];
return crc ^ 0xFFFFFFFFu;
}
var c1 = Crc32(System.Text.Encoding.UTF8.GetBytes("The quick brown fox jumps over the lazy dog"));
var c2 = Crc32(System.Text.Encoding.UTF8.GetBytes("hello"));
Console.WriteLine($"{c1:x8} {c1}");
Console.WriteLine($"{c2:x8} {c2}");
414fa339 1095738169 3610a686 907060870
Go v1.27.4
package main
import (
"fmt"
"hash/crc32"
)
func main() {
// stdlib hash/crc32: IEEE is exactly the CRC-32 of PHP's crc32()
c1 := crc32.ChecksumIEEE([]byte("The quick brown fox jumps over the lazy dog"))
c2 := crc32.ChecksumIEEE([]byte("hello"))
fmt.Printf("%08x %d\n", c1, c1)
fmt.Printf("%08x %d\n", c2, c2)
}
414fa339 1095738169 3610a686 907060870
Java v27.0.1
import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;
public class Main {
public static void main(String[] args) {
// java.util.zip.CRC32: getValue() returns the unsigned 32-bit value
CRC32 c = new CRC32();
c.update("The quick brown fox jumps over the lazy dog".getBytes(StandardCharsets.UTF_8));
long v1 = c.getValue();
c.reset();
c.update("hello".getBytes(StandardCharsets.UTF_8));
long v2 = c.getValue();
System.out.printf("%08x %d%n", v1, v1);
System.out.printf("%08x %d%n", v2, v2);
}
}
414fa339 1095738169 3610a686 907060870
JavaScript v26.13.1
// hand-rolled table-driven CRC-32 (Node has no built-in crc32)
function makeTable() {
const table = new Uint32Array(256);
for (let i = 0; i < 256; i++) {
let c = i;
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
table[i] = c;
}
return table;
}
function crc32(data) {
const table = makeTable();
let crc = 0xffffffff;
for (const b of data) crc = (crc >>> 8) ^ table[(crc ^ b) & 0xff];
return (crc ^ 0xffffffff) >>> 0; // >>> 0 keeps it unsigned
}
const c1 = crc32(Buffer.from("The quick brown fox jumps over the lazy dog"));
const c2 = crc32(Buffer.from("hello"));
console.log(c1.toString(16).padStart(8, "0"), c1);
console.log(c2.toString(16).padStart(8, "0"), c2);
414fa339 1095738169 3610a686 907060870
PHP v8.6.0
// checksum, not crypto: fast, fixed 32 bits, detects corruption not tampering
$s = "The quick brown fox jumps over the lazy dog";
printf("%08x %d\n", crc32($s), crc32($s));
printf("%08x %d\n", crc32("hello"), crc32("hello"));
// md5 is a cryptographic hash: the right tool for attacker-resistant integrity
echo md5("hello"), "\n";
414fa339 1095738169 3610a686 907060870 5d41402abc4b2a76b9719d911017c592
Python v3.15.2
# zlib.crc32 is the direct counterpart; mask keeps it unsigned on all platforms
import zlib
s = b"The quick brown fox jumps over the lazy dog"
c1 = zlib.crc32(s) & 0xFFFFFFFF
c2 = zlib.crc32(b"hello") & 0xFFFFFFFF
print(f"{c1:08x} {c1}")
print(f"{c2:08x} {c2}")
# hashlib.md5 is the cryptographic counterpart in the same stdlib
import hashlib
print(hashlib.md5(b"hello").hexdigest())
414fa339 1095738169 3610a686 907060870 5d41402abc4b2a76b9719d911017c592
Rust v1.99.1
// hand-rolled table-driven CRC-32 (the crc32fast crate wraps the same algorithm)
fn make_table() -> [u32; 256] {
let mut table = [0u32; 256];
for (i, slot) in table.iter_mut().enumerate() {
let mut crc = i as u32;
for _ in 0..8 {
crc = if crc & 1 != 0 { 0xEDB88320 ^ (crc >> 1) } else { crc >> 1 };
}
*slot = crc;
}
table
}
fn crc32_of(data: &[u8]) -> u32 {
let table = make_table();
let mut crc: u32 = 0xFFFF_FFFF;
for &b in data {
crc = (crc >> 8) ^ table[((crc ^ b as u32) & 0xFF) as usize];
}
crc ^ 0xFFFF_FFFF
}
fn main() {
let c1 = crc32_of(b"The quick brown fox jumps over the lazy dog");
let c2 = crc32_of(b"hello");
println!("{:08x} {}", c1, c1);
println!("{:08x} {}", c2, c2);
}
414fa339 1095738169 3610a686 907060870
TypeScript v26.13.1
// hand-rolled table-driven CRC-32 (Node has no built-in crc32)
function makeTable(): Uint32Array {
const table = new Uint32Array(256);
for (let i = 0; i < 256; i++) {
let c = i;
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
table[i] = c;
}
return table;
}
function crc32(data: Buffer): number {
const table = makeTable();
let crc = 0xffffffff;
for (const b of data) crc = (crc >>> 8) ^ table[(crc ^ b) & 0xff];
return (crc ^ 0xffffffff) >>> 0; // >>> 0 keeps it unsigned
}
const c1 = crc32(Buffer.from("The quick brown fox jumps over the lazy dog"));
const c2 = crc32(Buffer.from("hello"));
console.log(c1.toString(16).padStart(8, "0"), c1);
console.log(c2.toString(16).padStart(8, "0"), c2);
414fa339 1095738169 3610a686 907060870
Language differences
C: the classic table-driven algorithm (example) — the reflected polynomial 0xEDB88320 builds a 256-entry table with 0xFFFFFFFF as init/final xor; at the system level zlib's crc32() is available with the same interface.
C++: a lambda capturing the table plus a per-byte loop (example); in production link zlib or use Boost.CRC.
C#: hand-written table (example); the System.IO.Hashing NuGet package offers Crc32 (an official .NET 8+ package); the BCL itself does not include one.
Go: the stdlib hash/crc32 ChecksumIEEE is the exact same CRC-32 as PHP's crc32 (example); crc32.MakeTable pre-builds the table for reuse.
Java: java.util.zip.CRC32 is the direct counterpart (example); getValue() returns a long carrying the unsigned 32-bit value, never negative.
JavaScript: Node has no built-in crc32 — hand-write the table (example); >>> 0 is the key step that turns the bitwise result into an unsigned 32-bit number.
PHP: crc32 returns int — always non-negative on 64-bit platforms but possibly negative on 32-bit ones (normalize with sprintf("%u") for cross-platform work); the essential division of labor versus md5/sha: CRC-32 only detects random corruption (error detection), cryptographic hashes resist deliberate tampering, and the two are not interchangeable.
Python: zlib.crc32 is the direct counterpart (example); the & 0xFFFFFFFF mask keeps the unsigned semantics on signed platforms; hashlib in the same stdlib provides the cryptographic family.
Rust: hand-written table (example); the crc32fast crate is the production standard (SIMD-accelerated, same algorithm); the standard library has no built-in.
TypeScript: same hand-written table as JavaScript (example); no impact at the type layer.