diff options
| -rw-r--r-- | backend/src/Authentication/Password.zig | 37 | ||||
| -rw-r--r-- | backend/src/main.zig | 3 |
2 files changed, 39 insertions, 1 deletions
diff --git a/backend/src/Authentication/Password.zig b/backend/src/Authentication/Password.zig new file mode 100644 index 0000000..ba39543 --- /dev/null +++ b/backend/src/Authentication/Password.zig @@ -0,0 +1,37 @@ +const std = @import("std"); +const Sha512 = std.crypto.hash.sha2.Sha512; +const pg = @import("pg"); + +const alphabetChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[0..].*; + +pub const HashingErr = error { + InsufficientSalt, +}; + +// out_buf shound be [60]u8 to provide space for bcrypt +pub fn HashPassword(basePassword: []const u8, out_buf: []u8) ![]const u8 { + return try std.crypto.pwhash.bcrypt.strHash(basePassword, .{ + .params = .{ + .rounds_log = 7, // TODO: Move this to config + .silently_truncate_password = false, + }, + .encoding = .crypt + }, out_buf); +} + +pub fn CheckPasswordHash(stored: []const u8, provided: []const u8) bool { + std.crypto.pwhash.bcrypt.strVerify(stored, provided, .{ + .silently_truncate_password = false + }) catch { + return false; + }; + return true; +} + +test "Hash function" { + var buf: [60]u8 = undefined; + const data = "test_pasword_providedByUser"; + const hash = try HashPassword(data, &buf); + const checkResult = CheckPasswordHash(hash, data); + try std.testing.expect(checkResult); +} diff --git a/backend/src/main.zig b/backend/src/main.zig index e7641cc..6ed630f 100644 --- a/backend/src/main.zig +++ b/backend/src/main.zig @@ -7,7 +7,7 @@ const handler = @import("Handler.zig"); pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); - + var httpHandler = handler.Handler { }; @@ -43,4 +43,5 @@ fn index(_: *handler.Handler, _: *httpz.Request, res: *httpz.Response) !void { test "TestRunner" { _ = @import("Models/RangedWeapon.zig"); _ = @import("Models/User.zig"); + _ = @import("Authentication/Password.zig"); } |
