-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.h
More file actions
187 lines (159 loc) · 5.71 KB
/
Copy pathuser.h
File metadata and controls
187 lines (159 loc) · 5.71 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef USER_H
#define USER_H
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <random>
#include <iomanip>
#include <openssl/sha.h>
#include <cstring>
using namespace std;
// Function to generate a random salt
string generate_salt(int length) {
const char* chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string salt;
random_device rd;
mt19937 generator(rd());
uniform_int_distribution<> dist(0, strlen(chars) - 1);
for (int i = 0; i < length; ++i) {
salt += chars[dist(generator)];
}
return salt;
}
// Function to hash a password with salt using SHA-256
string hash_password(const string& password, const string& salt) {
string salted_password = password + salt;
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256((unsigned char*)salted_password.c_str(), salted_password.size(), hash);
stringstream ss;
for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
ss << hex << setw(2) << setfill('0') << (int)hash[i];
}
return ss.str();
}
// COUNT THE LINES FROM FILE
int count_lines() {
int count = 0;
char alpha;
ifstream file("updated_creds.txt");
if(!file){
cerr << "Error opening file!" << endl;
return 0;
}
while (!file.eof()) {
file.get(alpha);
if (alpha == '\n') {
count++;
}
}
return count;
}
static int record = 1;
// USER CLASS
class USER {
private:
string valid_email;
string unique_name;
string salt;
string hashed_pass;
public:
// Constructor
USER(string email = "", string name = "", string pass = "") {
this->valid_email = email;
this->unique_name = name;
this->hashed_pass = pass;
}
// Read and store data from creds file
void Read_Client_Data(ifstream& read) {
string name, email, temp_salt, temp_hash;
if (read >> name >> email >> temp_salt >> temp_hash) {
this->valid_email = email;
this->unique_name = name;
this->salt = temp_salt;
this->hashed_pass = temp_hash;
}
}
// Print info
void Print_Info() {
cout << "\n\t -------------------------";
cout << "\n\t | USER ID: " << record;
cout << "\n\t |------------------------";
cout << "\n\t | USER NAME: " << this->unique_name;
cout << "\n\t | EMAIL: " << this->valid_email;
cout << "\n\t | HASHED PASSWORD: " << this->hashed_pass;
cout << "\n\t | SALT: " << this->salt;
cout << "\n\t -------------------------\n";
}
// Verify identity
bool Verify_Identity(string name, string pass) {
string entered_hash = hash_password(pass, this->salt);
return this->unique_name == name && this->hashed_pass == entered_hash;
}
// Validate email
bool Valid_Email(string email) {
return email.find('@') != string::npos && email.find('.') != string::npos;
}
// Strong password requirement
bool Strong_Password(string pass) {
int digit_count = 0, small_count = 0, capital_count = 0, special_count = 0;
for (char& i : pass) {
if (isdigit(i)) digit_count++;
else if (islower(i)) small_count++;
else if (isupper(i)) capital_count++;
else if (ispunct(i)) special_count++;
}
return digit_count >= 1 && small_count >= 1 && capital_count >= 1 && special_count >= 2;
}
// User existence validation
bool User_Exists(string name) {
ifstream read("updated_creds.txt");
string name_check, temp_email, temp_salt, temp_hash;
while (read >> name_check >> temp_email >> temp_salt >> temp_hash) {
if (name_check == name) return true;
}
read.close();
return false;
}
// Register new user
void Register_User() {
string email, name, pass;
cout << "\n\t -----------------------------------";
cout << "\n\t | ENTER YOUR USERNAME [NO SPACE]: ";
cin >> name;
while (User_Exists(name)) {
cout << "\n\t | USERNAME ALREADY EXISTS!";
cout << "\n\t | ENTER YOUR USERNAME [NO SPACE]: ";
cin >> name;
}
cout << "\n\t | ENTER YOUR EMAIL: ";
cin >> email;
while (!Valid_Email(email)) {
cout << "\n\t | INVALID EMAIL!";
cout << "\n\t | ENTER YOUR EMAIL: ";
cin >> email;
}
cout << "\n\t | ENTER YOUR PASSWORD: ";
cin >> pass;
while (!Strong_Password(pass)) {
cout << "\n\t | STRONG PASSWORD REQUIRED!";
cout << "\n\t | ENTER YOUR PASSWORD: ";
cin >> pass;
}
cout << "\n\t -----------------------------------";
cout << "\n\t | CONFIRM PASSWORD: ";
string confirm_pass;
cin >> confirm_pass;
if (pass == confirm_pass) {
string salt = generate_salt(16);
string hashed_password = hash_password(pass, salt);
ofstream write("updated_creds.txt", ios::app);
write << name << " " << email << " " << salt << " " << hashed_password << endl;
cout << "\n\t | REGISTRATION COMPLETED!";
write.close();
} else {
cout << "\n\t | PASSWORDS DO NOT MATCH!";
}
}
};
#endif