# zig-passlib
The password hash schemes of passlib 1.7.4 [1, 2], the Python library, in
Zig 0.16. There are 77 of them: the Unix `crypt` family, bcrypt, scrypt,
Argon2, the PBKDF2 formats, and the formats of LDAP, Django, the databases,
Windows and Cisco. There are also four Cisco formats passlib never got, which
its issue #87 asks for [23]. Each scheme can do three things:
- create a hash;
- check a password against one;
- say whether a hash was made with out-of-date settings.
The hashes are byte for byte the ones passlib makes and reads. passlib's own
known-answer tables are the tests, and passlib itself checks hashes made here.
The API documentation is generated from the doc comments, which carry most
of the explanation, and is published at
****.
```console
$ git clone https://git.jcollie.dev/jeff/zig-passlib.git
$ cd zig-passlib
$ nix develop -c zig build test
```
## Where this lives
Three homes, with the same history in each.
* Forgejo, at , which is where the
workflow runs and where the documentation is published from.
* [Tangled](https://tangled.org/), at
.
* [Radicle](https://radicle.xyz/), as `rad:z3iq64oPEpxfFL8B4bN3pRMWZS8C8`. A
Radicle repository is only findable by its ID, so that string is the whole
address:
```console
$ rad clone rad:z3iq64oPEpxfFL8B4bN3pRMWZS8C8
```
## Scope
Only the hash schemes are ported. passlib's `CryptContext` policy object, its
Apache `htpasswd` file handling, its TOTP support and its password generator
are not.
## Most of these are obsolete
⚠️ **Most of the schemes here should not be used for new passwords, and
several were never secure.** An unsalted MD5 or SHA-1 digest, the LM hash, the
NT hash, DES `crypt`, MySQL's old hash and Cisco's types 4 and 7 can be reversed or
brute-forced cheaply. They exist so that passwords stored by systems that used
them can be checked, and then rehashed with something better when their users
next log in.
For a new hash, use `argon2`, `bcrypt` or `scrypt`. Where a standard requires
something else, use `pbkdf2_sha256` or `sha512_crypt`.
**A hash sets its own cost.** A bcrypt string can ask for 2^31 rounds, and an
Argon2 string for terabytes of memory. Verifying it means doing what it asks,
here as in passlib. Only verify hashes from a store you trust to hold what you
put there.
## Using it
```sh
zig fetch --save git+https://git.jcollie.dev/jeff/zig-passlib.git
```
```zig
// build.zig
const passlib = b.dependency("passlib", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("passlib", passlib.module("passlib"));
```
```zig
const passlib = @import("passlib");
const ctx: passlib.Context = .{ .io = io, .gpa = gpa };
// A scheme known at compile time, with settings. Each scheme's `Options` are
// passlib's `using()` settings, with passlib's defaults.
var buf: [256]u8 = undefined;
var w: std.Io.Writer = .fixed(&buf);
try passlib.hash.argon2.hash(ctx, password, .{}, &w);
const stored = w.buffered();
// A stored hash, whatever scheme wrote it.
if (try passlib.verify(ctx, password, stored)) {
// Rehash if it was made with weaker settings than today's.
const scheme = passlib.identify(stored).?;
if (try scheme.needsUpdate(stored)) {
// ...
}
}
```
`Context` carries the `Io` that random salts come from, the allocator that
scrypt and Argon2 need, and the user name and realm for the schemes salted
with them (`postgres_md5`, `oracle10`, `msdcc`, `msdcc2`, `htdigest`).
Passwords are bytes. passlib encodes text as UTF-8, so a UTF-8 password
here gives the hash passlib gives.
`passlib.registry` has every scheme as a value: `get` by name, and `identify`
to find a scheme from a hash. Several formats are bare hex strings of the same
length, and nothing in the string says which scheme made them. `identify`
tries the formats with a distinctive prefix first, so its answer for a bare
hex string is a guess, and `identifyAmong` narrows the candidates.
## The schemes
| Family | Schemes | Primitives |
| --- | --- | --- |
| Unix `crypt` | `sha512_crypt`, `sha256_crypt` [3], `md5_crypt`, `apr_md5_crypt`, `sha1_crypt`, `sun_md5_crypt`, `bsdi_crypt`, `des_crypt`, `bigcrypt`, `crypt16` | std's SHA-2, MD5 and HMAC-SHA1; crypt(3)'s salted DES [4] from zig-std-crypto-ext |
| Modern | `argon2` [5, 6, 22], `bcrypt` [7], `bcrypt_sha256`, `scrypt` [8] | std's bcrypt and scrypt; Argon2 at version 1.0 and 1.3 from zig-std-crypto-ext |
| PBKDF2 [9, 21] | `pbkdf2_sha1`, `pbkdf2_sha256`, `pbkdf2_sha512`, `cta_pbkdf2_sha1`, `dlitz_pbkdf2_sha1`, `atlassian_pbkdf2_sha1`, `grub_pbkdf2_sha512`, `ldap_pbkdf2_*` | std's PBKDF2 and HMAC |
| Other application formats | `scram` [10, 11], `phpass`, `fshp` | PBKDF2 and SASLprep [12, 13] from zig-scram; MD5; SHA |
| LDAP [14] | `ldap_md5`, `ldap_sha1`, `ldap_salted_md5`, `ldap_salted_sha1`, `ldap_salted_sha256`, `ldap_salted_sha512`, `ldap_plaintext`, and `{CRYPT}` around each Unix scheme | std's MD5, SHA-1, SHA-2 |
| Roundup | `ldap_hex_md5`, `ldap_hex_sha1`, `roundup_plaintext` | |
| Django [15] | `django_pbkdf2_sha256`, `django_pbkdf2_sha1`, `django_argon2`, `django_bcrypt`, `django_bcrypt_sha256`, `django_salted_sha1`, `django_salted_md5`, `django_des_crypt`, `django_disabled` | |
| Databases | `postgres_md5`, `mysql41`, `mysql323`, `mssql2005`, `mssql2000`, `oracle11`, `oracle10` | std's MD5 and SHA-1; DES-CBC from zig-std-crypto-ext |
| Windows [16] | `nthash`, `bsd_nthash`, `lmhash`, `msdcc`, `msdcc2` | MD4 [17] and DES from zig-std-crypto-ext |
| Cisco | `cisco_type9`, `cisco_type8`, `cisco_asa_pbkdf2`, `cisco_type4`, `cisco_asa`, `cisco_pix`, `cisco_type7` | std's scrypt, PBKDF2, SHA-256 and MD5 |
| Digests | `hex_md4`, `hex_md5` [18], `hex_sha1`, `hex_sha256`, `hex_sha512` [19], `htdigest` [20] | |
| Not hashes | `unix_disabled`, `unix_fallback`, `plaintext` | |
The four Cisco formats that passlib 1.7.4 lacks are types 8 (`$8$`,
PBKDF2-HMAC-SHA256) and 9 (`$9$`, scrypt), the ASA's PBKDF2-HMAC-SHA512
(`$sha512$`), and type 4 (an unsalted SHA-256) [26]. None has a published
specification. What is here was established from hashes the devices wrote:
hashcat's examples for types 4, 8 and 9 [24], and an ASA's hash with its
password [23]. It was checked against ciscoPWDhasher's implementation of
types 8 and 9 [25]. `zig build crosscheck` checks these four against Python's
hashlib, where passlib has nothing to say.
std has neither MD4, crypt(3)'s salted DES nor Argon2 version 1.0. They are
in zig-std-crypto-ext, and so are the DES and DES-CBC the LM hash and
`oracle10` need.
## How it is tested
- **passlib's own tables.** `tools/gen_vectors.py` reads every known-answer
table out of passlib 1.7.4's test suite, straight from its test classes, into
`tests/vectors/known.zon`. That is 960 rows of correct hashes, malformed
ones, unidentifiable ones and other schemes' hashes. It also has passlib hash
random passwords with every scheme, into `tests/vectors/generated.zon`.
`tests/vectors.zig` runs every row through the registry.
- **The other direction.** `zig build crosscheck` hashes a spread of passwords
with every scheme and has passlib 1.7.4 verify each hash and refuse a wrong
password. It needs the devshell, whose Python has passlib 1.7.4 and
argon2-cffi. nixpkgs' bcrypt 5.x cannot be driven by passlib 1.7.4, so
passlib's own pure-Python bcrypt stands in.
- **Each scheme's own tests**, whose expected values all came from running
passlib 1.7.4.
- **Fuzzing.** `tests/fuzz.zig` has three targets: every scheme's parser on
arbitrary strings, a hash-then-verify round trip for every scheme, and the
encodings. `zig build fuzz-run` drives them. It has found one bug so far:
`std.unicode.calcUtf16LeLen` panics on UTF-8 cut short at the end, in Zig
0.16.0.
```console
$ nix develop -c zig build test --summary all
$ nix develop -c zig build crosscheck
$ nix develop -c zig build fuzz-run -- --seconds 60
$ nix develop -c python3 tools/gen_vectors.py # regenerate the vectors
```
## Where it differs from passlib
These are the known differences. Each is documented on the scheme it
concerns.
- **Passwords are bytes.** For `lmhash`, passlib upper-cases text and then
encodes it in code page 437. Bytes are upper-cased only in ASCII, by passlib
and here alike, so non-ASCII text has to be upper-cased and encoded before it
is passed in.
- **SASLprep's NFKC** is from uucode's Unicode tables where passlib's is
Python's. `src/saslprep.zig` otherwise follows passlib's reading of
SASLprep exactly, which is neither PostgreSQL's nor the RFC's: it deletes
U+200B rather than making it a space, and it checks for unassigned code
points after normalizing. It agrees with passlib on thousands of strings
built from every class of character where readings differ.
- **Case mapping** for `msdcc`, `oracle10` and `mssql2000` uses Python's full
Unicode mapping, from tables taken from Python 3.14.7 (Unicode 16.0.0).
passlib running on an older Python can fold a few newer characters
differently.
- **Round counts written with `+`, `_` or spaces** are accepted by passlib's
use of Python's `int()`. The SHA-crypt, `sha1_crypt` and PBKDF2 parsers here
refuse them. No implementation writes them.
- **`unix_fallback`'s `enable_wildcard`** is not supported. It was deprecated in
passlib 1.7, and without it an empty hash verifies nothing, as here.
- **Parameters `std` cannot compute are refused**, rather than computed:
scrypt with p·128·r of 2^30 or more (std overflows a `u30` there), and Argon2
versions other than 1.0 and 1.3.
- **Inputs that crash passlib are errors here.** `$P$` with nothing after it,
for instance.
## Requirements
Zig 0.16.0. There are two dependencies:
- zig-std-crypto-ext, for MD4, crypt(3) DES, DES and Argon2 1.0;
- zig-scram, for SASLprep.
zig-scram brings uucode, for the Unicode data behind NFKC.
The devshell, `nix develop`, has Zig with the one-line patch its fuzzer
needs, passlib 1.7.4 for the vectors and the crosscheck, zon2nix, reuse and
git-pages-cli.
## The API documentation
`zig build docs` writes it to `zig-out/docs`, and `zig build docs-serve` serves
it at . It has to be served rather than opened as a
file, because the viewer fetches its sources at run time. The Forgejo workflow
publishes it from `main` to .
## References cited
1. Eli Collins, *Passlib 1.7.4 documentation*, Assurance Technologies,
8 October 2020. .
2. Eli Collins, *passlib* 1.7.4, 8 October 2020.
. The code this is a port of, and
the test suite its vectors come from.
3. Ulrich Drepper, *Unix crypt using SHA-256 and SHA-512*, 2016.
.
4. Robert Morris and Ken Thompson, "Password Security: A Case History",
*Communications of the ACM* 22(11), pp. 594–597, November 1979.
.
5. Alex Biryukov, Daniel Dinu, Dmitry Khovratovich and Simon Josefsson,
*Argon2 Memory-Hard Function for Password Hashing and Proof-of-Work
Applications*, RFC 9106, September 2021.
.
6. Thomas Pornin, *PHC string format*, Password Hashing Competition, 2017.
.
7. Niels Provos and David Mazières, "A Future-Adaptable Password Scheme",
*Proceedings of the FREENIX Track: 1999 USENIX Annual Technical
Conference*, pp. 81–91, June 1999.
.
8. Colin Percival and Simon Josefsson, *The scrypt Password-Based Key
Derivation Function*, RFC 7914, August 2016.
.
9. Kathleen Moriarty, Burt Kaliski and Andreas Rusch, *PKCS #5:
Password-Based Cryptography Specification Version 2.1*, RFC 8018,
January 2017. .
10. Chris Newman, Abhijit Menon-Sen, Alexey Melnikov and Nicolas Williams,
*Salted Challenge Response Authentication Mechanism (SCRAM) SASL and
GSS-API Mechanisms*, RFC 5802, July 2010.
.
11. Tony Hansen, *SCRAM-SHA-256 and SCRAM-SHA-256-PLUS Simple Authentication
and Security Layer (SASL) Mechanisms*, RFC 7677, November 2015.
.
12. Paul Hoffman and Marc Blanchet, *Preparation of Internationalized Strings
("stringprep")*, RFC 3454, December 2002.
.
13. Kurt D. Zeilenga, *SASLprep: Stringprep Profile for User Names and
Passwords*, RFC 4013, February 2005.
.
14. Luke Howard, *An Approach for Using LDAP as a Network Information
Service*, RFC 2307, March 1998. .
15. Django Software Foundation, *Password management in Django*.
.
16. Microsoft Corporation, *[MS-NLMP]: NT LAN Manager (NTLM) Authentication
Protocol*.
.
The LM and NT hashes.
17. Ronald L. Rivest, *The MD4 Message-Digest Algorithm*, RFC 1320, April
1992. .
18. Ronald L. Rivest, *The MD5 Message-Digest Algorithm*, RFC 1321, April
1992. .
19. National Institute of Standards and Technology, *Secure Hash Standard
(SHS)*, FIPS PUB 180-4, August 2015.
.
20. John Franks, Phillip Hallam-Baker, Jeffrey Hostetler, Scott Lawrence, Paul
Leach, Ari Luotonen and Lawrence Stewart, *HTTP Authentication: Basic and
Digest Access Authentication*, RFC 2617, June 1999.
.
21. Hugo Krawczyk, Mihir Bellare and Ran Canetti, *HMAC: Keyed-Hashing for
Message Authentication*, RFC 2104, February 1997.
.
22. Alex Biryukov, Daniel Dinu and Dmitry Khovratovich, *Argon2 reference
implementation*. . Through
argon2-cffi, the Argon2 backend passlib's vectors were made with.
23. Eli Collins and others, *Add support for Cisco PBKDF2 and Scrypt
hashes*, passlib issue 87, 29 January 2017.
; the ASA
format and its example hash with the password "cisco" are in the
discussion of it at
.
24. hashcat, *Example hashes*, modes 5700, 9200 and 9300.
.
25. Brett Verney, *ciscoPWDhasher* 1.1, 27 November 2025.
.
26. *Cisco Password Types Explained*, network-switch.com.
.
Types 0 to 9, where each came from and which to use.
## Licence
MIT. See `LICENSES/MIT.txt`.