From 97deedd97a1d026586afa23d47a00322bcd22e59 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 15 Jun 2025 09:40:02 +0200 Subject: Update auf neue lua-bcrypt version --- bcrypt-test.c | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) (limited to 'bcrypt-test.c') diff --git a/bcrypt-test.c b/bcrypt-test.c index 1df296d..19bd85e 100644 --- a/bcrypt-test.c +++ b/bcrypt-test.c @@ -1,19 +1,20 @@ +#include +#include #include #include -#include -#include #include -#include "lua-bcrypt/compat/include/pwd.h" +#include "bcrypt.h" #define USER_DATABASE "./user.db" #define SECURITY_LEVEL 12 #define SEP '|' +#define MAX_LINE_LENGTH 256 static bool -valid_char(int ch) +valid_char(int chr) { - return isalnum(ch) || ch == '_' || ch == '-' || ch == '.'; + return isalnum(chr) || chr == '_' || chr == '-' || chr == '.'; } /** @@ -25,7 +26,7 @@ valid_char(int ch) * \return true on success (user was created) or false on error */ bool -create_user(const char *username, const char *password) +create_user(const char *username, const char *password) // NOLINT { // check for invalid characters in username for ( const char *ptr = username; *ptr; ++ptr ) { @@ -34,8 +35,8 @@ create_user(const char *username, const char *password) } } - FILE *db = fopen(USER_DATABASE, "a"); - if ( db == NULL ) { + FILE *database = fopen(USER_DATABASE, "a"); + if ( database == NULL ) { return false; } @@ -45,8 +46,8 @@ create_user(const char *username, const char *password) return false; } - fprintf(db, "%s%c%s\n", username, SEP, hash); - fclose(db); + (void) fprintf(database, "%s%c%s\n", username, SEP, hash); + (void) fclose(database); return true; } @@ -60,18 +61,18 @@ create_user(const char *username, const char *password) * @return true, if the user was successfully authenticated, otherwise false */ bool -check_user(const char *const username, const char *const password) +check_user(const char *const username, const char *const password) // NOLINT { - FILE *db = fopen(USER_DATABASE, "r"); - if ( db == NULL ) { + FILE *database = fopen(USER_DATABASE, "r"); + if ( database == NULL ) { return false; } const size_t username_length = strlen(username); - char line[256]; + char line[MAX_LINE_LENGTH]; bool found = false; - while ( !found && fgets(line, sizeof line, db) ) { + while ( !found && fgets(line, sizeof line, database) ) { line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) if ( strncmp(line, username, username_length) != 0 ) { @@ -82,7 +83,7 @@ check_user(const char *const username, const char *const password) continue; } - if ( bcrypt_checkpass(password, &line[username_length+1]) != 0 ) { + if ( bcrypt_checkpass(password, &line[username_length + 1]) != 0 ) { continue; } @@ -90,7 +91,7 @@ check_user(const char *const username, const char *const password) found = true; } - fclose(db); + (void) fclose(database); return found; } @@ -105,16 +106,16 @@ check_user(const char *const username, const char *const password) bool check_user_if_exists(const char *username) { - FILE *db = fopen(USER_DATABASE, "r"); - if ( db == NULL ) { + FILE *database = fopen(USER_DATABASE, "r"); + if ( database == NULL ) { return false; } const size_t username_length = strlen(username); - char line[256]; + char line[MAX_LINE_LENGTH]; bool found = false; - while ( !found && fgets(line, sizeof line, db) ) { + while ( !found && fgets(line, sizeof line, database) ) { line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) if ( strncmp(line, username, username_length) != 0 ) { @@ -129,7 +130,7 @@ check_user_if_exists(const char *username) found = true; } - fclose(db); + (void) fclose(database); return found; } @@ -143,6 +144,3 @@ main(void) return EXIT_SUCCESS; } - - - -- cgit v1.3