-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordGenerator.cpp
More file actions
178 lines (147 loc) · 5.35 KB
/
Copy pathPasswordGenerator.cpp
File metadata and controls
178 lines (147 loc) · 5.35 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
#include <iostream>
#include <string>
#include <set>
#include <sstream>
#include <cstdlib>
#include <ctime>
int checkPasswordLength(std::string str) {
//error checking for whatever's input for passwordLength
int passwordLength;
try {
passwordLength = std::stoi(str);
}
catch (...) {
std::cout << "Invalid password length." << std::endl;
return -1;
}
if (passwordLength<1||passwordLength>50) {
std::cout << "Invalid password length." << std::endl;
return -1;
}
return passwordLength;
}
int getPasswordLength() {
std::string str;
std::cout << "Password length: ";
getline(std::cin, str);
return checkPasswordLength(str);
}
std::set<int> getOptions() {
std::cout << "Character set options:" << std::endl;
std::cout << " Lowercase (i.e. abcdef) - LC" << std::endl;
std::cout << " Uppercase (i.e. ABCDEF) - UC" << std::endl;
std::cout << " Digits (i.e. 012345) - DG" << std::endl;
std::cout << " Symbols (i.e. !@<{, ) - SM" << std::endl;
std::cout << " All - AL" << std::endl << std::endl;
std::cout << "You may choose All or any combination of the first 5 options. Type all desired options separated by a space, then hit \"ENTER\"." << std::endl << std::endl;
std::cout << "Option(s): ";
std::string optionInput;
getline(std::cin, optionInput);
//interpret input
std::set<int> options;
std::istringstream iss(optionInput);
std::string token;
while (iss >> token) {
if (token=="LC"||token=="lc") {
options.insert(1);
}
else if (token=="UC"||token=="uc") {
options.insert(2);
}
else if (token=="DG"||token=="dg") {
options.insert(3);
}
else if (token=="SM"||token=="sm") {
options.insert(4);
}
else if (token=="AL"||token=="al") {
options.insert(0);
}
else {
std::cout << "Invalid option." << std::endl;
exit(1);
}
}
return options;
}
std::string generateRandCharSet(std::set<int>* options, int passwordLength) {
std::string characterSets = "";
while (characterSets.length()!=passwordLength) {
int result = 1 + (rand() % 4);
if (result==1&&(options->find(1)!=options->end()||options->find(0)!=options->end())) {
characterSets+=std::to_string(result);
}
else if (result==2&&(options->find(2)!=options->end()||options->find(0)!=options->end())) {
characterSets+=std::to_string(result);
}
else if (result==3&&(options->find(3)!=options->end()||options->find(0)!=options->end())) {
characterSets+=std::to_string(result);
}
else if (result==4&&(options->find(4)!=options->end()||options->find(0)!=options->end())) {
characterSets+=std::to_string(result);
}
}
return characterSets;
}
std::string generatePassword(std::set<int>* options, int passwordLength) {
std::string characterSets;
//generates a string with the length of passwordLength where each digit is a random approved character set for the password
//i.e. 224332 is approved if they chose lowercase, uppercase, and digits
bool error = true;
while (error) {
characterSets = generateRandCharSet(options, passwordLength);
error = false;
if ((options->find(1)!=options->end()||options->find(0)!=options->end())&&characterSets.find("1")==std::string::npos) {
error = true;
}
if ((options->find(2)!=options->end()||options->find(0)!=options->end())&&characterSets.find("2")==std::string::npos) {
error = true;
}
if ((options->find(3)!=options->end()||options->find(0)!=options->end())&&characterSets.find("3")==std::string::npos) {
error = true;
}
if ((options->find(4)!=options->end()||options->find(0)!=options->end())&&characterSets.find("4")==std::string::npos) {
error = true;
}
}
std::string lowercase = "abcdefghijklmnopqrstuvwxyz";
std::string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string digits = "0123456789";
std::string symbols = " ~!@#$%^&*()-=+[{]}\\;:'""/?.>,<";
//generates the password by choosing a random character from each character set in the string characterSets
std::string password = "";
for (int i = 0; i < passwordLength; i++) {
if (characterSets[i]=='1') {
password+=lowercase[rand() % lowercase.length()];
}
else if (characterSets[i]=='2') {
password+=uppercase[rand() % uppercase.length()];
}
else if (characterSets[i]=='3') {
password+=digits[rand() % digits.length()];
}
else if (characterSets[i]=='4') {
password+=symbols[rand() % symbols.length()];
}
}
return password;
}
int main(int argc, char* argv[]) {
int passwordLength;
if (argc>1) {
passwordLength = checkPasswordLength(argv[1]);
}
else {
passwordLength = getPasswordLength();
}
if (passwordLength==-1) {
return 1;
}
std::set<int> options = getOptions();
srand((unsigned) time(0));
std::cout << std::endl;
for (int i = 0; i < 10; i++) {
std::cout << generatePassword(&options, passwordLength) << std::endl;
}
return 0;
}