Documentation
¶
Overview ¶
Package unixcrypt implements crypt(3)/MCF (Modular Crypt Format) password hash algorithms in pure Go (CGO=0): MD5-crypt ($1$), SHA-256-crypt ($5$) and SHA-512-crypt ($6$) per Ulrich Drepper's specification (https://www.akkadia.org/drepper/SHA-crypt.txt), and bcrypt ($2a$/$2b$/ $2x$/$2y$) via the pure-Go blowfish primitive. No component depends on libc's own crypt(3) or cgo; every output was validated against real `openssl passwd` and the canonical OpenBSD bcrypt test vectors.
Index ¶
- Constants
- Variables
- func Bcrypt(password, prefix string, cost int, salt []byte) (string, error)
- func BcryptFromMCF(password, prefix, mcfSalt string) (string, error)
- func BcryptStrict(password, prefix string, cost int, salt []byte) (string, error)
- func MD5Crypt(password, salt string) (string, error)
- func RandomSalt(n int) (string, error)
- func SHA256Crypt(password, salt string, rounds int) (string, error)
- func SHA512Crypt(password, salt string, rounds int) (string, error)
- func ValidSaltChars(s string) bool
Constants ¶
const ( MinRounds = 1000 MaxRounds = 999999999 )
MinRounds and MaxRounds are Drepper's spec's own clamp bounds: an explicitly requested round count outside this range is silently clamped to the nearer bound, matching real crypt(3)/libxcrypt exactly.
const BcryptMaxPasswordLen = 72
BcryptMaxPasswordLen is the number of password bytes bcrypt uses.
const DefaultRounds = 5000
DefaultRounds is SHA-crypt's own implicit round count, used whenever a caller passes rounds == 0 to SHA256Crypt/SHA512Crypt.
const MaxPasswordLen = 511
MaxPasswordLen is the longest password SHA256Crypt and SHA512Crypt accept, in bytes. libxcrypt defines CRYPT_MAX_PASSPHRASE_SIZE as 512 and counts the terminating NUL in it ("Passphrases longer than this (counting the terminating NUL) are not supported", lib/crypt.h.in), which leaves 511.
const SaltChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"
SaltChars is the character set every crypt(3) implementation accepts in a salt: base64-like, but with "./" instead of "+/".
Variables ¶
var ErrBadSalt = errors.New("unixcrypt: invalid salt")
ErrBadSalt is returned for a salt that crypt(3) would not accept.
var ErrPasswordTooLong = errors.New("unixcrypt: password too long")
ErrPasswordTooLong is returned for a password above the algorithm's bound instead of silently truncating it (a truncated password would verify for every password sharing its prefix).
Functions ¶
func Bcrypt ¶
Bcrypt computes a crypt(3)-format bcrypt hash: "$<prefix>$<cost>$<22-char salt><31-char hash>". prefix selects the variant ("2a", "2b", "2x", or "2y" — "2b" is the modern default every current implementation, including Ansible's own, uses); cost is the work factor, 4-31; salt must be exactly 16 raw bytes (bcrypt's own fixed salt size — RandomSalt is NOT the right source here, since bcrypt's salt is raw bytes, not crypt(3)'s usual printable charset; use crypto/rand directly).
Only the first 72 bytes of the password (plus the terminating NUL) enter the key schedule, so two passwords sharing their first 72 bytes hash equally; that is bcrypt's own limit, kept for compatibility. Use BcryptStrict to refuse longer passwords instead. A NUL byte inside the password does not end it here, unlike C crypt(3), which stops at the first NUL.
func BcryptFromMCF ¶
BcryptFromMCF reproduces crypt(3) bcrypt for a caller that already has a pre-formed MCF salt of the form "<cost>$<22-char base64>" — the shape Puppet's own pw_hash(password, type, salt) function takes as its third argument.
func BcryptStrict ¶ added in v0.3.0
BcryptStrict is Bcrypt, except that a password longer than BcryptMaxPasswordLen bytes is refused with ErrPasswordTooLong instead of being silently truncated.
func MD5Crypt ¶
MD5Crypt computes the classic MD5-crypt ($1$) hash of password with salt (created by Poul-Henning Kamp for FreeBSD, later adopted by glibc).
Like crypt(3), the salt ends at its first '$' and is truncated to 8 characters; the truncated salt must consist of SaltChars, otherwise ErrBadSalt is returned.
func RandomSalt ¶
RandomSalt returns a cryptographically random salt string of length n drawn from SaltChars, using crypto/rand.
func SHA256Crypt ¶
SHA256Crypt computes SHA-256-crypt ($5$) per Drepper's spec.
rounds == 0 means "unspecified": the algorithm's own DefaultRounds (5000) applies internally and the output omits the "rounds=N$" prefix — matching real crypt(3), which only prints that prefix when the caller explicitly requested a round count. Any other rounds value (even 5000 given explicitly) is clamped to [MinRounds, MaxRounds] and the clamped value is always printed, even if clamping left it at 5000.
It returns ErrPasswordTooLong for a password longer than MaxPasswordLen: SHA-crypt's cost is quadratic in the password length (Drepper's DP digest hashes the password len(pw) times, and every round hashes the len(pw)-byte P sequence), so an uncapped password is a CPU denial of service (same class as glibc CVE-2016-20013).
func SHA512Crypt ¶
SHA512Crypt is SHA256Crypt's sibling for $6$.
func ValidSaltChars ¶
ValidSaltChars reports whether every character in s is a valid crypt(3) salt character.
Types ¶
This section is empty.
