-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasswordgenerator.cpp
306 lines (254 loc) · 8.32 KB
/
passwordgenerator.cpp
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
#include "passwordgenerator.h"
#include "ui_passwordgenerator.h"
#include <QClipboard>
#include <random>
// =====================
// Constructor & Destructor
// =====================
PasswordGenerator::PasswordGenerator(QWidget *parent)
: QDialog(nullptr)
, ui(new Ui::PasswordGenerator)
{
ui->setupUi(this);
ui->excludeChar->hide();
setWindowTitle("Falkenberg's Password Manager");
_passLength = new int(1);
_passComp = new int(1);
_letters = new std::vector<char>({
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
});
_numbers = new std::vector<char>({'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'});
_symbols = new std::vector<char>({
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+',
'[', ']', '{', '}', ';', ':', '\'', '\"', '<', '>', ',', '.', '?', '/'
});
ui->PassLength->setText(QString::number(*_passLength));
ui->PassComp->setText(QString::number(*_passComp));
}
PasswordGenerator::~PasswordGenerator()
{
delete _passLength; // Free the allocated memory upon program close
delete _passComp;
delete _letters;
delete _numbers;
delete _symbols;
delete ui;
}
// =====================
// Event Handlers
// =====================
void PasswordGenerator::on_PassLengthSlider_sliderMoved(int position)
{
*_passLength = position;
ui->PassLength->setText(QString::number(position));
}
void PasswordGenerator::on_PassCompSlider_sliderMoved(int position)
{
*_passComp = position;
ui->PassComp->setText(QString::number(position));
// Show or hide the excludeChar checkbox based on password complexity
if(position == 3) {
ui->excludeChar->show();
} else {
ui->excludeChar->hide();
}
}
void PasswordGenerator::on_GenPass_clicked()
{
char* password = createPassword(*_passLength, *_passComp); // Generate a new password
ui->GeneratedPassLabel->setText(password);
delete[] password; // Free the allocated memory for the password
}
void PasswordGenerator::on_CopyButton_clicked()
{
QClipboard *clipboard = QApplication::clipboard();
QString textToCopy = ui->GeneratedPassLabel->text();
clipboard->setText(textToCopy); // Copy the generated password to the clipboard
}
void PasswordGenerator::on_exitButton_clicked()
{
this->hide(); // Hide the dialog instead of closing it completely
}
// =====================
// Password Generation Logic
// =====================
char* PasswordGenerator::createPassword(int len, int comp) {
char* array = new char[len + 1]; // Dynamically allocate memory for a char array of the given size (+1 for the null terminator)
std::random_device rd;
std::mt19937 gen(rd());
// Define the distribution and sources based on the complexity
std::uniform_int_distribution<size_t> distr;
switch(comp) {
case 1: // Letters only
distr = std::uniform_int_distribution<size_t>(0, _letters->size() - 1);
break;
case 2: // Letters and numbers
distr = std::uniform_int_distribution<size_t>(0, _letters->size() + _numbers->size() - 1);
break;
case 3: // Letters, numbers, and symbols
distr = std::uniform_int_distribution<size_t>(0, _letters->size() + _numbers->size() + _symbols->size() - 1);
break;
default:
throw std::invalid_argument("Invalid complexity level.");
}
for(int i = 0; i < len; ++i) {
size_t randInt = distr(gen);
if (comp == 1) {
array[i] = _letters->at(randInt);
} else if (comp == 2) {
if (randInt < _letters->size()) {
array[i] = _letters->at(randInt);
} else {
array[i] = _numbers->at(randInt - _letters->size());
}
} else if (comp == 3) {
if (randInt < _letters->size()) {
array[i] = _letters->at(randInt);
} else if (randInt < _letters->size() + _numbers->size()) {
array[i] = _numbers->at(randInt - _letters->size());
} else {
array[i] = _symbols->at(randInt - _letters->size() - _numbers->size());
}
}
}
array[len] = '\0'; // Null-terminate the string
return array; // Return the pointer to the allocated array
}
// =====================
// Checkbox Handlers
// =====================
void PasswordGenerator::updateSymbols(bool checked, char symbol)
{
if (checked) {
auto it = std::remove(_symbols->begin(), _symbols->end(), symbol);
if (it != _symbols->end()) {
_symbols->erase(it, _symbols->end()); // Remove symbol if checkbox is checked
}
} else {
if (std::find(_symbols->begin(), _symbols->end(), symbol) == _symbols->end()) {
_symbols->push_back(symbol); // Add symbol back if checkbox is unchecked
}
}
}
// =====================
// Checkbox State Change Handlers
// =====================
void PasswordGenerator::on_checkBox_1_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '&');
}
void PasswordGenerator::on_checkBox_2_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '@');
}
void PasswordGenerator::on_checkBox_3_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '{');
updateSymbols(arg1 == Qt::Checked, '}');
}
void PasswordGenerator::on_checkBox_4_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '#');
}
void PasswordGenerator::on_checkBox_5_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '[');
updateSymbols(arg1 == Qt::Checked, ']');
}
void PasswordGenerator::on_checkBox_6_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '=');
}
void PasswordGenerator::on_checkBox_7_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '(');
updateSymbols(arg1 == Qt::Checked, ')');
}
void PasswordGenerator::on_checkBox_8_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '-');
}
void PasswordGenerator::on_checkBox_9_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '?');
}
void PasswordGenerator::on_checkBox_10_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '`');
}
void PasswordGenerator::on_checkBox_11_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '\\');
}
void PasswordGenerator::on_checkBox_12_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '/');
}
void PasswordGenerator::on_checkBox_13_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '|');
}
void PasswordGenerator::on_checkBox_14_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '*');
}
void PasswordGenerator::on_checkBox_15_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '"');
}
void PasswordGenerator::on_checkBox_16_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, ':');
}
void PasswordGenerator::on_checkBox_17_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, ';');
}
void PasswordGenerator::on_checkBox_18_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '<');
updateSymbols(arg1 == Qt::Checked, '>');
}
void PasswordGenerator::on_checkBox_19_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '~');
}
void PasswordGenerator::on_checkBox_20_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '$');
}
void PasswordGenerator::on_checkBox_21_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '!');
}
void PasswordGenerator::on_checkBox_22_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '%');
}
void PasswordGenerator::on_checkBox_23_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '^');
}
void PasswordGenerator::on_checkBox_24_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '_');
}
void PasswordGenerator::on_checkBox_25_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '+');
}
void PasswordGenerator::on_checkBox_26_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '\'');
}
void PasswordGenerator::on_checkBox_27_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, ',');
}
void PasswordGenerator::on_checkBox_28_stateChanged(int arg1)
{
updateSymbols(arg1 == Qt::Checked, '.');
}