8 "golang.org/x/crypto/ripemd160"
10 cmn "github.com/tendermint/tmlibs/common"
13 func BinaryBytes(o interface{}) []byte {
14 w, n, err := new(bytes.Buffer), new(int), new(error)
15 WriteBinary(o, w, n, err)
22 // ptr: a pointer to the object to be filled
23 func ReadBinaryBytes(d []byte, ptr interface{}) error {
24 r, n, err := bytes.NewBuffer(d), new(int), new(error)
25 ReadBinaryPtr(ptr, r, len(d), n, err)
29 func JSONBytes(o interface{}) []byte {
30 w, n, err := new(bytes.Buffer), new(int), new(error)
31 WriteJSON(o, w, n, err)
39 func JSONBytesPretty(o interface{}) []byte {
40 jsonBytes := JSONBytes(o)
41 var object interface{}
42 err := json.Unmarshal(jsonBytes, &object)
46 jsonBytes, err = json.MarshalIndent(object, "", "\t")
53 // ptr: a pointer to the object to be filled
54 func ReadJSONBytes(d []byte, ptr interface{}) (err error) {
55 ReadJSONPtr(ptr, d, &err)
59 // NOTE: does not care about the type, only the binary representation.
60 func BinaryEqual(a, b interface{}) bool {
61 aBytes := BinaryBytes(a)
62 bBytes := BinaryBytes(b)
63 return bytes.Equal(aBytes, bBytes)
66 // NOTE: does not care about the type, only the binary representation.
67 func BinaryCompare(a, b interface{}) int {
68 aBytes := BinaryBytes(a)
69 bBytes := BinaryBytes(b)
70 return bytes.Compare(aBytes, bBytes)
73 // NOTE: only use this if you need 32 bytes.
74 func BinarySha256(o interface{}) []byte {
75 hasher, n, err := sha256.New(), new(int), new(error)
76 WriteBinary(o, hasher, n, err)
80 return hasher.Sum(nil)
83 // NOTE: The default hash function is Ripemd160.
84 func BinaryRipemd160(o interface{}) []byte {
85 hasher, n, err := ripemd160.New(), new(int), new(error)
86 WriteBinary(o, hasher, n, err)
90 return hasher.Sum(nil)