实现说明
CRC32 校验和与增量 Update以函数 main为骨架。
使用要点
- 跑完文件,输出里会留下标记——
checksum:、intact:、tampered:。
常见问题
运行「CRC32 校验和与增量 Update」会打印什么?
整个文件跑完,输出行以 checksum:、intact:、tampered: 这类前缀开头。
核心语句
rolling = crc32.Update(rolling, table, []byte("amount:1990"))
代码示例
package main
import (
"fmt"
"hash/crc32"
)
func main() {
// IEEE is the default polynomial for zlib, Ethernet frames, and similar formats
table := crc32.MakeTable(crc32.IEEE)
original := []byte("order:42|amount:1990")
checksum := crc32.Checksum(original, table)
fmt.Printf("checksum: %08x\n", checksum)
// Content unchanged after transfer: checksums match
received := []byte("order:42|amount:1990")
fmt.Println("intact:", crc32.Checksum(received, table) == checksum)
// Last digit tampered: checksum mismatch
tampered := []byte("order:42|amount:1999")
fmt.Println("tampered:", crc32.Checksum(tampered, table) == checksum)
// Update rolls incremental data into the existing checksum: chunked equals whole-buffer
rolling := crc32.Update(0, table, []byte("order:42|"))
rolling = crc32.Update(rolling, table, []byte("amount:1990"))
fmt.Println("streamed matches:", rolling == checksum)
}