A pure Go implementation of CRC32A (ITU I.363.5) checksum missing from Golang standard library. Largely based on C implementation found in PHP source files.
package main import ( "fmt" "github.com/boguslaw-wojcik/crc32a" ) func main() { b := []byte("123456789") sum := crc32a.Checksum(b) sumHex := crc32a.ChecksumHex(b) fmt.Println(sum) // 404326908 fmt.Println(sumHex) // 181989fc }
There are four popular implementations of CRC32 of which three are included in Golang standard library. This package provides missing implementation of CRC32A (ITU I.363.5) popularized by bzip2 and PHP. If you're unsure what CRC32 algorithm you're using, calculate checksum for string 123456789
and compare results with the table below.
Variant | Performance | Standard Library | Data | Hex | Sum |
---|---|---|---|---|---|
CRC32A (ITU I.363.5) | 31.4 ns/op | no | 123456789 | 181989fc | 404326908 |
CRC32B (ITU V.42 / IEEE 802.3) | 24.0 ns/op | yes | 123456789 | cbf43926 | 3421780262 |
CRC32C (Castagnoli) | 15.2 ns/op | yes | 123456789 | e3069283 | 3808858755 |
CRC32K (Koopman) | 4534 ns/op | yes | 123456789 | 2d3dd0ae | 759025838 |
Note: Unless you're required to use CRC32A for legacy reasons, in general you should choose CRC32B as the most popular and widely implemented CRC32 variation.