1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package crc32 implements the 32-bit cyclic redundancy check, or CRC-32,
6 // checksum. See http://en.wikipedia.org/wiki/Cyclic_redundancy_check for
15 // The size of a CRC-32 checksum in bytes.
18 // Predefined polynomials.
20 // Far and away the most common CRC-32 polynomial.
21 // Used by ethernet (IEEE 802.3), v.42, fddi, gzip, zip, png, mpeg-2, ...
24 // Castagnoli's polynomial, used in iSCSI.
25 // Has better error detection characteristics than IEEE.
26 // http://dx.doi.org/10.1109/26.231911
27 Castagnoli = 0x82f63b78
29 // Koopman's polynomial.
30 // Also has better error detection characteristics than IEEE.
31 // http://dx.doi.org/10.1109/DSN.2002.1028931
35 // Table is a 256-word table representing the polynomial for efficient processing.
36 type Table [256]uint32
38 // castagnoliTable points to a lazily initialized Table for the Castagnoli
39 // polynomial. MakeTable will always return this value when asked to make a
40 // Castagnoli table so we can compare against it to find when the caller is
41 // using this polynomial.
42 var castagnoliTable *Table
43 var castagnoliOnce sync.Once
45 func castagnoliInit() {
46 castagnoliTable = makeTable(Castagnoli)
49 // IEEETable is the table for the IEEE polynomial.
50 var IEEETable = makeTable(IEEE)
52 // MakeTable returns the Table constructed from the specified polynomial.
53 func MakeTable(poly uint32) *Table {
58 castagnoliOnce.Do(castagnoliInit)
59 return castagnoliTable
61 return makeTable(poly)
64 // makeTable returns the Table constructed from the specified polynomial.
65 func makeTable(poly uint32) *Table {
67 for i := 0; i < 256; i++ {
69 for j := 0; j < 8; j++ {
71 crc = (crc >> 1) ^ poly
81 // digest represents the partial evaluation of a checksum.
87 // New creates a new hash.Hash32 computing the CRC-32 checksum
88 // using the polynomial represented by the Table.
89 func New(tab *Table) hash.Hash32 { return &digest{0, tab} }
91 // NewIEEE creates a new hash.Hash32 computing the CRC-32 checksum
92 // using the IEEE polynomial.
93 func NewIEEE() hash.Hash32 { return New(IEEETable) }
95 func (d *digest) Size() int { return Size }
97 func (d *digest) Reset() { d.crc = 0 }
99 func update(crc uint32, tab *Table, p []byte) uint32 {
101 for _, v := range p {
102 crc = tab[byte(crc)^v] ^ (crc >> 8)
107 // Update returns the result of adding the bytes in p to the crc.
108 func Update(crc uint32, tab *Table, p []byte) uint32 {
109 if tab == castagnoliTable {
110 return updateCastagnoli(crc, p)
112 return update(crc, tab, p)
115 func (d *digest) Write(p []byte) (n int, err error) {
116 d.crc = Update(d.crc, d.tab, p)
120 func (d *digest) Sum32() uint32 { return d.crc }
122 func (d *digest) Sum(in []byte) []byte {
124 in = append(in, byte(s>>24))
125 in = append(in, byte(s>>16))
126 in = append(in, byte(s>>8))
127 in = append(in, byte(s))
131 // Checksum returns the CRC-32 checksum of data
132 // using the polynomial represented by the Table.
133 func Checksum(data []byte, tab *Table) uint32 { return Update(0, tab, data) }
135 // ChecksumIEEE returns the CRC-32 checksum of data
136 // using the IEEE polynomial.
137 func ChecksumIEEE(data []byte) uint32 { return update(0, IEEETable, data) }