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
- func Bcrypt(password, prefix string, cost int, salt []byte) (string, error)
- func BcryptFromMCF(password, prefix, mcfSalt string) (string, error)
- func MD5Crypt(password, salt string) string
- func RandomSalt(n int) (string, error)
- func SHA256Crypt(password, salt string, rounds int) string
- func SHA512Crypt(password, salt string, rounds int) string
- 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 DefaultRounds = 5000
DefaultRounds is SHA-crypt's own implicit round count, used whenever a caller passes rounds == 0 to SHA256Crypt/SHA512Crypt.
const SaltChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"
SaltChars is the character set every crypt(3) implementation accepts in a salt: base64-like, but with "./" instead of "+/".
Variables ¶
This section is empty.
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).
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 MD5Crypt ¶
MD5Crypt computes the classic MD5-crypt ($1$) hash of password with salt (created by Poul-Henning Kamp for FreeBSD, later adopted by glibc).
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.
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.
