password

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package password hashes and verifies passwords with argon2id, and checks new passwords against the length policy in the auth standard.

Hashes are stored in the standard PHC string format, which keeps the algorithm, parameters, salt, and hash together:

$argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash>

Storing the parameters with each hash lets them be raised later: after a successful login, NeedsRehash reports whether the hash should be replaced.

Index

Constants

View Source
const (
	MinLength = 12
	MaxLength = 256
)

Length limits for new passwords, counted in characters (runes), not bytes.

Variables

View Source
var (
	// ErrTooShort means a new password has fewer than MinLength characters.
	ErrTooShort = errors.New("password is too short")
	// ErrTooLong means a new password has more than MaxLength characters.
	ErrTooLong = errors.New("password is too long")
	// ErrInvalidUTF8 means a new password isn't valid UTF-8 text.
	ErrInvalidUTF8 = errors.New("password is not valid UTF-8")
	// ErrInvalidHash means a stored hash couldn't be parsed.
	ErrInvalidHash = errors.New("invalid password hash")
	// ErrIncompatibleVersion means a stored hash uses an argon2 version this
	// package doesn't support.
	ErrIncompatibleVersion = errors.New("incompatible argon2 version")
)
View Source
var DefaultParams = Params{
	Memory:      19 * 1024,
	Iterations:  2,
	Parallelism: 1,
	SaltLength:  16,
	KeyLength:   32,
}

DefaultParams are OWASP's minimum recommended settings for argon2id: 19 MiB of memory, 2 iterations, parallelism 1.

Functions

func Hash

func Hash(password string, p Params) (string, error)

Hash returns the encoded argon2id hash of password, using a new random salt.

func NeedsRehash

func NeedsRehash(encoded string, p Params) (bool, error)

NeedsRehash reports whether an encoded hash was made with different parameters than p, so it should be replaced after the next successful login.

func SimulateVerify

func SimulateVerify(password string)

SimulateVerify does the same amount of work as Verify with DefaultParams, for logins with an email that has no account. Without it, a login for a nonexistent account would return noticeably faster, revealing which emails are registered.

func Validate

func Validate(password string) error

Validate checks a new password against the length policy. It's for sign-up and password changes; existing hashes are never re-checked.

func Verify

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

Verify reports whether password matches the encoded hash. It returns an error only when the hash itself is malformed, never for a wrong password. The comparison runs in constant time.

Types

type Params

type Params struct {
	Memory      uint32 // in KiB
	Iterations  uint32
	Parallelism uint8
	SaltLength  uint32 // in bytes
	KeyLength   uint32 // in bytes
}

Params are the argon2id cost settings.

Jump to

Keyboard shortcuts

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