Something went wrong. Try again.
passlib's password hash schemes, ported to Zig 0.16
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The schemes that are not hashes: passlib's `passlib.handlers.misc`.//!//! `unix_disabled` is the marker a Unix password database stores for an//! account that cannot log in with a password: `!` or `*`, sometimes in front//! of the hash it replaced. `plaintext` is the password itself, for systems//! that stored it that way. `unix_fallback` is passlib's deprecated//! predecessor of `unix_disabled`, kept because passlib 1.7.4 still has it.
const std = @import("std");const Io = std.Io;const testing = std.testing;
const handler = @import("../handler.zig");const Context = handler.Context;const Error = handler.Error;
/// A locked account: the empty string, or anything starting with `!` or/// `*`. No password verifies against it.////// `disable` and `enable` put a marker in front of an existing hash and take/// it off again, which is how `passwd -l` locks an account without losing/// its password.pub const unix_disabled = struct { pub const name = "unix_disabled";
pub const Options = struct { /// What `hash` writes. It must itself be a disabled marker. passlib /// defaults to `*` on the BSDs and `!` everywhere else. marker: []const u8 = "!", };
pub fn identify(h: []const u8) bool { return h.len == 0 or h[0] == '!' or h[0] == '*'; }
/// Writes the marker, whatever the password. pub fn hash(ctx: Context, password: []const u8, options: Options, w: *Io.Writer) Error!void { _ = ctx; try handler.checkPasswordLen(password); if (options.marker.len == 0 or !identify(options.marker)) return error.InvalidOptions; try w.writeAll(options.marker); }
/// False for every password, and `error.MalformedHash` for a string that /// is not a disabled marker. pub fn verify(ctx: Context, password: []const u8, h: []const u8) Error!bool { _ = ctx; try handler.checkPasswordLen(password); if (!identify(h)) return error.MalformedHash; return false; }
pub fn needsUpdate(h: []const u8, options: Options) Error!bool { _ = options; if (!identify(h)) return error.MalformedHash; return false; }
/// `h` locked: the marker in front of it. A hash that is already locked /// is locked afresh rather than marked twice, and one that is only a /// marker, with nothing behind it, is `error.MalformedHash`, as passlib /// has it. pub fn disable(h: ?[]const u8, options: Options, w: *Io.Writer) Error!void { if (options.marker.len == 0 or !identify(options.marker)) return error.InvalidOptions; const inner = if (h) |original| if (identify(original)) try enable(original) else original else ""; try w.writeAll(options.marker); try w.writeAll(inner); }
/// The hash a locked one was made from. `error.MalformedHash` if `h` is /// not locked, or if there is nothing behind its marker to restore. pub fn enable(h: []const u8) Error![]const u8 { if (h.len == 0 or !identify(h)) return error.MalformedHash; if (h.len == 1) return error.MalformedHash; return h[1..]; }};
/// passlib's deprecated `unix_fallback`: claims every string and verifies/// nothing. passlib's `enable_wildcard`, which let an empty hash accept any/// password, is not reproduced; an empty hash here verifies nothing, which/// is what passlib does without it.pub const unix_fallback = struct { pub const name = "unix_fallback"; pub const catch_all = true; pub const Options = struct {};
pub fn identify(h: []const u8) bool { _ = h; return true; }
pub fn hash(ctx: Context, password: []const u8, options: Options, w: *Io.Writer) Error!void { _ = ctx; _ = options; try handler.checkPasswordLen(password); try w.writeAll("!"); }
pub fn verify(ctx: Context, password: []const u8, h: []const u8) Error!bool { _ = ctx; _ = h; try handler.checkPasswordLen(password); return false; }
pub fn needsUpdate(h: []const u8, options: Options) Error!bool { _ = h; _ = options; return false; }};
/// The password itself. It must be UTF-8, as passlib requires it to be/// text; a password that is not is `error.InvalidPassword`, and a stored/// string that is not is `error.MalformedHash`.pub const plaintext = Plaintext("plaintext", struct { fn claims(h: []const u8) bool { _ = h; return true; }}.claims, true);
/// `plaintext` with the set of strings it claims narrowed by `claims`, for/// `ldap_plaintext`, which must not claim the `{SCHEME}` strings of the/// other LDAP formats.pub fn Plaintext(comptime scheme_name: []const u8, comptime claims: fn ([]const u8) bool, comptime claims_all: bool) type { return struct { pub const name = scheme_name; pub const catch_all = claims_all; pub const Options = struct {};
pub fn identify(h: []const u8) bool { return claims(h); }
pub fn hash(ctx: Context, password: []const u8, options: Options, w: *Io.Writer) Error!void { _ = ctx; _ = options; try handler.checkPasswordLen(password); if (!std.unicode.utf8ValidateSlice(password)) return error.InvalidPassword; try w.writeAll(password); }
pub fn verify(ctx: Context, password: []const u8, h: []const u8) Error!bool { _ = ctx; if (!std.unicode.utf8ValidateSlice(h) or !claims(h)) return error.MalformedHash; try handler.checkPasswordLen(password); if (!std.unicode.utf8ValidateSlice(password)) return error.InvalidPassword; return handler.eql(password, h); }
pub fn needsUpdate(h: []const u8, options: Options) Error!bool { _ = options; if (!claims(h)) return error.MalformedHash; return false; } };}
/// The schemes this module defines, in the order `registry.identify` tries/// them.pub const schemes = .{ unix_disabled, plaintext, unix_fallback };
comptime { for (schemes) |S| handler.check(S);}
const test_ctx: Context = .{ .io = testing.io, .gpa = testing.allocator };
test "unix_disabled" { try testing.expect(unix_disabled.identify("")); try testing.expect(unix_disabled.identify("!$1$abc")); try testing.expect(!unix_disabled.identify("abc")); try testing.expect(!try unix_disabled.verify(test_ctx, "", "!")); try testing.expectError(error.MalformedHash, unix_disabled.verify(test_ctx, "x", "abc"));
var buf: [64]u8 = undefined; var w: Io.Writer = .fixed(&buf); try unix_disabled.disable("$1$abc", .{}, &w); try testing.expectEqualStrings("!$1$abc", w.buffered()); try testing.expectEqualStrings("$1$abc", try unix_disabled.enable(w.buffered())); try testing.expectError(error.MalformedHash, unix_disabled.enable("!"));
w = .fixed(&buf); try unix_disabled.disable("*$1$abc", .{}, &w); try testing.expectEqualStrings("!$1$abc", w.buffered());}
test "plaintext" { try testing.expect(try plaintext.verify(test_ctx, "pa\xc3\x9f", "pa\xc3\x9f")); try testing.expect(!try plaintext.verify(test_ctx, "pass", "pa\xc3\x9f")); try testing.expectError(error.InvalidPassword, plaintext.verify(test_ctx, "\xff", "pass")); try testing.expectError(error.MalformedHash, plaintext.verify(test_ctx, "pass", "\xff"));}