-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_generator.cpp
72 lines (62 loc) · 1.97 KB
/
password_generator.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
#include <iostream>
#include <random>
#include <string>
// Returns a score indicating the strength of the given password.
// A higher score indicates a stronger password.
int score_password(const std::string& password) {
int score = 0;
// Increase the score for longer passwords.
score += password.length() * 4;
// Check for the presence of lowercase letters, uppercase letters, digits, and special characters.
bool has_lower = false;
bool has_upper = false;
bool has_digit = false;
bool has_special = false;
for (char c : password) {
if (std::islower(c)) {
has_lower = true;
} else if (std::isupper(c)) {
has_upper = true;
} else if (std::isdigit(c)) {
has_digit = true;
} else if (!std::isalnum(c)) {
has_special = true;
}
}
score += (has_lower + has_upper + has_digit + has_special) * 10;
// Check for common patterns that might make the password easier to guess.
// For example, "password" and "qwerty" are common patterns to avoid.
if (password.find("password") != std::string::npos) {
score -= 50;
}
if (password.find("qwerty") != std::string::npos) {
score -= 50;
}
return score;
}
std::string generate_password(size_t length, const std::string& charset) {
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_int_distribution<int> distribution(0, charset.size() - 1);
std::string password;
while (true) {
password.clear();
for (size_t i = 0; i < length; ++i) {
password += charset[distribution(generator)];
}
if (score_password(password) >= 80) {
return password;
}
}
}
int main() {
std::cout << "Enter password length: ";
int length;
std::cin >> length;
std::cout << "Enter character set: ";
std::string charset;
std::cin >> charset;
std::string password = generate_password(length, charset);
std::cout << "Generated password: " << password << std::endl;
return 0;
}