-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
77 lines (63 loc) · 2.4 KB
/
main.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
73
74
75
76
77
#include <iostream>
#include <fstream>
#include <cmath>
#include <set>
#include <string>
// ANSI escape codes for colors
#define RESET "\033[0m" // Reset to default color
#define RED "\033[31m" // Red text
#define YELLOW "\033[33m" // Yellow text
#define GREEN "\033[32m" // Green text
double calculateEntropy(const std::string& password) {
int length = password.length();
std::set<char> uniqueChars(password.begin(), password.end());
int characterSetSize = uniqueChars.size();
return length * log2(characterSetSize);
}
bool isCommonPassword(const std::string& password, const std::string& filePath) {
std::ifstream file(filePath);
if (!file.is_open()) {
std::cerr << "Error: Unable to open the file at " << filePath << "\n";
return false; // Handle gracefully or exit
}
std::string commonPassword;
while (std::getline(file, commonPassword)) {
if (password == commonPassword) {
return true;
}
}
return false;
}
void evaluatePassword(const std::string& password, const std::string& commonPasswordFile) {
double entropy = calculateEntropy(password);
std::cout << "\nPassword Entropy: " << entropy << " bits\n";
if (isCommonPassword(password, commonPasswordFile)) {
std::cout << RED << "Warning: Your password is commonly used and weak!\n" << RESET;
} else {
std::cout << GREEN << "Good: Your password is not in the common passwords list.\n" << RESET;
}
// Password strength evaluation with colors
if (entropy < 28) {
std::cout << RED << "Password Strength: Weak\n" << RESET;
} else if (entropy < 36) {
std::cout << YELLOW << "Password Strength: Moderate\n" << RESET;
} else {
std::cout << GREEN << "Password Strength: Strong\n" << RESET;
}
std::cout << "-------------------------------------------------------\n\n";
}
int main() {
std::string password;
// Set the file path (adjust accordingly)
std::string commonPasswordFile = "common_passwords.txt";
while (true) {
std::cout << "Enter your password (or type 'exit' to quit): ";
std::cin >> password;
if (password == "exit") {
std::cout << "Exiting the program. Goodbye!\n";
break;
}
evaluatePassword(password, commonPasswordFile);
}
return 0;
}