// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Every scheme as a value, for code that learns which scheme it has only at //! run time: passlib's `passlib.registry`. //! //! The schemes themselves are namespaces in `passlib.hash`, used directly //! when the scheme is known at compile time, with `Options` to choose their //! settings. A `Scheme` is the same scheme as a table of function pointers //! with the default settings, so that a stored hash can be checked without //! knowing in advance what wrote it. //! //! ## Identifying a hash //! //! Most formats announce themselves with a prefix: `$1$`, `$2b$`, `{SSHA}`. //! Several do not. MD5, MD4, the NT hash, the LM hash and the domain cached //! credentials are all 32 hex digits, and nothing in the string says which. //! `identify` returns the first scheme in `all` that claims a hash, and `all` //! lists the prefixed formats first, so the answer for a prefixed hash is //! reliable and the answer for a bare hex string is a guess. Code that knows //! which of the ambiguous schemes it stores should ask with `identifyAmong`. //! //! The schemes that claim every string (`plaintext`, `ldap_plaintext`, //! `roundup_plaintext`, `unix_fallback`) are marked `catch_all`, and //! `identify` never returns them. They are still reachable by name. 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; const root = @import("root.zig"); pub const Scheme = struct { name: []const u8, /// Claims every string, so `identify` skips it. catch_all: bool, identifyFn: *const fn ([]const u8) bool, hashFn: *const fn (Context, []const u8, *Io.Writer) Error!void, verifyFn: *const fn (Context, []const u8, []const u8) Error!bool, needsUpdateFn: *const fn ([]const u8) Error!bool, pub fn identify(s: *const Scheme, h: []const u8) bool { return s.identifyFn(h); } /// Hashes `password` with this scheme's default settings. pub fn hash(s: *const Scheme, ctx: Context, password: []const u8, w: *Io.Writer) Error!void { return s.hashFn(ctx, password, w); } pub fn verify(s: *const Scheme, ctx: Context, password: []const u8, h: []const u8) Error!bool { return s.verifyFn(ctx, password, h); } /// Whether `h` was made with settings other than this scheme's defaults. pub fn needsUpdate(s: *const Scheme, h: []const u8) Error!bool { return s.needsUpdateFn(h); } }; /// `S` as a `Scheme`, with its default options. A scheme that claims every /// string declares `pub const catch_all = true`. pub fn of(comptime S: type) Scheme { handler.check(S); const wrap = struct { fn hashDefault(ctx: Context, password: []const u8, w: *Io.Writer) Error!void { return S.hash(ctx, password, .{}, w); } fn needsUpdateDefault(h: []const u8) Error!bool { return S.needsUpdate(h, .{}); } }; return .{ .name = S.name, .catch_all = @hasDecl(S, "catch_all") and S.catch_all, .identifyFn = S.identify, .hashFn = wrap.hashDefault, .verifyFn = S.verify, .needsUpdateFn = wrap.needsUpdateDefault, }; } /// Every scheme, in the order `identify` tries them: `root.scheme_modules` /// in order, and each module's `schemes` in order within it. pub const all = blk: { var n = 0; for (root.scheme_modules) |m| n += m.schemes.len; var out: [n]Scheme = undefined; var i = 0; for (root.scheme_modules) |m| { for (m.schemes) |S| { out[i] = of(S); i += 1; } } const final = out; break :blk final; }; /// The scheme called `name`, as passlib names it. pub fn get(name: []const u8) ?*const Scheme { for (&all) |*s| if (std.mem.eql(u8, s.name, name)) return s; return null; } /// The first scheme in `all` that claims `h`, skipping the catch-alls. See /// the top of this file for why that is a guess for a bare hex string. pub fn identify(h: []const u8) ?*const Scheme { for (&all) |*s| if (!s.catch_all and s.identify(h)) return s; return null; } /// The first of `candidates` that claims `h`. pub fn identifyAmong(h: []const u8, candidates: []const *const Scheme) ?*const Scheme { for (candidates) |s| if (s.identify(h)) return s; return null; } /// Checks `password` against `h`, whatever scheme wrote it. /// `error.UnknownHash` if no scheme claims it. pub fn verify(ctx: Context, password: []const u8, h: []const u8) (Error || error{UnknownHash})!bool { const s = identify(h) orelse return error.UnknownHash; return s.verify(ctx, password, h); } test "names are unique" { for (all, 0..) |a, i| { for (all[i + 1 ..]) |b| try testing.expect(!std.mem.eql(u8, a.name, b.name)); } } test "get and identify" { try testing.expectEqualStrings("md5_crypt", get("md5_crypt").?.name); try testing.expect(get("no_such_scheme") == null); try testing.expectEqualStrings("apr_md5_crypt", identify("$apr1$test$0e8oyYZUPnLv2JmB7wQOB1").?.name); const ctx: Context = .{ .io = testing.io, .gpa = testing.allocator }; try testing.expect(try verify(ctx, "test", "$1$test$pi/xDtU5WFVRqYS6BMU8X/")); try testing.expectError(error.UnknownHash, verify(ctx, "test", "$unknown$")); }