Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check password strength? [closed]

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

like image 614
Maciej Avatar asked May 27 '11 11:05

Maciej


1 Answers

Basic but a logical one:

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;
    }
}

Ref: http://passwordadvisor.com/CodeAspNet.aspx

like image 103
Teoman Soygul Avatar answered Sep 22 '22 12:09

Teoman Soygul