-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.cpp
More file actions
181 lines (158 loc) · 4.45 KB
/
authentication.cpp
File metadata and controls
181 lines (158 loc) · 4.45 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <iomanip>
#include <openssl/sha.h>
#include <termios.h>
#include <unistd.h>
using namespace std;
#define ROOT_FILE "root.dat"
#define USERS_FILE "users.dat"
// Function to hide password input
string getHiddenInput() {
string password;
struct termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
cin >> password;
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
cout << endl;
return password;
}
// Hashing function using SHA-256
string sha256(const string &str) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256((unsigned char *)str.c_str(), str.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();
}
bool rootExists() {
ifstream file(ROOT_FILE);
return file.good();
}
void createRootPassword() {
string rootPassword;
cout << "No root password found. Set up a root password (cannot be changed later): ";
rootPassword = getHiddenInput();
ofstream file(ROOT_FILE);
file << sha256(rootPassword);
file.close();
cout << "Root password set successfully!\n";
}
bool verifyRootPassword(const string &inputPassword) {
ifstream file(ROOT_FILE);
string storedHash;
getline(file, storedHash);
file.close();
return sha256(inputPassword) == storedHash;
}
bool userExists(const string &username) {
ifstream file(USERS_FILE);
string line;
while (getline(file, line)) {
if (line.find(username + ":") == 0) {
return true;
}
}
return false;
}
void addUser() {
string rootPassword, username, password;
cout << "Enter root password: ";
rootPassword = getHiddenInput();
if (!verifyRootPassword(rootPassword)) {
cout << "Incorrect root password!\n";
return;
}
cout << "Enter new username: ";
cin >> username;
if (userExists(username)) {
cout << "Username already taken.\n";
return;
}
cout << "Enter new password: ";
password = getHiddenInput();
ofstream file(USERS_FILE, ios::app);
file << username << ":" << sha256(password) << endl;
file.close();
cout << "User created successfully!\n";
}
void deleteUser() {
string rootPassword, username;
cout << "Enter root password: ";
rootPassword = getHiddenInput();
if (!verifyRootPassword(rootPassword)) {
cout << "Incorrect root password!\n";
return;
}
cout << "Enter username to delete: ";
cin >> username;
if (!userExists(username)) {
cout << "User does not exist!\n";
return;
}
cout << "Are you sure you want to delete user '" << username << "'? (yes/no): ";
string confirmation;
cin >> confirmation;
if (confirmation != "yes") {
cout << "User deletion canceled.\n";
return;
}
ifstream file(USERS_FILE);
ofstream tempFile("temp.dat");
string line;
while (getline(file, line)) {
if (line.find(username + ":") != 0) {
tempFile << line << "\n";
}
}
file.close();
tempFile.close();
remove(USERS_FILE);
rename("temp.dat", USERS_FILE);
cout << "User deleted successfully!\n";
}
void login() {
string username, password;
cout << "Username: ";
cin >> username;
cout << "Password: ";
password = getHiddenInput();
ifstream file(USERS_FILE);
string line;
while (getline(file, line)) {
size_t pos = line.find(":");
if (pos != string::npos) {
string storedUser = line.substr(0, pos);
string storedHash = line.substr(pos + 1);
if (storedUser == username && storedHash == sha256(password)) {
cout << "Login successful!\n";
return;
}
}
}
cout << "Invalid username or password.\n";
}
int main() {
if (!rootExists()) {
createRootPassword();
}
int choice;
while (true) {
cout << "\n1. Login\n2. Create User (requires root password)\n3. Delete User (requires root password)\n4. Exit\nChoice: ";
cin >> choice;
switch (choice) {
case 1: login(); break;
case 2: addUser(); break;
case 3: deleteUser(); break;
case 4: return 0;
default: cout << "Invalid choice!\n";
}
}
}