有些应用程序(或网站)在您输入密码时会计算其复杂性。
它们通常会显示一个红色的条形图,当您的密码变得更长,并包含更多类别的字符(例如小写字母、大写字母、标点符号、数字)时,该条形图会变成橙色、绿色或者更绿。
如何可靠地计算密码的复杂性?
我想出了以下算法,但我担心它将Password1!
评为“非常强”,而将]@feé:m
评为“弱”,因为它只有7个字符。
private int GetPasswordComplexity(string password)
{
if (password.Length <= 4)
return 1;
int complexity = 0;
int digit = 0;
int letter = 0;
int cap = 0;
int other = 0;
for (int i = 0; i < password.Length; i++)
{
if (char.IsDigit(password[i]) && i!=password.Length-1)
digit = 1;
else if (char.IsLower(password[i]))
letter = 1;
else if (char.IsUpper(password[i]) && i!=0)
cap = 1;
else
other = 1;
}
complexity = digit + letter + cap + other;
if (password.Length <= 7)
complexity = Math.Min(3, complexity);
return complexity;
}