version: 1.10

package crc32

import "hash/crc32"

Overview

Package crc32 implements the 32-bit cyclic redundancy check, or CRC-32,
checksum. See http://en.wikipedia.org/wiki/Cyclic_redundancy_check for
information.

Polynomials are represented in LSB-first form also known as reversed
representation.

Index

Examples

Package files

crc32.go crc32_amd64.go crc32_generic.go

Constants

  1. const (
  2. // IEEE is by far and away the most common CRC-32 polynomial.
  3. // Used by ethernet (IEEE 802.3), v.42, fddi, gzip, zip, png, ...
  4. IEEE = 0xedb88320
  5.  
  6. // Castagnoli's polynomial, used in iSCSI.
  7. // Has better error detection characteristics than IEEE.
  8. // http://dx.doi.org/10.1109/26.231911
  9. Castagnoli = 0x82f63b78
  10.  
  11. // Koopman's polynomial.
  12. // Also has better error detection characteristics than IEEE.
  13. // http://dx.doi.org/10.1109/DSN.2002.1028931
  14. Koopman = 0xeb31d82e
  15. )

Predefined polynomials.

  1. const Size = 4

The size of a CRC-32 checksum in bytes.

Variables

IEEETable is the table for the IEEE polynomial.

func Checksum

Checksum returns the CRC-32 checksum of data using the polynomial represented by
the Table.

ChecksumIEEE returns the CRC-32 checksum of data using the IEEE polynomial.

func New

New creates a new hash.Hash32 computing the CRC-32 checksum using the polynomial
represented by the Table. Its Sum method will lay the value out in big-endian
byte order. The returned Hash32 also implements encoding.BinaryMarshaler and
encoding.BinaryUnmarshaler to marshal and unmarshal the internal state of the
hash.

func NewIEEE

NewIEEE creates a new hash.Hash32 computing the CRC-32 checksum using the IEEE
polynomial. Its Sum method will lay the value out in big-endian byte order. The
returned Hash32 also implements encoding.BinaryMarshaler and
encoding.BinaryUnmarshaler to marshal and unmarshal the internal state of the
hash.

func Update

Update returns the result of adding the bytes in p to the crc.

type Table

Table is a 256-word table representing the polynomial for efficient processing.

func MakeTable

MakeTable returns a Table constructed from the specified polynomial. The
contents of this Table must not be modified.


Example:

  1. // In this package, the CRC polynomial is represented in reversed notation,
  2. // or LSB-first representation.
  3. //
  4. // LSB-first representation is a hexadecimal number with n bits, in which the
  5. // most significant bit represents the coefficient of x⁰ and the least significant
  6. // bit represents the coefficient of xⁿ⁻¹ (the coefficient for xⁿ is implicit).
  7. //
  8. // For example, CRC32-Q, as defined by the following polynomial,
  9. // x³²+ x³¹+ x²⁴+ x²²+ x¹⁶+ x¹⁴+ x⁸+ x⁷+ x⁵+ x³+ x¹+ x⁰
  10. // has the reversed notation 0b11010101100000101000001010000001, so the value
  11. // that should be passed to MakeTable is 0xD5828281.
  12. crc32q := crc32.MakeTable(0xD5828281)
  13. fmt.Printf("%08x\n", crc32.Checksum([]byte("Hello world"), crc32q))
  14. // Output:
  15. // 2964d064