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 // This package implements the 64-bit cyclic redundancy check, or CRC-64, checksum.
6 // See http://en.wikipedia.org/wiki/Cyclic_redundancy_check for information.
14 // The size of a CRC-64 checksum in bytes.
17 // Predefined polynomials.
19 // The ISO polynomial, defined in ISO 3309 and used in HDLC.
20 ISO = 0xD800000000000000
22 // The ECMA polynomial, defined in ECMA 182.
23 ECMA = 0xC96C5795D7870F42
26 // Table is a 256-word table representing the polynomial for efficient processing.
27 type Table [256]uint64
29 // MakeTable returns the Table constructed from the specified polynomial.
30 func MakeTable(poly uint64) *Table {
32 for i := 0; i < 256; i++ {
34 for j := 0; j < 8; j++ {
36 crc = (crc >> 1) ^ poly
46 // digest represents the partial evaluation of a checksum.
52 // New creates a new hash.Hash64 computing the CRC-64 checksum
53 // using the polynomial represented by the Table.
54 func New(tab *Table) hash.Hash64 { return &digest{0, tab} }
56 func (d *digest) Size() int { return Size }
58 func (d *digest) Reset() { d.crc = 0 }
60 func update(crc uint64, tab *Table, p []byte) uint64 {
63 crc = tab[byte(crc)^v] ^ (crc >> 8)
68 // Update returns the result of adding the bytes in p to the crc.
69 func Update(crc uint64, tab *Table, p []byte) uint64 {
70 return update(crc, tab, p)
73 func (d *digest) Write(p []byte) (n int, err os.Error) {
74 d.crc = update(d.crc, d.tab, p)
78 func (d *digest) Sum64() uint64 { return d.crc }
80 func (d *digest) Sum() []byte {
94 // Checksum returns the CRC-64 checksum of data
95 // using the polynomial represented by the Table.
96 func Checksum(data []byte, tab *Table) uint64 { return update(0, tab, data) }