// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! PostgreSQL's MD5 password hash: passlib's `passlib.handlers.postgres`. //! //! `md5` and 32 hex characters of MD5 over the password followed by the //! user name: what `pg_authid.rolpassword` held from PostgreSQL 7.2 until //! SCRAM-SHA-256 replaced it as the default in PostgreSQL 14. //! //! **It is obsolete.** The only salt is the user name, so every `postgres` //! account shares one table, and a single MD5 is fast enough to try billions //! of guesses a second. Worse, the MD5 challenge-response login protocol //! takes this hash, not the password, so anyone holding the hash can log in //! with it directly. It is here to read the catalogs of old servers so that //! their roles can be moved to SCRAM. const std = @import("std"); const Io = std.Io; const testing = std.testing; const Md5 = std.crypto.hash.Md5; const encoding = @import("../encoding.zig"); const handler = @import("../handler.zig"); const Context = handler.Context; const Error = handler.Error; /// PostgreSQL's MD5 hash, salted with the user name from `Context.user`, /// which it requires. /// /// passlib accepts hex of either case as well-formed, but compares it with /// the lower-case digest it computes without folding case, so a hash /// written in upper case never verifies. That is reproduced. pub const postgres_md5 = struct { pub const name = "postgres_md5"; pub const Options = struct {}; const prefix = "md5"; const hex_len = 2 * Md5.digest_length; fn parse(h: []const u8) Error![]const u8 { if (!std.mem.startsWith(u8, h, prefix)) return error.MalformedHash; const rest = h[prefix.len..]; if (rest.len != hex_len or !encoding.allIn(rest, encoding.hex_chars)) return error.MalformedHash; return rest; } fn compute(ctx: Context, password: []const u8, out: *[hex_len]u8) Error!void { const user = ctx.user orelse return error.MissingUser; var md5 = Md5.init(.{}); md5.update(password); md5.update(user); var digest: [Md5.digest_length]u8 = undefined; md5.final(&digest); _ = encoding.hexLower(out, &digest); } pub fn identify(h: []const u8) bool { _ = parse(h) catch return false; return true; } pub fn hash(ctx: Context, password: []const u8, options: Options, w: *Io.Writer) Error!void { _ = options; try handler.checkPasswordLen(password); var out: [hex_len]u8 = undefined; try compute(ctx, password, &out); try w.print(prefix ++ "{s}", .{out}); } pub fn verify(ctx: Context, password: []const u8, h: []const u8) Error!bool { try handler.checkPasswordLen(password); const stored = try parse(h); var out: [hex_len]u8 = undefined; try compute(ctx, password, &out); return handler.eql(stored, &out); } pub fn needsUpdate(h: []const u8, options: Options) Error!bool { _ = options; _ = try parse(h); return false; } }; /// The schemes this module defines, in the order `registry.identify` tries /// them. pub const schemes = .{postgres_md5}; comptime { handler.check(postgres_md5); } test "reproduces passlib" { // passlib 1.7.4: postgres_md5.hash("mypass", user="postgres"). const ctx: Context = .{ .io = testing.io, .gpa = testing.allocator, .user = "postgres" }; var buf: [64]u8 = undefined; var w: Io.Writer = .fixed(&buf); try postgres_md5.hash(ctx, "mypass", .{}, &w); try testing.expectEqualStrings("md55fba2ea04fd36069d2574ea71c8efe9d", w.buffered()); try testing.expect(try postgres_md5.verify(ctx, "mypass", w.buffered())); try testing.expect(!try postgres_md5.verify(ctx, "mypasS", w.buffered())); // Upper-case hex is well-formed and never verifies, as in passlib. try testing.expect(!try postgres_md5.verify(ctx, "mypass", "md55FBA2EA04FD36069D2574EA71C8EFE9D")); try testing.expect(!postgres_md5.identify("MD55fba2ea04fd36069d2574ea71c8efe9d")); } test "refusals" { const no_user: Context = .{ .io = testing.io, .gpa = testing.allocator }; var buf: [64]u8 = undefined; var w: Io.Writer = .fixed(&buf); try testing.expectError(error.MissingUser, postgres_md5.hash(no_user, "x", .{}, &w)); try testing.expectError(error.MalformedHash, postgres_md5.verify(no_user, "x", "md54zc31989b20437833f697e485811254b")); }