const std = @import("std"); const Allocator = std.mem.Allocator; var argon2_params = std.crypto.pwhash.argon2.Params{ .t = 4, // time cost .m = 256, // memory cost (in KiB) .p = 1, // parallelism (this doesnt do anything because we are targeting wasm, and we do multithreading differently anyways) }; const dk_len: usize = 32; // 16 or 32 byte key var derived: [dk_len]u8 = undefined; var buffer_hash_hex: [64]u8 = undefined; fn bytesToHex(bytes: []const u8, output: []u8) void { const hex_chars = "0123456789abcdef"; var i: usize = 0; while (i < bytes.len) : (i += 1) { output[i * 2] = hex_chars[(bytes[i] >> 4)]; output[i * 2 + 1] = hex_chars[bytes[i] & 0x0F]; } } pub fn hash(allocator: Allocator, challenge: []const u8, nonce: []const u8) ![]u8 { try std.crypto.pwhash.argon2.kdf(allocator, &derived, nonce, challenge, argon2_params, .argon2id); var hash_bytes: [32]u8 = undefined; std.crypto.hash.sha2.Sha256.hash(&derived, @ptrCast(hash_bytes[0..].ptr), .{}); bytesToHex(&hash_bytes, &buffer_hash_hex); return buffer_hash_hex[0..]; }