Packages

hash

bcrypt password hashing and verification. Two functions, no configuration — add it manually when you build an auth or user module.


Usage

import "github.com/BounkhongDev/bkgo/hash"

// Hash on sign-up
hashed, err := hash.Password(input.Password)
if err != nil {
    return nil, err
}
user := &User{Email: input.Email, HashedPassword: hashed}

// Verify on login
if !hash.CheckPassword(input.Password, user.HashedPassword) {
    return nil, errs.Unauthorized("invalid credentials")
}

Signatures

// Password hashes a plain-text password using bcrypt.
func Password(plain string) (string, error)

// CheckPassword returns true if plain matches the bcrypt hashed string.
func CheckPassword(plain, hashed string) bool

Notes

  • Uses bcrypt.DefaultCost — bcrypt generates and embeds the salt in the hash, so you never store one separately.
  • CheckPassword returns a plain bool, not an error: a mismatch is not an exceptional case. It compares in constant time.
  • Store the result of Password in a column of at least 60 characters.
  • Never log or return a hash in an API response.