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.

38 lines
749 B

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package binary
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. )
  6. func BinaryBytes(b Binary) []byte {
  7. buf := bytes.NewBuffer(nil)
  8. _, err := b.WriteTo(buf)
  9. if err != nil {
  10. panic(err)
  11. }
  12. return buf.Bytes()
  13. }
  14. // NOTE: does not care about the type, only the binary representation.
  15. func BinaryEqual(a, b Binary) bool {
  16. aBytes := BinaryBytes(a)
  17. bBytes := BinaryBytes(b)
  18. return bytes.Equal(aBytes, bBytes)
  19. }
  20. // NOTE: does not care about the type, only the binary representation.
  21. func BinaryCompare(a, b Binary) int {
  22. aBytes := BinaryBytes(a)
  23. bBytes := BinaryBytes(b)
  24. return bytes.Compare(aBytes, bBytes)
  25. }
  26. func BinaryHash(b Binary) []byte {
  27. hasher := sha256.New()
  28. _, err := b.WriteTo(hasher)
  29. if err != nil {
  30. panic(err)
  31. }
  32. return hasher.Sum(nil)
  33. }