crypto

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package crypto provides password hashing and verification using the argon2id algorithm. The PHC string format is used so parameters are self-describing and future algorithm changes do not require a separate migration column.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidKey is returned by NewCipher when the key is not 32 bytes.
	ErrInvalidKey = errors.New("crypto: encryption key must be 32 bytes")
	// ErrMalformedCiphertext is returned by Decrypt when the input is too short
	// to contain a nonce or otherwise fails authentication.
	ErrMalformedCiphertext = errors.New("crypto: malformed ciphertext")
)

Crypto errors for the symmetric cipher.

View Source
var ErrMalformedHash = errors.New("crypto: malformed argon2id hash")

ErrMalformedHash is returned by Verify when the encoded string is not a valid PHC-format argon2id hash produced by Hash.

Functions

func Hash

func Hash(password string) (string, error)

Hash derives a new argon2id hash from password using a freshly generated random salt and returns the PHC-encoded string. Each call produces a different output even for the same password (due to the random salt), so the returned string must be stored and compared with Verify, not compared directly.

Hash returns an error only when the system's random source is unavailable, which is a fatal condition.

func Verify

func Verify(password, encoded string) (bool, error)

Verify checks whether password matches the PHC-encoded argon2id hash produced by Hash. It returns (true, nil) on a match, (false, nil) on a mismatch, and (false, ErrMalformedHash) when encoded is not a valid PHC argon2id string. The comparison is constant-time to resist timing attacks.

Types

type Cipher

type Cipher struct {
	// contains filtered or unexported fields
}

Cipher encrypts and decrypts small secrets (e.g. OAuth tokens) at rest using AES-256-GCM, which provides confidentiality and authentication. The key is injected via the constructor (never a package global) so it can be sourced from configuration and kept out of logs.

func NewCipher

func NewCipher(key []byte) (*Cipher, error)

NewCipher constructs a Cipher from a 32-byte key, returning ErrInvalidKey for any other length.

func (*Cipher) Decrypt

func (c *Cipher) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt reverses Encrypt: it splits the nonce from the ciphertext and opens it, returning ErrMalformedCiphertext when the input is too short or fails the GCM authentication tag (tampering or a wrong key).

func (*Cipher) Encrypt

func (c *Cipher) Encrypt(plaintext []byte) ([]byte, error)

Encrypt seals plaintext under a fresh random nonce and returns the nonce prepended to the ciphertext (nonce || ciphertext+tag). It errors only when the system random source is unavailable.

type Hasher

type Hasher struct {
	// contains filtered or unexported fields
}

Hasher derives and verifies argon2id hashes at a fixed cost. Construct one with NewHasher and inject it, rather than calling the package-level Hash and Verify, wherever the cost needs to be selectable (see Params).

func NewHasher

func NewHasher(params Params) *Hasher

NewHasher constructs a Hasher that derives new hashes at the supplied cost.

func (*Hasher) Hash

func (h *Hasher) Hash(password string) (string, error)

Hash derives a new argon2id hash at h's configured cost. See the package-level Hash for the full contract.

func (*Hasher) Verify

func (h *Hasher) Verify(password, encoded string) (bool, error)

Verify checks password against the PHC-encoded hash. See the package-level Verify for the full contract.

The receiver's Params are deliberately unused: the cost parameters come from the encoded hash itself, so a Hasher verifies hashes produced at ANY cost, including ones written before it was configured. Verify is a method purely so that Hash and Verify can be injected together as one seam.

type Params

type Params struct {
	// Time is the number of passes over the memory.
	Time uint32
	// Memory is the memory cost in KiB.
	Memory uint32
	// Threads is the degree of parallelism.
	Threads uint8
}

Params holds the tunable argon2id cost parameters. Only the cost knobs are configurable; the salt and key lengths are fixed package-wide (see above).

Production code must use DefaultParams. The type is exported so that tests, which would otherwise pay a 64 MiB memory-hard derivation per hash, can construct a Hasher with cheap parameters: recovery-code flows hash ten codes per enrollment, which dominated the test suite's runtime.

Lowering these values weakens resistance to offline dictionary attacks and is only ever safe for throwaway test fixtures.

func DefaultParams

func DefaultParams() Params

DefaultParams returns the OWASP-recommended production cost parameters.

Directories

Path Synopsis
Package cryptotest provides test-only helpers for the crypto package, following the convention of the standard library's httptest.
Package cryptotest provides test-only helpers for the crypto package, following the convention of the standard library's httptest.

Jump to

Keyboard shortcuts

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