unixcrypt

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: BSD-3-Clause Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-encryptions/unixcrypt

go-encryptions/unixcrypt

ci Go Reference

Pure-Go (CGO_ENABLED=0) implementation of the crypt(3)/MCF (Modular Crypt Format) password hash algorithms: MD5-crypt ($1$, Poul-Henning Kamp's original FreeBSD algorithm), SHA-256-crypt/SHA-512-crypt ($5$/$6$, per Ulrich Drepper's specification), and bcrypt ($2a$/$2b$/$2x$/$2y$, via the pure-Go blowfish primitive). No component links against libc's own crypt(3) or uses cgo, so it cross-compiles and produces identical output on every Go target, including big-endian s390x.

Every algorithm's output was validated against real openssl passwd and the canonical OpenBSD bcrypt.c test vectors.

This package was extracted from go-puppet/puppet's own pw_hash() implementation to be shared, rather than reimplemented, by every consumer that needs it — currently go-puppet/puppet (Puppet's pw_hash() stdlib function) and go-ansible/template (Ansible's password_hash filter).

Install

go get github.com/go-encryptions/unixcrypt

API

func MD5Crypt(password, salt string) (string, error)
func SHA256Crypt(password, salt string, rounds int) (string, error)
func SHA512Crypt(password, salt string, rounds int) (string, error)
func Bcrypt(password, prefix string, cost int, salt []byte) (string, error)
func BcryptStrict(password, prefix string, cost int, salt []byte) (string, error)
func BcryptFromMCF(password, prefix, mcfSalt string) (string, error)

func RandomSalt(n int) (string, error)
func ValidSaltChars(s string) bool

const DefaultRounds = 5000
const MinRounds = 1000
const MaxRounds = 999999999
const MaxPasswordLen = 511

var ErrPasswordTooLong error
var ErrBadSalt error
  • Password length cap (SHA-crypt). SHA-crypt's cost is quadratic in the password length, so SHA256Crypt/SHA512Crypt return ErrPasswordTooLong for a password above MaxPasswordLen (511 bytes; libxcrypt's CRYPT_MAX_PASSPHRASE_SIZE is 512 counting the terminating NUL) instead of burning CPU or truncating. Callers that take rounds (up to MaxRounds) or a bcrypt cost (up to 31) from untrusted input should apply their own policy cap first: those bounds are caller-controlled CPU by design.

  • MD5Crypt/SHA256Crypt/SHA512Crypt take a plain salt string and return the full $id$salt$hash (or, for SHA-crypt with an explicit round count, $id$rounds=N$salt$hash) MCF string.

  • SHA256Crypt/SHA512Crypt's rounds parameter matches real crypt(3) exactly: 0 means "unspecified" — the algorithm's own default of 5000 applies internally and the output omits the rounds= prefix entirely. Any other value (even 5000 given explicitly) is clamped into [MinRounds, MaxRounds] and the clamped value is always printed, even if clamping left it at 5000 — a caller that explicitly asked for 5000 rounds gets a different string than one that didn't specify rounds at all, even though the digest itself is identical either way. This distinction is real crypt(3) behavior, confirmed against openssl passwd, not an invented convenience.

  • Bcrypt accepts only the prefixes 2a, 2b, 2x and 2y (any other prefix is an error) and a cost of 4-31; BcryptFromMCF requires the cost to be exactly two ASCII digits ("+4" and " 4" are refused). Both formerly built a malformed MCF string on request.

  • $2x$ reproduces openwall crypt_blowfish's sign-extension bug (a password byte >= 0x80 sign-extends into its key word), so legacy hashes made by affected pre-2011 implementations verify; it matches the wrapper.c vector for "\xa3". For passwords without such bytes it equals $2y$. $2x$ is for verifying old hashes only: never use it to create new ones. $2a$, $2b$ and $2y$ use the corrected algorithm (crypt_blowfish's extra $2a$ safety measure for some \xff passwords is not implemented).

  • bcrypt reads only the first 72 bytes of the password (bcrypt's own limit): Bcrypt silently ignores the rest, so two passwords sharing their first 72 bytes hash equally. The default is unchanged for compatibility; BcryptStrict returns ErrPasswordTooLong above BcryptMaxPasswordLen (72) bytes instead. A NUL byte inside a bcrypt password does not end it, unlike C crypt(3), which stops at the first NUL.

  • Salts follow crypt(3), as openssl passwd does: the salt ends at its first $, is truncated to 8 characters (MD5-crypt) or 16 (SHA-crypt), and the kept part must be [./A-Za-z0-9], otherwise MD5Crypt/SHA256Crypt/ SHA512Crypt return ErrBadSalt. SHA-crypt also accepts rounds=N$salt as the salt when the rounds argument is 0 (an error if both are given).

  • Bcrypt takes a raw 16-byte salt directly; BcryptFromMCF is a convenience for a caller that already has a pre-formed "<cost>$<22-char-base64>" salt (the shape Puppet's own pw_hash(password, type, salt) takes as its third argument).

  • RandomSalt generates a cryptographically random salt from crypt(3)'s own accepted charset ([A-Za-z0-9./]) via crypto/rand — not appropriate for bcrypt, whose salt is raw bytes rather than that printable charset; use crypto/rand.Read directly for a bcrypt salt.

Example

package main

import (
	"fmt"

	"github.com/go-encryptions/unixcrypt"
)

func main() {
	salt, _ := unixcrypt.RandomSalt(16)
	h, _ := unixcrypt.SHA512Crypt("hunter2", salt, 0) // real crypt(3) default: 5000 rounds, no prefix
	fmt.Println(h)
	h, _ = unixcrypt.SHA512Crypt("hunter2", salt, 100000) // $6$rounds=100000$...
	fmt.Println(h)
}

Changes in v0.3.0

Below v1, a breaking change bumps the MINOR version.

  • Breaking: MD5Crypt now returns (string, error), like the other hashes, because a bad salt is refused with ErrBadSalt.
  • Salts are cut at the first $, truncated and validated as crypt(3) does; SHA-crypt reads rounds=N$ from the salt.
  • SHA-crypt refuses passwords above MaxPasswordLen (ErrPasswordTooLong).
  • bcrypt: prefix and cost are validated, $2x$ reproduces the sign-extension bug, and BcryptStrict refuses passwords above 72 bytes.

Scope

Deliberately narrow: the four crypt(3)/MCF formats above, nothing else. apr1 (Apache's own MD5-crypt variant, $apr1$ instead of $1$ — same algorithm, different magic string) is not implemented, since neither current consumer needs it; it would be a one-line addition (MD5Crypt already takes its magic string as an internal constant) if a future consumer does.

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

View Source
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.

View Source
const BcryptMaxPasswordLen = 72

BcryptMaxPasswordLen is the number of password bytes bcrypt uses.

View Source
const DefaultRounds = 5000

DefaultRounds is SHA-crypt's own implicit round count, used whenever a caller passes rounds == 0 to SHA256Crypt/SHA512Crypt.

View Source
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.

View Source
const SaltChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"

SaltChars is the character set every crypt(3) implementation accepts in a salt: base64-like, but with "./" instead of "+/".

Variables

View Source
var ErrBadSalt = errors.New("unixcrypt: invalid salt")

ErrBadSalt is returned for a salt that crypt(3) would not accept.

View Source
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

func Bcrypt(password, prefix string, cost int, salt []byte) (string, error)

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

func BcryptFromMCF(password, prefix, mcfSalt string) (string, error)

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

func BcryptStrict(password, prefix string, cost int, salt []byte) (string, error)

BcryptStrict is Bcrypt, except that a password longer than BcryptMaxPasswordLen bytes is refused with ErrPasswordTooLong instead of being silently truncated.

func MD5Crypt

func MD5Crypt(password, salt string) (string, error)

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

func RandomSalt(n int) (string, error)

RandomSalt returns a cryptographically random salt string of length n drawn from SaltChars, using crypto/rand.

func SHA256Crypt

func SHA256Crypt(password, salt string, rounds int) (string, error)

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

func SHA512Crypt(password, salt string, rounds int) (string, error)

SHA512Crypt is SHA256Crypt's sibling for $6$.

func ValidSaltChars

func ValidSaltChars(s string) bool

ValidSaltChars reports whether every character in s is a valid crypt(3) salt character.

Types

This section is empty.

Jump to

Keyboard shortcuts

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