auth

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: BSD-3-Clause Imports: 9 Imported by: 0 modules, 0 packages

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 auth does the argon2id password hashing and makes the session and invitation tokens.

Index

Constants

View Source
const (
	// TagPasswordHashMalformed marks a stored password hash that is not a valid
	// argon2id PHC string or whose parameters are out of range.
	TagPasswordHashMalformed = "PasswordHashMalformed"
	// TagPasswordHashUnsupported marks a stored password hash made by an
	// algorithm or a version that Verify does not support.
	TagPasswordHashUnsupported = "PasswordHashUnsupported"
)

Variables

This section is empty.

Functions

func HashToken

func HashToken(token string) string

HashToken returns the SHA-256 of token as 64 lowercase hex characters.

func NewToken

func NewToken() (string, error)

NewToken returns a random token: 32 bytes from the system's random source, in base64url without padding (43 characters). Session and invitation tokens are made with it; the database keeps only HashToken of them.

func TokensEqual

func TokensEqual(a, b string) bool

TokensEqual reports whether a and b are the same string, in time that does not depend on where they differ.

Types

type Hasher

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

Hasher hashes and verifies passwords with argon2id. The zero value is not usable: make one with DefaultHasher (or FastHasher in tests). A Hasher holds no state besides its parameters, so it is safe for concurrent use.

func DefaultHasher

func DefaultHasher() Hasher

DefaultHasher returns the Hasher of the running program: argon2id with 64 MiB of memory, 3 iterations and 2 threads.

func FastHasher

func FastHasher() Hasher

FastHasher returns a Hasher with tiny parameters so that tests run quickly. It is for tests only: its hashes are not safe to keep.

func (Hasher) Hash

func (h Hasher) Hash(password string) (string, error)

Hash returns the argon2id hash of password in the PHC string format: $argon2id$v=19$m=65536,t=3,p=2$<salt>$<key>, with the salt and the key in standard base64 without padding. The salt is random, so hashing the same password twice gives two different strings.

func (Hasher) MemoryBytes added in v1.1.0

func (h Hasher) MemoryBytes() uint64

MemoryBytes is the memory one hash or one check of a password takes, which the Go runtime keeps as part of the process after the call: 64 MiB for DefaultHasher.

func (Hasher) Verify

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

Verify reports whether password matches encoded, a string made by Hash. It uses the parameters stored in encoded, not those of h, so hashes made with older parameters keep working. A wrong password is false and a nil error. A string that is not a valid hash is a PasswordHashMalformed error, and one made by another algorithm or version is a PasswordHashUnsupported error.

Jump to

Keyboard shortcuts

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