Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify that the password contains X uppercase letters and Y numbers?

How do I verify in C# that the password contains at least X uppercase letters and at least Y numbers, and the entire string is longer than Z?

Thanks.

like image 455
SexyMF Avatar asked Dec 07 '11 11:12

SexyMF


People also ask

What is uppercase and lowercase letter in password example?

Uppercase characters (A-Z) Lowercase characters (a-z)

How do you check if a character is a capital letter?

isUpperCase(char ch) determines if the specified character is an uppercase character. A character is uppercase if its general category type, provided by Character.

How do you check if a password has a capital letter python?

You can check all characters in the string with a list comprehension: contains_upper_case_character = any([letter. isupper() for letter in password]) # contains_upper_case_character is True if password contains an uppercase letter.

Which are the upper and lower case letters?

Uppercase letters are capital letters—the bigger, taller versions of letters (like W), as opposed to the smaller versions, which are called lowercase letters (like w). Uppercase means the same thing as capital.


1 Answers

Password Strength:

First, I would read up on password strength, and double-check your policy to make sure you were doing the right thing (I couldn't tell you off hand):

  • http://en.wikipedia.org/wiki/Password_strength
  • https://www.grc.com/haystack.htm
  • http://xkcd.com/936/ (a joke, but good food for thought)

Then I'd check other questions:

  • Creating a regex to check for a strong password
  • Password strength

Then I'd get down to business.

Implementation:

You could use Linq:

return password.Length >= z
    && password.Where(char.IsUpper).Count() >= x
    && password.Where(char.IsDigit).Count() >= y
    ;

You could use also regular expressions (which might be a good option to allow you to plug in more complicated validations in the future):

return password.Length >= z
    && new Regex("[A-Z]").Matches(password).Count >= x
    && new Regex("[0-9]").Matches(password).Count >= y
    ;

Or you could mix and match them.

If you had to do this multiple times, you could reuse the Regex instances by building a class:

public class PasswordValidator
{
    public bool IsValid(string password)
    {
        return password.Length > MinimumLength
            && uppercaseCharacterMatcher.Matches(password).Count
                >= FewestUppercaseCharactersAllowed
            && digitsMatcher.Matches(password).Count >= FewestDigitsAllowed
            ;
    }

    public int FewestUppercaseCharactersAllowed { get; set; }
    public int FewestDigitsAllowed { get; set; }
    public int MinimumLength { get; set; }

    private Regex uppercaseCharacterMatcher = new Regex("[A-Z]");
    private Regex digitsMatcher = new Regex("[a-z]");
}

var validator = new PasswordValidator()
{
    FewestUppercaseCharactersAllowed = x,
    FewestDigitsAllowed = y,
    MinimumLength = z,
};

return validator.IsValid(password);
like image 84
Merlyn Morgan-Graham Avatar answered Nov 15 '22 20:11

Merlyn Morgan-Graham