-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashing_auth.c
More file actions
60 lines (49 loc) · 1.6 KB
/
hashing_auth.c
File metadata and controls
60 lines (49 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
#define HASH_LEN 65
// Hash password using SHA-256
void hash_pass(const char *pass, char *hash) {
unsigned char tmp[SHA256_DIGEST_LENGTH];
SHA256((const unsigned char *)pass, strlen(pass), tmp);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
sprintf(hash + (i * 2), "%02x", tmp[i]);
}
int main() {
int choice;
printf("1. Register\n2. Login\nChoice: ");
scanf("%d", &choice);
char user[50], pass[50], hash[HASH_LEN];
if (choice == 1) { // Register
printf("Username: "); scanf("%s", user);
printf("Password: "); scanf("%s", pass);
hash_pass(pass, hash);
FILE *f = fopen("users.txt", "a");
fprintf(f, "%s %s\n", user, hash);
fclose(f);
printf("User registered!\n");
} else if (choice == 2) { // Login
char file_user[50], file_hash[HASH_LEN];
int found = 0;
printf("Username: "); scanf("%s", user);
printf("Password: "); scanf("%s", pass);
hash_pass(pass, hash);
FILE *f = fopen("users.txt", "r");
while (fscanf(f, "%s %s", file_user, file_hash) != EOF) {
if (strcmp(user, file_user) == 0) {
found = 1;
if (strcmp(hash, file_hash) == 0)
printf("Login successful!\n");
else
printf("Wrong password.\n");
break;
}
}
fclose(f);
if (!found) printf("User not found.\n");
} else {
printf("Invalid choice.\n");
}
return 0;
}