hash: implement the encoding.BinaryAppender interface

For #62384
This commit is contained in:
apocelipes 2024-08-07 05:56:38 +08:00
parent 44ed517c42
commit 192f389d46
12 changed files with 122 additions and 18 deletions

View File

@ -0,0 +1 @@
The value returned by [New] now also implements the [encoding.BinaryAppender] interface.

View File

@ -0,0 +1 @@
The values returned by [New] and [NewIEEE] now also implement the [encoding.BinaryAppender] interface.

View File

@ -0,0 +1 @@
The value returned by [New] now also implements the [encoding.BinaryAppender] interface.

View File

@ -0,0 +1 @@
The values returned by [New32], [New32a], [New64], [New64a], [New128] and [New128a] now also implement the [encoding.BinaryAppender] interface.

View File

@ -57,13 +57,16 @@ const (
marshaledSize = len(magic) + 4
)
func (d *digest) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize)
func (d *digest) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic...)
b = byteorder.BeAppendUint32(b, uint32(*d))
return b, nil
}
func (d *digest) MarshalBinary() ([]byte, error) {
return d.AppendBinary(make([]byte, 0, marshaledSize))
}
func (d *digest) UnmarshalBinary(b []byte) error {
if len(b) < len(magic) || string(b[:len(magic)]) != magic {
return errors.New("hash/adler32: invalid hash state identifier")

View File

@ -103,11 +103,23 @@ func TestGoldenMarshal(t *testing.T) {
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("checksum(%q) state = %q, want %q", g.in, state, g.halfState)
continue
}
if string(stateAppend) != g.halfState {
t.Errorf("checksum(%q) state = %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

View File

@ -170,14 +170,18 @@ const (
marshaledSize = len(magic) + 4 + 4
)
func (d *digest) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize)
func (d *digest) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic...)
b = byteorder.BeAppendUint32(b, tableSum(d.tab))
b = byteorder.BeAppendUint32(b, d.crc)
return b, nil
}
func (d *digest) MarshalBinary() ([]byte, error) {
return d.AppendBinary(make([]byte, 0, marshaledSize))
}
func (d *digest) UnmarshalBinary(b []byte) error {
if len(b) < len(magic) || string(b[:len(magic)]) != magic {
return errors.New("hash/crc32: invalid hash state identifier")

View File

@ -133,11 +133,23 @@ func TestGoldenMarshal(t *testing.T) {
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.halfStateIEEE {
t.Errorf("IEEE(%q) state = %q, want %q", g.in, state, g.halfStateIEEE)
continue
}
if string(stateAppend) != g.halfStateIEEE {
t.Errorf("IEEE(%q) state = %q, want %q", g.in, stateAppend, g.halfStateIEEE)
continue
}
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
continue
@ -165,11 +177,23 @@ func TestGoldenMarshal(t *testing.T) {
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.halfStateCastagnoli {
t.Errorf("Castagnoli(%q) state = %q, want %q", g.in, state, g.halfStateCastagnoli)
continue
}
if string(stateAppend) != g.halfStateCastagnoli {
t.Errorf("Castagnoli(%q) state = %q, want %q", g.in, stateAppend, g.halfStateCastagnoli)
continue
}
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
continue

View File

@ -111,14 +111,17 @@ const (
marshaledSize = len(magic) + 8 + 8
)
func (d *digest) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize)
func (d *digest) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic...)
b = byteorder.BeAppendUint64(b, tableSum(d.tab))
b = byteorder.BeAppendUint64(b, d.crc)
return b, nil
}
func (d *digest) MarshalBinary() ([]byte, error) {
return d.AppendBinary(make([]byte, 0, marshaledSize))
}
func (d *digest) UnmarshalBinary(b []byte) error {
if len(b) < len(magic) || string(b[:len(magic)]) != magic {
return errors.New("hash/crc64: invalid hash state identifier")

View File

@ -88,11 +88,23 @@ func TestGoldenMarshal(t *testing.T) {
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.halfStateISO {
t.Errorf("ISO crc64(%q) state = %q, want %q", g.in, state, g.halfStateISO)
continue
}
if string(stateAppend) != g.halfStateISO {
t.Errorf("ISO crc64(%q) state = %q, want %q", g.in, stateAppend, g.halfStateISO)
continue
}
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
continue
@ -120,11 +132,23 @@ func TestGoldenMarshal(t *testing.T) {
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.halfStateECMA {
t.Errorf("ECMA crc64(%q) state = %q, want %q", g.in, state, g.halfStateECMA)
continue
}
if string(stateAppend) != g.halfStateECMA {
t.Errorf("ECMA crc64(%q) state = %q, want %q", g.in, stateAppend, g.halfStateECMA)
continue
}
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
t.Errorf("could not unmarshal: %v", err)
continue

View File

@ -219,50 +219,68 @@ const (
marshaledSize128 = len(magic128) + 8*2
)
func (s *sum32) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize32)
func (s *sum32) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic32...)
b = byteorder.BeAppendUint32(b, uint32(*s))
return b, nil
}
func (s *sum32a) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize32)
func (s *sum32) MarshalBinary() ([]byte, error) {
return s.AppendBinary(make([]byte, 0, marshaledSize32))
}
func (s *sum32a) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic32a...)
b = byteorder.BeAppendUint32(b, uint32(*s))
return b, nil
}
func (s *sum64) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize64)
func (s *sum32a) MarshalBinary() ([]byte, error) {
return s.AppendBinary(make([]byte, 0, marshaledSize32))
}
func (s *sum64) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic64...)
b = byteorder.BeAppendUint64(b, uint64(*s))
return b, nil
}
func (s *sum64a) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize64)
func (s *sum64) MarshalBinary() ([]byte, error) {
return s.AppendBinary(make([]byte, 0, marshaledSize64))
}
func (s *sum64a) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic64a...)
b = byteorder.BeAppendUint64(b, uint64(*s))
return b, nil
}
func (s *sum128) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize128)
func (s *sum64a) MarshalBinary() ([]byte, error) {
return s.AppendBinary(make([]byte, 0, marshaledSize64))
}
func (s *sum128) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic128...)
b = byteorder.BeAppendUint64(b, s[0])
b = byteorder.BeAppendUint64(b, s[1])
return b, nil
}
func (s *sum128a) MarshalBinary() ([]byte, error) {
b := make([]byte, 0, marshaledSize128)
func (s *sum128) MarshalBinary() ([]byte, error) {
return s.AppendBinary(make([]byte, 0, marshaledSize128))
}
func (s *sum128a) AppendBinary(b []byte) ([]byte, error) {
b = append(b, magic128a...)
b = byteorder.BeAppendUint64(b, s[0])
b = byteorder.BeAppendUint64(b, s[1])
return b, nil
}
func (s *sum128a) MarshalBinary() ([]byte, error) {
return s.AppendBinary(make([]byte, 0, marshaledSize128))
}
func (s *sum32) UnmarshalBinary(b []byte) error {
if len(b) < len(magic32) || string(b[:len(magic32)]) != magic32 {
return errors.New("hash/fnv: invalid hash state identifier")

View File

@ -128,11 +128,23 @@ func TestGoldenMarshal(t *testing.T) {
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("checksum(%q) state = %q, want %q", g.in, state, g.halfState)
continue
}
if string(stateAppend) != g.halfState {
t.Errorf("checksum(%q) state = %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