crc32.go 691 B

123456789101112131415161718192021222324252627282930
  1. // Copyright 2011 The LevelDB-Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license that can be
  4. // found in the LICENSE file.
  5. package util
  6. import (
  7. "hash/crc32"
  8. )
  9. var table = crc32.MakeTable(crc32.Castagnoli)
  10. // CRC is a CRC-32 checksum computed using Castagnoli's polynomial.
  11. type CRC uint32
  12. // NewCRC creates a new crc based on the given bytes.
  13. func NewCRC(b []byte) CRC {
  14. return CRC(0).Update(b)
  15. }
  16. // Update updates the crc with the given bytes.
  17. func (c CRC) Update(b []byte) CRC {
  18. return CRC(crc32.Update(uint32(c), table, b))
  19. }
  20. // Value returns a masked crc.
  21. func (c CRC) Value() uint32 {
  22. return uint32(c>>15|c<<17) + 0xa282ead8
  23. }