You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
691 B

  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. }