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.

33 lines
776 B

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