github.com/d4l3k/go@v0.0.0-20151015000803-65fc379daeda/src/hash/crc64/crc64.go (about)

     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.
     4  
     5  // Package crc64 implements the 64-bit cyclic redundancy check, or CRC-64,
     6  // checksum. See http://en.wikipedia.org/wiki/Cyclic_redundancy_check for
     7  // information.
     8  package crc64
     9  
    10  import "hash"
    11  
    12  // The size of a CRC-64 checksum in bytes.
    13  const Size = 8
    14  
    15  // Predefined polynomials.
    16  const (
    17  	// The ISO polynomial, defined in ISO 3309 and used in HDLC.
    18  	ISO = 0xD800000000000000
    19  
    20  	// The ECMA polynomial, defined in ECMA 182.
    21  	ECMA = 0xC96C5795D7870F42
    22  )
    23  
    24  // Table is a 256-word table representing the polynomial for efficient processing.
    25  type Table [256]uint64
    26  
    27  // MakeTable returns a Table constructed from the specified polynomial.
    28  // The contents of this Table must not be modified.
    29  func MakeTable(poly uint64) *Table {
    30  	t := new(Table)
    31  	for i := 0; i < 256; i++ {
    32  		crc := uint64(i)
    33  		for j := 0; j < 8; j++ {
    34  			if crc&1 == 1 {
    35  				crc = (crc >> 1) ^ poly
    36  			} else {
    37  				crc >>= 1
    38  			}
    39  		}
    40  		t[i] = crc
    41  	}
    42  	return t
    43  }
    44  
    45  // digest represents the partial evaluation of a checksum.
    46  type digest struct {
    47  	crc uint64
    48  	tab *Table
    49  }
    50  
    51  // New creates a new hash.Hash64 computing the CRC-64 checksum
    52  // using the polynomial represented by the Table.
    53  // Its Sum method will lay the value out in big-endian byte order.
    54  func New(tab *Table) hash.Hash64 { return &digest{0, tab} }
    55  
    56  func (d *digest) Size() int { return Size }
    57  
    58  func (d *digest) BlockSize() int { return 1 }
    59  
    60  func (d *digest) Reset() { d.crc = 0 }
    61  
    62  func update(crc uint64, tab *Table, p []byte) uint64 {
    63  	crc = ^crc
    64  	for _, v := range p {
    65  		crc = tab[byte(crc)^v] ^ (crc >> 8)
    66  	}
    67  	return ^crc
    68  }
    69  
    70  // Update returns the result of adding the bytes in p to the crc.
    71  func Update(crc uint64, tab *Table, p []byte) uint64 {
    72  	return update(crc, tab, p)
    73  }
    74  
    75  func (d *digest) Write(p []byte) (n int, err error) {
    76  	d.crc = update(d.crc, d.tab, p)
    77  	return len(p), nil
    78  }
    79  
    80  func (d *digest) Sum64() uint64 { return d.crc }
    81  
    82  func (d *digest) Sum(in []byte) []byte {
    83  	s := d.Sum64()
    84  	return append(in, byte(s>>56), byte(s>>48), byte(s>>40), byte(s>>32), byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
    85  }
    86  
    87  // Checksum returns the CRC-64 checksum of data
    88  // using the polynomial represented by the Table.
    89  func Checksum(data []byte, tab *Table) uint64 { return update(0, tab, data) }