crypto

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package crypto provides password hashing behind a driver pattern: bcrypt (golang.org/x/crypto/bcrypt), argon (Argon2i) and argon2id (golang.org/x/crypto/argon2).

Role in architecture:

  • Infrastructure utility: no business logic; used by auth or user flows that need to hash/compare passwords.

Responsibilities:

  • Hasher: Driver / Hash / Check / NeedsRehash. Built with NewHasher(driver, HasherOptions) or NewHasherFromConfig(config.HashingConfiguration). Drivers: DriverBcrypt ("bcrypt", default), DriverArgon ("argon" = Argon2i), DriverArgon2id ("argon2id"); unknown names return ErrUnknownDriver.
  • Setup: build the default hasher from HASH_DRIVER / HASH_BCRYPT_ROUNDS / HASH_ARGON_* (config.GetConfig().Hashing). SetDefault / Default replace or read it; without Setup the default is bcrypt at DefaultCost.
  • Hash / NeedsRehash: package-level helpers using Default().
  • ComparePassword and Hasher.Check: constant-time check that accepts every supported format ($argon2id$, $argon2i$, bcrypt) regardless of the configured driver, so switching drivers keeps old hashes valid.
  • HashArgon2id / HashArgon2idWithParams / VerifyArgon2id / IsArgon2idHash: direct argon2id helpers. Argon2 hashes are PHC strings ($argon2id$v=19$m=..,t=..,p=..$salt$hash); DefaultArgon2Params follows the OWASP baseline (19 MiB, 2 iterations, parallelism 1).
  • HashAndSalt: hash with bcrypt.DefaultCost; log and return empty string on error.

Constraints:

  • Decoded argon2 parameters are bounded (memory <= 1 GiB, iterations <= 64) so stored values cannot force huge work.
  • Depends on config (Setup) and logger (error logging).

This package must NOT:

  • Store or retrieve passwords; only hash and compare.

Index

Constants

View Source
const (
	DriverBcrypt   = "bcrypt"
	DriverArgon    = "argon"    // Argon2i
	DriverArgon2id = "argon2id" // Argon2id (recommended)
)

Hash driver names accepted by NewHasher and HASH_DRIVER (Laravel-compatible naming).

Variables

View Source
var DefaultArgon2Params = Argon2Params{
	Memory:      19 * 1024,
	Iterations:  2,
	Parallelism: 1,
	SaltLength:  16,
	KeyLength:   32,
}

DefaultArgon2Params follows the OWASP Password Storage Cheat Sheet baseline for argon2id (19 MiB, 2 iterations, parallelism 1), with a 16-byte salt and 32-byte key.

View Source
var ErrInvalidArgon2Hash = errors.New("invalid argon2 hash")

ErrInvalidArgon2Hash is returned when a stored value is not a well-formed argon2 PHC string of the expected variant or its parameters are out of the accepted range.

View Source
var (
	// ErrUnknownDriver is returned by NewHasher for an unsupported driver name.
	ErrUnknownDriver = errors.New("unknown hash driver")
)

Functions

func ComparePassword

func ComparePassword(hashedPassword string, plainPassword []byte) bool

ComparePassword returns true if plainPassword matches hashedPassword. Accepts bcrypt, argon2i and argon2id hashes (detected by prefix), so hashes from any driver can coexist while migrating. Malformed hashes are logged and return false; empty or unrecognised values return false silently.

func Hash added in v0.6.0

func Hash(plainPassword []byte) (string, error)

Hash hashes plainPassword with the default Hasher (see Setup).

func HashAndSalt

func HashAndSalt(plainPassword []byte) string

HashAndSalt hashes plainPassword with bcrypt (DefaultCost) and returns the hash string. On bcrypt error logs and returns "". Hashes created with a lower cost still verify with ComparePassword (the cost is stored in the hash). To honour HASH_DRIVER use Hash (after Setup) or a Hasher from NewHasher instead.

func HashArgon2id added in v0.6.0

func HashArgon2id(plainPassword []byte) (string, error)

HashArgon2id hashes plainPassword with argon2id and DefaultArgon2Params. The result is a PHC string: $argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash> (raw std base64).

func HashArgon2idWithParams added in v0.6.0

func HashArgon2idWithParams(plainPassword []byte, p Argon2Params) (string, error)

HashArgon2idWithParams hashes plainPassword with argon2id and the given parameters.

func IsArgon2idHash added in v0.6.0

func IsArgon2idHash(encodedHash string) bool

IsArgon2idHash reports whether encodedHash looks like an argon2id PHC string.

func NeedsRehash added in v0.6.0

func NeedsRehash(hashedPassword string) bool

NeedsRehash reports whether hashedPassword should be re-hashed with the default Hasher.

func SetDefault added in v0.6.0

func SetDefault(h Hasher)

SetDefault replaces the package default Hasher; nil restores the bcrypt fallback.

func Setup added in v0.6.0

func Setup() error

Setup configures the package default Hasher from config.GetConfig().Hashing (HASH_DRIVER etc.).

func VerifyArgon2id added in v0.6.0

func VerifyArgon2id(encodedHash string, plainPassword []byte) (bool, error)

VerifyArgon2id reports whether plainPassword matches the argon2id PHC string encodedHash. Returns ErrInvalidArgon2Hash if encodedHash is malformed; a wrong password is (false, nil).

Types

type Argon2Params added in v0.6.0

type Argon2Params struct {
	Memory      uint32
	Iterations  uint32
	Parallelism uint8
	SaltLength  uint32
	KeyLength   uint32
}

Argon2Params configures argon2i / argon2id. Memory is in KiB.

type Hasher added in v0.6.0

type Hasher interface {
	Driver() string
	Hash(plainPassword []byte) (string, error)
	Check(hashedPassword string, plainPassword []byte) bool
	NeedsRehash(hashedPassword string) bool
}

Hasher hashes passwords with one driver.

Check accepts bcrypt, argon2i and argon2id hashes regardless of the hasher's driver, so switching HASH_DRIVER never locks users out; NeedsRehash reports hashes that are in another format or were made with different parameters, so they can be upgraded after a successful login.

func Default added in v0.6.0

func Default() Hasher

Default returns the Hasher set by Setup / SetDefault, or bcrypt with DefaultCost if none was set.

func NewHasher added in v0.6.0

func NewHasher(driver string, opts HasherOptions) (Hasher, error)

NewHasher returns a Hasher for driver "bcrypt", "argon" (Argon2i) or "argon2id". Empty means bcrypt.

func NewHasherFromConfig added in v0.6.0

func NewHasherFromConfig(cfg config.HashingConfiguration) (Hasher, error)

NewHasherFromConfig builds a Hasher from HASH_* settings (see config.HashingConfiguration).

type HasherOptions added in v0.6.0

type HasherOptions struct {
	BcryptCost int
	Argon      Argon2Params
}

HasherOptions tunes NewHasher. Zero values use defaults: bcrypt.DefaultCost and DefaultArgon2Params (per field, so setting only Argon.Memory keeps the default iterations, parallelism, salt and key length).

Jump to

Keyboard shortcuts

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