crc.go 533 B

123456789101112131415161718192021222324252627282930
  1. package storage
  2. import (
  3. "fmt"
  4. "github.com/klauspost/crc32"
  5. "github.com/chrislusf/seaweedfs/weed/util"
  6. )
  7. var table = crc32.MakeTable(crc32.Castagnoli)
  8. type CRC uint32
  9. func NewCRC(b []byte) CRC {
  10. return CRC(0).Update(b)
  11. }
  12. func (c CRC) Update(b []byte) CRC {
  13. return CRC(crc32.Update(uint32(c), table, b))
  14. }
  15. func (c CRC) Value() uint32 {
  16. return uint32(c>>15|c<<17) + 0xa282ead8
  17. }
  18. func (n *Needle) Etag() string {
  19. bits := make([]byte, 4)
  20. util.Uint32toBytes(bits, uint32(n.Checksum))
  21. return fmt.Sprintf("\"%x\"", bits)
  22. }