-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassword_Generator.cpp
More file actions
174 lines (96 loc) · 3.6 KB
/
Password_Generator.cpp
File metadata and controls
174 lines (96 loc) · 3.6 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
#include <iostream>
#include <string>
#include <random>
#include <vector>
#include <algorithm>
#include <chrono>
#include <ctime>
#include <thread>
#include <cctype>
#include <cstdlib>
#include <fstream>
//typewriter effect
void tprt(const std::string& text, int delay_ms = 30){
for (const char& c : text) {
std::cout << c << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
}
}
void clearScreen() {
// Clear the console screen (platform-dependent)
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
int PSWC(std::string Password){
int score=0;
bool hasUpper=false, hasLower=false, hasDigit=false, hasSpecial=false;
if(Password.length()>=8) score++;
for(char z: Password){
if(std::isupper(z)) hasUpper=true;
else if(std::islower(z)) hasLower=true;
else if(std::isdigit(z)) hasDigit=true;
else hasSpecial=true;
}
if(hasUpper) score++;
if(hasLower) score++;
if(hasDigit) score++;
if(hasSpecial) score++;
return score;
}
void PasswordGenerator(const int& passwordLength) {
const std::string CharacterStore = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=[]{}|;:,.<>?";
std::string FinalPassword; // Declare here so it is accessible after the loop
std::vector<char> GeneratedPassword;
int score = 0;
std::vector<std::string> blacklist = {"password123", "12345678", "admin123"};
// This loop "makes it again" if the strength is less than 5
while (score < 5) {
GeneratedPassword.clear(); // Clear the previous attempt
for (int i = 0; i < passwordLength; ++i) {
char randomChar = CharacterStore[rand() % CharacterStore.size()];
GeneratedPassword.push_back(randomChar);
}
// Convert vector to string
FinalPassword = std::string(GeneratedPassword.begin(), GeneratedPassword.end());
// Check the strength and update the score
score = PSWC(FinalPassword);
auto it = std::find(blacklist.begin(), blacklist.end(), FinalPassword);
if (it != blacklist.end()) {
score = 0; // Reset score to trigger a retry
}
}
// Now FinalPassword is guaranteed to have a score of 5
tprt("Generated Password: \033[30m" + FinalPassword + "\033[0m\n");
}
int main() {
srand(time(0));
int passwordLength;
tprt("Welcome to the Password Generator!\n");
tprt("Before you continue, please note that the genarated passwords will be hidden on screen but copypasteable.\n");
std::string choice="y";
while(choice=="y" || choice=="Y"){
tprt("Please enter the desired password length (minimum 8 characters): ");
std::cin>>passwordLength;
if(passwordLength < 8){
tprt("\033[4;31mERROR: Password length must be at least 8 characters. Setting to 8.\033[0m\n");
passwordLength = 8;
}
PasswordGenerator(passwordLength);
std::this_thread::sleep_for(std::chrono::seconds(5));
tprt("Do you want to generate another password? (y/n):\033[36m ");
std::cin >> choice;
clearScreen();
}
tprt("\033[32mThank you for using the Password Generator! Goodbye!\033[0m\n");
std::this_thread::sleep_for(std::chrono::seconds(2));
clearScreen();
for(int i = 5; i > 0; i--) {
std::cout << "Closing in " + std::to_string(i) + " seconds...\n";
std::this_thread::sleep_for(std::chrono::seconds(1));
clearScreen();
}
return 0;
}