-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.cpp
More file actions
279 lines (229 loc) · 8.87 KB
/
Copy pathauth.cpp
File metadata and controls
279 lines (229 loc) · 8.87 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "auth.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
#include <limits>
#include <cctype>
#include "vault.h"
#include "../security/security.h"
std::vector<Entry> Auth::decoyData;
Auth::Auth(Config& cfg) : config(cfg), attempts(0), isRealAccess(false), currentVault(nullptr), masterPassword("") {
initDecoyData();
}
Auth::~Auth() {
if (currentVault) delete currentVault;
}
void Auth::initDecoyData() {
decoyData = {
{"Gmail", "john.doe@gmail.com", "mypass123", "Social", "2026-03-15 10:30"},
{"Instagram", "johndoe", "insta123", "Social", "2026-02-20 14:22"},
{"Chase Bank", "john.doe@bank.com", "bank456", "Finance", "2026-01-10 09:15"}
};
}
std::string Auth::getPasswordInput(const std::string& prompt) {
std::string input;
std::cout << prompt;
std::cin >> input;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return input;
}
bool Auth::fileExists(const std::string& filename) {
std::ifstream file(filename);
return file.good();
}
bool Auth::isSystemResetRequired() {
bool masterExists = fileExists("data/master.txt");
bool vaultExists = fileExists("data/vault.txt");
if (!masterExists || !vaultExists) {
return true;
}
std::ifstream masterCheck("data/master.txt");
std::ifstream vaultCheck("data/vault.txt");
masterCheck.seekg(0, std::ios::end);
vaultCheck.seekg(0, std::ios::end);
bool masterEmpty = (masterCheck.tellg() == 0);
bool vaultEmpty = (vaultCheck.tellg() == 0);
masterCheck.close();
vaultCheck.close();
return masterEmpty || vaultEmpty;
}
void Auth::initialSetup() {
if (isSystemResetRequired()) {
std::cout << "\n[!] SYSTEM RESET DETECTED - Full Re-initialization\n";
std::cout << "[X] Previous vault was destroyed. Setting up NEW system\n\n";
std::remove("data/master.txt");
std::remove("data/vault.txt");
std::cout << "[*] CREATE NEW MASTER PASSWORD\n";
std::cout << "[?] 1st RUN: Set your own Master Password\n\n";
std::string pwd1, pwd2;
do {
pwd1 = getPasswordInput("[+] New Master Password: ");
pwd2 = getPasswordInput("[R] Confirm Password: ");
if (pwd1 != pwd2) {
std::cout << "[!] Passwords don't match! Try again.\n\n";
} else if (pwd1.length() < 6) {
std::cout << "[!] Password too short! Use at least 6 characters.\n\n";
}
} while (pwd1 != pwd2 || pwd1.length() < 6);
masterPassword = pwd1;
std::ofstream masterOut("data/master.txt");
masterOut << masterPassword;
masterOut.close();
std::ofstream vaultReset("data/vault.txt", std::ios::trunc);
vaultReset.close();
std::cout << "[OK] NEW Master Password set successfully!\n";
std::cout << "[*] Fresh vault initialized. Stay safe!\n\n";
std::cin.get();
} else {
std::ifstream masterFile("data/master.txt");
if (masterFile.good()) {
masterFile >> masterPassword;
masterFile.close();
}
}
}
bool Auth::checkSpecialCondition() {
static bool firstAttempt = true;
bool result = firstAttempt;
firstAttempt = false;
return result;
}
void Auth::showDecoyMessage() {
std::cout << "\n[+] Vault decrypted successfully!\n";
std::cout << "[*] Secured with AES-256 + RSA encryption\n\n";
}
void Auth::authenticate() {
if (masterPassword.empty()) {
std::cout << "\n[!] No master password configured. Run initial setup first.\n";
initialSetup();
return;
}
std::string input = getPasswordInput("\n[+] Enter Master Password: ");
bool correctPassword = (input == masterPassword);
if (correctPassword && checkSpecialCondition()) {
std::cout << "\n[OK] ACCESS GRANTED TO SECURE VAULT!\n";
std::cout << "[*] REAL vault active - All features unlocked\n\n";
if (currentVault) delete currentVault;
currentVault = new Vault("data/vault.txt", true);
isRealAccess = true;
} else {
attempts++;
if (correctPassword) {
std::cout << "\n[!] Special access condition failed (1st attempt only)\n";
}
showDecoyMessage();
if (currentVault) delete currentVault;
currentVault = new Vault("data/decoy.txt", false);
isRealAccess = false;
std::cout << "[*] Attempts: " << attempts << "/" << config.getMaxAttempts() << "\n";
if (attempts >= config.getMaxAttempts()) {
triggerSelfDestruct();
attempts = 0;
}
}
}
void Auth::viewCurrentVault() {
if (currentVault) {
std::cout << "\n" << (isRealAccess ? "[*] REAL VAULT ACTIVE" : "[*] DECOY VAULT ACTIVE") << "\n";
currentVault->viewEntriesWithStrength();
} else {
std::cout << "[!] No vault active. Authenticate first.\n\n";
}
}
void Auth::addToCurrentVault() {
if (currentVault) {
std::cout << (isRealAccess ? "[*] Adding to REAL vault" : "[*] Adding to DECOY (temporary)") << "\n";
currentVault->addEntry(isRealAccess);
} else {
std::cout << "[!] No vault active. Authenticate first.\n\n";
}
}
void Auth::searchEntry() {
if (!currentVault) {
std::cout << "[!] No vault active.\n\n";
return;
}
std::string searchTerm;
std::cout << "\n[?] Enter search term: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(std::cin, searchTerm);
currentVault->searchEntries(searchTerm);
}
void Auth::editCurrentVault() {
if (currentVault) {
currentVault->editEntry();
} else {
std::cout << "[!] No vault active.\n\n";
}
}
void Auth::checkPasswordStrength() {
std::string pwd;
std::cout << "\n[?] Enter password to analyze: ";
std::cin >> pwd;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
int score = 0;
if (pwd.length() >= 8) score++;
bool hasUpper = false, hasLower = false, hasDigit = false, hasSpecial = false;
for (char c : pwd) {
if (isupper(c)) hasUpper = true;
if (islower(c)) hasLower = true;
if (isdigit(c)) hasDigit = true;
if (!isalnum(c)) hasSpecial = true;
}
if (hasUpper) score++;
if (hasLower) score++;
if (hasDigit) score++;
if (hasSpecial) score++;
std::cout << "\n[*] STRENGTH ANALYSIS:\n";
std::cout << " ├─ Length: " << (pwd.length() >= 8 ? "[+] GOOD" : "[-] WEAK") << "\n";
std::cout << " ├─ Uppercase: " << (hasUpper ? "[+] YES" : "[-] NO") << "\n";
std::cout << " ├─ Lowercase: " << (hasLower ? "[+] YES" : "[-] NO") << "\n";
std::cout << " ├─ Numbers: " << (hasDigit ? "[+] YES" : "[-] NO") << "\n";
std::cout << " └─ Special: " << (hasSpecial ? "[+] YES" : "[-] NO") << "\n";
std::cout << "[*] RESULT: ";
if (score <= 2) std::cout << "[-] WEAK";
else if (score <= 3) std::cout << "[~] MEDIUM";
else std::cout << "[+] STRONG";
std::cout << " (" << score << "/5)\n\n";
}
void Auth::revealPasswordInVault() {
if (!currentVault || !isRealAccess) {
std::cout << "\n[!] REAL VAULT + ADMIN PASSWORD REQUIRED!\n\n";
return;
}
std::string adminPwd = getPasswordInput("\n[+] Admin Password (Master Password): ");
if (adminPwd != masterPassword) {
std::cout << "\n[!] Admin authentication failed!\n";
std::cout << "[*] Vault remains secure.\n\n";
return;
}
currentVault->viewEntriesWithStrength();
std::cout << "\n[+] Select entry number to DECRYPT: ";
size_t index;
std::cin >> index;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
currentVault->revealPassword(index - 1);
}
void Auth::logoutReauthenticate() {
if (currentVault) {
delete currentVault;
currentVault = nullptr;
}
std::cout << "\n[!] Session logged out. Re-authenticating...\n";
authenticate();
}
void Auth::triggerSelfDestruct() {
std::cout << "\n[!] SELF-DESTRUCT ACTIVATED! WIPING ALL DATA.\n";
std::cout << "[!] Master Password RESET\n";
std::cout << "[!] Next RUN: You will set NEW master password\n\n";
std::remove("data/master.txt");
std::remove("data/vault.txt");
std::cout << "[OK] Self-destruct COMPLETE. System fully reset.\n";
std::cout << "[*] Vault locked. Stay safe!\n\n";
if (currentVault) {
delete currentVault;
currentVault = nullptr;
masterPassword = "";
}
}