crc.go 981 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. package crc
  5. import (
  6. "hash"
  7. "hash/crc32"
  8. )
  9. // The size of a CRC-32 checksum in bytes.
  10. const Size = 4
  11. type digest struct {
  12. crc uint32
  13. tab *crc32.Table
  14. }
  15. // New creates a new hash.Hash32 computing the CRC-32 checksum
  16. // using the polynomial represented by the Table.
  17. // Modified by xiangli to take a prevcrc.
  18. func New(prev uint32, tab *crc32.Table) hash.Hash32 { return &digest{prev, tab} }
  19. func (d *digest) Size() int { return Size }
  20. func (d *digest) BlockSize() int { return 1 }
  21. func (d *digest) Reset() { d.crc = 0 }
  22. func (d *digest) Write(p []byte) (n int, err error) {
  23. d.crc = crc32.Update(d.crc, d.tab, p)
  24. return len(p), nil
  25. }
  26. func (d *digest) Sum32() uint32 { return d.crc }
  27. func (d *digest) Sum(in []byte) []byte {
  28. s := d.Sum32()
  29. return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
  30. }