checksum

package
v1.0.0-rc.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2023 License: Apache-2.0 Imports: 6 Imported by: 5

Documentation

Overview

Package checksum provides primitives to work with checksums.

Checksum is a basic type of data checksums.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Calculate

func Calculate(c *Checksum, t Type, v []byte)

Calculate calculates checksum and sets it to the passed checksum. Checksum must not be nil.

Does nothing if the passed type is not one of the:

  • SHA256;
  • TZ.

Does not mutate the passed value.

See also SetSHA256, SetTillichZemor.

Example
payload := []byte{0, 1, 2, 3, 4, 5, 6}
var checksum Checksum

// checksum contains SHA256 hash of the payload
Calculate(&checksum, SHA256, payload)

// checksum contains TZ hash of the payload
Calculate(&checksum, TZ, payload)
Output:

Types

type Checksum

type Checksum refs.Checksum

Checksum represents checksum of some digital data.

Checksum is mutually compatible with github.com/nspcc-dev/neofs-api-go/v2/refs.Checksum message. See ReadFromV2 / WriteToV2 methods.

Instances can be created using built-in var declaration.

Note that direct typecast is not safe and may result in loss of compatibility:

_ = Checksum(refs.Checksum{}) // not recommended
Example (Marshalling)

Instances can be also used to process NeoFS API V2 protocol messages with [https://github.com/nspcc-dev/neofs-api] package.

var (
	csRaw [sha256.Size]byte
	csV2  refs.Checksum
	cs    Checksum
)

rand.Read(csRaw[:])
cs.SetSHA256(csRaw)

// On the client side.

cs.WriteToV2(&csV2)

fmt.Println(bytes.Equal(cs.Value(), csV2.GetSum()))
// Example output: true

// *send message*

// On the server side.

_ = cs.ReadFromV2(csV2)
Output:

func (*Checksum) ReadFromV2

func (c *Checksum) ReadFromV2(m refs.Checksum) error

ReadFromV2 reads Checksum from the refs.Checksum message. Checks if the message conforms to NeoFS API V2 protocol.

See also WriteToV2.

func (*Checksum) SetSHA256

func (c *Checksum) SetSHA256(v [sha256.Size]byte)

SetSHA256 sets checksum to SHA256 hash.

See also Calculate.

func (*Checksum) SetTillichZemor

func (c *Checksum) SetTillichZemor(v [tz.Size]byte)

SetTillichZemor sets checksum to Tillich-Zémor hash.

See also Calculate.

func (Checksum) String

func (c Checksum) String() string

String implements fmt.Stringer.

String is designed to be human-readable, and its format MAY differ between SDK versions.

func (Checksum) Type

func (c Checksum) Type() Type

Type returns checksum type.

Zero Checksum has Unknown checksum type.

See also SetTillichZemor and SetSHA256.

func (Checksum) Value

func (c Checksum) Value() []byte

Value returns checksum bytes. Return value MUST NOT be mutated.

Zero Checksum has nil sum.

The value returned shares memory with the structure itself, so changing it can lead to data corruption. Make a copy if you need to change it.

See also SetTillichZemor and SetSHA256.

func (Checksum) WriteToV2

func (c Checksum) WriteToV2(m *refs.Checksum)

WriteToV2 writes Checksum to the refs.Checksum message. The message must not be nil.

See also ReadFromV2.

type Type

type Type uint8

Type represents the enumeration of checksum types.

const (
	// Unknown is an undefined checksum type.
	Unknown Type = iota

	// SHA256 is a SHA256 checksum type.
	SHA256

	// TZ is a Tillich-Zémor checksum type.
	TZ
)

func (Type) String

func (m Type) String() string

String implements fmt.Stringer.

String is designed to be human-readable, and its format MAY differ between SDK versions.

Directories

Path Synopsis
Package checksumtest provides functions for convenient testing of checksum package API.
Package checksumtest provides functions for convenient testing of checksum package API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL