hashing

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: AGPL-3.0 Imports: 1 Imported by: 0

Documentation

Overview

Package hashing is the seam for reducing content to a digest, so that which algorithm computes it can be a runtime choice.

Implementations live in subpackages and vary in what they guarantee, which is the thing to get right before picking one. sha256 and sha512 are cryptographic digests. adler32, crc64, and fnv are checksums: fast, fine for detecting accidental corruption or spreading keys across buckets, and no obstacle at all to someone constructing a second input with the same digest. hmac is keyed, and therefore not interchangeable with any of them — two hmac hashers built from different keys disagree on every input by design, and their output is compared with hmac.Equal rather than through a hex rendering and ==, which is not constant-time.

None of these is a password hasher. A digest is fast on purpose, and the property a stored password needs is the opposite one; use authentication/argon2.

Hash returns raw bytes rather than an encoding of them, and Hex and HexString render them for the common case. There is no error return: every implementation here wraps a hash.Hash, whose Write is documented never to fail, so an error would be an unreachable branch at every call site.

The interface fits a hasher that digests one in-memory buffer. Anything needing streaming, Size, or BlockSize wants a hash.Hash directly rather than a wider interface here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Hex

func Hex(h Hasher, content []byte) string

Hex returns h's digest of content, hex-encoded. It is the conventional rendering of a digest for logs, storage, and comparison; reach past it to Hasher.Hash only when the raw bytes matter.

func HexString

func HexString(h Hasher, content string) string

HexString is Hex for a string input, saving callers that already hold a string a conversion at every call site.

Types

type Hasher

type Hasher interface {
	Hash(content []byte) []byte
}

Hasher reduces content to a fixed-size digest.

Hash returns the raw digest bytes rather than an encoding of them, so callers that need an integer, a prefix, or a non-hex encoding are not forced through a string. Hex covers the common case.

There is no error return: every implementation here wraps a hash.Hash, whose Write is documented never to fail, so an error would only ever be an unreachable branch at each call site.

NOTE: implementations of this interface vary in cryptographic strength. The sha256 and sha512 implementations are cryptographic hashes; the adler32, crc64, and fnv implementations are NON-CRYPTOGRAPHIC checksums and MUST NOT be selected for security, password, or tamper-resistance purposes. Choose the implementation deliberately.

The hmac implementations are keyed, and so are not interchangeable with the rest at all: they authenticate content under a key fixed at construction rather than digest it, and two hashers built from different keys disagree on every input by design. Compare their output with hmac.Equal rather than through Hex and ==, which is not constant-time.

A Hasher that only ever hashes one in-memory buffer belongs here; one that needs streaming, Size, or BlockSize should take a hash.Hash directly rather than widening this interface.

Directories

Path Synopsis
Package adler32 implements hashing.Hasher using the Adler-32 checksum.
Package adler32 implements hashing.Hasher using the Adler-32 checksum.
Package canonical hashes Go values by content, producing the same digest for semantically identical values regardless of which process built them, in what order, or how their types declare fields.
Package canonical hashes Go values by content, producing the same digest for semantically identical values regardless of which process built them, in what order, or how their types declare fields.
Package crc64 implements hashing.Hasher using the CRC-64 (ISO) checksum, and exposes ChecksumISO for callers that want the checksum as the integer it natively is.
Package crc64 implements hashing.Hasher using the CRC-64 (ISO) checksum, and exposes ChecksumISO for callers that want the checksum as the integer it natively is.
Package fnv implements hashing.Hasher using the FNV-1a hash, and exposes Sum64a and Sum128a for callers that want the hash as the integer it natively is.
Package fnv implements hashing.Hasher using the FNV-1a hash, and exposes Sum64a and Sum128a for callers that want the hash as the integer it natively is.
Package hmac provides keyed hashing.Hasher implementations, for the cases where a digest has to prove who computed it rather than only what was computed.
Package hmac provides keyed hashing.Hasher implementations, for the cases where a digest has to prove who computed it rather than only what was computed.
Package sha256 implements hashing.Hasher using SHA-256, producing a 32-byte digest.
Package sha256 implements hashing.Hasher using SHA-256, producing a 32-byte digest.
Package sha512 implements hashing.Hasher using SHA-512, producing a 64-byte digest.
Package sha512 implements hashing.Hasher using SHA-512, producing a 64-byte digest.

Jump to

Keyboard shortcuts

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