summaryrefslogtreecommitdiff
path: root/bcrypt-test.c
diff options
context:
space:
mode:
Diffstat (limited to 'bcrypt-test.c')
-rw-r--r--bcrypt-test.c48
1 files changed, 23 insertions, 25 deletions
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 @@
1#include <ctype.h>
2#include <stdbool.h>
1#include <stdio.h> 3#include <stdio.h>
2#include <stdlib.h> 4#include <stdlib.h>
3#include <stdbool.h>
4#include <ctype.h>
5#include <string.h> 5#include <string.h>
6 6
7#include "lua-bcrypt/compat/include/pwd.h" 7#include "bcrypt.h"
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#define SEP '|'
12#define MAX_LINE_LENGTH 256
12 13
13static bool 14static bool
14valid_char(int ch) 15valid_char(int chr)
15{ 16{
16 return isalnum(ch) || ch == '_' || ch == '-' || ch == '.'; 17 return isalnum(chr) || chr == '_' || chr == '-' || chr == '.';
17} 18}
18 19
19/** 20/**
@@ -25,7 +26,7 @@ valid_char(int ch)
25 * \return true on success (user was created) or false on error 26 * \return true on success (user was created) or false on error
26 */ 27 */
27bool 28bool
28create_user(const char *username, const char *password) 29create_user(const char *username, const char *password) // NOLINT
29{ 30{
30 // check for invalid characters in username 31 // check for invalid characters in username
31 for ( const char *ptr = username; *ptr; ++ptr ) { 32 for ( const char *ptr = username; *ptr; ++ptr ) {
@@ -34,8 +35,8 @@ create_user(const char *username, const char *password)
34 } 35 }
35 } 36 }
36 37
37 FILE *db = fopen(USER_DATABASE, "a"); 38 FILE *database = fopen(USER_DATABASE, "a");
38 if ( db == NULL ) { 39 if ( database == NULL ) {
39 return false; 40 return false;
40 } 41 }
41 42
@@ -45,8 +46,8 @@ create_user(const char *username, const char *password)
45 return false; 46 return false;
46 } 47 }
47 48
48 fprintf(db, "%s%c%s\n", username, SEP, hash); 49 (void) fprintf(database, "%s%c%s\n", username, SEP, hash);
49 fclose(db); 50 (void) fclose(database);
50 51
51 return true; 52 return true;
52} 53}
@@ -60,18 +61,18 @@ create_user(const char *username, const char *password)
60 * @return true, if the user was successfully authenticated, otherwise false 61 * @return true, if the user was successfully authenticated, otherwise false
61 */ 62 */
62bool 63bool
63check_user(const char *const username, const char *const password) 64check_user(const char *const username, const char *const password) // NOLINT
64{ 65{
65 FILE *db = fopen(USER_DATABASE, "r"); 66 FILE *database = fopen(USER_DATABASE, "r");
66 if ( db == NULL ) { 67 if ( database == NULL ) {
67 return false; 68 return false;
68 } 69 }
69 70
70 const size_t username_length = strlen(username); 71 const size_t username_length = strlen(username);
71 char line[256]; 72 char line[MAX_LINE_LENGTH];
72 73
73 bool found = false; 74 bool found = false;
74 while ( !found && fgets(line, sizeof line, db) ) { 75 while ( !found && fgets(line, sizeof line, database) ) {
75 line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) 76 line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists)
76 77
77 if ( strncmp(line, username, username_length) != 0 ) { 78 if ( strncmp(line, username, username_length) != 0 ) {
@@ -82,7 +83,7 @@ check_user(const char *const username, const char *const password)
82 continue; 83 continue;
83 } 84 }
84 85
85 if ( bcrypt_checkpass(password, &line[username_length+1]) != 0 ) { 86 if ( bcrypt_checkpass(password, &line[username_length + 1]) != 0 ) {
86 continue; 87 continue;
87 } 88 }
88 89
@@ -90,7 +91,7 @@ check_user(const char *const username, const char *const password)
90 found = true; 91 found = true;
91 } 92 }
92 93
93 fclose(db); 94 (void) fclose(database);
94 95
95 return found; 96 return found;
96} 97}
@@ -105,16 +106,16 @@ check_user(const char *const username, const char *const password)
105bool 106bool
106check_user_if_exists(const char *username) 107check_user_if_exists(const char *username)
107{ 108{
108 FILE *db = fopen(USER_DATABASE, "r"); 109 FILE *database = fopen(USER_DATABASE, "r");
109 if ( db == NULL ) { 110 if ( database == NULL ) {
110 return false; 111 return false;
111 } 112 }
112 113
113 const size_t username_length = strlen(username); 114 const size_t username_length = strlen(username);
114 char line[256]; 115 char line[MAX_LINE_LENGTH];
115 116
116 bool found = false; 117 bool found = false;
117 while ( !found && fgets(line, sizeof line, db) ) { 118 while ( !found && fgets(line, sizeof line, database) ) {
118 line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) 119 line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists)
119 120
120 if ( strncmp(line, username, username_length) != 0 ) { 121 if ( strncmp(line, username, username_length) != 0 ) {
@@ -129,7 +130,7 @@ check_user_if_exists(const char *username)
129 found = true; 130 found = true;
130 } 131 }
131 132
132 fclose(db); 133 (void) fclose(database);
133 134
134 return found; 135 return found;
135} 136}
@@ -143,6 +144,3 @@ main(void)
143 144
144 return EXIT_SUCCESS; 145 return EXIT_SUCCESS;
145} 146}
146
147
148