PS D:\ZigProjects\Encryption> zig run .\src\main.zig
The password length should not be less than 4 and should not exceed 128 characters.
Enter the desired Password: !(1DEJ6L%#W3Gh!00NEzv&$1*^7#@U%6
Entered Password: !(1DEJ6L%#W3Gh!00NEzv&$1*^7#@U%6
Password accepted. Proceeding with encryption…
Hit the error:
Then, I hit the error from hashing the password using SHA-256:
PS D:\ZigProjects\Encryption> zig run .\src\main.zig
src\main.zig:36:34: error: struct ‘crypto.hash’ has no member named ‘sha256’ var hasher = std.crypto.hash.sha256.init(); ^~~~~~ D:\Programs\Zig\lib\std\crypto.zig:90:18: note: struct declared here pub const hash = struct {
const std : type = @import(“std”);
const random = @import(“std”).Random;
const crypto = @import(“std”).crypto;
const sha256 = @import(“std”).crypto.hash.sha256;
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
const stdin = std.io.getStdIn().reader();
const allocator = std.heap.page_allocator;
// Prompt the user for the desired password length
std.debug.print(“The password length should not be less than 4 and should not exceed 128 characters.\n”, .{});
try stdout.print("Enter the desired Password: ", .{});
// Read the password from the user const password_input = try allocator.alloc(u8, 128);
defer allocator.free(password_input); const password = try stdin.readUntilDelimiterOrEof(password_input, ‘\n’) orelse return;
// Validate the password length
const length: usize = password.len; if (length < 4 or length > 128) {
try stdout.print(“Invalid password length. Please enter a password between 4 and 128 characters.\n”, .{});
return;
}
// Print the password for demonstration purposes
try stdout.print(“Entered Password: {s}\n”, .{password});
// Proceed without showing the hashed password
try stdout.print(“Password accepted. Proceeding with encryption…\n”, .{});
// Hash the password using SHA-256
var hasher = std.crypto.hash.sha256.init();
try hasher.update(password);
const hash = try hasher.final();
// Convert hash to a hexadecimal string
const hex_output = try allocator.alloc(u8, hash.len * 2);
defer allocator.free(hex_output);
for (hash) |byte| {
try std.fmt.bufPrint(hex_output[byte * 2 … byte * 2 + 2], “{02x}”, .{byte});
}
// Output the hashed password
try stdout.print(“Hashed Password (SHA-256): {s}\n”, .{hex_output}); }
```
I wonder if the missing symbols are LLM hallucinations…
I think the original poster would find better success with Zig by reading the standard library reference more carefully and stopping to think where the compile errors are coming from.
// Prompt for password with better instructions
try stdout.print("Password requirements:\n", .{});
try stdout.print("- Length: 4-128 characters\n", .{});
try stdout.print("- Must contain letters and numbers\n", .{});
try stdout.print("Enter password: ", .{});
// Read password with proper buffer
var password_buffer: [128]u8 = undefined;
const password = try stdin.readUntilDelimiter(&password_buffer, '\n');
// Validate password strength
if (!isPasswordValid(password)) {
try stdout.print("Password too weak! Must contain both letters and numbers.\n", .{});
return;
}
// Hash password
var hash: [Sha256.digest_length]u8 = undefined;
Sha256.hash(password, &hash, .{});
// Convert to hex more efficiently
var hex_output: [Sha256.digest_length * 2]u8 = undefined;
_ = try std.fmt.bufPrint(&hex_output, "{s}", .{std.fmt.fmtSliceHexLower(&hash)});
try stdout.print("Hashed Password (SHA-256): {s}\n", .{hex_output});
}
fn isPasswordValid(password: const u8) bool {
var has_letter = false;
var has_number = false;
for (password) |char| {
if (std.ascii.isAlphabetic(char)) has_letter = true;
if (std.ascii.isDigit(char)) has_number = true;
}
return has_letter and has_number;
Thank you for prompt with the useful link. That’s trumandous.
Here is the output from the test:
Password requirements:
Length: 4-128 characters
Must contain letters and numbers
Enter password: !(1DEJ6L%#W3Gh!00NEzv&$1*^7#@U%6
Hashed Password (SHA-256): d7f4fe4a0c47681cb95076a48c478ca51e4ce2f25bdfa890980f54618cac1daf