diff --git a/util/bytes.go b/util/bytes.go index 85691d2fb..747de6e50 100644 --- a/util/bytes.go +++ b/util/bytes.go @@ -24,12 +24,29 @@ func Uint64AsBytes(i uint64) []byte { return b } +func Uint64AsPaddedBytes(i uint64, n int) []byte { + return AddPaddingToBytes(Uint64AsBytes(i), n) +} + func Uint16AsBytes(i uint16) []byte { b := make([]byte, 2) binary.BigEndian.PutUint16(b, i) return b } +func Uint16AsPaddedBytes(i uint16, n int) []byte { + return AddPaddingToBytes(Uint16AsBytes(i), n) +} + +func AddPaddingToBytes(b []byte, n int) []byte { + if len(b)/8 >= n { + return b + } + tmp := make([]byte, n, n) + copy(tmp[n-len(b):], b) + return tmp +} + func BytesAsUint64(b []byte) uint64 { return binary.BigEndian.Uint64(b) }