-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingUp.cpp
More file actions
88 lines (67 loc) · 1.94 KB
/
SingUp.cpp
File metadata and controls
88 lines (67 loc) · 1.94 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
#include "SignUp.h"
string HashPassword(const string& password) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256(reinterpret_cast<const unsigned char*>(password.c_str()), password.size(), hash);
// Convert hash to a hexadecimal string
ostringstream oss;
for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
oss << hex << setw(2) << setfill('0') << (int)hash[i];
}
return oss.str();
}
void SignUpAndSignIn::MakeNewAccount()
{
cout << "User name: ";
string username;
cin >> username;
string password;
cout << "Password: ";
cin >> password;
password = HashPassword(password);
SaveAccountToFile(username, password);
}
void SignUpAndSignIn::SaveAccountToFile(string& username, string& hashedPassword)
{
ifstream inputFile("accounts.json");
json accounts;
if (inputFile.is_open()) {
inputFile >> accounts;
inputFile.close();
}
accounts[username] = { {"password", hashedPassword}};
ofstream outputFile("accounts.json");
outputFile << setw(4) << accounts << endl;
outputFile.close();
}
int SignUpAndSignIn::SignInToAccount() {
cout << "User name: ";
string username;
cin >> username;
cout << "Password: ";
string password;
cin >> password;
if (username == "Admin" && password == "Admin")
{
cout << "Wellcome Admin \n";
return 1;
}
string hashedPassword = HashPassword(password);
ifstream inputFile("accounts.json");
json accounts;
if (inputFile.is_open()) {
inputFile >> accounts;
inputFile.close();
}
else {
cout << "Error: Could not open accounts file.\n";
return - 1;
}
if (accounts.contains(username) && accounts[username]["password"] == hashedPassword) {
cout << "Sign-in successful. Welcome, " << username << "!\n";
return 0;
}
else {
cout << "Error: Invalid username or password.\n";
return -1;
}
}