Help us shape the future of Go! Take the 2026 Go Developer Survey

hash

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package hash hashes and verifies passwords: argon2id by default (PHC string format), bcrypt for compatibility. Verify recognises both, so a database can move from bcrypt to argon2id through NeedsRehash on login.

Index

Constants

This section is empty.

Variables

View Source
var DefaultArgon2 = Argon2Params{
	Memory:  64 * 1024,
	Time:    1,
	Threads: 4,
	SaltLen: 16,
	KeyLen:  32,

} //nolint:gochecknoglobals // read-only defaults

DefaultArgon2 follows the OWASP password storage recommendation (m=64 MiB, t=1, p=4) with 16-byte salts and 32-byte keys.

View Source
var ErrPasswordTooLong = errors.New("hash: password longer than 72 bytes (bcrypt limit)")

ErrPasswordTooLong is returned by bcrypt for inputs over 72 bytes, which bcrypt would otherwise silently truncate.

View Source
var ErrUnknownFormat = errors.New("hash: unknown hash format")

ErrUnknownFormat is returned for hashes neither argon2id nor bcrypt.

Functions

func Verify

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

Verify checks password against an argon2id or bcrypt hash.

Types

type Argon2Params

type Argon2Params struct {
	Memory  uint32 // KiB
	Time    uint32
	Threads uint8
	SaltLen uint32
	KeyLen  uint32
}

Argon2Params configures argon2id (RFC 9106).

type Hasher

type Hasher interface {
	// Make hashes password with a fresh random salt.
	Make(password string) (string, error)
	// Verify reports whether password matches hash (argon2id or bcrypt).
	Verify(hash, password string) (bool, error)
	// NeedsRehash reports whether hash was made with other parameters or
	// algorithm than this hasher's, so it should be replaced after login.
	NeedsRehash(hash string) bool
}

Hasher creates password hashes and verifies them in constant time.

func Argon2id

func Argon2id(p Argon2Params) Hasher

Argon2id returns an argon2id hasher.

func Bcrypt

func Bcrypt(cost int) Hasher

Bcrypt returns a bcrypt hasher (cost 4–31; 12 is a good default).

func Default

func Default() Hasher

Default returns the argon2id hasher with DefaultArgon2.

func Fake

func Fake() Hasher

Fake returns a fast argon2id hasher for tests (never in production).

Jump to

Keyboard shortcuts

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