Documentation
¶
Overview ¶
Package pwdhash is a Go-first password hashing helper that embraces the PHC (Password Hashing Competition) format.
It wraps Argon2id with safe defaults and surfaces a minimal API for hashing, verification, and upgrades. pwdhash intentionally supports Argon2id only, reducing the chance of accidentally selecting outdated primitives. If a superior successor emerges, pwdhash will adopt it behind the same API surface.
pwdhash ships with opinionated Argon2id policies so applications can select a strength profile without touching raw parameters (Interactive, Moderate, and Sensitive).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidHash indicates that an encoded hash cannot be parsed. ErrInvalidHash = errors.New("invalid encoded hash") )
Functions ¶
This section is empty.
Types ¶
type Hasher ¶
type Hasher interface {
ID() string
Hash(password []byte) (string, error)
Verify(password []byte, encoded string) (bool, error)
NeedsRehash(encoded string) (bool, error)
}
Hasher represents a password hashing algorithm implementation.
type Option ¶
type Option func(*config)
Option configures PasswordHasher construction.
func WithHasher ¶
WithHasher overrides the default hashing algorithm.
func WithPolicy ¶ added in v0.2.0
WithPolicy selects a preset Argon2id configuration for the PasswordHasher.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/allisson/go-pwdhash"
)
func main() {
// Instantiate a hasher with a specific policy
hasher, err := pwdhash.New(
pwdhash.WithPolicy(pwdhash.PolicyModerate),
)
if err != nil {
panic(err)
}
// Use standard operations
encoded, err := hasher.Hash([]byte("my_secure_password"))
if err != nil {
panic(err)
}
fmt.Println(strings.HasPrefix(encoded, "$argon2id$"))
}
Output: true
type PasswordHasher ¶
type PasswordHasher struct {
// contains filtered or unexported fields
}
PasswordHasher manages password hashing operations via registered algorithms.
func New ¶
func New(opts ...Option) (*PasswordHasher, error)
New constructs a PasswordHasher configured via the provided options.
Example ¶
package main
import (
"fmt"
"github.com/allisson/go-pwdhash"
)
func main() {
hasher, err := pwdhash.New()
if err != nil {
panic(err)
}
fmt.Printf("Default hasher created: %T\n", hasher)
}
Output: Default hasher created: *pwdhash.PasswordHasher
func (*PasswordHasher) Hash ¶
func (p *PasswordHasher) Hash(password []byte) (string, error)
Hash encodes the provided password using the active hasher.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/allisson/go-pwdhash"
)
func main() {
hasher, err := pwdhash.New(pwdhash.WithPolicy(pwdhash.PolicyInteractive))
if err != nil {
panic(err)
}
// Hash a password
encoded, err := hasher.Hash([]byte("s3cret"))
if err != nil {
panic(err)
}
// PHC hashes for Argon2id start with $argon2id$
fmt.Println(strings.HasPrefix(encoded, "$argon2id$"))
}
Output: true
func (*PasswordHasher) NeedsRehash ¶
func (p *PasswordHasher) NeedsRehash(encoded string) (bool, error)
NeedsRehash reports whether the encoded hash should be regenerated.
Example ¶
package main
import (
"fmt"
"github.com/allisson/go-pwdhash"
)
func main() {
// 1. Simulate an old hash created with the Interactive policy
oldHasher, err := pwdhash.New(pwdhash.WithPolicy(pwdhash.PolicyInteractive))
if err != nil {
panic(err)
}
encoded, err := oldHasher.Hash([]byte("s3cret"))
if err != nil {
panic(err)
}
// 2. Initialize a new hasher with a stronger Moderate policy
newHasher, err := pwdhash.New(pwdhash.WithPolicy(pwdhash.PolicyModerate))
if err != nil {
panic(err)
}
// 3. Check if the old hash needs to be upgraded using the new hasher
needsRehash, err := newHasher.NeedsRehash(encoded)
if err != nil {
panic(err)
}
fmt.Println("Needs rehash with stronger policy:", needsRehash)
}
Output: Needs rehash with stronger policy: true
func (*PasswordHasher) Verify ¶
func (p *PasswordHasher) Verify(password []byte, encoded string) (bool, error)
Verify checks whether the encoded hash matches the provided password.
Example ¶
package main
import (
"fmt"
"github.com/allisson/go-pwdhash"
)
func main() {
hasher, err := pwdhash.New(pwdhash.WithPolicy(pwdhash.PolicyInteractive))
if err != nil {
panic(err)
}
password := []byte("s3cret")
// Generate a hash to verify
encoded, err := hasher.Hash(password)
if err != nil {
panic(err)
}
// Verify the correct password
ok, err := hasher.Verify([]byte("s3cret"), encoded)
if err != nil {
panic(err)
}
fmt.Println("Correct password:", ok)
// Verify an incorrect password
ok, err = hasher.Verify([]byte("wrong_password"), encoded)
if err != nil {
panic(err)
}
fmt.Println("Incorrect password:", ok)
}
Output: Correct password: true Incorrect password: false
Directories
¶
| Path | Synopsis |
|---|---|
|
Package argon2 contains the Argon2id hasher implementation.
|
Package argon2 contains the Argon2id hasher implementation. |
|
internal
|
|
|
cast
Package cast provides narrow numeric conversion helpers.
|
Package cast provides narrow numeric conversion helpers. |
|
encoding
Package encoding handles serialization and parsing of PHC strings.
|
Package encoding handles serialization and parsing of PHC strings. |
|
subtle
Package subtle provides wrappers for constant-time operations.
|
Package subtle provides wrappers for constant-time operations. |