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.

57 lines
1.3 KiB

10 years ago
9 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. "github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go.crypto/ripemd160"
  6. )
  7. func BinaryBytes(o interface{}) []byte {
  8. w, n, err := new(bytes.Buffer), new(int64), new(error)
  9. WriteBinary(o, w, n, err)
  10. if *err != nil {
  11. panic(*err)
  12. }
  13. return w.Bytes()
  14. }
  15. func JSONBytes(o interface{}) []byte {
  16. w, n, err := new(bytes.Buffer), new(int64), new(error)
  17. WriteJSON(o, w, n, err)
  18. if *err != nil {
  19. panic(*err)
  20. }
  21. return w.Bytes()
  22. }
  23. // NOTE: does not care about the type, only the binary representation.
  24. func BinaryEqual(a, b interface{}) bool {
  25. aBytes := BinaryBytes(a)
  26. bBytes := BinaryBytes(b)
  27. return bytes.Equal(aBytes, bBytes)
  28. }
  29. // NOTE: does not care about the type, only the binary representation.
  30. func BinaryCompare(a, b interface{}) int {
  31. aBytes := BinaryBytes(a)
  32. bBytes := BinaryBytes(b)
  33. return bytes.Compare(aBytes, bBytes)
  34. }
  35. func BinarySha256(o interface{}) []byte {
  36. hasher, n, err := sha256.New(), new(int64), new(error)
  37. WriteBinary(o, hasher, n, err)
  38. if *err != nil {
  39. panic(*err)
  40. }
  41. return hasher.Sum(nil)
  42. }
  43. func BinaryRipemd160(o interface{}) []byte {
  44. hasher, n, err := ripemd160.New(), new(int64), new(error)
  45. WriteBinary(o, hasher, n, err)
  46. if *err != nil {
  47. panic(*err)
  48. }
  49. return hasher.Sum(nil)
  50. }