tendermint/binary/util.go

36 lines
706 B
Go
Raw Normal View History

2014-06-05 02:33:50 -07:00
package binary
import (
2014-07-01 14:50:24 -07:00
"bytes"
"crypto/sha256"
2014-06-05 02:33:50 -07:00
)
func BinaryBytes(b Binary) []byte {
2014-07-01 14:50:24 -07:00
buf := bytes.NewBuffer(nil)
b.WriteTo(buf)
return buf.Bytes()
2014-06-05 02:33:50 -07:00
}
// NOTE: does not care about the type, only the binary representation.
func BinaryEqual(a, b Binary) bool {
2014-07-01 14:50:24 -07:00
aBytes := BinaryBytes(a)
bBytes := BinaryBytes(b)
return bytes.Equal(aBytes, bBytes)
2014-06-05 02:33:50 -07:00
}
// NOTE: does not care about the type, only the binary representation.
func BinaryCompare(a, b Binary) int {
2014-07-01 14:50:24 -07:00
aBytes := BinaryBytes(a)
bBytes := BinaryBytes(b)
return bytes.Compare(aBytes, bBytes)
2014-06-05 02:33:50 -07:00
}
func BinaryHash(b Binary) []byte {
2014-07-01 14:50:24 -07:00
hasher := sha256.New()
_, err := b.WriteTo(hasher)
if err != nil {
panic(err)
}
return hasher.Sum(nil)
2014-06-05 02:33:50 -07:00
}