bcrypt

package
v0.2.14 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: BSD-3-Clause Imports: 8 Imported by: 1

Documentation

Overview

Package bcrypt implements Provos and Mazières's bcrypt adaptive hashing algorithm. See http://www.usenix.org/event/usenix99/provos/provos.pdf

Index

Constants

View Source
const (
	MinCost         int = 4  // the minimum allowable cost as passed in to GenerateFromPassword
	MaxCost         int = 31 // the maximum allowable cost as passed in to GenerateFromPassword
	DefaultCost     int = 10 // the cost that will actually be set if a cost below MinCost is passed into GenerateFromPassword
	EncodedSaltSize     = 22
	EncodedHashSize     = 31
)

Variables

View Source
var (
	// ErrMismatchedHashAndPassword is the error returned from CompareHashAndPassword when a password and hash do
	// not match.
	ErrMismatchedHashAndPassword = errors.New("github.com/go-crypt/x/bcrypt: the provided password is not a match for the provided hashed password")

	// ErrHashTooShort is the error returned from CompareHashAndPassword when a hash is too short to
	// be a bcrypt hash.
	ErrHashTooShort = errors.New("github.com/go-crypt/x/bcrypt: hashed secret key is too short to be a bcrypt hashed secret key")

	// ErrSecretInvalidLength is the error returned when a hash secret is too short to be a bcrypt secret.
	ErrSecretInvalidLength = errors.New("github.com/go-crypt/x/bcrypt: secret has an invalid length for a bcrypt secret")
)

Functions

func Base64Decode

func Base64Decode(src []byte) ([]byte, error)

Base64Decode is the base64 decoder for bcrypt.

func Base64Encode

func Base64Encode(src []byte) []byte

Base64Encode is the base64 encoder for bcrypt.

func CompareHashAndPassword

func CompareHashAndPassword(hashedPassword, password []byte) error

CompareHashAndPassword compares a bcrypt hashed password with its possible plaintext equivalent. Returns nil on success, or an error on failure.

func Cost

func Cost(hashedPassword []byte) (int, error)

Cost returns the hashing cost used to create the given hashed password. When, in the future, the hashing cost of a password system needs to be increased in order to adjust for greater computational power, this function allows one to establish which passwords need to be updated.

func DecodeSecret

func DecodeSecret(secret []byte) (salt, key []byte)

DecodeSecret decodes a valid bcrypt secret into a salt and key. This function will panic if the secret does not have the len of EncodedSaltSize + EncodedHashSize.

func GenerateFromPassword

func GenerateFromPassword(password []byte, cost int) ([]byte, error)

GenerateFromPassword returns the bcrypt hash of the password at the given cost. If the cost given is less than MinCost, the cost will be set to DefaultCost, instead. Use CompareHashAndPassword, as defined in this package, to compare the returned hashed password with its cleartext version.

func GenerateFromPasswordSalt

func GenerateFromPasswordSalt(password, salt []byte, cost int) ([]byte, error)

GenerateFromPasswordSalt returns the bcrypt hash of the password at the given cost. If the cost given is less than MinCost, the cost will be set to DefaultCost, instead. Use CompareHashAndPassword, as defined in this package, to compare the returned hashed password with its cleartext version.

func Key

func Key(password, salt []byte, cost int) ([]byte, error)

Key returns a new key from password/salt combination. Salt must be 16 bytes. For storage the salt needs to be encoded with bcrypt.Base64Encode.

func NewSalt

func NewSalt() (salt []byte, err error)

NewSalt generates a salt with an appropriate length.

Types

type HashVersionTooNewError

type HashVersionTooNewError byte

The error returned from CompareHashAndPassword when a hash was created with a bcrypt algorithm newer than this implementation.

func (HashVersionTooNewError) Error

func (hv HashVersionTooNewError) Error() string

type InvalidCostError

type InvalidCostError int

func (InvalidCostError) Error

func (ic InvalidCostError) Error() string

type InvalidHashPrefixError

type InvalidHashPrefixError byte

The error returned from CompareHashAndPassword when a hash starts with something other than '$'

func (InvalidHashPrefixError) Error

func (ih InvalidHashPrefixError) Error() string

type InvalidSaltSizeError

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

func (InvalidSaltSizeError) Error

func (iss InvalidSaltSizeError) Error() string

Jump to

Keyboard shortcuts

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