2020-09-10 11:30:29 +00:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
import (
|
2020-09-11 15:18:02 +00:00
|
|
|
"bytes"
|
2020-09-10 11:30:29 +00:00
|
|
|
"crypto/sha256"
|
2020-12-17 15:25:17 +00:00
|
|
|
"encoding/hex"
|
2021-05-17 12:56:17 +00:00
|
|
|
"fmt"
|
2020-09-10 11:30:29 +00:00
|
|
|
|
|
|
|
"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.
|
2021-06-08 18:02:30 +00:00
|
|
|
//
|
|
|
|
// Nil refs.Checksum converts to nil.
|
2020-09-10 11:30:29 +00:00
|
|
|
func NewChecksumFromV2(cV2 *refs.Checksum) *Checksum {
|
|
|
|
return (*Checksum)(cV2)
|
|
|
|
}
|
|
|
|
|
2021-06-08 18:09:06 +00:00
|
|
|
// NewChecksum creates and initializes blank Checksum.
|
2020-09-10 11:30:29 +00:00
|
|
|
//
|
2021-06-08 18:09:06 +00:00
|
|
|
// Works similar as NewChecksumFromV2(new(Checksum)).
|
|
|
|
//
|
|
|
|
// Defaults:
|
|
|
|
// - sum: nil;
|
|
|
|
// - type: ChecksumUnknown.
|
2020-09-10 11:30:29 +00:00
|
|
|
func NewChecksum() *Checksum {
|
|
|
|
return NewChecksumFromV2(new(refs.Checksum))
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:21:28 +00:00
|
|
|
// Type returns checksum type.
|
|
|
|
func (c *Checksum) Type() ChecksumType {
|
2020-09-10 11:30:29 +00:00
|
|
|
switch (*refs.Checksum)(c).GetType() {
|
|
|
|
case refs.SHA256:
|
|
|
|
return ChecksumSHA256
|
|
|
|
case refs.TillichZemor:
|
|
|
|
return ChecksumTZ
|
|
|
|
default:
|
|
|
|
return ChecksumUnknown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:21:28 +00:00
|
|
|
// Sum returns checksum bytes.
|
|
|
|
func (c *Checksum) Sum() []byte {
|
2020-09-10 11:30:29 +00:00
|
|
|
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[:])
|
|
|
|
}
|
|
|
|
|
2021-06-08 18:02:30 +00:00
|
|
|
// ToV2 converts Checksum to v2 Checksum message.
|
|
|
|
//
|
|
|
|
// Nil Checksum converts to nil.
|
2020-09-10 11:30:29 +00:00
|
|
|
func (c *Checksum) ToV2() *refs.Checksum {
|
|
|
|
return (*refs.Checksum)(c)
|
|
|
|
}
|
2020-09-11 15:18:02 +00:00
|
|
|
|
|
|
|
func EqualChecksums(cs1, cs2 *Checksum) bool {
|
2020-11-16 08:21:28 +00:00
|
|
|
return cs1.Type() == cs2.Type() && bytes.Equal(cs1.Sum(), cs2.Sum())
|
2020-09-11 15:18:02 +00:00
|
|
|
}
|
2020-11-13 13:30:35 +00:00
|
|
|
|
|
|
|
// Marshal marshals Checksum into a protobuf binary form.
|
|
|
|
//
|
|
|
|
// Buffer is allocated when the argument is empty.
|
|
|
|
// Otherwise, the first buffer is used.
|
|
|
|
func (c *Checksum) Marshal(b ...[]byte) ([]byte, error) {
|
|
|
|
var buf []byte
|
|
|
|
if len(b) > 0 {
|
|
|
|
buf = b[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*refs.Checksum)(c).
|
|
|
|
StableMarshal(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of Checksum.
|
|
|
|
func (c *Checksum) Unmarshal(data []byte) error {
|
|
|
|
return (*refs.Checksum)(c).
|
|
|
|
Unmarshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes Checksum to protobuf JSON format.
|
|
|
|
func (c *Checksum) MarshalJSON() ([]byte, error) {
|
|
|
|
return (*refs.Checksum)(c).
|
|
|
|
MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes Checksum from protobuf JSON format.
|
|
|
|
func (c *Checksum) UnmarshalJSON(data []byte) error {
|
|
|
|
return (*refs.Checksum)(c).
|
|
|
|
UnmarshalJSON(data)
|
|
|
|
}
|
2020-12-17 15:25:17 +00:00
|
|
|
|
|
|
|
func (c *Checksum) String() string {
|
|
|
|
return hex.EncodeToString(
|
|
|
|
(*refs.Checksum)(c).
|
|
|
|
GetSum(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses Checksum from its string representation.
|
|
|
|
func (c *Checksum) Parse(s string) error {
|
|
|
|
data, err := hex.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var typ refs.ChecksumType
|
|
|
|
|
|
|
|
switch ln := len(data); ln {
|
|
|
|
default:
|
2021-05-17 12:56:17 +00:00
|
|
|
return fmt.Errorf("unsupported checksum length %d", ln)
|
2020-12-17 15:25:17 +00:00
|
|
|
case sha256.Size:
|
|
|
|
typ = refs.SHA256
|
|
|
|
case 64:
|
|
|
|
typ = refs.TillichZemor
|
|
|
|
}
|
|
|
|
|
|
|
|
cV2 := (*refs.Checksum)(c)
|
|
|
|
cV2.SetType(typ)
|
|
|
|
cV2.SetSum(data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-18 12:27:01 +00:00
|
|
|
|
|
|
|
// String returns string representation of ChecksumType.
|
|
|
|
//
|
|
|
|
// String mapping:
|
|
|
|
// * ChecksumTZ: TZ;
|
|
|
|
// * ChecksumSHA256: SHA256;
|
|
|
|
// * ChecksumUnknown, default: CHECKSUM_TYPE_UNSPECIFIED.
|
|
|
|
func (m ChecksumType) String() string {
|
|
|
|
var m2 refs.ChecksumType
|
|
|
|
|
|
|
|
switch m {
|
|
|
|
default:
|
|
|
|
m2 = refs.UnknownChecksum
|
|
|
|
case ChecksumTZ:
|
|
|
|
m2 = refs.TillichZemor
|
|
|
|
case ChecksumSHA256:
|
|
|
|
m2 = refs.SHA256
|
|
|
|
}
|
|
|
|
|
|
|
|
return m2.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromString parses ChecksumType from a string representation.
|
|
|
|
// It is a reverse action to String().
|
|
|
|
//
|
|
|
|
// Returns true if s was parsed successfully.
|
|
|
|
func (m *ChecksumType) FromString(s string) bool {
|
|
|
|
var g refs.ChecksumType
|
|
|
|
|
|
|
|
ok := g.FromString(s)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
switch g {
|
|
|
|
default:
|
|
|
|
*m = ChecksumUnknown
|
|
|
|
case refs.TillichZemor:
|
|
|
|
*m = ChecksumTZ
|
|
|
|
case refs.SHA256:
|
|
|
|
*m = ChecksumSHA256
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok
|
|
|
|
}
|