summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2020-08-25 17:08:00 +0200
committerThomas Schmucker <ts@its1.de>2020-08-25 17:08:00 +0200
commitf4a3bef33294814193d3433dcddec117e9ac73c7 (patch)
tree96246ad77df7c5b2394d9c21a474cf1d6e402e95
parent44bf68e036ac68166695cb07c7713fab69914615 (diff)
downloaduse-bcrypt-f4a3bef33294814193d3433dcddec117e9ac73c7.tar.gz
use-bcrypt-f4a3bef33294814193d3433dcddec117e9ac73c7.tar.bz2
use-bcrypt-f4a3bef33294814193d3433dcddec117e9ac73c7.zip
Beispiel für bcrypt hinzugefügt
-rw-r--r--bcrypt-test.c96
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
12static bool
13valid_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 */
26bool
27create_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 */
61bool
62check_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
6int 97int
7main(void) 98main(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}