Documentation
¶
Overview ¶
Package digest provides a type-safe SHA256 digest implementation.
The Digest type enforces the canonical format "sha256:<64 lowercase hex chars>" at construction time and provides constant-time comparison to prevent timing side-channel attacks.
Usage ¶
d, err := digest.Parse("sha256:abc123...")
if err != nil {
// handle invalid format
}
// Compute from bytes
d := digest.FromBytes(data)
// Constant-time comparison
if d.Equal(other) { ... }
Security Properties ¶
- Format validation at parse time (rejects malformed digests)
- Constant-time comparison (prevents timing attacks)
- Immutable after construction (prevents TOCTOU)
- JSON marshaling preserves format exactly
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Digest ¶
type Digest struct {
// contains filtered or unexported fields
}
Digest represents a SHA256 digest in canonical format. The zero value is invalid; use Parse, FromBytes, or FromReader to create.
func FromReader ¶
FromReader computes the SHA256 digest by reading from r.
func MustParse ¶
MustParse parses a digest string, panicking if invalid. Use only for compile-time constants and tests.
func Parse ¶
Parse parses a digest string in canonical format. Returns an error if the format is invalid.
func (Digest) Equal ¶
Equal reports whether d and other represent the same digest. Uses constant-time comparison to prevent timing side-channel attacks. Returns false if either digest is zero.
func (Digest) Hex ¶
Hex returns just the hex portion of the digest (without "sha256:" prefix). Returns empty string for zero-value Digest.
func (Digest) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (Digest) String ¶
String returns the canonical string representation. Returns empty string for zero-value Digest.
func (*Digest) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. Validates the digest format during unmarshaling.
type Hasher ¶
type Hasher struct {
// contains filtered or unexported fields
}
Hasher accumulates data for digest computation. Use NewHasher to create, Write to add data, and Digest to get the result. Hasher implements io.Writer for use with io.Copy, io.TeeReader, etc.
func NewHasher ¶
func NewHasher() *Hasher
NewHasher creates a new Hasher for incremental digest computation.