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.
Code
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: TrueHow 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
ISBN ValidatorValidate ISBN-10 and ISBN-13 identifiers with checksum verification, hyphen-tolerant input, and conversion between the two forms.Luhn Checksum ValidatorValidate numeric identifiers with the Luhn algorithm and see the checksum sum and suggested final digit on failure.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
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`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.