1
0
mirror of https://github.com/golang/go synced 2024-11-17 05:54:46 -07:00

crypto: use encoding append functions

Replace custom append functions in the hash functions with the implementation of the encoding/binary package that do the same thing.
The binary bigendian functions are already used in other parts of the code in the crypto package.

Change-Id: I76d2dbe143fc72a3b4ac06be312caf72bd71378a
GitHub-Last-Rev: 1c6c68279e
GitHub-Pull-Request: golang/go#55085
Reviewed-on: https://go-review.googlesource.com/c/go/+/431035
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Erik Pellizzon 2022-09-19 08:30:01 +00:00 committed by Gopher Robot
parent f8c659b62f
commit 1eeb257b88
4 changed files with 29 additions and 71 deletions

View File

@ -59,13 +59,13 @@ const (
func (d *digest) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize)
b = append(b, magic...)
b = appendUint32(b, d.s[0])
b = appendUint32(b, d.s[1])
b = appendUint32(b, d.s[2])
b = appendUint32(b, d.s[3])
b = binary.BigEndian.AppendUint32(b, d.s[0])
b = binary.BigEndian.AppendUint32(b, d.s[1])
b = binary.BigEndian.AppendUint32(b, d.s[2])
b = binary.BigEndian.AppendUint32(b, d.s[3])
b = append(b, d.x[:d.nx]...)
b = b[:len(b)+len(d.x)-d.nx] // already zero
b = appendUint64(b, d.len)
b = binary.BigEndian.AppendUint64(b, d.len)
return b, nil
}
@ -87,18 +87,6 @@ func (d *digest) UnmarshalBinary(b []byte) error {
return nil
}
func appendUint64(b []byte, x uint64) []byte {
var a [8]byte
binary.BigEndian.PutUint64(a[:], x)
return append(b, a[:]...)
}
func appendUint32(b []byte, x uint32) []byte {
var a [4]byte
binary.BigEndian.PutUint32(a[:], x)
return append(b, a[:]...)
}
func consumeUint64(b []byte) ([]byte, uint64) {
return b[8:], binary.BigEndian.Uint64(b[0:8])
}

View File

@ -50,14 +50,14 @@ const (
func (d *digest) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize)
b = append(b, magic...)
b = appendUint32(b, d.h[0])
b = appendUint32(b, d.h[1])
b = appendUint32(b, d.h[2])
b = appendUint32(b, d.h[3])
b = appendUint32(b, d.h[4])
b = binary.BigEndian.AppendUint32(b, d.h[0])
b = binary.BigEndian.AppendUint32(b, d.h[1])
b = binary.BigEndian.AppendUint32(b, d.h[2])
b = binary.BigEndian.AppendUint32(b, d.h[3])
b = binary.BigEndian.AppendUint32(b, d.h[4])
b = append(b, d.x[:d.nx]...)
b = b[:len(b)+len(d.x)-int(d.nx)] // already zero
b = appendUint64(b, d.len)
b = binary.BigEndian.AppendUint64(b, d.len)
return b, nil
}
@ -80,18 +80,6 @@ func (d *digest) UnmarshalBinary(b []byte) error {
return nil
}
func appendUint64(b []byte, x uint64) []byte {
var a [8]byte
binary.BigEndian.PutUint64(a[:], x)
return append(b, a[:]...)
}
func appendUint32(b []byte, x uint32) []byte {
var a [4]byte
binary.BigEndian.PutUint32(a[:], x)
return append(b, a[:]...)
}
func consumeUint64(b []byte) ([]byte, uint64) {
_ = b[7]
x := uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |

View File

@ -70,17 +70,17 @@ func (d *digest) MarshalBinary() ([]byte, error) {
} else {
b = append(b, magic256...)
}
b = appendUint32(b, d.h[0])
b = appendUint32(b, d.h[1])
b = appendUint32(b, d.h[2])
b = appendUint32(b, d.h[3])
b = appendUint32(b, d.h[4])
b = appendUint32(b, d.h[5])
b = appendUint32(b, d.h[6])
b = appendUint32(b, d.h[7])
b = binary.BigEndian.AppendUint32(b, d.h[0])
b = binary.BigEndian.AppendUint32(b, d.h[1])
b = binary.BigEndian.AppendUint32(b, d.h[2])
b = binary.BigEndian.AppendUint32(b, d.h[3])
b = binary.BigEndian.AppendUint32(b, d.h[4])
b = binary.BigEndian.AppendUint32(b, d.h[5])
b = binary.BigEndian.AppendUint32(b, d.h[6])
b = binary.BigEndian.AppendUint32(b, d.h[7])
b = append(b, d.x[:d.nx]...)
b = b[:len(b)+len(d.x)-int(d.nx)] // already zero
b = appendUint64(b, d.len)
b = binary.BigEndian.AppendUint64(b, d.len)
return b, nil
}
@ -106,18 +106,6 @@ func (d *digest) UnmarshalBinary(b []byte) error {
return nil
}
func appendUint64(b []byte, x uint64) []byte {
var a [8]byte
binary.BigEndian.PutUint64(a[:], x)
return append(b, a[:]...)
}
func appendUint32(b []byte, x uint32) []byte {
var a [4]byte
binary.BigEndian.PutUint32(a[:], x)
return append(b, a[:]...)
}
func consumeUint64(b []byte) ([]byte, uint64) {
_ = b[7]
x := uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |

View File

@ -153,17 +153,17 @@ func (d *digest) MarshalBinary() ([]byte, error) {
default:
return nil, errors.New("crypto/sha512: invalid hash function")
}
b = appendUint64(b, d.h[0])
b = appendUint64(b, d.h[1])
b = appendUint64(b, d.h[2])
b = appendUint64(b, d.h[3])
b = appendUint64(b, d.h[4])
b = appendUint64(b, d.h[5])
b = appendUint64(b, d.h[6])
b = appendUint64(b, d.h[7])
b = binary.BigEndian.AppendUint64(b, d.h[0])
b = binary.BigEndian.AppendUint64(b, d.h[1])
b = binary.BigEndian.AppendUint64(b, d.h[2])
b = binary.BigEndian.AppendUint64(b, d.h[3])
b = binary.BigEndian.AppendUint64(b, d.h[4])
b = binary.BigEndian.AppendUint64(b, d.h[5])
b = binary.BigEndian.AppendUint64(b, d.h[6])
b = binary.BigEndian.AppendUint64(b, d.h[7])
b = append(b, d.x[:d.nx]...)
b = b[:len(b)+len(d.x)-int(d.nx)] // already zero
b = appendUint64(b, d.len)
b = binary.BigEndian.AppendUint64(b, d.len)
return b, nil
}
@ -197,12 +197,6 @@ func (d *digest) UnmarshalBinary(b []byte) error {
return nil
}
func appendUint64(b []byte, x uint64) []byte {
var a [8]byte
binary.BigEndian.PutUint64(a[:], x)
return append(b, a[:]...)
}
func consumeUint64(b []byte) ([]byte, uint64) {
_ = b[7]
x := uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |