hash

package
v1.0.4-gitspaces-beta Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SerializeHead

func SerializeHead(value string) []byte

SerializeHead serializes the head to prepare it for hashing.

func SerializeReference

func SerializeReference(ref string, sha string) []byte

SerializeReference serializes a reference to prepare it for hashing.

Types

type AggregationType

type AggregationType string

AggregationType defines the different types of hash aggregation types available.

const (
	// AggregationTypeXOR aggregates a list of hashes using XOR.
	// It provides commutative, self-inverse hashing, e.g.:
	// - order of elements doesn't matter
	// - two equal elements having the same hash cancel each other out.
	AggregationTypeXOR AggregationType = "xor"
)

type Aggregator

type Aggregator interface {
	// Empty returns the empty hash of an aggregator. It is returned when hashing an empty Source
	// or hashing a Source who's hash is equal to an empty source. Furthermore, the following is always true:
	// `Hash(s) == Append(Empty(), s)` FOR ALL sources s.
	Empty() []byte

	// Hash returns the hash aggregated over all elements of the provided source.
	Hash(source Source) ([]byte, error)

	// Append returns the hash that results when aggregating the existing hash
	// with the hashes of all elements of the provided source.
	// IMPORTANT: size of existing hash has to be compatible (Empty() can be used for reference).
	Append(hash []byte, source Source) ([]byte, error)
}

Aggregator is an abstraction of a component that aggregates a list of values into a single hash.

func New

func New(t Type, at AggregationType) (Aggregator, error)

New returns a new aggregator for the given aggregation and hashing type.

type Source

type Source interface {
	Next() ([]byte, error)
}

Source is an abstraction of a source of values that have to be hashed.

func SourceFromChannel

func SourceFromChannel(ctx context.Context, nextChan <-chan SourceNext) Source

SourceFromChannel creates a source that returns all elements read from nextChan. The .Data and .Err of a SourceNext object in the channel will be returned as is. If the channel is closed, the source indicates the end of the data.

func SourceFromSlice

func SourceFromSlice(slice [][]byte) Source

SourceFromSlice returns a source that iterates over the slice.

type SourceFunc

type SourceFunc func() ([]byte, error)

SourceFunc is an alias for a function that returns the content of a source call by call.

func (SourceFunc) Next

func (f SourceFunc) Next() ([]byte, error)

type SourceNext

type SourceNext struct {
	Data []byte
	Err  error
}

SourceNext encapsulates the data that is needed to serve a call to Source.Next(). It is being used by SourceFromChannel to expose a channel as Source.

type Type

type Type string

Type defines the different types of hashing that are supported. NOTE: package doesn't take hash.Hash as input to allow external callers to both calculate the hash themselves using this package, or call git to calculate the hash, without the caller having to know internal details on what hash.Hash implementation is used.

const (
	// TypeSHA256 represents the sha256 hashing method.
	TypeSHA256 Type = "sha256"
)

Jump to

Keyboard shortcuts

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