Documentation ¶
Overview ¶
Package bcrypt implements the bcrypt hashing algorithm for crypt(3).
Index ¶
- Constants
- Variables
- func Check(hash, password string) error
- func Key(password, salt []byte, cost uint8, opts *CompatibilityOptions) ([]byte, error)
- func NewHash(password string, cost uint8) (string, error)
- type CompatibilityOptions
- type InvalidCostError
- type InvalidSaltError
- type InvalidSaltLengthError
- type UnsupportedPrefixError
Examples ¶
Constants ¶
const ( MinCost = 4 MaxCost = 31 DefaultCost = 12 )
const ( Prefix2 = "$2$" // the original bcrypt specification Prefix2a = "$2a$" // requires the string must be UTF-8 encoded and the null terminator must be included Prefix2b = "$2b$" // fixing bug with storing the string length in an unsigned char )
const SaltLength = 22
Variables ¶
var Encoding = base64.NewEncoding(encoder).WithPadding(base64.NoPadding)
Encoding is the unpadded base64 encoding, defined by a 64-character alphabet used by bcrypt.
Functions ¶
func Check ¶
Check compares the given crypt(3) bcrypt hash with a new hash derived from the password. Returns nil on success, or an error on failure.
Example ¶
package main import ( "fmt" "github.com/sergeymakinen/go-crypt/bcrypt" ) func main() { hash := "$2b$12$mBhJFLLDJCBCcmMN4DLyrOV.LLSl/mdwGfzwsqvIL0OQN5yXzRihO" fmt.Println(bcrypt.Check(hash, "password")) fmt.Println(bcrypt.Check(hash, "test")) }
Output: <nil> hash and password mismatch
func Key ¶
func Key(password, salt []byte, cost uint8, opts *CompatibilityOptions) ([]byte, error)
Key returns a bcrypt key derived from the password, salt, cost and compatibility options.
The opts parameter is optional. If nil, default options are used.
Example ¶
package main import ( "fmt" "github.com/sergeymakinen/go-crypt/bcrypt" ) func main() { salt, cost, opts, _ := bcrypt.Params("$2b$10$UVjcf7m8L91VOpIRwEprguF4o9Inqj7aNhqvSzUElX4GWGyIkYLuG") fmt.Println(string(salt)) fmt.Println(cost) key, _ := bcrypt.Key([]byte("password"), salt, cost, opts) fmt.Println(bcrypt.Encoding.EncodeToString(key)) }
Output: UVjcf7m8L91VOpIRwEprgu 10 F4o9Inqj7aNhqvSzUElX4GWGyIkYLuG
Types ¶
type CompatibilityOptions ¶
type CompatibilityOptions struct {
Prefix string
}
CompatibilityOptions are the key derivation parameters required to produce keys from old/non-standard hashes.
func Params ¶
func Params(hash string) (salt []byte, cost uint8, opts *CompatibilityOptions, err error)
Params returns the hashing salt, cost, version and compatibility options used to create the given crypt(3) bcrypt hash.
Example ¶
package main import ( "fmt" "github.com/sergeymakinen/go-crypt/bcrypt" ) func main() { salt, cost, _, _ := bcrypt.Params("$2b$10$UVjcf7m8L91VOpIRwEprguF4o9Inqj7aNhqvSzUElX4GWGyIkYLuG") fmt.Println(string(salt)) fmt.Println(cost) }
Output: UVjcf7m8L91VOpIRwEprgu 10
type InvalidCostError ¶
type InvalidCostError uint8
InvalidCostError values describe errors resulting from an invalid cost.
func (InvalidCostError) Error ¶
func (e InvalidCostError) Error() string
type InvalidSaltError ¶
type InvalidSaltError byte
InvalidSaltError values describe errors resulting from an invalid character in a hash string.
func (InvalidSaltError) Error ¶
func (e InvalidSaltError) Error() string
type InvalidSaltLengthError ¶
type InvalidSaltLengthError int
InvalidSaltLengthError values describe errors resulting from an invalid length of a salt.
func (InvalidSaltLengthError) Error ¶
func (e InvalidSaltLengthError) Error() string
type UnsupportedPrefixError ¶
type UnsupportedPrefixError string
UnsupportedPrefixError values describe errors resulting from an unsupported prefix string.
func (UnsupportedPrefixError) Error ¶
func (e UnsupportedPrefixError) Error() string