From 5902f96f3e7d81e2deffa354ddfafe0539141d05 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 10 Sep 2020 14:30:29 +0300 Subject: [PATCH] [#140] sdk: Define checksum type Signed-off-by: Leonard Lyubich --- pkg/checksum.go | 74 ++++++++++++++++++++++++++++++++++++++++++++ pkg/checksum_test.go | 40 ++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 pkg/checksum.go create mode 100644 pkg/checksum_test.go diff --git a/pkg/checksum.go b/pkg/checksum.go new file mode 100644 index 0000000..4fa84cd --- /dev/null +++ b/pkg/checksum.go @@ -0,0 +1,74 @@ +package pkg + +import ( + "crypto/sha256" + + "github.com/nspcc-dev/neofs-api-go/v2/refs" +) + +// Checksum represents v2-compatible checksum. +type Checksum refs.Checksum + +// ChecksumType represents the enumeration +// of checksum types. +type ChecksumType uint8 + +const ( + // ChecksumUnknown is an undefined checksum type. + ChecksumUnknown ChecksumType = iota + + // ChecksumSHA256 is a SHA256 checksum type. + ChecksumSHA256 + + // ChecksumTZ is a Tillich-Zemor checksum type. + ChecksumTZ +) + +// NewChecksumFromV2 wraps v2 Checksum message to Checksum. +func NewChecksumFromV2(cV2 *refs.Checksum) *Checksum { + return (*Checksum)(cV2) +} + +// NewVersion creates and initializes blank Version. +// +// Works similar as NewVersionFromV2(new(Version)). +func NewChecksum() *Checksum { + return NewChecksumFromV2(new(refs.Checksum)) +} + +// GetType returns checksum type. +func (c *Checksum) GetType() ChecksumType { + switch (*refs.Checksum)(c).GetType() { + case refs.SHA256: + return ChecksumSHA256 + case refs.TillichZemor: + return ChecksumTZ + default: + return ChecksumUnknown + } +} + +// GetSum returns checksum bytes. +func (c *Checksum) GetSum() []byte { + return (*refs.Checksum)(c).GetSum() +} + +// SetSHA256 sets checksum to SHA256 hash. +func (c *Checksum) SetSHA256(v [sha256.Size]byte) { + checksum := (*refs.Checksum)(c) + + checksum.SetType(refs.SHA256) + checksum.SetSum(v[:]) +} + +// SetTillichZemor sets checksum to Tillich-Zemor hash. +func (c *Checksum) SetTillichZemor(v [64]byte) { + checksum := (*refs.Checksum)(c) + + checksum.SetType(refs.TillichZemor) + checksum.SetSum(v[:]) +} + +func (c *Checksum) ToV2() *refs.Checksum { + return (*refs.Checksum)(c) +} diff --git a/pkg/checksum_test.go b/pkg/checksum_test.go new file mode 100644 index 0000000..e0da6ef --- /dev/null +++ b/pkg/checksum_test.go @@ -0,0 +1,40 @@ +package pkg + +import ( + "crypto/rand" + "crypto/sha256" + "testing" + + "github.com/nspcc-dev/neofs-api-go/v2/refs" + "github.com/stretchr/testify/require" +) + +func TestChecksum(t *testing.T) { + c := NewChecksum() + + cSHA256 := [sha256.Size]byte{} + _, _ = rand.Read(cSHA256[:]) + + c.SetSHA256(cSHA256) + + require.Equal(t, ChecksumSHA256, c.GetType()) + require.Equal(t, cSHA256[:], c.GetSum()) + + cV2 := c.ToV2() + + require.Equal(t, refs.SHA256, cV2.GetType()) + require.Equal(t, cSHA256[:], cV2.GetSum()) + + cTZ := [64]byte{} + _, _ = rand.Read(cSHA256[:]) + + c.SetTillichZemor(cTZ) + + require.Equal(t, ChecksumTZ, c.GetType()) + require.Equal(t, cTZ[:], c.GetSum()) + + cV2 = c.ToV2() + + require.Equal(t, refs.TillichZemor, cV2.GetType()) + require.Equal(t, cTZ[:], cV2.GetSum()) +}