Documentation
¶
Overview ¶
Package bcrypt provides bcrypt password hashing helpers for go-service.
This package wraps golang.org/x/crypto/bcrypt to provide a simple Signer that:
- hashes passwords using golang.org/x/crypto/bcrypt.GenerateFromPassword with golang.org/x/crypto/bcrypt.DefaultCost, and
- verifies password hashes using golang.org/x/crypto/bcrypt.CompareHashAndPassword.
Index ¶
Constants ¶
const DefaultCost = bcrypt.DefaultCost
DefaultCost is the cost used by Sign.
Variables ¶
var Module = di.Module( di.Constructor(NewSigner), )
Module wires the bcrypt subsystem into go.uber.org/fx/go.uber.org/dig.
It provides a constructor for *Signer via NewSigner, which can be used for password hashing and verification using bcrypt with the default cost.
This module does not require configuration.
Functions ¶
Types ¶
type Signer ¶
type Signer struct{}
Signer hashes and verifies secrets using bcrypt.
This type is intended for password hashing and password hash verification. It is not a general-purpose message signing primitive.
Sign returns a bcrypt hash for the provided secret (typically a password). Verify checks whether the provided bcrypt hash matches the provided secret.
func NewSigner ¶
func NewSigner() *Signer
NewSigner constructs a bcrypt-based Signer intended for password hashing.
The returned Signer uses golang.org/x/crypto/bcrypt.DefaultCost when hashing. If you need a different cost or more control over parameters, use golang.org/x/crypto/bcrypt directly.
func (*Signer) Sign ¶
Sign hashes msg using bcrypt with golang.org/x/crypto/bcrypt.DefaultCost.
The returned value is a bcrypt hash suitable for storage. The input msg is typically a user password. Callers should store only the returned hash, never the plaintext secret.
This is a thin wrapper around golang.org/x/crypto/bcrypt.GenerateFromPassword.
func (*Signer) Verify ¶
Verify checks that sig is a valid bcrypt hash for msg.
This is a thin wrapper around golang.org/x/crypto/bcrypt.CompareHashAndPassword. It returns nil if the hash matches, otherwise it returns github.com/alexfalkowski/go-service/v2/crypto/errors.ErrInvalidMatch joined with the bcrypt package error.