Implement hashing of username and password
This commit is contained in:
@@ -1,7 +1,58 @@
|
||||
#include <stdio.h>
|
||||
#include <sha256.h>
|
||||
#define NOH_IMPLEMENTATION
|
||||
#include "noh.h"
|
||||
|
||||
static void show_usage(char *program_name, char *error) {
|
||||
noh_log(NOH_INFO, "Usage:");
|
||||
noh_log(NOH_INFO, "%s username password", program_name);
|
||||
noh_log(NOH_INFO, " username: The username of the user to register.");
|
||||
noh_log(NOH_INFO, " password: The password of the user to register.");
|
||||
noh_log(NOH_INFO, "");
|
||||
noh_log(NOH_ERROR, "%s", error);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Noh_String hash_password(Noh_Arena *arena, Noh_String_View username, Noh_String_View password) {
|
||||
// Build the sha input string.
|
||||
Noh_String input = {0};
|
||||
noh_string_append_sv(&input, username);
|
||||
noh_string_append_cstr(&input, "<><>");
|
||||
noh_string_append_sv(&input, password);
|
||||
|
||||
// Calculate the hash.
|
||||
SHA256_CTX ctx = {0};
|
||||
BYTE hash[32];
|
||||
sha256_init(&ctx);
|
||||
sha256_update(&ctx, (BYTE*)input.elems, input.count);
|
||||
sha256_final(&ctx, hash);
|
||||
|
||||
// Build the result string.
|
||||
noh_arena_save(arena);
|
||||
Noh_String result = {0};
|
||||
for (int i = 0; i < 32; i++) {
|
||||
noh_string_append_cstr(&result, noh_arena_sprintf(arena, "%02x", hash[i]));
|
||||
}
|
||||
noh_arena_reset(arena);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
// Parse input parameters.
|
||||
char *program_name = noh_shift_args(&argc, &argv);
|
||||
|
||||
if (argc < 1) show_usage(program_name, "Username not provided.");
|
||||
Noh_String_View username = noh_sv_from_cstr(noh_shift_args(&argc, &argv));
|
||||
|
||||
if (argc < 1) show_usage(program_name, "Password not provided.");
|
||||
Noh_String_View password = noh_sv_from_cstr(noh_shift_args(&argc, &argv));
|
||||
|
||||
Noh_Arena arena = noh_arena_init(64 KB);
|
||||
Noh_String result = hash_password(&arena, username, password);
|
||||
noh_string_append_null(&result);
|
||||
printf("%s\n", result.elems);
|
||||
|
||||
int main() {
|
||||
printf("Hello, world!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user