@@ -159,16 +159,19 @@ const (
159
159
)
160
160
161
161
func (h * sha1Hash ) MarshalBinary () ([]byte , error ) {
162
+ return h .AppendBinary (make ([]byte , 0 , sha1MarshaledSize ))
163
+ }
164
+
165
+ func (h * sha1Hash ) AppendBinary (b []byte ) ([]byte , error ) {
162
166
d := (* sha1Ctx )(unsafe .Pointer (& h .ctx ))
163
- b := make ([]byte , 0 , sha1MarshaledSize )
164
167
b = append (b , sha1Magic ... )
165
168
b = appendUint32 (b , d .h [0 ])
166
169
b = appendUint32 (b , d .h [1 ])
167
170
b = appendUint32 (b , d .h [2 ])
168
171
b = appendUint32 (b , d .h [3 ])
169
172
b = appendUint32 (b , d .h [4 ])
170
173
b = append (b , d .x [:d .nx ]... )
171
- b = b [: len (b ) + len ( d .x )- int (d .nx )] // already zero
174
+ b = append ( b , make ([] byte , len (d .x )- int (d .nx )) ... )
172
175
b = appendUint64 (b , uint64 (d .nl )>> 3 | uint64 (d .nh )<< 29 )
173
176
return b , nil
174
177
}
@@ -285,8 +288,11 @@ type sha256Ctx struct {
285
288
}
286
289
287
290
func (h * sha224Hash ) MarshalBinary () ([]byte , error ) {
291
+ return h .AppendBinary (make ([]byte , 0 , marshaledSize256 ))
292
+ }
293
+
294
+ func (h * sha224Hash ) AppendBinary (b []byte ) ([]byte , error ) {
288
295
d := (* sha256Ctx )(unsafe .Pointer (& h .ctx ))
289
- b := make ([]byte , 0 , marshaledSize256 )
290
296
b = append (b , magic224 ... )
291
297
b = appendUint32 (b , d .h [0 ])
292
298
b = appendUint32 (b , d .h [1 ])
@@ -297,14 +303,17 @@ func (h *sha224Hash) MarshalBinary() ([]byte, error) {
297
303
b = appendUint32 (b , d .h [6 ])
298
304
b = appendUint32 (b , d .h [7 ])
299
305
b = append (b , d .x [:d .nx ]... )
300
- b = b [: len (b ) + len ( d .x )- int (d .nx )] // already zero
306
+ b = append ( b , make ([] byte , len (d .x )- int (d .nx )) ... )
301
307
b = appendUint64 (b , uint64 (d .nl )>> 3 | uint64 (d .nh )<< 29 )
302
308
return b , nil
303
309
}
304
310
305
311
func (h * sha256Hash ) MarshalBinary () ([]byte , error ) {
312
+ return h .AppendBinary (make ([]byte , 0 , marshaledSize256 ))
313
+ }
314
+
315
+ func (h * sha256Hash ) AppendBinary (b []byte ) ([]byte , error ) {
306
316
d := (* sha256Ctx )(unsafe .Pointer (& h .ctx ))
307
- b := make ([]byte , 0 , marshaledSize256 )
308
317
b = append (b , magic256 ... )
309
318
b = appendUint32 (b , d .h [0 ])
310
319
b = appendUint32 (b , d .h [1 ])
@@ -315,7 +324,7 @@ func (h *sha256Hash) MarshalBinary() ([]byte, error) {
315
324
b = appendUint32 (b , d .h [6 ])
316
325
b = appendUint32 (b , d .h [7 ])
317
326
b = append (b , d .x [:d .nx ]... )
318
- b = b [: len (b ) + len ( d .x )- int (d .nx )] // already zero
327
+ b = append ( b , make ([] byte , len (d .x )- int (d .nx )) ... )
319
328
b = appendUint64 (b , uint64 (d .nl )>> 3 | uint64 (d .nh )<< 29 )
320
329
return b , nil
321
330
}
@@ -455,15 +464,16 @@ type sha512Ctx struct {
455
464
456
465
const (
457
466
magic384 = "sha\x04 "
458
- magic512_224 = "sha\x05 "
459
- magic512_256 = "sha\x06 "
460
467
magic512 = "sha\x07 "
461
468
marshaledSize512 = len (magic512 ) + 8 * 8 + 128 + 8
462
469
)
463
470
464
471
func (h * sha384Hash ) MarshalBinary () ([]byte , error ) {
472
+ return h .AppendBinary (make ([]byte , 0 , marshaledSize512 ))
473
+ }
474
+
475
+ func (h * sha384Hash ) AppendBinary (b []byte ) ([]byte , error ) {
465
476
d := (* sha512Ctx )(unsafe .Pointer (& h .ctx ))
466
- b := make ([]byte , 0 , marshaledSize512 )
467
477
b = append (b , magic384 ... )
468
478
b = appendUint64 (b , d .h [0 ])
469
479
b = appendUint64 (b , d .h [1 ])
@@ -474,14 +484,17 @@ func (h *sha384Hash) MarshalBinary() ([]byte, error) {
474
484
b = appendUint64 (b , d .h [6 ])
475
485
b = appendUint64 (b , d .h [7 ])
476
486
b = append (b , d .x [:d .nx ]... )
477
- b = b [: len (b ) + len ( d .x )- int (d .nx )] // already zero
487
+ b = append ( b , make ([] byte , len (d .x )- int (d .nx )) ... )
478
488
b = appendUint64 (b , d .nl >> 3 | d .nh << 61 )
479
489
return b , nil
480
490
}
481
491
482
492
func (h * sha512Hash ) MarshalBinary () ([]byte , error ) {
493
+ return h .AppendBinary (make ([]byte , 0 , marshaledSize512 ))
494
+ }
495
+
496
+ func (h * sha512Hash ) AppendBinary (b []byte ) ([]byte , error ) {
483
497
d := (* sha512Ctx )(unsafe .Pointer (& h .ctx ))
484
- b := make ([]byte , 0 , marshaledSize512 )
485
498
b = append (b , magic512 ... )
486
499
b = appendUint64 (b , d .h [0 ])
487
500
b = appendUint64 (b , d .h [1 ])
@@ -492,7 +505,7 @@ func (h *sha512Hash) MarshalBinary() ([]byte, error) {
492
505
b = appendUint64 (b , d .h [6 ])
493
506
b = appendUint64 (b , d .h [7 ])
494
507
b = append (b , d .x [:d .nx ]... )
495
- b = b [: len (b ) + len ( d .x )- int (d .nx )] // already zero
508
+ b = append ( b , make ([] byte , len (d .x )- int (d .nx )) ... )
496
509
b = appendUint64 (b , d .nl >> 3 | d .nh << 61 )
497
510
return b , nil
498
511
}
0 commit comments