summaryrefslogtreecommitdiff
path: root/backend/src/Authentication/Password.zig
blob: ba395432e5a1d34e386e31f968c5834e0c31a07a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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);
}