-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathB24EE1007_B24CS1023_B24CM1031_B24CM1050_B24CH1047_auth.c
More file actions
75 lines (70 loc) · 1.95 KB
/
Copy pathB24EE1007_B24CS1023_B24CM1031_B24CM1050_B24CH1047_auth.c
File metadata and controls
75 lines (70 loc) · 1.95 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "B24EE1007_B24CS1023_B24CM1031_B24CM1050_B24CH1047_auth.h"
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <wincrypt.h>
int validateCredentials(const char *username, const char *password) {
if (strlen(username) == 0 || strlen(username) > MAX_USERNAME_LEN - 1) {
return 0;
}
if (strlen(password) == 0 || strlen(password) > MAX_PASSWORD_LEN - 1) {
return 0;
}
for (int i = 0; i < strlen(username); i++) {
if (username[i] == ':' || username[i] == '\n') {
return 0;
}
}
return 1;
}
int userExists(const char *username) {
FILE *file = fopen(USERS_FILE, "rb");
if (!file) {
return 0;
}
UserCredentials user;
while (fread(&user, sizeof(UserCredentials), 1, file)) {
if (strcmp(user.username, username) == 0) {
fclose(file);
return 1;
}
}
fclose(file);
return 0;
}
int authenticateUser(const char *username, const char *password) {
if (!validateCredentials(username, password)) {
return 0;
}
FILE *file = fopen(USERS_FILE, "rb");
if (!file) {
return 0;
}
UserCredentials user;
while (fread(&user, sizeof(UserCredentials), 1, file)) {
if (strcmp(user.username, username) == 0) {
fclose(file);
return (strcmp(user.password, password) == 0);
}
}
fclose(file);
return 0;
}
int registerUser(const char *username, const char *password) {
if (!validateCredentials(username, password)) {
return 0;
}
if (userExists(username)) {
return 0;
}
FILE *file = fopen(USERS_FILE, "ab");
if (!file) {
return 0;
}
UserCredentials newUser;
strncpy(newUser.username, username, MAX_USERNAME_LEN);
strncpy(newUser.password, password, MAX_PASSWORD_LEN);
int result = fwrite(&newUser, sizeof(UserCredentials), 1, file);
fclose(file);
return result == 1;
}