-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
523 lines (452 loc) · 15.8 KB
/
Copy pathmain.cpp
File metadata and controls
523 lines (452 loc) · 15.8 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include <algorithm>
#include "main.h"
/**
* Funkcja, która zapisuje hasła do pliku w postaci zaszyfrowanej.
*
* Wykorzystuje do tego funkcję write, która przekazuje binarne dane.
*
*/
void PasswordManager::encryptFile() {
std::ofstream file(filename, std::ios::binary);
if (file.is_open()) {
for (const auto &password: passwords) {
file.write(reinterpret_cast<const char *>(&password), sizeof(password));
}
file.close();
} else {
std::cout << "Error saving file.\n";
}
}
/**
* Funkcja odczytuje hasła z pliku dzięki metodzie read, która odczytuje binarne dane
*
*
*/
void PasswordManager::decryptFile() {
passwords.clear();
std::ifstream file(filename, std::ios::binary);
if (file.is_open()) {
while (!file.eof()) {
Password password;
file.read(reinterpret_cast<char *>(&password), sizeof(password));
if (!file.eof()) {
passwords.push_back(password);
}
}
file.close();
} else {
std::cout << "Error reading file.\n";
}
}
/**
* Sprawdzenie czy hasło zostało już użyte.
*
* @param password Parametr, w którym sprawdzamy czy był już użyty
* @return true jeśli parametr został użyty
* @return false jeśli parametr nie został użyty
*/
bool PasswordManager::isPasswordUsed(const std::string &password) const {
for (const auto &entry: passwords) {
if (entry.password == password) {
return true;
}
}
return false;
}
/**
* Konstruktor klasy PasswordManager
*
* @param file przekazujemy plik do konstruktora, który zostanie odszyfrowany
*/
PasswordManager::PasswordManager(const std::string &file) : filename(file) {
decryptFile();
}
/**
* Metoda, która wyszukuje hasło po odpowiednim zapytaniu przekazanym jako argument
*
* Używamy do tego funkcji find() dla typu std::string z przekazanym argumentem query
*
* Następnie zostają wyświetlone znalezione stringi z danych kategorii
*
* @param query podajemy zapytanie, pod którym chcemy żeby nasze hasło zostało znalezione
*/
void PasswordManager::searchPasswords(const std::string &query) const {
std::cout << "Searched passwords:\n";
for (const auto &password: passwords) {
if (password.name.find(query) != std::string::npos ||
password.password.find(query) != std::string::npos ||
password.category.find(query) != std::string::npos ||
password.website.find(query) != std::string::npos ||
password.login.find(query) != std::string::npos) {
std::cout << "Name: " << password.name << std::endl;
std::cout << "Password: " << password.password << std::endl;
std::cout << "Category: " << password.category << std::endl;
std::cout << "Website: " << password.website << std::endl;
std::cout << "Login: " << password.login << std::endl;
std::cout << std::endl;
}
}
}
/**
* Sortujemy hasła po różnych parametrach.
*
* Wykorzystujemy do tego bibliotekę <algorithm> i funkcje sort().
*
*
*
* @param fields wybieramy po jakich parametrach chcemy sortowac
* @param result tworzymy wektor z posortowanymi hasłami
* @return false jeśli nie znajdziemy nic
*/
void PasswordManager::sortPasswords(const std::vector<std::string> &fields) {
std::vector<Password> result = passwords;
sort(result.begin(), result.end(), [&fields](const Password &firstPasswd, const Password &secondPasswd) -> bool {
for (const auto &field: fields) {
if (field == "name") {
if (firstPasswd.name != secondPasswd.name) {
return firstPasswd.name < secondPasswd.name;
}
} else if (field == "password") {
if (firstPasswd.password != secondPasswd.password) {
return firstPasswd.password < secondPasswd.password;
}
} else if (field == "category") {
if (firstPasswd.category != secondPasswd.category) {
return firstPasswd.category < secondPasswd.category;
}
} else if (field == "website") {
if (firstPasswd.website != secondPasswd.website) {
return firstPasswd.website < secondPasswd.website;
}
} else if (field == "login") {
if (firstPasswd.login != secondPasswd.login) {
return firstPasswd.login < secondPasswd.login;
}
}
}
return false;
});
std::cout << "Sorted passwords:\n";
for (const auto &password: result) {
std::cout << "Name: " << password.name << std::endl;
std::cout << "Password: " << password.password << std::endl;
std::cout << "Category: " << password.category << std::endl;
std::cout << "Website: " << password.website << std::endl;
std::cout << "Login: " << password.login << std::endl;
std::cout << std::endl;
}
}
/**
* Funkcja sprawdza, które opcje mają zostać rozpatrzone w tworzeniu losowego hasła.
*
* Następnie tworzy i zwraca losowe hasło.
*
* @param length dlugosc hasla
* @param upperCase czy w hasle maja zostac uzyte wielkie litery
* @param lowerCase czy w hasle maja zostac uzyte male litery
* @param specialChar czy w hasle maja zostac uzyte znaki specjalne
* @return zwraca losowe hasło
*/
std::string PasswordManager::randomPassword(int length, bool upperCase, bool lowerCase, bool specialChar) const {
srand(time(NULL));
const std::string lowerChars = "abcdefghijklmnopqrstuvwxyz";
const std::string upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const std::string specialChars = "!@#$%^&*()-+=~`;:'?/";
std::string combinedChars;
if (upperCase) {
combinedChars += upperChars;
}
if (lowerCase) {
combinedChars += lowerChars;
}
if (specialChar) {
combinedChars += specialChars;
}
std::string result;
int combinedCharsLength = combinedChars.length();
for (int i = 0; i < length; i++) {
int randomIndex = rand() % combinedCharsLength;
result += combinedChars[randomIndex];
}
return result;
}
void PasswordManager::printVector() {
std::cout << "Available categories: " << std::endl;
for (const auto &category: categories) {
std::cout << "- " << category << std::endl;
}
}
/**
* Funkcja dodająca hasło z wybranymi przez nas parametrami.
*
* Funkcja pozwala na wygenerowanie losowego hasła z wybranymi przez nas parametrami takimi jak:
* długość hasła, wielkie litery, małe litery oraz znaki specjalne.
*
*
* Po wybraniu wszystkich parametrów tworzy hasło z odpowiednimi polami i dodaje do wektora.
*
*/
void PasswordManager::addPassword() {
Password password;
std::cout << "Adding new password:" << std::endl;
do {
std::cout << "Name: ";
getline(std::cin, password.name);
} while (password.name.empty());
std::cout << "Do you want to generate random password? (Y/N): ";
char generateChoice;
std::cin >> generateChoice;
tolower(generateChoice);
std::cin.ignore();
if (generateChoice == 'y') {
int length;
bool includeUppercase, includeLowercase, includeSpecialChars;
std::cout << "Password length: ";
std::cin >> length;
std::cin.ignore();
std::cout << "Include uppercase letters? (Y/N): ";
char upperChoice;
std::cin >> upperChoice;
std::cin.ignore();
tolower(upperChoice);
includeUppercase = (upperChoice == 'y');
std::cout << "Include lowercase letters? (Y/N): ";
char lowerChoice;
std::cin >> lowerChoice;
std::cin.ignore();
tolower(lowerChoice);
includeLowercase = (lowerChoice == 'y');
std::cout << "Include special characters? (Y/N): ";
char specialChoice;
std::cin >> specialChoice;
std::cin.ignore();
tolower(specialChoice);
includeSpecialChars = (specialChoice == 'y');
password.password = randomPassword(length, includeUppercase, includeLowercase, includeSpecialChars);
} else if (generateChoice == 'n') {
do {
std::cout << "Password: ";
getline(std::cin, password.password);
if (isPasswordUsed(password.password)) {
std::cout << "Password is already used and can be unsafe.\n";
}
} while (password.password.empty());
} else {
std::cout << "Error. \n";
}
std::cout << "Do you want to add category that has been already created? (Y/n): ";
char categoryChoice;
std::cin >> categoryChoice;
tolower(categoryChoice);
std::cin.ignore();
if (categoryChoice == 'y') {
printVector();
std::cout << "Category: ";
getline(std::cin, password.category);
} else {
do {
std::cout << "Category: ";
getline(std::cin, password.category);
} while (password.category.empty());
}
std::cout << "Website (optional): ";
getline(std::cin, password.website);
std::cout << "Login (optional): ";
getline(std::cin, password.login);
passwords.push_back(password);
encryptFile();
}
/**
* Funkcja, która pozwala użytkownikowi na zmiane wartości w haśle
*
* Pobiera od użytkownika nazwę hasła, a następnie użytkownik wybiera co chce zmienić
*
*
* @param name nazwa hasła do edytowania
*/
void PasswordManager::editPassword() {
std::string name;
std::cout << "Enter name of the password to edit: ";
getline(std::cin, name);
bool found = false;
for (auto &password: passwords) {
if (password.name == name) {
std::cout << "Editing password:" << std::endl;
std::cout << "Name: " << password.name << std::endl;
std::cout << "New name (click enter if stays the same): ";
getline(std::cin, name);
if (!name.empty()) {
password.name = name;
}
std::cout << "New password (click enter if stays the same): ";
getline(std::cin, password.password);
std::cout << "New category (click enter if stays the same): ";
getline(std::cin, password.category);
std::cout << "New website (click enter if stays the same): ";
getline(std::cin, password.website);
std::cout << "New login (click enter if stays the same): ";
getline(std::cin, password.login);
encryptFile();
std::cout << "Password edited." << std::endl;
found = true;
break;
}
}
if (!found) {
std::cout << "Password not found." << std::endl;
}
}
/**
* Funkcja, która pobiera od użytkownika nazwe hasła, a później je usuwa.
*
* @param name nazwa hasła do usunięcia
*
*/
void PasswordManager::removePassword() {
std::string name;
std::cout << "Enter the name of the password to delete: ";
getline(std::cin, name);
std::cout << "You are about to delete a password. Are you sure? (Y/N): " << std::endl;
char confirmation;
std::cin >> confirmation;
tolower(confirmation);
std::cin.ignore();
if (confirmation == 'y') {
auto it = remove_if(passwords.begin(), passwords.end(), [&name](const Password &password) {
return password.name == name;
});
if (it != passwords.end()) {
passwords.erase(it, passwords.end());
encryptFile();
std::cout << "Password deleted." << std::endl;
} else {
std::cout << "Password not found." << std::endl;
}
} else {
std::cout << "Operation canceled." << std::endl;
}
}
/**
* Funkcja, która dodaje nową kategorię do wektora kategorii.
*
* Pobiera od użytkownika nazwę kategorii
*
* @param category nazwa kategorii do dodania
*/
void PasswordManager::addCategory() {
std::string category;
std::cout << "Enter name of the category: ";
getline(std::cin, category);
categories.push_back(category);
std::cout << "Category added." << std::endl;
}
/**
* Funkcja, która pozwala na usunięcie kategorii wraz ze wszystkimi hasłami przypisanymi do tej kategorii
*
* Pobiera od użytkownika nazwę kategorii.
*
* @param category nazwa kategorii do usunięcia
*/
void PasswordManager::removeCategory() {
std::string category;
std::cout << "Enter name of the category to delete: ";
getline(std::cin, category);
auto it = remove_if(passwords.begin(), passwords.end(), [&category](const Password &password) {
return password.category == category;
});
passwords.erase(it, passwords.end());
encryptFile();
std::cout << "Category deleted." << std::endl;
}
/**
* Funkcja sprawdzająca czy podany plik z parametru istnieje
*
*
* @param filename wczytujemy nazwę pliku
* @return true jeśli plik istnieje
*/
bool fileExists(const std::string &filename) {
std::ifstream file(filename);
return file.good();
}
int main() {
std::string filename;
int choice;
std::string query;
std::vector<std::string> sortFields;
char input;
std::cout << "Enter name of the file: ";
getline(std::cin, filename);
std::cout << std::endl;
PasswordManager manager(filename);
if (!fileExists(filename)) {
std::cout << "File not exist.\n";
} else {
while (true) {
std::cout << "1. Search password" << std::endl;
std::cout << "2. Sort password" << std::endl;
std::cout << "3. Add password" << std::endl;
std::cout << "4. Edit password" << std::endl;
std::cout << "5. Delete password" << std::endl;
std::cout << "6. Add category" << std::endl;
std::cout << "7. Delete category" << std::endl;
std::cout << "8. Close program" << std::endl;
std::cout << "Choose an option: ";
std::cin >> choice;
std::cin.ignore();
switch (choice) {
case 1:
std::cout << "Enter query: ";
getline(std::cin, query);
manager.searchPasswords(query);
std::cout << "Type q to continue: ";
std::cin >> input;
tolower(input);
if (input == 'q')
break;
case 2:
sortFields.clear();
while (true) {
std::string field;
std::cout << "Enter field/fields to sort (type q to close): ";
getline(std::cin, field);
if (field == "q" || field == "Q") {
break;
}
sortFields.push_back(field);
}
manager.sortPasswords(sortFields);
std::cout << "Type q to continue: ";
std::cin >> input;
tolower(input);
if (input == 'q')
break;
case 3:
manager.addPassword();
break;
case 4:
manager.editPassword();
break;
case 5:
manager.removePassword();
break;
case 6:
manager.addCategory();
break;
case 7:
manager.removeCategory();
break;
case 8:
return 0;
default:
std::cout << "Invalid option." << std::endl;
break;
}
std::cout << std::endl;
}
}
}