-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
400 lines (333 loc) · 15 KB
/
Copy pathmain.cpp
File metadata and controls
400 lines (333 loc) · 15 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <json.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>
#include <thread>
#include <stdexcept>
#include <sstream>
#include <sodium.h>
#include "VaultCrypto.h"
using namespace std;
using json = nlohmann::json;
void prntmenu(string option1, string option2 = "", string option3 = "", string option4 = "", string text1 = "")
{
if (option1 != "") option1 += "\n";
if (option2 != "") option2 += "\n";
if (option3 != "") option3 += "\n";
if (option4 != "") option4 += "\n";
cout << option1 << option2 << option3 << option4 << text1 << "\n";
}
struct Entry {
string jsonURL;
string jsonUsername;
string jsonPassword;
};
void to_json(json& j, const Entry& e) {
j = json{
{"URL", e.jsonURL},
{"Username", e.jsonUsername},
{"Password", e.jsonPassword}
};
}
void from_json(const json& j, Entry& e) {
j.at("URL").get_to(e.jsonURL);
j.at("Username").get_to(e.jsonUsername);
j.at("Password").get_to(e.jsonPassword);
}
int main() {
bool CloseProgram = false;
bool InvalidInput = false;
bool isNewVault = false;
int index = 1;
string encryptedData;
string answer;
int answer1;
string URL, username, password;
bool skip = false;
int entrysize = 0;
bool RunLoop = true;
json jFile;
string storedMasterPassword;
vector<Entry> entries;
if(sodium_init() < 0) {
cout << "Sodium not initialized!\n\n";
return 1;
}
ifstream infile("../../data.json", ios::binary);
if (infile.good()) {
encryptedData.assign(
(istreambuf_iterator<char>(infile)),
istreambuf_iterator<char>()
);
} else {
isNewVault = true;
}
infile.close();
if (isNewVault) {
cout << "Create Master Password:\n> ";
cin >> storedMasterPassword;
jFile["Entries"] = json::array();
string plainJson = jFile.dump(4);
string encryptedOut =
VaultCrypto::encryptJSON(plainJson, storedMasterPassword);
ofstream out("../../data.json", ios::binary);
out.write(encryptedOut.data(), encryptedOut.size());
out.close();
cout << "Vault created!\n\n";
}
if (!isNewVault) {
while (true) {
cout << "Enter Master Password:\n> ";
cin >> storedMasterPassword;
try {
string decryptedJson =
VaultCrypto::decryptJSON(encryptedData, storedMasterPassword);
jFile = json::parse(decryptedJson);
break;
} catch (...) {
cout << "Incorrect Master Password!\n\n";
}
}
}
cout << "Access granted!\n\n";
this_thread::sleep_for(chrono::milliseconds(800));
do {
if (jFile.contains("Entries")) {
entries.clear();
for (auto& item : jFile["Entries"]) {
entries.push_back(item.get<Entry>());
}
}
while (RunLoop) {
RunLoop = false;
prntmenu("1.Add Password", "2.Show Passwords", "3.Close", "", "Choose an option:");
if (!(cin >> answer1)) {
cout << "Invalid Input!\n\n";
cin.clear(); // Fehlerzustand löschen
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Müll entfernen
RunLoop = true;
continue;
}
if (answer1 == 0) {
cout << "Invalid Input!\n\n";
RunLoop = true;
continue;
}
else
switch (answer1) {
case 1: {
RunLoop = true; //festlegen das der while loop ausgeführt wird, falls weg = infinite loop
prntmenu("Website URL:");
cin >> URL;
prntmenu("Username:");
cin >> username;
prntmenu("Password:");
cin >> password;
while (RunLoop) {
RunLoop = false;
prntmenu("Are these Informations correct?:",
"URL: " + URL,
"Username: " + username,
"Password: " + password,
"");
cout << "Please type Yes or No:\n> ";
cin >> answer;
transform(answer.begin(), answer.end(), answer.begin(), ::tolower);
if (answer == "yes") {
Entry newEntry{URL, username, password};
jFile["Entries"].push_back(newEntry);
prntmenu("Password Saved!\n", "1.Return to menu", "2.Quit", "", "Please Choose an Option");
cin >> answer;
switch (answer1) {
case 1: {
continue;//TODO Freeze
}
case 2: {
return 0; //TODO doesnt end programm just freezes
}
}
}
else if (answer == "no") {
continue;
}
else {
cout << "Invalid input.\n\n";
RunLoop = true;
continue;
}
break;
}
break;
}
case 2: {
RunLoop = true;
while (RunLoop) {
RunLoop = false;
cout << "\n >>>>> STORED PASSWORDS <<<<< \n";
if (entries.empty()) {
cout << "No entries found!\n\n";
}
else {
for (auto& e : entries) {
cout << "--------------------\n";
cout << "URL:\n"+e.jsonURL << "\n\n";
cout << "Username:\n"+e.jsonUsername << "\n\n";
cout << "Password:\n"+e.jsonPassword << "\n\n";
}
if (InvalidInput == true) {
cout << "Invalid input!\n\n";
}
prntmenu("Please choose an option\n", "1.Return to menu", "2.Edit Entry", "3.Delete Entry", "4.Quit\nChoose an option:");
cin >> answer;
if (answer == "1") {
}
else if (answer == "2") {
RunLoop = true;
while (RunLoop) {
RunLoop = false;
index = 1;
for (auto& e : entries) {
cout << index <<"."+e.jsonURL << "\n";
index++;
}
cout << "\nSelect an Entry:\n\n";
cin >> answer1;
if (answer1 >= 1 && answer1 <= entries.size()) {
Entry& e = entries[answer1 -1];
RunLoop = true;
while (RunLoop) {
RunLoop = false;
cout << "\nYou selected:\n";
cout << "1.URL: " << e.jsonURL << "\n";
cout << "2.Username: " << e.jsonUsername << "\n";
cout << "3.Password: " << e.jsonPassword << "\n";
cout << "Which information would you like to edit?\n\n";
cin >> answer1;
if (answer1 < 1 || answer1 > 3) {
cout << "Invalid input!\n\n";
RunLoop = true;
continue;
}
else if (answer1 >= 1 && answer1 <= 3) {
switch (answer1) {
case 1: {
cout << "Please enter the new URL:\n\n";
cin >> e.jsonURL;
cout << "URL Saved!\n\n";
jFile["Entries"].clear();
for (auto& entry : entries) {
jFile["Entries"].push_back(entry);
}
RunLoop = true;
break;//TODO doesnt jump to main menu
}
case 2: {
cout << "Please enter the new Username:\n\n";
cin >> e.jsonUsername;
cout << "Username Saved!\n\n";
jFile["Entries"].clear();
for (auto& entry : entries) {
jFile["Entries"].push_back(entry);
}
continue;
}
case 3: {
cout << "Please enter the new Password:\n\n";
cin >> e.jsonPassword;
jFile["Entries"].clear();
for (auto& entry : entries) {
jFile["Entries"].push_back(entry);
}
cout << "Password Saved!\n\n";
continue;
}
}
}
}
}
else if (answer1 <= 1 || answer1 >= entrysize) {
cout << "Invalid input.\n\n";
RunLoop = true;
continue;
}
}
}
else if (answer == "3") {
RunLoop = true;
while (RunLoop) {
RunLoop = false;
index = 1;
for (auto& e : entries) {
cout << index <<"."+e.jsonURL << "\n";
index++;
}
cout << "Select an Entry:\n\n";
cin >> answer1;
entrysize = entries.size();
if (answer1 >= 1 && answer1 <= entries.size()) {
Entry& e = entries[answer1 -1];
RunLoop = true;
while (RunLoop) {
RunLoop = false;
cout << "\nYou selected:\n";
cout << "URL: " << e.jsonURL << "\n";
cout << "Username: " << e.jsonUsername << "\n";
cout << "Password: " << e.jsonPassword << "\n";
cout << "Are you sure you want to delete the entry? 1.Yes 2.No\n\n";
cin >> answer;
if (answer == "yes" || answer == "1") {
RunLoop = false;
//delete entry
entries.erase(entries.begin() + (answer1 - 1));
jFile["Entries"].clear();
for (auto& entry : entries) {
jFile["Entries"].push_back(entry);
}
cout << "Entry successfully deleted!\n\n";
break;
}
else if (answer == "no" || answer == "2") {
RunLoop = false;
break;
}
else {
cout << "Invalid input.\n\n";
RunLoop = true;
}
}
}
else if (answer1 < 1 || answer1 > entrysize) {
cout << "Invalid input.\n\n";
RunLoop = true;
continue;
}
}
}
else if (answer == "4") {
return 0;
}
else {
RunLoop = true;
InvalidInput = true;
continue;
}
}
break;
}
}
case 3:
cout << "Closing...\n";
return 0;
}
}
string plainJson = jFile.dump(4);
string encryptedOut =
VaultCrypto::encryptJSON(plainJson, storedMasterPassword);
ofstream out("../../data.json", ios::binary);
out.write(encryptedOut.data(), encryptedOut.size());
out.close();
sodium_memzero(storedMasterPassword.data(), storedMasterPassword.size());
} while (true);
}