Skip to content

Commit e90f1e1

Browse files
hasheddangopherbot
authored andcommitted
cryptobyte: add uint48 methods
Adds uint48 methods for cryptobyte.Builder and cryptobyte.String. Supporting 48-bit unsigned integers is useful for working with protocols that use them for sequence numbers, such as DTLS. Fixes golang/go#61275 Change-Id: Ibe49422d37644b9212b28b123dc5e01850f7b05b GitHub-Last-Rev: 11b388c GitHub-Pull-Request: #265 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/508675 Reviewed-by: Roland Shoemaker <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: qiulaidongfeng <[email protected]> Run-TryBot: Roland Shoemaker <[email protected]> Auto-Submit: Roland Shoemaker <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent d359caa commit e90f1e1

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

cryptobyte/builder.go

+5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ func (b *Builder) AddUint32(v uint32) {
9595
b.add(byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
9696
}
9797

98+
// AddUint48 appends a big-endian, 48-bit value to the byte string.
99+
func (b *Builder) AddUint48(v uint64) {
100+
b.add(byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
101+
}
102+
98103
// AddUint64 appends a big-endian, 64-bit value to the byte string.
99104
func (b *Builder) AddUint64(v uint64) {
100105
b.add(byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))

cryptobyte/cryptobyte_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,27 @@ func TestUint32(t *testing.T) {
179179
}
180180
}
181181

182+
func TestUint48(t *testing.T) {
183+
var b Builder
184+
var u uint64 = 0xfefcff3cfdfc
185+
b.AddUint48(u)
186+
if err := builderBytesEq(&b, 254, 252, 255, 60, 253, 252); err != nil {
187+
t.Error(err)
188+
}
189+
190+
var s String = b.BytesOrPanic()
191+
var v uint64
192+
if !s.ReadUint48(&v) {
193+
t.Error("ReadUint48() = false, want true")
194+
}
195+
if v != u {
196+
t.Errorf("v = %x, want %x", v, u)
197+
}
198+
if len(s) != 0 {
199+
t.Errorf("len(s) = %d, want 0", len(s))
200+
}
201+
}
202+
182203
func TestUint64(t *testing.T) {
183204
var b Builder
184205
b.AddUint64(0xf2fefefcff3cfdfc)

cryptobyte/string.go

+11
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ func (s *String) ReadUint32(out *uint32) bool {
8181
return true
8282
}
8383

84+
// ReadUint48 decodes a big-endian, 48-bit value into out and advances over it.
85+
// It reports whether the read was successful.
86+
func (s *String) ReadUint48(out *uint64) bool {
87+
v := s.read(6)
88+
if v == nil {
89+
return false
90+
}
91+
*out = uint64(v[0])<<40 | uint64(v[1])<<32 | uint64(v[2])<<24 | uint64(v[3])<<16 | uint64(v[4])<<8 | uint64(v[5])
92+
return true
93+
}
94+
8495
// ReadUint64 decodes a big-endian, 64-bit value into out and advances over it.
8596
// It reports whether the read was successful.
8697
func (s *String) ReadUint64(out *uint64) bool {

0 commit comments

Comments
 (0)