[#140] sdk: Define checksum type
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
e0c34a51f2
commit
5902f96f3e
2 changed files with 114 additions and 0 deletions
74
pkg/checksum.go
Normal file
74
pkg/checksum.go
Normal file
|
@ -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)
|
||||
}
|
40
pkg/checksum_test.go
Normal file
40
pkg/checksum_test.go
Normal file
|
@ -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())
|
||||
}
|
Loading…
Reference in a new issue