hashing

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package hashing exposes the full set of cryptographic hash algorithms the Truestamp backend accepts for a claim's `hash_type` field. The goal is feature-parity with sha256sum / shasum / openssl dgst so users do not have to shell out to those tools for a one-off digest.

Algorithm names match the string forms in internal/tscrypto/hash.go's hashTypes registry, with common aliases (sha3-256/sha3_256, blake2b vs blake2b-512) accepted case-insensitively. Output formatting is byte-identical to GNU coreutils' sha256sum (text and binary modes, with the standard `\` escaping for filenames containing `\` or `\n`) and to `shasum --tag` / BSD md5(1)'s tagged form.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compute

func Compute(ctx context.Context, alg Algorithm, r io.Reader) ([]byte, int64, error)

Compute streams r through alg's hasher and returns the digest bytes and the total byte count consumed. Never buffers the whole input. The context is checked once before starting; I/O errors are wrapped.

func FormatBSD

func FormatBSD(algBSDName, digestHex, filename string) string

FormatBSD produces output byte-identical to `shasum --tag` / BSD md5(1)'s tagged form:

"<NAME> (<filename>) = <hex>\n"

func FormatGNU

func FormatGNU(digestHex, filename string, binaryMode bool) string

FormatGNU produces output byte-identical to GNU coreutils' sha256sum (and friends):

text mode:   "<hex>  <filename>\n"
binary mode: "<hex> *<filename>\n"

Filenames containing '\' or '\n' get GNU's escape treatment: the line is prefixed with '\' and the problematic bytes are escaped as "\\" and "\n". Callers on filesystems that cannot produce such names still get the unescaped form.

Types

type Algorithm

type Algorithm struct {
	Name    string // canonical name (kebab-case)
	Aliases []string
	Size    int              // digest length in bytes
	Legacy  bool             // true for broken algorithms (MD5, SHA-1)
	BSDName string           // upper-case name used in shasum --tag / BSD output
	New     func() hash.Hash // constructor; never returns nil
}

Algorithm describes a single supported hash algorithm.

func Algorithms

func Algorithms() []Algorithm

Algorithms returns a copy of the registry, ordered for display.

func Lookup

func Lookup(name string) (Algorithm, error)

Lookup resolves a name (canonical or alias, case-insensitive) to an Algorithm entry. An unknown name returns a wrapped error listing the supported canonical names so CLI users get immediate guidance.

Jump to

Keyboard shortcuts

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