--- /dev/null
+The value returned by [md5.New] now also implements the [encoding.BinaryAppender] interface.
--- /dev/null
+The value returned by [sha1.New] now also implements the [encoding.BinaryAppender] interface.
--- /dev/null
+The values returned by [sha256.New] and [sha256.New224] now also implement the [encoding.BinaryAppender] interface
--- /dev/null
+The values returned by [sha512.New], [sha512.New384], [sha512.New512_224] and [sha512.New512_256] now also implement the [encoding.BinaryAppender] interface.
)
func (h *sha1Hash) MarshalBinary() ([]byte, error) {
+ return h.AppendBinary(make([]byte, 0, sha1MarshaledSize))
+}
+
+func (h *sha1Hash) AppendBinary(b []byte) ([]byte, error) {
d := (*sha1Ctx)(unsafe.Pointer(&h.ctx))
- b := make([]byte, 0, sha1MarshaledSize)
b = append(b, sha1Magic...)
b = appendUint32(b, d.h[0])
b = appendUint32(b, d.h[1])
b = appendUint32(b, d.h[3])
b = 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 = append(b, make([]byte, len(d.x)-int(d.nx))...)
b = appendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
return b, nil
}
}
func (h *sha224Hash) MarshalBinary() ([]byte, error) {
+ return h.AppendBinary(make([]byte, 0, marshaledSize256))
+}
+
+func (h *sha224Hash) AppendBinary(b []byte) ([]byte, error) {
d := (*sha256Ctx)(unsafe.Pointer(&h.ctx))
- b := make([]byte, 0, marshaledSize256)
b = append(b, magic224...)
b = appendUint32(b, d.h[0])
b = appendUint32(b, d.h[1])
b = appendUint32(b, d.h[6])
b = 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 = append(b, make([]byte, len(d.x)-int(d.nx))...)
b = appendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
return b, nil
}
func (h *sha256Hash) MarshalBinary() ([]byte, error) {
+ return h.AppendBinary(make([]byte, 0, marshaledSize256))
+}
+
+func (h *sha256Hash) AppendBinary(b []byte) ([]byte, error) {
d := (*sha256Ctx)(unsafe.Pointer(&h.ctx))
- b := make([]byte, 0, marshaledSize256)
b = append(b, magic256...)
b = appendUint32(b, d.h[0])
b = appendUint32(b, d.h[1])
b = appendUint32(b, d.h[6])
b = 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 = append(b, make([]byte, len(d.x)-int(d.nx))...)
b = appendUint64(b, uint64(d.nl)>>3|uint64(d.nh)<<29)
return b, nil
}
)
func (h *sha384Hash) MarshalBinary() ([]byte, error) {
+ return h.AppendBinary(make([]byte, 0, marshaledSize512))
+}
+
+func (h *sha384Hash) AppendBinary(b []byte) ([]byte, error) {
d := (*sha512Ctx)(unsafe.Pointer(&h.ctx))
- b := make([]byte, 0, marshaledSize512)
b = append(b, magic384...)
b = appendUint64(b, d.h[0])
b = appendUint64(b, d.h[1])
b = appendUint64(b, d.h[6])
b = 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 = append(b, make([]byte, len(d.x)-int(d.nx))...)
b = appendUint64(b, d.nl>>3|d.nh<<61)
return b, nil
}
func (h *sha512Hash) MarshalBinary() ([]byte, error) {
+ return h.AppendBinary(make([]byte, 0, marshaledSize512))
+}
+
+func (h *sha512Hash) AppendBinary(b []byte) ([]byte, error) {
d := (*sha512Ctx)(unsafe.Pointer(&h.ctx))
- b := make([]byte, 0, marshaledSize512)
b = append(b, magic512...)
b = appendUint64(b, d.h[0])
b = appendUint64(b, d.h[1])
b = appendUint64(b, d.h[6])
b = 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 = append(b, make([]byte, len(d.x)-int(d.nx))...)
b = appendUint64(b, d.nl>>3|d.nh<<61)
return b, nil
}
)
func (d *digest) MarshalBinary() ([]byte, error) {
- b := make([]byte, 0, marshaledSize)
+ return d.AppendBinary(make([]byte, 0, marshaledSize))
+}
+
+func (d *digest) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic...)
b = byteorder.BeAppendUint32(b, d.s[0])
b = byteorder.BeAppendUint32(b, d.s[1])
b = byteorder.BeAppendUint32(b, d.s[2])
b = byteorder.BeAppendUint32(b, d.s[3])
b = append(b, d.x[:d.nx]...)
- b = b[:len(b)+len(d.x)-d.nx] // already zero
+ b = append(b, make([]byte, len(d.x)-d.nx)...)
b = byteorder.BeAppendUint64(b, d.len)
return b, nil
}
return b[4:], byteorder.BeUint32(b[0:4])
}
-// New returns a new hash.Hash computing the MD5 checksum. The Hash also
-// implements [encoding.BinaryMarshaler] and [encoding.BinaryUnmarshaler] to
-// marshal and unmarshal the internal state of the hash.
+// New returns a new [hash.Hash] computing the MD5 checksum. The Hash
+// also implements [encoding.BinaryMarshaler], [encoding.AppendBinary] and
+// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
+// state of the hash.
func New() hash.Hash {
d := new(digest)
d.Reset()
continue
}
+ stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
+ if err != nil {
+ t.Errorf("could not marshal: %v", err)
+ continue
+ }
+ stateAppend = stateAppend[4:]
+
if string(state) != g.halfState {
t.Errorf("md5(%q) state = %q, want %q", g.in, state, g.halfState)
continue
}
+ if string(stateAppend) != g.halfState {
+ t.Errorf("md5(%q) stateAppend = %q, want %q", g.in, stateAppend, g.halfState)
+ continue
+ }
+
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
continue
)
func (d *digest) MarshalBinary() ([]byte, error) {
- b := make([]byte, 0, marshaledSize)
+ return d.AppendBinary(make([]byte, 0, marshaledSize))
+}
+
+func (d *digest) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic...)
b = byteorder.BeAppendUint32(b, d.h[0])
b = byteorder.BeAppendUint32(b, d.h[1])
b = byteorder.BeAppendUint32(b, d.h[3])
b = byteorder.BeAppendUint32(b, d.h[4])
b = append(b, d.x[:d.nx]...)
- b = b[:len(b)+len(d.x)-d.nx] // already zero
+ b = append(b, make([]byte, len(d.x)-d.nx)...)
b = byteorder.BeAppendUint64(b, d.len)
return b, nil
}
d.len = 0
}
-// New returns a new hash.Hash computing the SHA1 checksum. The Hash also
-// implements [encoding.BinaryMarshaler] and [encoding.BinaryUnmarshaler] to
-// marshal and unmarshal the internal state of the hash.
+// New512_224 returns a new [hash.Hash] computing the SHA1 checksum. The Hash
+// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and
+// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
+// state of the hash.
func New() hash.Hash {
if boring.Enabled {
return boring.NewSHA1()
continue
}
+ stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
+ if err != nil {
+ t.Errorf("could not marshal: %v", err)
+ continue
+ }
+ stateAppend = stateAppend[4:]
+
if string(state) != g.halfState {
t.Errorf("sha1(%q) state = %+q, want %+q", g.in, state, g.halfState)
continue
}
+ if string(stateAppend) != g.halfState {
+ t.Errorf("sha1(%q) stateAppend = %+q, want %+q", g.in, stateAppend, g.halfState)
+ continue
+ }
+
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
continue
)
func (d *digest) MarshalBinary() ([]byte, error) {
- b := make([]byte, 0, marshaledSize)
+ return d.AppendBinary(make([]byte, 0, marshaledSize))
+}
+
+func (d *digest) AppendBinary(b []byte) ([]byte, error) {
if d.is224 {
b = append(b, magic224...)
} else {
b = byteorder.BeAppendUint32(b, d.h[6])
b = byteorder.BeAppendUint32(b, d.h[7])
b = append(b, d.x[:d.nx]...)
- b = b[:len(b)+len(d.x)-d.nx] // already zero
+ b = append(b, make([]byte, len(d.x)-d.nx)...)
b = byteorder.BeAppendUint64(b, d.len)
return b, nil
}
d.len = 0
}
-// New returns a new hash.Hash computing the SHA256 checksum. The Hash
-// also implements [encoding.BinaryMarshaler] and
+// New returns a new [hash.Hash] computing the SHA256 checksum. The Hash
+// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and
// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
// state of the hash.
func New() hash.Hash {
return d
}
-// New224 returns a new hash.Hash computing the SHA224 checksum.
+// New224 returns a new [hash.Hash] computing the SHA224 checksum. The Hash
+// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and
+// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
+// state of the hash.
func New224() hash.Hash {
if boring.Enabled {
return boring.NewSHA224()
continue
}
+ stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
+ if err != nil {
+ t.Errorf("could not marshal: %v", err)
+ continue
+ }
+ stateAppend = stateAppend[4:]
+
if string(state) != g.halfState {
t.Errorf("sha%s(%q) state = %q, want %q", tt.name, g.in, state, g.halfState)
continue
}
+ if string(stateAppend) != g.halfState {
+ t.Errorf("sha%s(%q) stateAppend = %q, want %q", tt.name, g.in, stateAppend, g.halfState)
+ continue
+ }
+
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
continue
)
func (d *digest) MarshalBinary() ([]byte, error) {
- b := make([]byte, 0, marshaledSize)
+ return d.AppendBinary(make([]byte, 0, marshaledSize))
+}
+
+func (d *digest) AppendBinary(b []byte) ([]byte, error) {
switch d.function {
case crypto.SHA384:
b = append(b, magic384...)
b = byteorder.BeAppendUint64(b, d.h[6])
b = byteorder.BeAppendUint64(b, d.h[7])
b = append(b, d.x[:d.nx]...)
- b = b[:len(b)+len(d.x)-d.nx] // already zero
+ b = append(b, make([]byte, len(d.x)-d.nx)...)
b = byteorder.BeAppendUint64(b, d.len)
return b, nil
}
return b[8:], byteorder.BeUint64(b)
}
-// New returns a new hash.Hash computing the SHA-512 checksum.
+// New returns a new [hash.Hash] computing the SHA-512 checksum. The Hash
+// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and
+// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
+// state of the hash.
func New() hash.Hash {
if boring.Enabled {
return boring.NewSHA512()
return d
}
-// New512_224 returns a new hash.Hash computing the SHA-512/224 checksum.
+// New512_224 returns a new [hash.Hash] computing the SHA-512/224 checksum. The Hash
+// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and
+// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
+// state of the hash.
func New512_224() hash.Hash {
d := &digest{function: crypto.SHA512_224}
d.Reset()
return d
}
-// New512_256 returns a new hash.Hash computing the SHA-512/256 checksum.
+// New512_256 returns a new [hash.Hash] computing the SHA-512/256 checksum. The Hash
+// also implements [encoding.BinaryMarshaler], [encoding.BinaryAppender] and
+// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
+// state of the hash.
func New512_256() hash.Hash {
d := &digest{function: crypto.SHA512_256}
d.Reset()
return d
}
-// New384 returns a new hash.Hash computing the SHA-384 checksum.
+// New384 returns a new [hash.Hash] computing the SHA-384 checksum. The Hash
+// also implements [encoding.BinaryMarshaler], [encoding.AppendBinary] and
+// [encoding.BinaryUnmarshaler] to marshal and unmarshal the internal
+// state of the hash.
func New384() hash.Hash {
if boring.Enabled {
return boring.NewSHA384()
return
}
+ stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
+ if err != nil {
+ t.Errorf("could not marshal: %v", err)
+ return
+ }
+ stateAppend = stateAppend[4:]
+
if string(state) != test.halfState {
t.Errorf("New%s(%q) state = %q, want %q", tt.name, test.in, state, test.halfState)
continue
}
+ if string(stateAppend) != test.halfState {
+ t.Errorf("New%s(%q) stateAppend = %q, want %q", tt.name, test.in, stateAppend, test.halfState)
+ continue
+ }
+
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
return