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

hash

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 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.

Every Hasher (Default, Argon2id, Bcrypt, Fake for tests) makes and verifies hashes in constant time; the auth guards take one to check passwords on login.

See https://github.com/AlexGalhardo/vukem/blob/main/docs/authentication.md.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/AlexGalhardo/vukem/auth/hash"
)

func main() {
	// Apps use hash.Default(); Fake keeps the example fast.
	h := hash.Fake()

	stored, _ := h.Make("correct horse battery staple")
	ok, _ := h.Verify(stored, "correct horse battery staple")
	bad, _ := h.Verify(stored, "wrong")

	fmt.Println(strings.HasPrefix(stored, "$argon2id$"), ok, bad)
}
Output:
true true false

Index

Examples

Constants

This section is empty.

Variables

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).

func DefaultArgon2Params added in v0.21.0

func DefaultArgon2Params() Argon2Params

DefaultArgon2Params returns the OWASP password storage recommendation (m=64 MiB, t=1, p=4) with 16-byte salts and 32-byte keys. It returns a copy, so tuning one field never weakens Default for the whole process.

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 DefaultArgon2Params.

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