C++安全密码生成与强度检测
目标
密码生成
// Function to generate a random password
std::string generatePassword(int length, bool includeUpper, bool includeNumbers, bool includeSymbols) {
std::string lower = "abcdefghijklmnopqrstuvwxyz";
std::string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string numbers = "0123456789";
std::string symbols = "!@#$%^&*()_+-=[]{}|;:',.<>?/`~";
std::string characters = lower;
if (includeUpper) characters += upper;
if (includeNumbers) characters += numbers;
if (includeSymbols) characters += symbols;
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_int_distribution<> dist(0, characters.size() - 1);
std::string password;
for (int i = 0; i < length; ++i) {
password += characters[dist(generator)];
}
return password;
}
def generate_strong_password(length):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
return password
密码检测
// Function to assess password strength
std::string assessPasswordStrength(const std::string& password, bool includeUpper, bool includeNumbers, bool includeSymbols) {
int length = password.length();
bool hasLower = false, hasUpper = false, hasDigit = false, hasSymbol = false;
for (char c : password) {
if (islower(c)) hasLower = true;
if (isupper(c)) hasUpper = true;
if (isdigit(c)) hasDigit = true;
if (ispunct(c)) hasSymbol = true;
}
// Determine password strength
if (length >= 12 && hasLower && hasUpper && hasDigit && hasSymbol) {
return GREEN; // Strong
} else if (length >= 8 && ((hasUpper && hasLower) || (hasLower && hasDigit) || (hasUpper && hasDigit))) {
return YELLOW; // Medium
} else {
return RED; // Weak
}
}
效果
pass.exe -l 12 -u -n -s
i7{wQx?qkr-<
参考
GitHub - anlaki-py/pass-gen: Simple/secure password generator that you can quickly run everywhere in your terminal.
C++密码安全检测-CSDN博客