summaryrefslogtreecommitdiff
path: root/bcrypt-test.c
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2020-10-09 18:15:36 +0200
committerThomas Schmucker <ts@its1.de>2020-10-09 18:15:36 +0200
commit7bf6bc9385adff81f0f66962630bdd60572d5c57 (patch)
tree34d8807fed8564fb1300adb827fbfeacdaaf5558 /bcrypt-test.c
parentf4a3bef33294814193d3433dcddec117e9ac73c7 (diff)
downloaduse-bcrypt-7bf6bc9385adff81f0f66962630bdd60572d5c57.tar.gz
use-bcrypt-7bf6bc9385adff81f0f66962630bdd60572d5c57.tar.bz2
use-bcrypt-7bf6bc9385adff81f0f66962630bdd60572d5c57.zip
cleanup
Diffstat (limited to 'bcrypt-test.c')
-rw-r--r--bcrypt-test.c55
1 files changed, 49 insertions, 6 deletions
diff --git a/bcrypt-test.c b/bcrypt-test.c
index 2bbdfe7..1df296d 100644
--- a/bcrypt-test.c
+++ b/bcrypt-test.c
@@ -8,6 +8,7 @@
8 8
9#define USER_DATABASE "./user.db" 9#define USER_DATABASE "./user.db"
10#define SECURITY_LEVEL 12 10#define SECURITY_LEVEL 12
11#define SEP '|'
11 12
12static bool 13static bool
13valid_char(int ch) 14valid_char(int ch)
@@ -44,7 +45,7 @@ create_user(const char *username, const char *password)
44 return false; 45 return false;
45 } 46 }
46 47
47 fprintf(db, "%s|%s\n", username, hash); 48 fprintf(db, "%s%c%s\n", username, SEP, hash);
48 fclose(db); 49 fclose(db);
49 50
50 return true; 51 return true;
@@ -53,13 +54,13 @@ create_user(const char *username, const char *password)
53/** 54/**
54 * Check is a valid username/password exists in the user-database 55 * Check is a valid username/password exists in the user-database
55 * 56 *
56 * \param[in] username 57 * username A username
57 * \param[in] password 58 * password A strong password
58 * 59 *
59 * \return true, if the user was successfully authenticated, otherwise false 60 * @return true, if the user was successfully authenticated, otherwise false
60 */ 61 */
61bool 62bool
62check_user(const char *username, const char *password) 63check_user(const char *const username, const char *const password)
63{ 64{
64 FILE *db = fopen(USER_DATABASE, "r"); 65 FILE *db = fopen(USER_DATABASE, "r");
65 if ( db == NULL ) { 66 if ( db == NULL ) {
@@ -77,7 +78,7 @@ check_user(const char *username, const char *password)
77 continue; 78 continue;
78 } 79 }
79 80
80 if ( line[username_length] != '|' ) { 81 if ( line[username_length] != SEP ) {
81 continue; 82 continue;
82 } 83 }
83 84
@@ -94,6 +95,45 @@ check_user(const char *username, const char *password)
94 return found; 95 return found;
95} 96}
96 97
98/**
99 * Check if a valid username already exists in the user-database
100 *
101 * \param[in] username to be checked
102 *
103 * \return true, if the user was successfully authenticated, otherwise false
104 */
105bool
106check_user_if_exists(const char *username)
107{
108 FILE *db = fopen(USER_DATABASE, "r");
109 if ( db == NULL ) {
110 return false;
111 }
112
113 const size_t username_length = strlen(username);
114 char line[256];
115
116 bool found = false;
117 while ( !found && fgets(line, sizeof line, db) ) {
118 line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists)
119
120 if ( strncmp(line, username, username_length) != 0 ) {
121 continue;
122 }
123
124 if ( line[username_length] != '|' ) {
125 continue;
126 }
127
128 // User successfully authenticated!
129 found = true;
130 }
131
132 fclose(db);
133
134 return found;
135}
136
97int 137int
98main(void) 138main(void)
99{ 139{
@@ -103,3 +143,6 @@ main(void)
103 143
104 return EXIT_SUCCESS; 144 return EXIT_SUCCESS;
105} 145}
146
147
148