crc.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package oss
  2. import (
  3. "hash"
  4. "hash/crc64"
  5. )
  6. // digest represents the partial evaluation of a checksum.
  7. type digest struct {
  8. crc uint64
  9. tab *crc64.Table
  10. }
  11. // NewCRC creates a new hash.Hash64 computing the CRC-64 checksum
  12. // using the polynomial represented by the Table.
  13. func NewCRC(tab *crc64.Table, init uint64) hash.Hash64 { return &digest{init, tab} }
  14. // Size returns the number of bytes Sum will return.
  15. func (d *digest) Size() int { return crc64.Size }
  16. // BlockSize returns the hash's underlying block size.
  17. // The Write method must be able to accept any amount
  18. // of data, but it may operate more efficiently if all writes
  19. // are a multiple of the block size.
  20. func (d *digest) BlockSize() int { return 1 }
  21. // Reset resets the Hash to its initial state.
  22. func (d *digest) Reset() { d.crc = 0 }
  23. // Write (via the embedded io.Writer interface) adds more data to the running hash.
  24. // It never returns an error.
  25. func (d *digest) Write(p []byte) (n int, err error) {
  26. d.crc = crc64.Update(d.crc, d.tab, p)
  27. return len(p), nil
  28. }
  29. // Sum64 returns crc64 value.
  30. func (d *digest) Sum64() uint64 { return d.crc }
  31. // Sum returns hash value.
  32. func (d *digest) Sum(in []byte) []byte {
  33. s := d.Sum64()
  34. 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))
  35. }