Documentation
¶
Overview ¶
Package bcrypt is a pure-Go (no cgo) reimplementation of Ruby's bcrypt gem — BCrypt::Password and BCrypt::Engine — for hashing and verifying passwords with the OpenBSD bcrypt() algorithm.
It produces and consumes the same "$2a$NN$...." hashes the gem does: a hash Create emits verifies under the gem's Password#==, and a gem-emitted hash verifies under Password.Equal. Secrets longer than 72 bytes are truncated exactly like the gem (and the older C libraries it stays compatible with), so hashes cross-verify in both directions at the 72-byte edge.
The crypto core is the OpenBSD bcrypt key schedule over golang.org/x/crypto/blowfish; there is no cgo and no C bcrypt. It is the password-hashing backend for go-embedded-ruby, but is a standalone, reusable module — a sibling of go-ruby-yaml, go-ruby-regexp and go-ruby-erb.
Quick start ¶
pw, _ := bcrypt.CreateString("secret", bcrypt.WithCost(12))
pw.EqualString("secret") // true
pw.EqualString("nope") // false
stored, _ := bcrypt.NewPassword(pw.String())
stored.Cost() // 12
stored.Version() // "2a"
Index ¶
- Constants
- Variables
- func AutodetectCost(salt string) int
- func GenerateSalt(cost int) (string, error)
- func HashSecret(secret []byte, salt string) (string, error)
- func ValidHash(h string) bool
- func ValidSalt(salt string) bool
- func ValidSecret(secret []byte) bool
- type CreateOption
- type Engine
- type Password
Constants ¶
const ( // DefaultCost is the cost used when none is given (BCrypt::Engine::DEFAULT_COST). DefaultCost = 12 // MinCost is the minimum cost the algorithm supports (BCrypt::Engine::MIN_COST). MinCost = 4 // MaxCost is the maximum cost the algorithm supports (BCrypt::Engine::MAX_COST). MaxCost = 31 // MaxSecretBytesize is the byte length secrets are truncated to before // hashing (BCrypt::Engine::MAX_SECRET_BYTESIZE). Older bcrypt libraries // truncated here and the gem keeps doing so for forward compatibility. MaxSecretBytesize = 72 // MaxSaltLength is the raw salt length in bytes (BCrypt::Engine::MAX_SALT_LENGTH). MaxSaltLength = 16 )
Engine cost constants, matching BCrypt::Engine.
const Version = "0.1.0"
Version is the version of this Go port. It is independent of the upstream Ruby gem's version.
Variables ¶
var ( // ErrInvalidSecret is returned when a secret cannot be coerced to a string // (BCrypt::Errors::InvalidSecret). In this Go port a []byte / string secret // is always valid, so this only surfaces from the low-level engine guard. ErrInvalidSecret = errors.New("bcrypt: invalid secret") // ErrInvalidCost is returned when a cost is not numeric and > 0 // (BCrypt::Errors::InvalidCost), matching Engine.generate_salt. ErrInvalidCost = errors.New("bcrypt: cost must be numeric and > 0") // ErrInvalidSalt is returned when a salt does not match the bcrypt salt // grammar (BCrypt::Errors::InvalidSalt). ErrInvalidSalt = errors.New("bcrypt: invalid salt") // ErrInvalidHash is returned when a stored hash does not match the bcrypt // hash grammar (BCrypt::Errors::InvalidHash). ErrInvalidHash = errors.New("bcrypt: invalid hash") // ErrCostTooHigh is returned by Password.create when the requested cost // exceeds MaxCost. The gem raises a bare ArgumentError here. ErrCostTooHigh = errors.New("bcrypt: cost exceeds the maximum allowed") )
The package's sentinel errors mirror the exception hierarchy of the Ruby bcrypt gem's BCrypt::Errors module: InvalidSecret, InvalidCost, InvalidSalt and InvalidHash. A caller can match them with errors.Is.
Functions ¶
func AutodetectCost ¶
AutodetectCost extracts the cost embedded in a salt or hash string (BCrypt::Engine.autodetect_cost: salt[4..5].to_i). It returns 0 for a string too short to carry a cost.
func GenerateSalt ¶
GenerateSalt returns a random salt of the form "$2a$NN$...." for the given cost. A cost <= 0 returns ErrInvalidCost; a cost below MinCost is raised to MinCost, matching BCrypt::Engine.generate_salt.
func HashSecret ¶
HashSecret hashes secret against a valid salt, returning the full "$2a$NN$...."-form hash. Secrets longer than MaxSecretBytesize are truncated, exactly like BCrypt::Engine.hash_secret. An invalid salt returns ErrInvalidSalt.
func ValidHash ¶
ValidHash reports whether h matches the bcrypt hash grammar (BCrypt::Password.valid_hash?).
func ValidSalt ¶
ValidSalt reports whether salt matches the bcrypt salt grammar (BCrypt::Engine.valid_salt?).
func ValidSecret ¶
ValidSecret reports whether secret is acceptable, mirroring BCrypt::Engine.valid_secret? (which accepts anything coercible to a string). A Go []byte is always valid.
Types ¶
type CreateOption ¶
type CreateOption func(*createOptions)
CreateOption configures Create; the only knob the gem exposes is :cost.
func WithCost ¶
func WithCost(cost int) CreateOption
WithCost sets the cost factor for Create (the gem's :cost option).
type Engine ¶
type Engine struct{}
Engine mirrors Ruby's BCrypt::Engine: the low-level surface that hashes a secret against a salt, generates salts, and validates secrets and salts. Its methods are exposed as package functions on the Engine value returned by DefaultEngine, but are also available directly as package-level functions.
type Password ¶
type Password struct {
// contains filtered or unexported fields
}
Password is a parsed bcrypt password hash, mirroring Ruby's BCrypt::Password (which subclasses String). Its String form is the stored "$2a$NN$...." hash; Equal / EqualString compare a candidate secret against it in constant time.
func Create ¶
func Create(secret []byte, opts ...CreateOption) (*Password, error)
Create hashes secret and returns a *Password, mirroring BCrypt::Password.create. Without WithCost it uses DefaultCost. A cost above MaxCost returns ErrCostTooHigh (the gem raises ArgumentError); a cost below MinCost is raised to MinCost by the salt generator.
func CreateString ¶
func CreateString(secret string, opts ...CreateOption) (*Password, error)
CreateString is Create for a string secret.
func NewPassword ¶
NewPassword parses a stored hash into a *Password, mirroring BCrypt::Password.new. A string that is not a valid bcrypt hash returns ErrInvalidHash.
func (*Password) Equal ¶
Equal reports whether secret is the secret this hash was derived from, comparing in constant time (BCrypt::Password#==). It re-hashes secret against this password's salt and compares the result to the stored hash.
func (*Password) EqualString ¶
EqualString is Equal for a string secret.
func (*Password) Salt ¶
Salt returns the salt portion, the first 29 chars including version and cost (BCrypt::Password#salt).
