crc32 Checksums Chain Across Chunks

Compute CRC-32 checksums over byte payloads and chain chunked calls by passing the previous value back in, so streamed bytes hash identically to the whole.

PythonData Integrity

Code

pythonsnippet
15 lines
import zlib

# crc32 对字节块计算循环冗余校验;相同内容必得相同校验值
payload = b"order:42|amount:1999"
crc = zlib.crc32(payload)
print("crc32:", crc, "| hex:", f"{crc:08x}")

# 分块计算:把上一步的校验值作为起始值传入,结果与整块一致
head, tail = payload[:10], payload[10:]
chained = zlib.crc32(tail, zlib.crc32(head))
print("chained == whole:", chained == crc)

# 内容变一位,校验值跟着变:用于传输/落盘后的完整性比对
tampered = payload.replace(b"1999", b"2999")
print("tampered differs:", zlib.crc32(tampered) != crc)

Output

Captured from a sandboxed run of this snippet in the build pipeline.

crc32: 2084800057 | hex: 7c438639
chained == whole: True
tampered differs: True

How the code behaves

Top-level bindings hold everything: payload, crc, chained, tampered.

Watch out for

  • No package manager is involved; each call resolves inside the language.

Putting it to use

Each run prints the 3 labeled blocks crc32:, chained == whole:, tampered differs: in source order.

Interactive Tools for This Topic

FAQ

Does it depend on any third-party library?
No third-party library is referenced; the standard library covers it. Its state lives in module-level bindings: `payload`, `crc`, `chained`.