-
Notifications
You must be signed in to change notification settings - Fork 0
/
passwordgenerator.cpp
119 lines (92 loc) · 3.79 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
#include "passwordgenerator.h"
#include "ui_passwordgenerator.h"
#define qRand QRandomGenerator::global()
PasswordGenerator::PasswordGenerator(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::PasswordGenerator)
{
ui->setupUi(this);
{
QPalette darkPalette;
darkPalette.setColor (QPalette::BrightText, Qt::red);
darkPalette.setColor (QPalette::WindowText, Qt::white);
darkPalette.setColor (QPalette::ToolTipBase, Qt::white);
darkPalette.setColor (QPalette::ToolTipText, Qt::white);
darkPalette.setColor (QPalette::Text, Qt::white);
darkPalette.setColor (QPalette::ButtonText, Qt::white);
darkPalette.setColor (QPalette::HighlightedText, Qt::black);
darkPalette.setColor (QPalette::Window, QColor (53, 53, 53));
darkPalette.setColor (QPalette::Base, QColor (25, 25, 25));
darkPalette.setColor (QPalette::AlternateBase, QColor (53, 53, 53));
darkPalette.setColor (QPalette::Button, QColor (53, 53, 53));
darkPalette.setColor (QPalette::Link, QColor (42, 130, 218));
darkPalette.setColor (QPalette::Highlight, QColor (42, 130, 218));
qApp->setPalette(darkPalette);
}
connect(ui->QPB_generatePassword, &QPushButton::clicked,
this, &PasswordGenerator::SLOT_generatePasswords);
connect(ui->QPB_clear, &QPushButton::clicked,
this, &PasswordGenerator::SLOT_clear);
ui->QPTE_listOfPasswords->setLineWrapMode(QPlainTextEdit::NoWrap);
ui->QPTE_listOfPasswords->setWordWrapMode(QTextOption::NoWrap);
}
PasswordGenerator::~PasswordGenerator()
{
delete ui;
}
void PasswordGenerator::SLOT_generatePasswords()
{
const uchar
minimum = ui->QSB_minimum->value(),
maximum = ui->QSB_maximum->value();
if(minimum > maximum){
ui->QSB_maximum->setValue(minimum);
ui->QPTE_listOfPasswords->setPlainText("Error: minimum length bigger "
"than maximum length");
return;
}
QVector<char> charAlowed = QVector<char>();
if(ui->QCB_smallLetters->isChecked())
charAlowed.append(smallLetters.toVector());
if(ui->QCB_capitalLetters->isChecked())
charAlowed.append(capitalLetters.toVector());
if(ui->QCB_numbers->isChecked())
charAlowed.append(numbers.toVector());
if(ui->QCB_specialCharacters->isChecked())
charAlowed.append(specialCharacters.toVector());
if(charAlowed.isEmpty())
{
ui->QPTE_listOfPasswords->setPlainText("Error: Select at Least one"
"type of character");
return;
}
ui->QPTE_listOfPasswords->clear();
generatePasswords(minimum, maximum, ui->QSB_numberOfPasswords->value(), charAlowed);
}
void PasswordGenerator::SLOT_clear()
{
ui->QPTE_listOfPasswords->clear();
}
void PasswordGenerator::generatePasswords(const uchar& minimum,
const uchar& maximum,
const uchar& numberOfPasswords,
const QVector<char>& charAlowed)
{
const uchar numberOfCharacters = charAlowed.size();
for (uchar i=0; i<numberOfPasswords; i++)
{
uchar length = qRand->bounded(minimum, (maximum+1));
QString QS_password = "";
QS_password.reserve(length);
for(uchar j=0; j<length; j++)
{
QS_password.append(
charAlowed.at(
qRand->bounded(0, numberOfCharacters)
)
);
}
ui->QPTE_listOfPasswords->appendPlainText(QS_password);
ui->QPTE_listOfPasswords->appendPlainText("\n");
}
}