crc.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2014-2022 Ulrich Kunitz. 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 xz
  5. import (
  6. "hash"
  7. "hash/crc32"
  8. "hash/crc64"
  9. )
  10. // crc32Hash implements the hash.Hash32 interface with Sum returning the
  11. // crc32 value in little-endian encoding.
  12. type crc32Hash struct {
  13. hash.Hash32
  14. }
  15. // Sum returns the crc32 value as little endian.
  16. func (h crc32Hash) Sum(b []byte) []byte {
  17. p := make([]byte, 4)
  18. putUint32LE(p, h.Hash32.Sum32())
  19. b = append(b, p...)
  20. return b
  21. }
  22. // newCRC32 returns a CRC-32 hash that returns the 64-bit value in
  23. // little-endian encoding using the IEEE polynomial.
  24. func newCRC32() hash.Hash {
  25. return crc32Hash{Hash32: crc32.NewIEEE()}
  26. }
  27. // crc64Hash implements the Hash64 interface with Sum returning the
  28. // CRC-64 value in little-endian encoding.
  29. type crc64Hash struct {
  30. hash.Hash64
  31. }
  32. // Sum returns the CRC-64 value in little-endian encoding.
  33. func (h crc64Hash) Sum(b []byte) []byte {
  34. p := make([]byte, 8)
  35. putUint64LE(p, h.Hash64.Sum64())
  36. b = append(b, p...)
  37. return b
  38. }
  39. // crc64Table is used to create a CRC-64 hash.
  40. var crc64Table = crc64.MakeTable(crc64.ECMA)
  41. // newCRC64 returns a CRC-64 hash that returns the 64-bit value in
  42. // little-endian encoding using the ECMA polynomial.
  43. func newCRC64() hash.Hash {
  44. return crc64Hash{Hash64: crc64.New(crc64Table)}
  45. }