Something went wrong. Try again.
passlib's password hash schemes, ported to Zig 0.16
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! passlib's password hash schemes, in Zig.//!//! A port of the hash schemes of passlib 1.7.4, the Python library: the Unix//! `crypt` family, bcrypt, scrypt, Argon2, the PBKDF2 formats, and the//! formats of LDAP, Django, the databases, Windows and Cisco. Each can create//! a hash, check a password against one, and say whether a hash was made with//! out-of-date settings. The hashes are byte-for-byte the ones passlib makes//! and reads.//!//! ```zig//! const passlib = @import("passlib");//!//! const ctx: passlib.Context = .{ .io = io, .gpa = gpa };//!//! // A scheme known at compile time, with settings.//! var buf: [128]u8 = undefined;//! var w: std.Io.Writer = .fixed(&buf);//! try passlib.hash.sha512_crypt.hash(ctx, password, .{ .rounds = 700_000 }, &w);//!//! // A stored hash, whatever wrote it.//! const ok = try passlib.verify(ctx, password, stored);//! ```//!//! Most of the schemes here are obsolete, and several were never secure.//! They exist to check passwords stored by systems that used them, so that//! the passwords can be rehashed with something better when their users next//! log in. For a new hash, use `argon2`, `bcrypt`, `scrypt`, or//! `pbkdf2_sha256` / `sha512_crypt` where a standard demands one.//!//! `handler.zig` describes what every scheme provides, and `registry.zig`//! how to reach them by name or by identifying a hash.
const std = @import("std");
pub const handler = @import("handler.zig");pub const encoding = @import("encoding.zig");pub const registry = @import("registry.zig");
pub const Context = handler.Context;pub const Error = handler.Error;pub const Scheme = registry.Scheme;
pub const identify = registry.identify;pub const verify = registry.verify;
/// Every scheme, by passlib's name for it: `passlib.hash` in Python.pub const hash = struct { const md5_crypt_mod = @import("schemes/md5_crypt.zig"); pub const md5_crypt = md5_crypt_mod.md5_crypt; pub const apr_md5_crypt = md5_crypt_mod.apr_md5_crypt;
const sha2_crypt_mod = @import("schemes/sha2_crypt.zig"); pub const sha256_crypt = sha2_crypt_mod.sha256_crypt; pub const sha512_crypt = sha2_crypt_mod.sha512_crypt;
const sha1_crypt_mod = @import("schemes/sha1_crypt.zig"); pub const sha1_crypt = sha1_crypt_mod.sha1_crypt;
const sun_md5_crypt_mod = @import("schemes/sun_md5_crypt.zig"); pub const sun_md5_crypt = sun_md5_crypt_mod.sun_md5_crypt;
const bcrypt_mod = @import("schemes/bcrypt.zig"); pub const bcrypt = bcrypt_mod.bcrypt; pub const bcrypt_sha256 = bcrypt_mod.bcrypt_sha256;
const scrypt_mod = @import("schemes/scrypt.zig"); pub const scrypt = scrypt_mod.scrypt;
const argon2_mod = @import("schemes/argon2.zig"); pub const argon2 = argon2_mod.argon2;
const pbkdf2_mod = @import("schemes/pbkdf2.zig"); pub const pbkdf2_sha1 = pbkdf2_mod.pbkdf2_sha1; pub const pbkdf2_sha256 = pbkdf2_mod.pbkdf2_sha256; pub const pbkdf2_sha512 = pbkdf2_mod.pbkdf2_sha512; pub const cta_pbkdf2_sha1 = pbkdf2_mod.cta_pbkdf2_sha1; pub const dlitz_pbkdf2_sha1 = pbkdf2_mod.dlitz_pbkdf2_sha1; pub const atlassian_pbkdf2_sha1 = pbkdf2_mod.atlassian_pbkdf2_sha1; pub const grub_pbkdf2_sha512 = pbkdf2_mod.grub_pbkdf2_sha512; pub const ldap_pbkdf2_sha1 = pbkdf2_mod.ldap_pbkdf2_sha1; pub const ldap_pbkdf2_sha256 = pbkdf2_mod.ldap_pbkdf2_sha256; pub const ldap_pbkdf2_sha512 = pbkdf2_mod.ldap_pbkdf2_sha512;
const scram_mod = @import("schemes/scram.zig"); pub const scram = scram_mod.scram;
const phpass_mod = @import("schemes/phpass.zig"); pub const phpass = phpass_mod.phpass;
const fshp_mod = @import("schemes/fshp.zig"); pub const fshp = fshp_mod.fshp;
const ldap_digests_mod = @import("schemes/ldap_digests.zig"); pub const ldap_md5 = ldap_digests_mod.ldap_md5; pub const ldap_sha1 = ldap_digests_mod.ldap_sha1; pub const ldap_salted_md5 = ldap_digests_mod.ldap_salted_md5; pub const ldap_salted_sha1 = ldap_digests_mod.ldap_salted_sha1; pub const ldap_salted_sha256 = ldap_digests_mod.ldap_salted_sha256; pub const ldap_salted_sha512 = ldap_digests_mod.ldap_salted_sha512; pub const ldap_plaintext = ldap_digests_mod.ldap_plaintext; pub const ldap_des_crypt = ldap_digests_mod.ldap_des_crypt; pub const ldap_bsdi_crypt = ldap_digests_mod.ldap_bsdi_crypt; pub const ldap_md5_crypt = ldap_digests_mod.ldap_md5_crypt; pub const ldap_bcrypt = ldap_digests_mod.ldap_bcrypt; pub const ldap_sha1_crypt = ldap_digests_mod.ldap_sha1_crypt; pub const ldap_sha256_crypt = ldap_digests_mod.ldap_sha256_crypt; pub const ldap_sha512_crypt = ldap_digests_mod.ldap_sha512_crypt;
const roundup_mod = @import("schemes/roundup.zig"); pub const ldap_hex_md5 = roundup_mod.ldap_hex_md5; pub const ldap_hex_sha1 = roundup_mod.ldap_hex_sha1; pub const roundup_plaintext = roundup_mod.roundup_plaintext;
const django_mod = @import("schemes/django.zig"); pub const django_salted_sha1 = django_mod.django_salted_sha1; pub const django_salted_md5 = django_mod.django_salted_md5; pub const django_bcrypt = django_mod.django_bcrypt; pub const django_bcrypt_sha256 = django_mod.django_bcrypt_sha256; pub const django_pbkdf2_sha256 = django_mod.django_pbkdf2_sha256; pub const django_pbkdf2_sha1 = django_mod.django_pbkdf2_sha1; pub const django_argon2 = django_mod.django_argon2; pub const django_des_crypt = django_mod.django_des_crypt; pub const django_disabled = django_mod.django_disabled;
const mssql_mod = @import("schemes/mssql.zig"); pub const mssql2000 = mssql_mod.mssql2000; pub const mssql2005 = mssql_mod.mssql2005;
const postgres_mod = @import("schemes/postgres.zig"); pub const postgres_md5 = postgres_mod.postgres_md5;
const windows_mod = @import("schemes/windows.zig"); pub const lmhash = windows_mod.lmhash; pub const nthash = windows_mod.nthash; pub const bsd_nthash = windows_mod.bsd_nthash; pub const msdcc = windows_mod.msdcc; pub const msdcc2 = windows_mod.msdcc2;
const mysql_mod = @import("schemes/mysql.zig"); pub const mysql323 = mysql_mod.mysql323; pub const mysql41 = mysql_mod.mysql41;
const oracle_mod = @import("schemes/oracle.zig"); pub const oracle10 = oracle_mod.oracle10; pub const oracle11 = oracle_mod.oracle11;
const cisco_mod = @import("schemes/cisco.zig"); pub const cisco_pix = cisco_mod.cisco_pix; pub const cisco_asa = cisco_mod.cisco_asa; pub const cisco_type7 = cisco_mod.cisco_type7; pub const cisco_type4 = cisco_mod.cisco_type4; pub const cisco_type8 = cisco_mod.cisco_type8; pub const cisco_type9 = cisco_mod.cisco_type9; pub const cisco_asa_pbkdf2 = cisco_mod.cisco_asa_pbkdf2;
const des_crypt_mod = @import("schemes/des_crypt.zig"); pub const des_crypt = des_crypt_mod.des_crypt; pub const bsdi_crypt = des_crypt_mod.bsdi_crypt; pub const bigcrypt = des_crypt_mod.bigcrypt; pub const crypt16 = des_crypt_mod.crypt16;
const digests_mod = @import("schemes/digests.zig"); pub const hex_md4 = digests_mod.hex_md4; pub const hex_md5 = digests_mod.hex_md5; pub const hex_sha1 = digests_mod.hex_sha1; pub const hex_sha256 = digests_mod.hex_sha256; pub const hex_sha512 = digests_mod.hex_sha512; pub const htdigest = digests_mod.htdigest;
const misc_mod = @import("schemes/misc.zig"); pub const unix_disabled = misc_mod.unix_disabled; pub const unix_fallback = misc_mod.unix_fallback; pub const plaintext = misc_mod.plaintext;};
/// Every module of schemes, in the order `registry.identify` tries them:/// the formats with a distinctive prefix first, the bare hex and `crypt`/// strings after, and the catch-alls last.pub const scheme_modules = .{ hash.md5_crypt_mod, hash.sha2_crypt_mod, hash.sha1_crypt_mod, hash.sun_md5_crypt_mod, hash.bcrypt_mod, hash.scrypt_mod, hash.argon2_mod, hash.pbkdf2_mod, hash.scram_mod, hash.phpass_mod, hash.fshp_mod, hash.roundup_mod, hash.ldap_digests_mod, hash.misc_mod, hash.django_mod, hash.mssql_mod, hash.postgres_mod, hash.windows_mod, hash.mysql_mod, hash.oracle_mod, hash.cisco_mod, hash.des_crypt_mod, hash.digests_mod,};
test { std.testing.refAllDecls(@This()); _ = handler; _ = encoding; _ = registry; _ = @import("python.zig"); _ = @import("saslprep.zig"); _ = @import("unicode_case.zig"); inline for (scheme_modules) |m| _ = m;}