// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Plain hex digests, and Apache's `htdigest`: passlib's //! `passlib.handlers.digests`. //! //! **None of these is a password hash.** An unsalted digest makes every user //! with the same password share a hash, and a single precomputed table covers //! all of them. A fast digest lets a GPU try billions of guesses a second. //! They are here to read and check passwords stored by systems that did this //! anyway, so that those can be moved to something better. const std = @import("std"); const Io = std.Io; const testing = std.testing; const crypto_ext = @import("std_crypto_ext"); const encoding = @import("../encoding.zig"); const handler = @import("../handler.zig"); const Context = handler.Context; const Error = handler.Error; /// A digest of the password, as lower-case hex. Verification accepts either /// case, as passlib does. pub fn HexDigest(comptime scheme_name: []const u8, comptime Hash: type) type { return struct { pub const name = scheme_name; pub const Options = struct {}; const hex_len = Hash.digest_length * 2; pub fn identify(h: []const u8) bool { return h.len == hex_len and encoding.allIn(h, encoding.hex_chars); } fn compute(password: []const u8, out: *[hex_len]u8) void { var digest: [Hash.digest_length]u8 = undefined; Hash.hash(password, &digest, .{}); _ = encoding.hexLower(out, &digest); } pub fn hash(ctx: Context, password: []const u8, options: Options, w: *Io.Writer) Error!void { _ = ctx; _ = options; try handler.checkPasswordLen(password); var out: [hex_len]u8 = undefined; compute(password, &out); try w.writeAll(&out); } pub fn verify(ctx: Context, password: []const u8, h: []const u8) Error!bool { _ = ctx; try handler.checkPasswordLen(password); if (!identify(h)) return error.MalformedHash; var stored: [hex_len]u8 = undefined; for (&stored, h) |*s, c| s.* = std.ascii.toLower(c); var out: [hex_len]u8 = undefined; compute(password, &out); return handler.eql(&stored, &out); } pub fn needsUpdate(h: []const u8, options: Options) Error!bool { _ = options; if (!identify(h)) return error.MalformedHash; return false; } }; } pub const hex_md4 = HexDigest("hex_md4", crypto_ext.Md4); pub const hex_md5 = HexDigest("hex_md5", std.crypto.hash.Md5); pub const hex_sha1 = HexDigest("hex_sha1", std.crypto.hash.Sha1); pub const hex_sha256 = HexDigest("hex_sha256", std.crypto.hash.sha2.Sha256); pub const hex_sha512 = HexDigest("hex_sha512", std.crypto.hash.sha2.Sha512); /// Apache's `htdigest`: MD5 over `user:realm:password`, as lower-case hex. /// /// The user and realm are the salt, and both are required: they come from /// `Context.user` and `Context.realm`. Only lower-case hex is accepted, as /// passlib accepts only that. pub const htdigest = struct { pub const name = "htdigest"; pub const Options = struct {}; pub fn identify(h: []const u8) bool { return h.len == 32 and encoding.allIn(h, encoding.lower_hex_chars); } fn compute(ctx: Context, password: []const u8, out: *[32]u8) Error!void { const user = ctx.user orelse return error.MissingUser; const realm = ctx.realm orelse return error.MissingRealm; var md5 = std.crypto.hash.Md5.init(.{}); md5.update(user); md5.update(":"); md5.update(realm); md5.update(":"); md5.update(password); var digest: [16]u8 = undefined; md5.final(&digest); _ = encoding.hexLower(out, &digest); } pub fn hash(ctx: Context, password: []const u8, options: Options, w: *Io.Writer) Error!void { _ = options; try handler.checkPasswordLen(password); var out: [32]u8 = undefined; try compute(ctx, password, &out); try w.writeAll(&out); } pub fn verify(ctx: Context, password: []const u8, h: []const u8) Error!bool { try handler.checkPasswordLen(password); if (!identify(h)) return error.MalformedHash; var out: [32]u8 = undefined; try compute(ctx, password, &out); return handler.eql(h, &out); } pub fn needsUpdate(h: []const u8, options: Options) Error!bool { _ = options; if (!identify(h)) return error.MalformedHash; return false; } }; /// The schemes this module defines, in the order `registry.identify` tries /// them. pub const schemes = .{ hex_md4, hex_md5, hex_sha1, hex_sha256, hex_sha512, htdigest }; comptime { for (.{ hex_md4, hex_md5, hex_sha1, hex_sha256, hex_sha512, htdigest }) |S| handler.check(S); } const test_ctx: Context = .{ .io = testing.io, .gpa = testing.allocator }; test "hex digests of \"password\"" { var buf: [128]u8 = undefined; var w: Io.Writer = .fixed(&buf); try hex_md5.hash(test_ctx, "password", .{}, &w); try testing.expectEqualStrings("5f4dcc3b5aa765d61d8327deb882cf99", w.buffered()); try testing.expect(try hex_md5.verify(test_ctx, "password", "5F4DCC3B5AA765D61D8327DEB882CF99")); try testing.expect(!try hex_md5.verify(test_ctx, "Password", "5f4dcc3b5aa765d61d8327deb882cf99")); try testing.expect(try hex_md4.verify(test_ctx, "password", "8a9d093f14f8701df17732b2bb182c74")); try testing.expectError(error.MalformedHash, hex_md5.verify(test_ctx, "password", "5f4dcc3b")); } test "htdigest needs a user and a realm" { // passlib: htdigest.hash("password", "user", "realm") const ctx: Context = .{ .io = testing.io, .gpa = testing.allocator, .user = "user", .realm = "realm" }; try testing.expect(try htdigest.verify(ctx, "password", "ebbc0ff9a121dbb6789bbe5f82174fa0")); try testing.expectError(error.MissingUser, htdigest.verify(test_ctx, "password", "ebbc0ff9a121dbb6789bbe5f82174fa0")); try testing.expect(!htdigest.identify("EBBC0FF9A121DBB6789BBE5F82174FA0")); }