crc32
packagestandard libraryThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package crc32 implements the 32-bit cyclic redundancy check, or CRC-32,checksum. Seehttps://en.wikipedia.org/wiki/Cyclic_redundancy_check forinformation.
Polynomials are represented in LSB-first form also known as reversed representation.
Seehttps://en.wikipedia.org/wiki/Mathematics_of_cyclic_redundancy_checks#Reversed_representations_and_reciprocal_polynomialsfor information.
Index¶
Examples¶
Constants¶
const (// IEEE is by far and away the most common CRC-32 polynomial.// Used by ethernet (IEEE 802.3), v.42, fddi, gzip, zip, png, ...IEEE = 0xedb88320// Castagnoli's polynomial, used in iSCSI.// Has better error detection characteristics than IEEE.//https://dx.doi.org/10.1109/26.231911Castagnoli = 0x82f63b78// Koopman's polynomial.// Also has better error detection characteristics than IEEE.//https://dx.doi.org/10.1109/DSN.2002.1028931Koopman = 0xeb31d82e)
Predefined polynomials.
const Size = 4The size of a CRC-32 checksum in bytes.
Variables¶
var IEEETable = simpleMakeTable(IEEE)IEEETable is the table for theIEEE polynomial.
Functions¶
funcChecksum¶
Checksum returns the CRC-32 checksum of datausing the polynomial represented by theTable.
funcChecksumIEEE¶
ChecksumIEEE returns the CRC-32 checksum of datausing theIEEE polynomial.
funcNew¶
New creates a newhash.Hash32 computing the CRC-32 checksum using thepolynomial represented by theTable. Its Sum method will lay thevalue out in big-endian byte order. The returned Hash32 alsoimplementsencoding.BinaryMarshaler andencoding.BinaryUnmarshaler tomarshal and unmarshal the internal state of the hash.
funcNewIEEE¶
NewIEEE creates a newhash.Hash32 computing the CRC-32 checksum usingtheIEEE polynomial. Its Sum method will lay the value out inbig-endian byte order. The returned Hash32 also implementsencoding.BinaryMarshaler andencoding.BinaryUnmarshaler to marshaland unmarshal the internal state of the hash.
Types¶
typeTable¶
type Table [256]uint32
Table is a 256-word table representing the polynomial for efficient processing.
funcMakeTable¶
MakeTable returns aTable constructed from the specified polynomial.The contents of thisTable must not be modified.
Example¶
package mainimport ("fmt""hash/crc32")func main() {// In this package, the CRC polynomial is represented in reversed notation,// or LSB-first representation.//// LSB-first representation is a hexadecimal number with n bits, in which the// most significant bit represents the coefficient of x⁰ and the least significant// bit represents the coefficient of xⁿ⁻¹ (the coefficient for xⁿ is implicit).//// For example, CRC32-Q, as defined by the following polynomial,//x³²+ x³¹+ x²⁴+ x²²+ x¹⁶+ x¹⁴+ x⁸+ x⁷+ x⁵+ x³+ x¹+ x⁰// has the reversed notation 0b11010101100000101000001010000001, so the value// that should be passed to MakeTable is 0xD5828281.crc32q := crc32.MakeTable(0xD5828281)fmt.Printf("%08x\n", crc32.Checksum([]byte("Hello world"), crc32q))}Output:2964d064