hasher

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: BSD-2-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package hasher provides types and interfaces for hash calculating.

Index

Constants

View Source
const (
	AlgoSHA256 = "sha256"
	AlgoSHA1   = "sha1"
)

Algorithm names returned by the built-in hashers' Name method. Use these with integrity codec location options (e.g. WithHashLocation) instead of retyping the bare string.

Variables

View Source
var ErrHashMismatch = errors.New("hasher: hash mismatch")

ErrHashMismatch is returned by Verify when the stored digest does not match any encoding of the computed digest accepted under the hasher's Mode.

Functions

This section is empty.

Types

type Hasher

type Hasher interface {
	Name() string
	// Hash returns the digest of data, encoded according to the hasher's Mode.
	Hash(data []byte) ([]byte, error)
	// Verify reports whether stored is an accepted encoding of the digest of
	// data under the hasher's Mode. It returns nil on a match.
	Verify(data, stored []byte) error
}

Hasher is the interface that storage hashers must implement. It provides low-level operations for hash calculating.

The encoding of the produced digest and the encodings accepted by Verify are controlled by the hasher's Mode (see ModeAuto, ModeHex, ModeBin). By default (ModeAuto) Hash returns the raw digest bytes and Verify accepts a stored digest in either raw or hex form.

Implementations must accept nil and zero-length input and return the well-defined hash of the empty bit string for both. This matters because storage backends round-trip empty values as nil, so a read of a legitimately-empty value must hash without error.

func NewSHA1Hasher

func NewSHA1Hasher(opts ...Option) Hasher

NewSHA1Hasher creates a new SHA-1 hasher. Its encoding behaviour is controlled by the given options; see WithMode (default ModeAuto).

func NewSHA256Hasher

func NewSHA256Hasher(opts ...Option) Hasher

NewSHA256Hasher creates a new SHA-256 hasher. Its encoding behaviour is controlled by the given options; see WithMode (default ModeAuto).

type Mode

type Mode int

Mode selects how a Hasher encodes the digest it produces and which encodings it accepts when verifying a stored digest.

const (
	// ModeAuto produces the raw digest bytes and, on verification, accepts a
	// stored digest in either raw or lower-case hex form. It is the default.
	ModeAuto Mode = iota
	// ModeHex produces a lower-case hex digest and accepts only hex on
	// verification.
	ModeHex
	// ModeBin produces the raw digest bytes and accepts only raw bytes on
	// verification.
	ModeBin
)

type Option

type Option func(*config)

Option configures a Hasher constructor.

func WithMode

func WithMode(m Mode) Option

WithMode sets the encoding mode of the hasher. See ModeAuto, ModeHex and ModeBin. When omitted, ModeAuto is used.

Jump to

Keyboard shortcuts

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