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
- Variables
- func ComparePassword(hashedPassword string, plainPassword []byte) bool
- func Hash(plainPassword []byte) (string, error)
- func HashAndSalt(plainPassword []byte) string
- func HashArgon2id(plainPassword []byte) (string, error)
- func HashArgon2idWithParams(plainPassword []byte, p Argon2Params) (string, error)
- func IsArgon2idHash(encodedHash string) bool
- func NeedsRehash(hashedPassword string) bool
- func SetDefault(h Hasher)
- func Setup() error
- func VerifyArgon2id(encodedHash string, plainPassword []byte) (bool, error)
- type Argon2Params
- type Hasher
- type HasherOptions
Constants ¶
const ( DriverBcrypt = "bcrypt" DriverArgon = "argon" // Argon2i DriverArgon2id = "argon2id" // Argon2id (recommended) )
Hash driver names accepted by NewHasher and HASH_DRIVER (Laravel-compatible naming).
Variables ¶
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.
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.
var ( // ErrUnknownDriver is returned by NewHasher for an unsupported driver name. ErrUnknownDriver = errors.New("unknown hash driver") )
Functions ¶
func ComparePassword ¶
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 HashAndSalt ¶
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
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
IsArgon2idHash reports whether encodedHash looks like an argon2id PHC string.
func NeedsRehash ¶ added in v0.6.0
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.
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).