sm3: remove duplicated code

This commit is contained in:
Sun Yimin 2025-02-26 11:16:47 +08:00 committed by GitHub
parent dc1c5806c9
commit 51a003b022
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -50,17 +50,17 @@ func (d *digest) MarshalBinary() ([]byte, error) {
func (d *digest) AppendBinary(b []byte) ([]byte, error) { func (d *digest) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic...) b = append(b, magic...)
b = appendUint32(b, d.h[0]) b = byteorder.BEAppendUint32(b, d.h[0])
b = appendUint32(b, d.h[1]) b = byteorder.BEAppendUint32(b, d.h[1])
b = appendUint32(b, d.h[2]) b = byteorder.BEAppendUint32(b, d.h[2])
b = appendUint32(b, d.h[3]) b = byteorder.BEAppendUint32(b, d.h[3])
b = appendUint32(b, d.h[4]) b = byteorder.BEAppendUint32(b, d.h[4])
b = appendUint32(b, d.h[5]) b = byteorder.BEAppendUint32(b, d.h[5])
b = appendUint32(b, d.h[6]) b = byteorder.BEAppendUint32(b, d.h[6])
b = appendUint32(b, d.h[7]) b = byteorder.BEAppendUint32(b, d.h[7])
b = append(b, d.x[:d.nx]...) b = append(b, d.x[:d.nx]...)
b = append(b, make([]byte, len(d.x)-d.nx)...) b = append(b, make([]byte, len(d.x)-d.nx)...)
b = appendUint64(b, d.len) b = byteorder.BEAppendUint64(b, d.len)
return b, nil return b, nil
} }
@ -86,29 +86,12 @@ func (d *digest) UnmarshalBinary(b []byte) error {
return nil return nil
} }
func appendUint64(b []byte, x uint64) []byte {
var a [8]byte
byteorder.BEPutUint64(a[:], x)
return append(b, a[:]...)
}
func appendUint32(b []byte, x uint32) []byte {
var a [4]byte
byteorder.BEPutUint32(a[:], x)
return append(b, a[:]...)
}
func consumeUint64(b []byte) ([]byte, uint64) { func consumeUint64(b []byte) ([]byte, uint64) {
_ = b[7] return b[8:], byteorder.BEUint64(b)
x := uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
return b[8:], x
} }
func consumeUint32(b []byte) ([]byte, uint32) { func consumeUint32(b []byte) ([]byte, uint32) {
_ = b[3] return b[4:], byteorder.BEUint32(b)
x := uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
return b[4:], x
} }
// New returns a new hash.Hash computing the SM3 checksum. The Hash // New returns a new hash.Hash computing the SM3 checksum. The Hash