Skip to content

Commit 192f389

Browse files
committed
hash: implement the encoding.BinaryAppender interface
For #62384
1 parent 44ed517 commit 192f389

File tree

12 files changed

+122
-18
lines changed

12 files changed

+122
-18
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The value returned by [New] now also implements the [encoding.BinaryAppender] interface.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The values returned by [New] and [NewIEEE] now also implement the [encoding.BinaryAppender] interface.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The value returned by [New] now also implements the [encoding.BinaryAppender] interface.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The values returned by [New32], [New32a], [New64], [New64a], [New128] and [New128a] now also implement the [encoding.BinaryAppender] interface.

src/hash/adler32/adler32.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,16 @@ const (
5757
marshaledSize = len(magic) + 4
5858
)
5959

60-
func (d *digest) MarshalBinary() ([]byte, error) {
61-
b := make([]byte, 0, marshaledSize)
60+
func (d *digest) AppendBinary(b []byte) ([]byte, error) {
6261
b = append(b, magic...)
6362
b = byteorder.BeAppendUint32(b, uint32(*d))
6463
return b, nil
6564
}
6665

66+
func (d *digest) MarshalBinary() ([]byte, error) {
67+
return d.AppendBinary(make([]byte, 0, marshaledSize))
68+
}
69+
6770
func (d *digest) UnmarshalBinary(b []byte) error {
6871
if len(b) < len(magic) || string(b[:len(magic)]) != magic {
6972
return errors.New("hash/adler32: invalid hash state identifier")

src/hash/adler32/adler32_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,23 @@ func TestGoldenMarshal(t *testing.T) {
103103
continue
104104
}
105105

106+
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
107+
if err != nil {
108+
t.Errorf("could not marshal: %v", err)
109+
continue
110+
}
111+
stateAppend = stateAppend[4:]
112+
106113
if string(state) != g.halfState {
107114
t.Errorf("checksum(%q) state = %q, want %q", g.in, state, g.halfState)
108115
continue
109116
}
110117

118+
if string(stateAppend) != g.halfState {
119+
t.Errorf("checksum(%q) state = %q, want %q", g.in, stateAppend, g.halfState)
120+
continue
121+
}
122+
111123
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
112124
t.Errorf("could not unmarshal: %v", err)
113125
continue

src/hash/crc32/crc32.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,18 @@ const (
170170
marshaledSize = len(magic) + 4 + 4
171171
)
172172

173-
func (d *digest) MarshalBinary() ([]byte, error) {
174-
b := make([]byte, 0, marshaledSize)
173+
func (d *digest) AppendBinary(b []byte) ([]byte, error) {
175174
b = append(b, magic...)
176175
b = byteorder.BeAppendUint32(b, tableSum(d.tab))
177176
b = byteorder.BeAppendUint32(b, d.crc)
178177
return b, nil
179178
}
180179

180+
func (d *digest) MarshalBinary() ([]byte, error) {
181+
return d.AppendBinary(make([]byte, 0, marshaledSize))
182+
183+
}
184+
181185
func (d *digest) UnmarshalBinary(b []byte) error {
182186
if len(b) < len(magic) || string(b[:len(magic)]) != magic {
183187
return errors.New("hash/crc32: invalid hash state identifier")

src/hash/crc32/crc32_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,23 @@ func TestGoldenMarshal(t *testing.T) {
133133
continue
134134
}
135135

136+
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
137+
if err != nil {
138+
t.Errorf("could not marshal: %v", err)
139+
continue
140+
}
141+
stateAppend = stateAppend[4:]
142+
136143
if string(state) != g.halfStateIEEE {
137144
t.Errorf("IEEE(%q) state = %q, want %q", g.in, state, g.halfStateIEEE)
138145
continue
139146
}
140147

148+
if string(stateAppend) != g.halfStateIEEE {
149+
t.Errorf("IEEE(%q) state = %q, want %q", g.in, stateAppend, g.halfStateIEEE)
150+
continue
151+
}
152+
141153
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
142154
t.Errorf("could not unmarshal: %v", err)
143155
continue
@@ -165,11 +177,23 @@ func TestGoldenMarshal(t *testing.T) {
165177
continue
166178
}
167179

180+
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
181+
if err != nil {
182+
t.Errorf("could not marshal: %v", err)
183+
continue
184+
}
185+
stateAppend = stateAppend[4:]
186+
168187
if string(state) != g.halfStateCastagnoli {
169188
t.Errorf("Castagnoli(%q) state = %q, want %q", g.in, state, g.halfStateCastagnoli)
170189
continue
171190
}
172191

192+
if string(stateAppend) != g.halfStateCastagnoli {
193+
t.Errorf("Castagnoli(%q) state = %q, want %q", g.in, stateAppend, g.halfStateCastagnoli)
194+
continue
195+
}
196+
173197
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
174198
t.Errorf("could not unmarshal: %v", err)
175199
continue

src/hash/crc64/crc64.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,17 @@ const (
111111
marshaledSize = len(magic) + 8 + 8
112112
)
113113

114-
func (d *digest) MarshalBinary() ([]byte, error) {
115-
b := make([]byte, 0, marshaledSize)
114+
func (d *digest) AppendBinary(b []byte) ([]byte, error) {
116115
b = append(b, magic...)
117116
b = byteorder.BeAppendUint64(b, tableSum(d.tab))
118117
b = byteorder.BeAppendUint64(b, d.crc)
119118
return b, nil
120119
}
121120

121+
func (d *digest) MarshalBinary() ([]byte, error) {
122+
return d.AppendBinary(make([]byte, 0, marshaledSize))
123+
}
124+
122125
func (d *digest) UnmarshalBinary(b []byte) error {
123126
if len(b) < len(magic) || string(b[:len(magic)]) != magic {
124127
return errors.New("hash/crc64: invalid hash state identifier")

src/hash/crc64/crc64_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,23 @@ func TestGoldenMarshal(t *testing.T) {
8888
continue
8989
}
9090

91+
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
92+
if err != nil {
93+
t.Errorf("could not marshal: %v", err)
94+
continue
95+
}
96+
stateAppend = stateAppend[4:]
97+
9198
if string(state) != g.halfStateISO {
9299
t.Errorf("ISO crc64(%q) state = %q, want %q", g.in, state, g.halfStateISO)
93100
continue
94101
}
95102

103+
if string(stateAppend) != g.halfStateISO {
104+
t.Errorf("ISO crc64(%q) state = %q, want %q", g.in, stateAppend, g.halfStateISO)
105+
continue
106+
}
107+
96108
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
97109
t.Errorf("could not unmarshal: %v", err)
98110
continue
@@ -120,11 +132,23 @@ func TestGoldenMarshal(t *testing.T) {
120132
continue
121133
}
122134

135+
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
136+
if err != nil {
137+
t.Errorf("could not marshal: %v", err)
138+
continue
139+
}
140+
stateAppend = stateAppend[4:]
141+
123142
if string(state) != g.halfStateECMA {
124143
t.Errorf("ECMA crc64(%q) state = %q, want %q", g.in, state, g.halfStateECMA)
125144
continue
126145
}
127146

147+
if string(stateAppend) != g.halfStateECMA {
148+
t.Errorf("ECMA crc64(%q) state = %q, want %q", g.in, stateAppend, g.halfStateECMA)
149+
continue
150+
}
151+
128152
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
129153
t.Errorf("could not unmarshal: %v", err)
130154
continue

src/hash/fnv/fnv.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,50 +219,68 @@ const (
219219
marshaledSize128 = len(magic128) + 8*2
220220
)
221221

222-
func (s *sum32) MarshalBinary() ([]byte, error) {
223-
b := make([]byte, 0, marshaledSize32)
222+
func (s *sum32) AppendBinary(b []byte) ([]byte, error) {
224223
b = append(b, magic32...)
225224
b = byteorder.BeAppendUint32(b, uint32(*s))
226225
return b, nil
227226
}
228227

229-
func (s *sum32a) MarshalBinary() ([]byte, error) {
230-
b := make([]byte, 0, marshaledSize32)
228+
func (s *sum32) MarshalBinary() ([]byte, error) {
229+
return s.AppendBinary(make([]byte, 0, marshaledSize32))
230+
}
231+
232+
func (s *sum32a) AppendBinary(b []byte) ([]byte, error) {
231233
b = append(b, magic32a...)
232234
b = byteorder.BeAppendUint32(b, uint32(*s))
233235
return b, nil
234236
}
235237

236-
func (s *sum64) MarshalBinary() ([]byte, error) {
237-
b := make([]byte, 0, marshaledSize64)
238+
func (s *sum32a) MarshalBinary() ([]byte, error) {
239+
return s.AppendBinary(make([]byte, 0, marshaledSize32))
240+
}
241+
242+
func (s *sum64) AppendBinary(b []byte) ([]byte, error) {
238243
b = append(b, magic64...)
239244
b = byteorder.BeAppendUint64(b, uint64(*s))
240245
return b, nil
241246
}
242247

243-
func (s *sum64a) MarshalBinary() ([]byte, error) {
244-
b := make([]byte, 0, marshaledSize64)
248+
func (s *sum64) MarshalBinary() ([]byte, error) {
249+
return s.AppendBinary(make([]byte, 0, marshaledSize64))
250+
}
251+
252+
func (s *sum64a) AppendBinary(b []byte) ([]byte, error) {
245253
b = append(b, magic64a...)
246254
b = byteorder.BeAppendUint64(b, uint64(*s))
247255
return b, nil
248256
}
249257

250-
func (s *sum128) MarshalBinary() ([]byte, error) {
251-
b := make([]byte, 0, marshaledSize128)
258+
func (s *sum64a) MarshalBinary() ([]byte, error) {
259+
return s.AppendBinary(make([]byte, 0, marshaledSize64))
260+
}
261+
262+
func (s *sum128) AppendBinary(b []byte) ([]byte, error) {
252263
b = append(b, magic128...)
253264
b = byteorder.BeAppendUint64(b, s[0])
254265
b = byteorder.BeAppendUint64(b, s[1])
255266
return b, nil
256267
}
257268

258-
func (s *sum128a) MarshalBinary() ([]byte, error) {
259-
b := make([]byte, 0, marshaledSize128)
269+
func (s *sum128) MarshalBinary() ([]byte, error) {
270+
return s.AppendBinary(make([]byte, 0, marshaledSize128))
271+
}
272+
273+
func (s *sum128a) AppendBinary(b []byte) ([]byte, error) {
260274
b = append(b, magic128a...)
261275
b = byteorder.BeAppendUint64(b, s[0])
262276
b = byteorder.BeAppendUint64(b, s[1])
263277
return b, nil
264278
}
265279

280+
func (s *sum128a) MarshalBinary() ([]byte, error) {
281+
return s.AppendBinary(make([]byte, 0, marshaledSize128))
282+
}
283+
266284
func (s *sum32) UnmarshalBinary(b []byte) error {
267285
if len(b) < len(magic32) || string(b[:len(magic32)]) != magic32 {
268286
return errors.New("hash/fnv: invalid hash state identifier")

src/hash/fnv/fnv_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,23 @@ func TestGoldenMarshal(t *testing.T) {
128128
continue
129129
}
130130

131+
stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
132+
if err != nil {
133+
t.Errorf("could not marshal: %v", err)
134+
continue
135+
}
136+
stateAppend = stateAppend[4:]
137+
131138
if string(state) != g.halfState {
132139
t.Errorf("checksum(%q) state = %q, want %q", g.in, state, g.halfState)
133140
continue
134141
}
135142

143+
if string(stateAppend) != g.halfState {
144+
t.Errorf("checksum(%q) state = %q, want %q", g.in, stateAppend, g.halfState)
145+
continue
146+
}
147+
136148
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
137149
t.Errorf("could not unmarshal: %v", err)
138150
continue

0 commit comments

Comments
 (0)