如何检查密码强度?强度、密码

由网友(Secret Weapon)分享简介:我如何检查密码的强度(作为字符串)使用.NET Framework?How can I check the strength of a password (as a string) using the .Net Framework?推荐答案但基本逻辑之一:enum PasswordScore{Blank =...

我如何检查密码的强度(作为字符串)使用.NET Framework?

How can I check the strength of a password (as a string) using the .Net Framework?

推荐答案

但基本逻辑之一:

enum PasswordScore
{
    Blank = 0,
    VeryWeak = 1,
    Weak = 2,
    Medium = 3,
    Strong = 4,
    VeryStrong = 5
}

public class PasswordAdvisor
{
    public static PasswordScore CheckStrength(string password)
    {
        int score = 1;

        if (password.Length < 1)
            return PasswordScore.Blank;
        if (password.Length < 4)
            return PasswordScore.VeryWeak;

        if (password.Length >= 8)
            score++;
        if (password.Length >= 12)
            score++;
        if (Regex.Match(password, @"/d+/", RegexOptions.ECMAScript))
            score++;
        if (Regex.Match(password, @"/[a-z]/", RegexOptions.ECMAScript) &&
            Regex.Match(password, @"/[A-Z]/", RegexOptions.ECMAScript))
            score++;
        if (Regex.Match(password, @"/.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]/",  RegexOptions.ECMAScript))
            score++;

        return (PasswordScore)score;
    }
}

参考: http://passwordadvisor.com/$c$cAspNet.aspx

阅读全文

相关推荐

最新文章