crc.go 1.0 KB

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