diff options
| -rw-r--r-- | bcrypt-test.c | 96 |
1 files changed, 95 insertions, 1 deletions
diff --git a/bcrypt-test.c b/bcrypt-test.c index b09dee6..2bbdfe7 100644 --- a/bcrypt-test.c +++ b/bcrypt-test.c | |||
| @@ -1,11 +1,105 @@ | |||
| 1 | #include <stdio.h> | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 3 | #include <stdbool.h> | ||
| 4 | #include <ctype.h> | ||
| 5 | #include <string.h> | ||
| 3 | 6 | ||
| 4 | #include "lua-bcrypt/compat/include/pwd.h" | 7 | #include "lua-bcrypt/compat/include/pwd.h" |
| 5 | 8 | ||
| 9 | #define USER_DATABASE "./user.db" | ||
| 10 | #define SECURITY_LEVEL 12 | ||
| 11 | |||
| 12 | static bool | ||
| 13 | valid_char(int ch) | ||
| 14 | { | ||
| 15 | return isalnum(ch) || ch == '_' || ch == '-' || ch == '.'; | ||
| 16 | } | ||
| 17 | |||
| 18 | /** | ||
| 19 | * Create a new user/password | ||
| 20 | * | ||
| 21 | * \param[in] username | ||
| 22 | * \param[in] password | ||
| 23 | * | ||
| 24 | * \return true on success (user was created) or false on error | ||
| 25 | */ | ||
| 26 | bool | ||
| 27 | create_user(const char *username, const char *password) | ||
| 28 | { | ||
| 29 | // check for invalid characters in username | ||
| 30 | for ( const char *ptr = username; *ptr; ++ptr ) { | ||
| 31 | if ( !valid_char(*ptr) ) { | ||
| 32 | return false; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | FILE *db = fopen(USER_DATABASE, "a"); | ||
| 37 | if ( db == NULL ) { | ||
| 38 | return false; | ||
| 39 | } | ||
| 40 | |||
| 41 | char hash[_PASSWORD_LEN + 1] = { 0 }; | ||
| 42 | |||
| 43 | if ( bcrypt_newhash(password, SECURITY_LEVEL, hash, _PASSWORD_LEN) != 0 ) { | ||
| 44 | return false; | ||
| 45 | } | ||
| 46 | |||
| 47 | fprintf(db, "%s|%s\n", username, hash); | ||
| 48 | fclose(db); | ||
| 49 | |||
| 50 | return true; | ||
| 51 | } | ||
| 52 | |||
| 53 | /** | ||
| 54 | * Check is a valid username/password exists in the user-database | ||
| 55 | * | ||
| 56 | * \param[in] username | ||
| 57 | * \param[in] password | ||
| 58 | * | ||
| 59 | * \return true, if the user was successfully authenticated, otherwise false | ||
| 60 | */ | ||
| 61 | bool | ||
| 62 | check_user(const char *username, const char *password) | ||
| 63 | { | ||
| 64 | FILE *db = fopen(USER_DATABASE, "r"); | ||
| 65 | if ( db == NULL ) { | ||
| 66 | return false; | ||
| 67 | } | ||
| 68 | |||
| 69 | const size_t username_length = strlen(username); | ||
| 70 | char line[256]; | ||
| 71 | |||
| 72 | bool found = false; | ||
| 73 | while ( !found && fgets(line, sizeof line, db) ) { | ||
| 74 | line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) | ||
| 75 | |||
| 76 | if ( strncmp(line, username, username_length) != 0 ) { | ||
| 77 | continue; | ||
| 78 | } | ||
| 79 | |||
| 80 | if ( line[username_length] != '|' ) { | ||
| 81 | continue; | ||
| 82 | } | ||
| 83 | |||
| 84 | if ( bcrypt_checkpass(password, &line[username_length+1]) != 0 ) { | ||
| 85 | continue; | ||
| 86 | } | ||
| 87 | |||
| 88 | // User successfully authenticated! | ||
| 89 | found = true; | ||
| 90 | } | ||
| 91 | |||
| 92 | fclose(db); | ||
| 93 | |||
| 94 | return found; | ||
| 95 | } | ||
| 96 | |||
| 6 | int | 97 | int |
| 7 | main(void) | 98 | main(void) |
| 8 | { | 99 | { |
| 9 | puts("Hallo Welt..."); | 100 | if ( check_user("Thomas", "Passwort") ) { |
| 101 | puts("Benutzer erfolgreich überprüft!"); | ||
| 102 | } | ||
| 103 | |||
| 10 | return EXIT_SUCCESS; | 104 | return EXIT_SUCCESS; |
| 11 | } | 105 | } |
