Repository navigation
Expand file tree
/
Copy pathgen.cpp
More file actions
70 lines (63 loc) · 2.32 KB
/
Copy pathgen.cpp
File metadata and controls
70 lines (63 loc) · 2.32 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
#include "gen.h"
#include <random>
#include <string>
#include <set>
Gen::Gen(int length, bool symbols, bool numbers, bool lowercase, bool uppercase, bool similar, bool ambigous) :
m_length{ length }, m_symbols{ symbols }, m_numbers{ numbers }, m_lowercase{ lowercase }, m_uppercase{ uppercase }, m_no_similar{ similar }, m_no_ambigous{ ambigous }
{}
//insert characters (chars_to_insert) to m_chars (m_chars are characters used during generating password)
void Gen::insertChars(bool chars_type, std::string chars_to_insert)
{
if (chars_type)
{
for (char temp_char : chars_to_insert)
{
m_chars.insert(temp_char);
}
}
}
//erase characters (chars_to_erase) from m_chars (m_chars are characters used during generating password)
void Gen::eraseChars(bool chars_type, std::string chars_to_erase)
{
if (chars_type)
{
for (char temp_char : chars_to_erase)
{
m_chars.erase(temp_char);
}
}
}
//modify m_chars according to password properties settings
void Gen::modifyChars()
{
insertChars(m_lowercase, m_low);
insertChars(m_uppercase, m_upp);
insertChars(m_numbers, m_num);
insertChars(m_symbols, m_sym);
eraseChars(m_no_similar, m_sim);
eraseChars(m_no_ambigous, m_amb);
}
void Gen::generatePassword()
{
m_chars.clear(); //clear previously used characters
modifyChars(); //set up m_chars
if (m_chars.empty()) return; //if all possible characters are excluded (m_chars is empty) password is not generated
std::random_device rd; //create random device
std::mt19937 mersenne(rd()); //create mersenne twister using random device as a seed
std::uniform_int_distribution<> rand(33, 125); //create random number generator that generates numbers between 33 and 125 (possible ASCII characters)
m_password.clear(); //clear previously generated password
for (int i = 0; i < m_length; ++i)
{
int temp = rand(mersenne); //generate random number here (ASCII code between 33 and 125)
if (m_chars.find(temp) != m_chars.end()) //if character with generated code is in m_chars set, push it back to m_password
{
m_password.push_back(temp);
}
else --i; //in case there is no such character in m_chars, another iteration will be needed
}
}
void Gen::slotGeneratePassword()
{
generatePassword(); //generate password
emit signalPassword(m_password); //emit generated password
}