-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
145 lines (126 loc) · 3.4 KB
/
Copy pathmain.cpp
File metadata and controls
145 lines (126 loc) · 3.4 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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#ifdef __linux__
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#elif _WIN32
#include <conio.h>
#endif
using namespace std;
string pool = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz123456789!@#$%^&*()";
string upp = "ABCDEFGHJKLMNPQRSTUVWXYZ";
string low = "abcdefghijkmnopqrstuvwxyz";
string num = "123456789";
string sym = "!@#$%^&*()";
string shuffle(string input) {
string output = "";
while (input.size() > 0) {
int idx = rand() % input.size();
output += input[idx];
input = input.substr(0, idx) + input.substr(idx + 1);
}
return output;
}
string generate(int length) {
pool = shuffle(pool);
upp = shuffle(upp);
low = shuffle(low);
num = shuffle(num);
sym = shuffle(sym);
string password = "";
int mod = pool.size();
password += upp[rand() % upp.size()];
password += low[rand() % low.size()];
password += num[rand() % num.size()];
password += sym[rand() % sym.size()];
length -= 4;
while (length > 0) {
password += pool[rand() % mod];
length --;
}
password = shuffle(password);
return password;
}
bool try_parse_int(string input) {
for (char c : input)
if (c < '0' or c > '9')
return false;
return true;
}
int parse_int(string input) {
int parsed = 0;
for (char c : input) {
parsed *= 10;
parsed += c - '0';
}
return parsed;
}
#ifdef __linux__
char getch(void) {
struct termios oldt, newt;
char ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
#endif
int main() {
srand(time(nullptr));
while (true) {
string input;
int length;
#ifdef __linux__
system("clear");
#elif _WIN32
system("cls");
#endif
cout << "Enter the password length: ";
getline(cin, input);
while (try_parse_int(input) == false or parse_int(input) < 8) {
#ifdef __linux__
system("clear");
#elif _WIN32
system("cls");
#endif
cout << "An error occurred! Possible causes:" << endl;
cout << " Your input is not a number" << endl;
cout << " Your input is a number less than 8" << endl << endl;
cout << "Enter the password length: ";
getline(cin, input);
}
length = parse_int(input);
while (true) {
#ifdef __linux__
system("clear");
#elif _WIN32
system("cls");
#endif
cout << "Generated password: " << generate(length) << endl;
cout << "r - regenerate" << endl;
cout << "m - return to the menu" << endl;
cout << "q - quit" << endl;
cout << "Enter your option... ";
char opt = getch();
if (opt == 'q') {
cout << endl;
return 0;
}
while ((opt == 'r' or opt == 'm' or opt == 'q') == false)
opt = getch();
if (opt == 'q') {
cout << endl;
return 0;
}
else if (opt == 'm')
break;
}
}
return 0;
}