password

package
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 14 Imported by: 3 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package password hashes and verifies passwords.

New hashes use argon2id in the PHC string format:

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

bcrypt hashes ($2a$, $2b$, $2y$) are accepted for verification only, so existing users can be migrated: after a successful login, NeedsRehash reports true and the caller stores a fresh argon2id hash.

Index

Examples

Constants

View Source
const ProviderName = "password"

ProviderName is the provider name used in iam.AuthRequest.Provider.

Variables

View Source
var (
	// ErrTooShort is returned by Policy.Check.
	ErrTooShort = errors.New("password: too short")
	// ErrTooLong is returned by Policy.Check.
	ErrTooLong = errors.New("password: too long")
)
View Source
var DefaultParams = Params{Memory: 19 * 1024, Iterations: 2, Parallelism: 1, SaltLength: 16, KeyLength: 32}

DefaultParams is the OWASP baseline for argon2id (19 MiB, t=2, p=1).

View Source
var DefaultPolicy = Policy{MinLength: 12, MaxLength: 1024}

DefaultPolicy follows NIST SP 800-63B guidance: length over composition rules.

View Source
var (
	// ErrMalformedHash is returned for encoded hashes that cannot be parsed.
	ErrMalformedHash = errors.New("password: malformed hash")
)

Functions

func NormalizeLogin

func NormalizeLogin(login string) string

NormalizeLogin trims spaces and lower-cases a login name.

Types

type Argon2id

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

Argon2id is the default Hasher.

func NewArgon2id

func NewArgon2id(params Params, maxConcurrent int) (*Argon2id, error)

NewArgon2id returns an argon2id hasher. maxConcurrent bounds how many hash computations run at once (each uses params.Memory KiB); zero means GOMAXPROCS.

Example
package main

import (
	"context"
	"fmt"
	"strings"

	"github.com/kararnab/iam/v2/password"
)

func main() {
	ctx := context.Background()
	h, _ := password.NewArgon2id(password.DefaultParams, 0)

	encoded, _ := h.Hash(ctx, "correct horse battery staple")
	fmt.Println(strings.HasPrefix(encoded, "$argon2id$v=19$m=19456,t=2,p=1$"))

	ok, _ := h.Verify(ctx, "correct horse battery staple", encoded)
	bad, _ := h.Verify(ctx, "wrong", encoded)
	fmt.Println(ok, bad, h.NeedsRehash(encoded))
}
Output:
true
true false false

func (*Argon2id) Hash

func (a *Argon2id) Hash(ctx context.Context, password string) (string, error)

Hash implements Hasher.

func (*Argon2id) NeedsRehash

func (a *Argon2id) NeedsRehash(encoded string) bool

NeedsRehash implements Hasher.

func (*Argon2id) Verify

func (a *Argon2id) Verify(ctx context.Context, password, encoded string) (bool, error)

Verify implements Hasher.

type CredentialStore

type CredentialStore interface {
	// GetCredential returns the encoded hash for login, or iam.ErrNotFound.
	GetCredential(ctx context.Context, login string) (encodedHash string, err error)

	// CreateCredential stores a new credential, or returns iam.ErrConflict.
	CreateCredential(ctx context.Context, login, encodedHash string) error

	// UpdateCredential replaces an existing hash (rehash, password change).
	UpdateCredential(ctx context.Context, login, encodedHash string) error

	// DeleteCredential removes a credential. Missing is not an error.
	DeleteCredential(ctx context.Context, login string) error
}

CredentialStore persists password hashes by login name. It is implemented by the application (see memstore for a reference implementation).

Logins are normalized by the provider (trimmed, lower-cased) before they reach the store.

type Hasher

type Hasher interface {
	// Hash returns an encoded hash of password.
	Hash(ctx context.Context, password string) (string, error)

	// Verify reports whether password matches encoded. A malformed encoded
	// hash is an error; a wrong password is (false, nil).
	Verify(ctx context.Context, password, encoded string) (bool, error)

	// NeedsRehash reports whether encoded should be replaced by a new Hash,
	// because it uses another algorithm or other parameters.
	NeedsRehash(encoded string) bool
}

Hasher hashes and verifies passwords.

type Params

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

Params are argon2id parameters.

type Policy

type Policy struct {
	MinLength int // characters; DefaultPolicy uses 12
	MaxLength int // bytes; DefaultPolicy uses 1024
}

Policy holds password length rules. Lengths are counted in Unicode characters for the minimum and bytes for the maximum (which only exists to bound hashing cost).

func (Policy) Check

func (p Policy) Check(password string) error

Check validates a new password against the policy.

type Provider

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

Provider authenticates logins with passwords.

Params for Authenticate and Register: "username" and "password". The identity's ProviderID is the normalized login, which the application links to a subject through iam.IdentityStore.

func NewProvider

func NewProvider(creds CredentialStore, hasher Hasher, policy Policy) (*Provider, error)

NewProvider returns a password provider. A zero Policy means DefaultPolicy.

func (*Provider) Authenticate

func (p *Provider) Authenticate(ctx context.Context, params map[string]string) (*provider.Identity, error)

Authenticate implements provider.AuthProvider.

func (*Provider) CheckPassword added in v2.2.0

func (p *Provider) CheckPassword(password string) error

CheckPassword implements provider.PasswordSetter. It returns an error wrapping ErrTooShort or ErrTooLong for policy violations.

func (*Provider) Name

func (p *Provider) Name() string

Name implements provider.AuthProvider.

func (*Provider) Register

func (p *Provider) Register(ctx context.Context, params map[string]string) (*provider.Identity, error)

Register implements provider.Registrar. It returns an error wrapping ErrTooShort/ErrTooLong for policy violations and iam.ErrConflict if the login is taken.

func (*Provider) SetPassword added in v2.2.0

func (p *Provider) SetPassword(ctx context.Context, providerID, password string) error

SetPassword implements provider.PasswordSetter. It returns iam.ErrNotFound if the login has no credential.

func (*Provider) Unregister

func (p *Provider) Unregister(ctx context.Context, providerID string) error

Unregister implements provider.Registrar.

Jump to

Keyboard shortcuts

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