forked from parakramgambhir14/CryptVault
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileio.cpp
More file actions
115 lines (90 loc) · 2.84 KB
/
Copy pathfileio.cpp
File metadata and controls
115 lines (90 loc) · 2.84 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
#include "fileio.h"
#include "../security/security.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
// ================= LOAD ENTRIES =================
vector<Entry> loadEntries(string filename) {
vector<Entry> vault;
ifstream file(filename);
string line;
while (getline(file, line)) {
stringstream ss(line);
Entry e;
getline(ss, e.site, '|');
getline(ss, e.username, '|');
getline(ss, e.password, '|');
// Decrypt password before storing in memory
e.password = decrypt(e.password);
vault.push_back(e);
}
return vault;
}
// ================= SAVE ENTRIES =================
void saveEntries(string filename, vector<Entry>& vault) {
ofstream file(filename);
for (auto &e : vault) {
// Encrypt password before writing to file
file << e.site << "|" << e.username << "|" << encrypt(e.password) << endl;
}
}
// ================= ADD ENTRY =================
void addEntry(vector<Entry>& vault) {
Entry e;
cout << "Site: "; getline(cin, e.site);
cout << "Username: "; getline(cin, e.username);
cout << "Password: "; getline(cin, e.password);
int score = passwordStrength(e.password);
if (score < 3) {
cout << "⚠️ Warning: Password is weak!\n";
}
vault.push_back(e);
cout << "Entry added successfully.\n";
}
// ================= VIEW ENTRIES =================
void viewEntries(vector<Entry>& vault) {
if (vault.empty()) {
cout << "Vault is empty.\n";
return;
}
for (size_t i = 0; i < vault.size(); i++) {
cout << i + 1 << ". "
<< vault[i].site << " | "
<< vault[i].username << " | "
<< vault[i].password << "\n";
}
}
// ================= SEARCH ENTRY =================
void searchEntry(vector<Entry>& vault) {
string site;
cout << "Enter site to search: ";
getline(cin, site);
bool found = false;
for (auto &e : vault) {
if (e.site == site) {
cout << "Found: " << e.site << " | "
<< e.username << " | "
<< e.password << "\n";
found = true;
break;
}
}
if (!found) cout << "No entry found for site: " << site << "\n";
}
// ================= DELETE ENTRY =================
void deleteEntry(vector<Entry>& vault) {
string site;
cout << "Enter site to delete: ";
getline(cin, site);
auto it = remove_if(vault.begin(), vault.end(), [&](Entry &e) {
return e.site == site;
});
if (it != vault.end()) {
vault.erase(it, vault.end());
cout << "Entry deleted.\n";
} else {
cout << "No entry found to delete.\n";
}
}