Date: 2026apr17
OS: Linux
Language: mixed
Q. Linux: Why doesn't the CRC from the `cksum` command match a regular CRC-32
A.
Summary
1. cksum uses a CRC algorithm that's become less common now.
2. cksum injects the file size into the CRC.
Details
1. cksum uses the same CRC as Ethernet.
That's in standard ISO/IEC 8802-3:1996 (Ethernet)
And cksum using it was codified in IEEE Std 1003.1-2008 (“POSIX.1”).
The CRC used by PNG, zlib etc is different.
cksum cannot be updated to the newer CRC by default because it has to match
older files with checksums from cksum.
You can use option:
cksum -a crc32b myfile.dat
To make it use the more familiar CRC (same as PNG, zlib, etc)
Standardized in ISO 3309 [ISO-3309] or ITU-T V.42 [ITU-V42].
https://www.w3.org/TR/PNG-CRCAppendix.html
But, be aware, cksum injects the file length into the CRC.
The PHP's hash() implements both these CRCs:
hash('crc32', ...) = same as cksum
hash('crc32b', ... ) = same as PNG
2. cksum adds the file size to the CRC at as the last step.
It keeps adding bytes until they are used up:
void UpdateWithFileSize(const int64_t totalFileSize) {
int64_t sizeVal = totalFileSize;
while (sizeVal != 0) {
BYTE buf[2];
buf[0] = sizeVal & 0xFF;
Update(buf, 1);
sizeVal >>= 8;
}
}