Something went wrong. Try again.
passlib's password hash schemes, ported to Zig 0.16
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Hashes made here, for passlib to check: the other half of the vectors.//!//! `tests/vectors.zig` shows that this library reads what passlib wrote.//! This shows the converse. Every scheme hashes a spread of passwords with//! random salts, at its cheapest settings, and writes one line per hash to//! the file named on the command line://!//! ```//! scheme <TAB> hex(password) <TAB> user <TAB> realm <TAB> hex(hash)//! ```//!//! `tools/crosscheck.py` then asks passlib 1.7.4 to verify each one.//! `zig build crosscheck` runs both.
const std = @import("std");const passlib = @import("passlib");const targets = @import("fuzz_targets");
/// Passwords worth trying: empty, ASCII, the characters that separate a/// hash's fields, and text that is not ASCII, including characters SASLprep/// maps and the NT hash needs surrogates for.const fixed_passwords = [_][]const u8{ "", "password", "Tr0ub4dor&3", "$:{}=,.", "\xc3\xa9t\xc3\xa9", "\xe2\x82\xac\xc2\xa5$", "\xe4\xb8\xad\xe6\x96\x87", "\xf0\x9f\x98\x80", "a" ** 80,};
const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !#%&*+-./:;<=>?@^_~";
pub fn main(init: std.process.Init) !void { const io = init.io; const gpa = init.gpa;
var args: std.process.Args.Iterator = .init(init.minimal.args); _ = args.skip(); const path = args.next() orelse { std.debug.print("usage: crosscheck OUTPUT\n", .{}); std.process.exit(2); };
var file = try std.Io.Dir.cwd().createFile(io, path, .{}); defer file.close(io); var file_buffer: [4096]u8 = undefined; var fw = file.writer(io, &file_buffer); const out = &fw.interface;
targets.backing = gpa; targets.io = io;
var seed: u64 = undefined; io.random(std.mem.asBytes(&seed)); var prng: std.Random.DefaultPrng = .init(seed); const random = prng.random();
var written: usize = 0; var refused: usize = 0; for (&passlib.registry.all) |*scheme| { var random_passwords: [4][24]u8 = undefined; var passwords: [fixed_passwords.len + random_passwords.len][]const u8 = undefined; for (fixed_passwords, 0..) |p, i| passwords[i] = p; for (&random_passwords, 0..) |*buf, i| { const len = random.uintLessThan(usize, buf.len); for (buf[0..len]) |*c| c.* = alphabet[random.uintLessThan(usize, alphabet.len)]; passwords[fixed_passwords.len + i] = buf[0..len]; }
for (passwords, 0..) |password, i| { // passlib hashes the empty password to the empty string under // ldap_plaintext and then refuses that string, and so does this // port, so there is nothing to ask it. if (std.mem.eql(u8, scheme.name, "ldap_plaintext") and password.len == 0) continue; var user_buf: [16]u8 = undefined; const user = try std.fmt.bufPrint(&user_buf, "user{d}", .{i}); const ctx: passlib.Context = .{ .io = io, .gpa = gpa, .user = user, .realm = "realm" }; var hash_buf: [2048]u8 = undefined; var w: std.Io.Writer = .fixed(&hash_buf); targets.hashCheap(scheme, ctx, password, &w) catch |err| switch (err) { error.InvalidPassword, error.PasswordTooLong => { refused += 1; continue; }, else => return err, }; try out.print("{s}\t{x}\t{s}\t{s}\t{x}\n", .{ scheme.name, password, user, "realm", w.buffered() }); written += 1; } } try out.flush(); std.debug.print("{d} hashes written, {d} passwords refused by their scheme\n", .{ written, refused });}