diff --git a/pkg/checksum.go b/pkg/checksum.go index 4fa84cda..2a93b399 100644 --- a/pkg/checksum.go +++ b/pkg/checksum.go @@ -1,6 +1,7 @@ package pkg import ( + "bytes" "crypto/sha256" "github.com/nspcc-dev/neofs-api-go/v2/refs" @@ -72,3 +73,7 @@ func (c *Checksum) SetTillichZemor(v [64]byte) { func (c *Checksum) ToV2() *refs.Checksum { return (*refs.Checksum)(c) } + +func EqualChecksums(cs1, cs2 *Checksum) bool { + return cs1.GetType() == cs2.GetType() && bytes.Equal(cs1.GetSum(), cs2.GetSum()) +} diff --git a/pkg/checksum_test.go b/pkg/checksum_test.go index e0da6ef8..906f68b5 100644 --- a/pkg/checksum_test.go +++ b/pkg/checksum_test.go @@ -38,3 +38,23 @@ func TestChecksum(t *testing.T) { require.Equal(t, refs.TillichZemor, cV2.GetType()) require.Equal(t, cTZ[:], cV2.GetSum()) } + +func TestEqualChecksums(t *testing.T) { + require.True(t, EqualChecksums(nil, nil)) + + csSHA := [sha256.Size]byte{} + _, _ = rand.Read(csSHA[:]) + + cs1 := NewChecksum() + cs1.SetSHA256(csSHA) + + cs2 := NewChecksum() + cs2.SetSHA256(csSHA) + + require.True(t, EqualChecksums(cs1, cs2)) + + csSHA[0]++ + cs2.SetSHA256(csSHA) + + require.False(t, EqualChecksums(cs1, cs2)) +}