pass

package
v0.0.0-...-2c4037b Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package pass provides multi-scheme password hashing with transparent migration support. Argon2id is the default; bcrypt is accepted for migration from existing stores. Django's "argon2$argon2id$..." hash format is also supported.

Typical login flow:

pc := pass.NewDefaultContext()

if err := pc.Verify(stored, input); err != nil {
    return err
}
if pc.NeedsUpdate(stored) {
    if newHash, err := pc.Hash(input); err == nil {
        _ = db.UpdatePasswordHash(ctx, userID, newHash)
    }
}

Index

Constants

This section is empty.

Variables

View Source
var ErrMismatch = errors.New("password does not match")

ErrMismatch is returned by Verify when the password does not match the hash.

View Source
var ErrUnknownHash = errors.New("unrecognized hash format")

ErrUnknownHash is returned by Verify when no registered hasher recognizes the hash format.

View Source
var RFC9106HighMemory = Argon2id{
	Memory:      1 << 21,
	Iterations:  1,
	Parallelism: 4,
	KeyLen:      32,
	SaltLen:     16,
}

RFC9106HighMemory is the Argon2id parameter set from RFC 9106's first recommendation, for environments where memory is not at a premium.

View Source
var RFC9106LowMemory = Argon2id{
	Memory:      1 << 16,
	Iterations:  3,
	Parallelism: 4,
	KeyLen:      32,
	SaltLen:     16,
}

RFC9106LowMemory is the Argon2id parameter set from RFC 9106's second recommendation, for environments where memory is constrained to ~64 MiB. This is the default used by pass.Default.

Functions

This section is empty.

Types

type Argon2id

type Argon2id struct {
	Memory      uint32 // kibibytes; 0 uses default (65536)
	Iterations  uint32 // 0 uses default (1)
	Parallelism uint8  // 0 uses default (runtime.NumCPU())
	KeyLen      uint32 // 0 uses default (32)
	SaltLen     uint32 // 0 uses default (16)
}

Argon2id hashes passwords using the argon2id algorithm. The zero value uses the defaults from github.com/alexedwards/argon2id (64 MiB memory, 1 iteration, runtime.NumCPU() parallelism, 32-byte key, 16-byte salt).

Django's "argon2$argon2id$..." hash format is accepted for verification and always triggers NeedsUpdate so hashes are migrated to the canonical format on next login.

func (Argon2id) Hash

func (a Argon2id) Hash(password string) (string, error)

Hash hashes password using argon2id with the configured parameters.

func (Argon2id) Identify

func (Argon2id) Identify(hash string) bool

Identify reports whether hash was produced by argon2id, including Django's "argon2$argon2id$..." format.

func (Argon2id) NeedsUpdate

func (a Argon2id) NeedsUpdate(hash string) bool

NeedsUpdate reports whether hash should be rehashed. Returns true for Django-format hashes (to migrate to canonical format) or if the stored parameters differ from the current configuration.

func (Argon2id) Verify

func (a Argon2id) Verify(hash, password string) error

Verify checks password against hash, accepting both standard and Django formats.

type Bcrypt

type Bcrypt struct {
	Cost    int // 0 uses bcrypt.DefaultCost
	MinCost int // 0 uses Cost; hashes below this trigger NeedsUpdate
}

Bcrypt hashes passwords using bcrypt. The zero value uses bcrypt.DefaultCost (10) for both hashing and the minimum acceptable cost.

func (Bcrypt) Hash

func (b Bcrypt) Hash(password string) (string, error)

Hash hashes password using bcrypt with the configured cost.

func (Bcrypt) Identify

func (Bcrypt) Identify(hash string) bool

Identify reports whether hash is a bcrypt hash.

func (Bcrypt) NeedsUpdate

func (b Bcrypt) NeedsUpdate(hash string) bool

NeedsUpdate reports whether hash was hashed with a cost below MinCost.

func (Bcrypt) Verify

func (b Bcrypt) Verify(hash, password string) error

Verify checks password against hash.

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context holds an ordered list of hashers. The first hasher is the default for new hashes; the rest are accepted for verification but treated as deprecated (NeedsUpdate returns true for any hash they identify).

func NewContext

func NewContext(hashers ...Hasher) *Context

NewContext returns a Context using the given hashers. The first hasher is used for new hashes; remaining hashers are treated as deprecated.

func NewDefaultContext

func NewDefaultContext() *Context

NewDefaultContext returns a Context suitable for most applications. Outside of tests it uses RFC 9106 low-memory Argon2id as the primary hasher with bcrypt accepted for migration. Under go test it delegates to NewTestContext so that test suites do not pay full hashing costs.

func NewTestContext

func NewTestContext() *Context

NewTestContext returns a Context with minimal-cost hashers for use in tests. The parameters are intentionally weak — do not use in production.

func (*Context) DummyVerify

func (c *Context) DummyVerify(password string)

DummyVerify performs a full hash verification against a pre-computed dummy hash to prevent timing-based user enumeration. Call it in the "user not found" branch of your authentication flow so that missing-user lookups take the same time as wrong-password lookups:

user, err := db.GetUser(ctx, username)
if errors.Is(err, db.ErrNotFound) {
    pass.DummyVerify(password)
    return ErrInvalidCredentials
}

The dummy hash is computed lazily on the first call using the default hasher's parameters.

func (*Context) Hash

func (c *Context) Hash(password string) (string, error)

Hash hashes password using the default (first) hasher.

func (*Context) NeedsUpdate

func (c *Context) NeedsUpdate(hash string) bool

NeedsUpdate reports whether hash should be rehashed. Returns true if the hash belongs to a non-default hasher or if the default hasher's own NeedsUpdate reports true.

func (*Context) Verify

func (c *Context) Verify(hash, password string) error

Verify checks password against hash, dispatching to whichever registered hasher identifies the hash format. Returns ErrUnknownHash if none match.

type Hasher

type Hasher interface {
	// Hash produces an encoded hash string for password.
	Hash(password string) (string, error)
	// Verify checks password against hash. Returns ErrMismatch on wrong
	// password or ErrUnknownHash if the format is not recognized.
	Verify(hash, password string) error
	// Identify reports whether this hasher produced hash.
	Identify(hash string) bool
	// NeedsUpdate reports whether hash should be rehashed with current
	// parameters.
	NeedsUpdate(hash string) bool
}

Hasher is implemented by each supported password hashing algorithm.

Jump to

Keyboard shortcuts

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