Something went wrong. Try again.
passlib's password hash schemes, ported to Zig 0.16
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! MySQL's `PASSWORD()` hashes: passlib's `passlib.handlers.mysql`.//!//! * `mysql323`, from MySQL 3.2.3 to 4.0: a home-made 62-bit hash of the//! password's bytes, skipping spaces and tabs, as sixteen hex characters.//! * `mysql41`, from MySQL 4.1 on: `*` and SHA-1 of SHA-1 of the password,//! as forty upper-case hex characters.//!//! **Both are obsolete.** `mysql323` is not a cryptographic hash at all://! collisions and preimages can be computed directly, and it ignores//! whitespace. `mysql41` is unsalted, so one table covers every server, and//! double SHA-1 is as fast as SHA-1. MySQL 8 replaced it with//! `caching_sha2_password`. They are here to read the `mysql.user` tables of//! old servers and the applications that copied the format.
const std = @import("std");const Io = std.Io;const testing = std.testing;
const Sha1 = std.crypto.hash.Sha1;const encoding = @import("../encoding.zig");const handler = @import("../handler.zig");const Context = handler.Context;const Error = handler.Error;
/// MySQL 3.2.3's hash: sixteen hex characters, of either case when/// verifying, written in lower case.pub const mysql323 = struct { pub const name = "mysql323"; pub const Options = struct {};
const hex_len = 16;
fn parse(h: []const u8) Error![hex_len]u8 { if (h.len != hex_len or !encoding.allIn(h, encoding.hex_chars)) return error.MalformedHash; var out: [hex_len]u8 = undefined; for (&out, h) |*o, c| o.* = std.ascii.toLower(c); return out; }
/// The hash of `password`'s bytes: two 31-bit accumulators, stirred by /// every byte that is not a space or a tab. fn compute(password: []const u8, out: *[hex_len]u8) void { var nr1: u32 = 0x50305735; var nr2: u32 = 0x12345671; var add: u32 = 7; for (password) |c| { if (c == ' ' or c == '\t') continue; const tmp: u32 = c; nr1 ^= (((nr1 & 63) +% add) *% tmp) +% (nr1 << 8); nr2 +%= (nr2 << 8) ^ nr1; add +%= tmp; } var words: [8]u8 = undefined; std.mem.writeInt(u32, words[0..4], nr1 & 0x7fffffff, .big); std.mem.writeInt(u32, words[4..8], nr2 & 0x7fffffff, .big); _ = encoding.hexLower(out, &words); }
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 { _ = 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); const stored = try parse(h); 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; _ = try parse(h); return false; }};
/// MySQL 4.1's hash: `*` and forty hex characters, of either case when/// verifying, written in upper case.pub const mysql41 = struct { pub const name = "mysql41"; pub const Options = struct {};
const hex_len = 2 * Sha1.digest_length;
fn parse(h: []const u8) Error![hex_len]u8 { if (h.len != 1 + hex_len or h[0] != '*' or !encoding.allIn(h[1..], encoding.hex_chars)) return error.MalformedHash; var out: [hex_len]u8 = undefined; for (&out, h[1..]) |*o, c| o.* = std.ascii.toUpper(c); return out; }
fn compute(password: []const u8, out: *[hex_len]u8) void { var inner: [Sha1.digest_length]u8 = undefined; Sha1.hash(password, &inner, .{}); var outer: [Sha1.digest_length]u8 = undefined; Sha1.hash(&inner, &outer, .{}); _ = encoding.hexUpper(out, &outer); }
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 { _ = ctx; _ = options; try handler.checkPasswordLen(password); var out: [hex_len]u8 = undefined; compute(password, &out); try w.print("*{s}", .{out}); }
pub fn verify(ctx: Context, password: []const u8, h: []const u8) Error!bool { _ = ctx; try handler.checkPasswordLen(password); const stored = try parse(h); 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; _ = try parse(h); return false; }};
/// The schemes this module defines, in the order `registry.identify` tries/// them.pub const schemes = .{ mysql323, mysql41 };
comptime { for (schemes) |S| handler.check(S);}
const test_ctx: Context = .{ .io = testing.io, .gpa = testing.allocator };
fn hashTo(buf: []u8, comptime S: type, password: []const u8) ![]const u8 { var w: Io.Writer = .fixed(buf); try S.hash(test_ctx, password, .{}, &w); return w.buffered();}
test "hashes reproduce passlib" { // passlib 1.7.4: mysql323.hash("mypass") and so on. var buf: [64]u8 = undefined; try testing.expectEqualStrings("6f8c114b58f2ce9e", try hashTo(&buf, mysql323, "mypass")); try testing.expectEqualStrings("639e9c436a42d0e1", try hashTo(&buf, mysql323, "my pass\tx")); try testing.expectEqualStrings("50df30e0614edcd2", try hashTo(&buf, mysql323, "\xff" ** 50)); try testing.expectEqualStrings("*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4", try hashTo(&buf, mysql41, "mypass"));}
test "verification" { try testing.expect(try mysql323.verify(test_ctx, "my pass", "6F8C114B58F2CE9E")); try testing.expect(try mysql41.verify(test_ctx, "mypass", "*6c8989366eaf75bb670ad8ea7a7fc1176a95cef4")); try testing.expect(!try mysql41.verify(test_ctx, "Mypass", "*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4")); try testing.expectError(error.MalformedHash, mysql323.verify(test_ctx, "x", "6z8c114b58f2ce9e")); try testing.expectError(error.MalformedHash, mysql41.verify(test_ctx, "x", "6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4")); try testing.expect(!mysql41.identify("*6Z8989366EAF75BB670AD8EA7A7FC1176A95CEF4"));}