Documentation
¶
Overview ¶
Package hasher provides types and interfaces for hash calculating.
Index ¶
Constants ¶
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 ¶
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 ¶
NewSHA1Hasher creates a new SHA-1 hasher. Its encoding behaviour is controlled by the given options; see WithMode (default ModeAuto).
func NewSHA256Hasher ¶
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 )