From f142f6563efa5e2e56b32e620284ebd9b6fb4f32 Mon Sep 17 00:00:00 2001 From: CodingAndTechnology Date: Thu, 28 Aug 2025 13:44:37 +0530 Subject: [PATCH 1/8] argon2id password hash --- assets/src/02-03.zig | 33 +++++++++++++++++++++++++++++++++ src/en-US/02-03-argon2.smd | 14 ++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 assets/src/02-03.zig create mode 100644 src/en-US/02-03-argon2.smd diff --git a/assets/src/02-03.zig b/assets/src/02-03.zig new file mode 100644 index 0000000..86da622 --- /dev/null +++ b/assets/src/02-03.zig @@ -0,0 +1,33 @@ +const std = @import("std"); +const testing = std.testing; +test "Argon2id Password Hash" { + var dbg = std.heap.DebugAllocator(.{}){}; + defer _ = dbg.deinit(); + const allocator = dbg.allocator(); + + const password = "happy"; + const salt = "salt1234"; // Must be at least 8 bytes, recommended 16+ + + // Parameters for Argon2id + const params = std.crypto.pwhash.argon2.Params{ + .t = 3, // Iterations (time cost) + .m = 16, // Memory cost in KiB (here 16 MiB) + .p = 1, // Threads + }; + + const dk_len: usize = 16; // derive 16 or 32-byte key + var derived: [dk_len]u8 = undefined; + + try std.crypto.pwhash.argon2.kdf( + allocator, + &derived, + password, + salt, + params, + .argon2id, //argon2i, argon2d and argon2id + ); + const hex_digest = try std.fmt.allocPrint(allocator, "{s}", .{std.fmt.bytesToHex(derived, .lower)}); + defer allocator.free(hex_digest); + + try testing.expectEqualStrings("84594570e92044a3546416ec973b8f7f", hex_digest); +} diff --git a/src/en-US/02-03-argon2.smd b/src/en-US/02-03-argon2.smd new file mode 100644 index 0000000..3422522 --- /dev/null +++ b/src/en-US/02-03-argon2.smd @@ -0,0 +1,14 @@ +--- +.title = "Argon2 Password Hash", +.date = "2024-08-0", +.author = "ZigCC", +.layout = "section.shtml", +--- +Argon2 Password Hash + +This Zig program derives a cryptographic key from a password and salt using the Argon2id password hashing algorithm. It uses [std.crypto.pwhash.argon2] to hash a salted password, where the salt is generated using [std.rand.DefaultPrng] to ensure randomness. + +[]($code.siteAsset('src/02-03.zig').language('zig')) + +[`std.crypto.pwhash.argon2`]: https://ziglang.org/documentation/0.14.0/std/#std.crypto.pwhash.argon2 +[`std.rand.defaultprng`]: https://ziglang.org/documentation/0.11.0/std/#A;std:rand.DefaultPrng From 41cbe8c6ceef0ed4732199f6ad7c3b993d5c1cf4 Mon Sep 17 00:00:00 2001 From: CodingAndTechnology Date: Thu, 28 Aug 2025 13:52:25 +0530 Subject: [PATCH 2/8] fixed the date issue in shtml file --- src/en-US/02-03-argon2.smd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/en-US/02-03-argon2.smd b/src/en-US/02-03-argon2.smd index 3422522..32ce523 100644 --- a/src/en-US/02-03-argon2.smd +++ b/src/en-US/02-03-argon2.smd @@ -1,6 +1,6 @@ --- .title = "Argon2 Password Hash", -.date = "2024-08-0", +.date = "2024-08-28", .author = "ZigCC", .layout = "section.shtml", --- From 64fee2b1d3620ba7c81564e462a917ee32810c7e Mon Sep 17 00:00:00 2001 From: CodingAndTechnology Date: Thu, 28 Aug 2025 17:33:37 +0530 Subject: [PATCH 3/8] added main method and scope in toc.smd file --- assets/src/02-03.zig | 5 +++-- src/en-US/toc.smd | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/assets/src/02-03.zig b/assets/src/02-03.zig index 86da622..89fb383 100644 --- a/assets/src/02-03.zig +++ b/assets/src/02-03.zig @@ -1,6 +1,7 @@ const std = @import("std"); const testing = std.testing; -test "Argon2id Password Hash" { + +pub fn main() !void { var dbg = std.heap.DebugAllocator(.{}){}; defer _ = dbg.deinit(); const allocator = dbg.allocator(); @@ -24,7 +25,7 @@ test "Argon2id Password Hash" { password, salt, params, - .argon2id, //argon2i, argon2d and argon2id + .argon2id, //argon2i, argon2d and argon2id ); const hex_digest = try std.fmt.allocPrint(allocator, "{s}", .{std.fmt.bytesToHex(derived, .lower)}); defer allocator.free(hex_digest); diff --git a/src/en-US/toc.smd b/src/en-US/toc.smd index 661089e..0e09ff4 100644 --- a/src/en-US/toc.smd +++ b/src/en-US/toc.smd @@ -19,6 +19,7 @@ - [Calculate SHA-256 digest of a file](02-01-sha-digest) - [Salt and hash a password with PBKDF2](02-02-pbkdf2) + - [Argon2 Password Hash](02-03-argon2) - Date and Time From d2be24e793dad263677dc67e4dfc90f3b9d39a9d Mon Sep 17 00:00:00 2001 From: gmcodentech <144211927+gmcodentech@users.noreply.github.com> Date: Thu, 28 Aug 2025 18:15:02 +0530 Subject: [PATCH 4/8] Update src/en-US/toc.smd Co-authored-by: Jiacai Liu --- src/en-US/toc.smd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/en-US/toc.smd b/src/en-US/toc.smd index 0e09ff4..a8cb2b6 100644 --- a/src/en-US/toc.smd +++ b/src/en-US/toc.smd @@ -19,7 +19,7 @@ - [Calculate SHA-256 digest of a file](02-01-sha-digest) - [Salt and hash a password with PBKDF2](02-02-pbkdf2) - - [Argon2 Password Hash](02-03-argon2) + - [Salt and hash a password with Argon2](02-03-argon2) - Date and Time From 485bc04bb46b7ab36d6c0fcf24cc8fdab486f94e Mon Sep 17 00:00:00 2001 From: CodingAndTechnology Date: Thu, 28 Aug 2025 18:20:25 +0530 Subject: [PATCH 5/8] changed the description of argon2 hash example --- src/en-US/02-03-argon2.smd | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/en-US/02-03-argon2.smd b/src/en-US/02-03-argon2.smd index 32ce523..b7066c0 100644 --- a/src/en-US/02-03-argon2.smd +++ b/src/en-US/02-03-argon2.smd @@ -1,14 +1,13 @@ --- .title = "Argon2 Password Hash", .date = "2024-08-28", -.author = "ZigCC", +.author = "gmcodentech", .layout = "section.shtml", --- Argon2 Password Hash -This Zig program derives a cryptographic key from a password and salt using the Argon2id password hashing algorithm. It uses [std.crypto.pwhash.argon2] to hash a salted password, where the salt is generated using [std.rand.DefaultPrng] to ensure randomness. +This Zig program derives a cryptographic key from a password and salt using the Argon2id password hashing algorithm. It uses [std.crypto.pwhash.argon2] to hash a salted password, where the salt should be generated using to ensure randomness. The salt in the example is hardcoded to write the test. []($code.siteAsset('src/02-03.zig').language('zig')) -[`std.crypto.pwhash.argon2`]: https://ziglang.org/documentation/0.14.0/std/#std.crypto.pwhash.argon2 -[`std.rand.defaultprng`]: https://ziglang.org/documentation/0.11.0/std/#A;std:rand.DefaultPrng +[`std.crypto.pwhash.argon2`]: https://ziglang.org/documentation/0.14.0/std/#std.crypto.pwhash.argon2 \ No newline at end of file From 5b2f29a3e296256f070a16dbb27c2dc4dc23da0d Mon Sep 17 00:00:00 2001 From: CodingAndTechnology Date: Thu, 28 Aug 2025 20:06:48 +0530 Subject: [PATCH 6/8] corrected header values in smd file --- src/en-US/02-03-argon2.smd | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/en-US/02-03-argon2.smd b/src/en-US/02-03-argon2.smd index b7066c0..cffccab 100644 --- a/src/en-US/02-03-argon2.smd +++ b/src/en-US/02-03-argon2.smd @@ -1,10 +1,10 @@ --- -.title = "Argon2 Password Hash", -.date = "2024-08-28", -.author = "gmcodentech", +.title = "Salt and hash a password with Argon2", +.date = "2025-08-28", +.author = "ZigCC", .layout = "section.shtml", --- -Argon2 Password Hash +Salt and hash a password with Argon2 This Zig program derives a cryptographic key from a password and salt using the Argon2id password hashing algorithm. It uses [std.crypto.pwhash.argon2] to hash a salted password, where the salt should be generated using to ensure randomness. The salt in the example is hardcoded to write the test. From 64f7d492efacdb6bc3fdca66bd11c4e50d2bdbe3 Mon Sep 17 00:00:00 2001 From: CodingAndTechnology Date: Thu, 28 Aug 2025 20:34:50 +0530 Subject: [PATCH 7/8] changes to print the hash --- assets/src/02-03.zig | 15 +++++++++------ src/en-US/02-03-argon2.smd | 5 +++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/assets/src/02-03.zig b/assets/src/02-03.zig index 89fb383..6ae44c7 100644 --- a/assets/src/02-03.zig +++ b/assets/src/02-03.zig @@ -1,5 +1,4 @@ const std = @import("std"); -const testing = std.testing; pub fn main() !void { var dbg = std.heap.DebugAllocator(.{}){}; @@ -7,9 +6,14 @@ pub fn main() !void { const allocator = dbg.allocator(); const password = "happy"; - const salt = "salt1234"; // Must be at least 8 bytes, recommended 16+ - // Parameters for Argon2id + //Random salt (Must be at least 8 bytes, recommended 16+) + var raw: [8]u8 = undefined; + std.crypto.random.bytes(&raw); + const salt = try std.fmt.allocPrint(allocator, "{s}", .{std.fmt.bytesToHex(raw, .lower)}); + defer allocator.free(salt); + + //Parameters for Argon2id const params = std.crypto.pwhash.argon2.Params{ .t = 3, // Iterations (time cost) .m = 16, // Memory cost in KiB (here 16 MiB) @@ -27,8 +31,7 @@ pub fn main() !void { params, .argon2id, //argon2i, argon2d and argon2id ); - const hex_digest = try std.fmt.allocPrint(allocator, "{s}", .{std.fmt.bytesToHex(derived, .lower)}); - defer allocator.free(hex_digest); - try testing.expectEqualStrings("84594570e92044a3546416ec973b8f7f", hex_digest); + const stdout = std.io.getStdOut().writer(); + try stdout.print("Argon2id derived key: {s}\n", .{std.fmt.bytesToHex(derived, .lower)}); } diff --git a/src/en-US/02-03-argon2.smd b/src/en-US/02-03-argon2.smd index cffccab..66fa0f9 100644 --- a/src/en-US/02-03-argon2.smd +++ b/src/en-US/02-03-argon2.smd @@ -6,8 +6,9 @@ --- Salt and hash a password with Argon2 -This Zig program derives a cryptographic key from a password and salt using the Argon2id password hashing algorithm. It uses [std.crypto.pwhash.argon2] to hash a salted password, where the salt should be generated using to ensure randomness. The salt in the example is hardcoded to write the test. +This Zig program derives a cryptographic key from a password and salt using the Argon2id password hashing algorithm. It uses [std.crypto.pwhash.argon2] to hash a salted password, where the salt is generated using [`std.crypto.random`]. []($code.siteAsset('src/02-03.zig').language('zig')) -[`std.crypto.pwhash.argon2`]: https://ziglang.org/documentation/0.14.0/std/#std.crypto.pwhash.argon2 \ No newline at end of file +[`std.crypto.pwhash.argon2`]: https://ziglang.org/documentation/0.14.0/std/#std.crypto.pwhash.argon2 +[`std.crypto.random`]: https://ziglang.org/documentation/0.14.0/std/#std.crypto.tlcsprng.interface \ No newline at end of file From a446ac9b90ef41a6d78b90cc15b32663da846c73 Mon Sep 17 00:00:00 2001 From: CodingAndTechnology Date: Thu, 28 Aug 2025 21:02:38 +0530 Subject: [PATCH 8/8] used debug to print the output --- assets/src/02-03.zig | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/assets/src/02-03.zig b/assets/src/02-03.zig index 6ae44c7..eb58cf2 100644 --- a/assets/src/02-03.zig +++ b/assets/src/02-03.zig @@ -32,6 +32,5 @@ pub fn main() !void { .argon2id, //argon2i, argon2d and argon2id ); - const stdout = std.io.getStdOut().writer(); - try stdout.print("Argon2id derived key: {s}\n", .{std.fmt.bytesToHex(derived, .lower)}); -} + std.debug.print("Argon2id derived key: {s}\n", .{std.fmt.bytesToHex(derived, .lower)}); +} \ No newline at end of file