-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordEntry.cpp
More file actions
256 lines (227 loc) · 8.2 KB
/
Copy pathPasswordEntry.cpp
File metadata and controls
256 lines (227 loc) · 8.2 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
#include "PasswordEntry.h"
#include <algorithm>
#include <string.h>
#include <sstream>
#include <vector>
#include <iostream>
PasswordEntry::PasswordEntry() {
name = "";
password_text = "";
category = "";
login = "";
service = "";
}
PasswordEntry::PasswordEntry(const std::string &name, const std::string &password_text,
const std::string &category,
const std::string &login, const std::string &service) :
name(name),
password_text(password_text),
category(category),
login(login),
service(service) {};
const std::string PasswordEntry::getName() const {
return name;
}
const std::string PasswordEntry::getPasswordText() {
return password_text;
}
const std::string PasswordEntry::getCategory() const {
return category;
}
const std::string PasswordEntry::getLogin() {
return login;
}
const std::string PasswordEntry::getService() {
return service;
}
void PasswordEntry::setName(const std::string &name) {
PasswordEntry::name = name;
}
void PasswordEntry::setPasswordText(const std::string &password_text) {
PasswordEntry::password_text = password_text;
}
void PasswordEntry::setCategory(const std::string &category) {
PasswordEntry::category = category;
}
void PasswordEntry::setLogin(const std::string &login) {
PasswordEntry::login = login;
}
void PasswordEntry::setService(const std::string &service) {
PasswordEntry::service = service;
}
std::string PasswordEntry::toDisplayString() {
std::string result = "Name: " + name + "\n" + "Password: " + password_text + "\n" +
"Category: " + category + "\n" + "Login: " + login + "\n" + "Service: " + service + "\n";
return result;
}
std::string PasswordEntry::toStorageString() {
std::string result = name + ";" + password_text + ";" + category + ";" + login + ";" + service + ";";
return result;
}
void PasswordEntry::setFieldsFromStorageLine(std::string storage_line) {
std::stringstream line_stream(storage_line);
std::string parsed_field;
std::vector<std::string> fields;
while (std::getline(line_stream, parsed_field, ';')) {
fields.push_back(parsed_field);
}
setName(fields.at(0));
setPasswordText(fields.at(1));
setCategory(fields.at(2));
setLogin(fields.at(3));
setService(fields.at(4));
}
void PasswordEntry::addCategory(std::vector<std::string> &categories) {
if (!categories.empty()) {
std::cout << "Existing categories: " << std::endl;
for (std::string existing_category : categories) {
std::cout << existing_category << std::endl;
}
}
std::cout << "Enter the password category: " << std::endl;
std::cin.ignore();
std::getline(std::cin, category);
std::vector<std::string>::iterator category_iter = std::find(categories.begin(), categories.end(), category);
if (category_iter == categories.end()) {
categories.push_back(category);
}
}
bool PasswordEntry::createNewPassword(std::vector<std::string> &categories) {
std::cout << "Please enter the password name: " << std::endl;
std::cin.ignore();
std::getline(std::cin, name);
if (!generatePassword()) {
return false;
}
addCategory(categories);
std::cout << "Please enter the login: " << std::endl;
std::getline(std::cin, login);
std::cout << "Please enter the service: " << std::endl;
std::getline(std::cin, service);
if (name == "" || password_text == "" || category == "") {
std::cout << "Invalid data: name, password, and category must be provided." << std::endl;
std::cout << toDisplayString() << std::endl;
return false;
}
return true;
}
bool PasswordEntry::generatePassword() {
std::cout << "Choose a password generation method.\n";
std::cout << std::endl;
std::cout << "0. Automatic password generation.\n";
std::cout << "1. Manual password entry.\n";
std::string generation_choice;
std::cin >> generation_choice;
if (generation_choice == "0") {
std::string length_string;
int char_count;
std::string include_special_chars;
std::string include_mixed_case;
std::cout << "How many characters should the password contain? (minimum 8)" << std::endl;
std::cin >> char_count;
if (char_count <= 7) {
std::cout << "Too few characters. The password will default to 8 characters." << std::endl;
char_count = 8;
}
std::cout << "Should the password include special characters? [T/N]" << std::endl;
std::cin >> include_special_chars;
if (include_special_chars != "T" && include_special_chars != "N") {
std::cout << "Invalid parameter value. The password will not include special characters." << std::endl;
include_special_chars = "N";
}
std::cout << "Should the password include uppercase and lowercase letters? [T/N]" << std::endl;
std::cin >> include_mixed_case;
if (include_mixed_case != "T" && include_mixed_case != "N") {
std::cout << "Invalid parameter value. The password will include uppercase and lowercase letters." << std::endl;
include_mixed_case = "T";
}
static const char digits_and_uppercase[] =
"0123456789"
"QWERTYUIOPLKJHGFDSAZXCVBNM";
static const char lowercase[] =
"qwertyuioplkjhgfdsazxcvbnm";
static const char special_chars[] =
"!@#$%*?>[:|]_+-=";
std::string generation_pool = digits_and_uppercase;
if (include_special_chars == "T") {
generation_pool += special_chars;
}
if (include_mixed_case == "T") {
generation_pool += lowercase;
}
std::string new_password;
new_password.reserve(char_count);
int pool_size = generation_pool.size();
for (int i = 0; i < char_count; i++) {
int random_index = rand() % (pool_size - 1);
new_password += generation_pool[random_index];
}
std::cout << "New password: " + new_password << std::endl;
password_text = new_password;
} else if (generation_choice == "1") {
std::cout << "Please enter the password: " << std::endl;
std::cin.ignore();
std::getline(std::cin, password_text);
} else {
std::cout << "Invalid generation option." << std::endl;
return false;
}
return true;
}
bool PasswordEntry::compareEntries(const PasswordEntry &entryA, const PasswordEntry &entryB) {
if (entryA.getName() < entryB.getName()) {
return true;
}
if (entryA.getName() > entryB.getName()) {
return false;
}
if (entryA.getCategory() > entryB.getCategory()) {
return false;
}
if (entryA.getCategory() < entryB.getCategory()) {
return true;
}
return false;
}
void PasswordEntry::editEntry(std::vector<std::string> &categories) {
showEditMenu();
std::cout << "Please enter the field number you would like to edit: " << std::endl;
int user_choice;
std::cin >> user_choice;
std::string new_value;
if (user_choice == 2) {
generatePassword();
new_value = password_text;
} else if (user_choice == 3) {
addCategory(categories);
new_value = category;
} else {
std::cout << "Enter the new value:" << std::endl;
std::cin.ignore();
std::getline(std::cin, new_value);
}
if (user_choice < 4 && new_value == "") {
std::cout << "The selected field cannot be empty." << std::endl;
} else {
switch (user_choice) {
case 1:
name = new_value;
break;
case 4:
login = new_value;
break;
case 5:
service = new_value;
break;
}
}
std::cout << toDisplayString() << std::endl;
}
void PasswordEntry::showEditMenu() {
std::cout << "Menu: " << std::endl;
std::cout << "1. Edit name." << std::endl;
std::cout << "2. Edit password." << std::endl;
std::cout << "3. Edit category." << std::endl;
std::cout << "4. Edit login." << std::endl;
std::cout << "5. Edit service." << std::endl;
}