Thursday, 22 January 2015

UppercaseCharacterRule


UppercaseCharacterRule checks whether password contains specified number of upper case characters or not.

UppercaseCharacterRule provides below constructors.

UppercaseCharacterRule()
Default constructor.

UppercaseCharacterRule(int num)
Create a new uppercase character rule. Parameter "num" specifies number of uppercase characters to enforce.

import edu.vt.middleware.password.AllowedCharacterRule;
import edu.vt.middleware.password.AlphabeticalSequenceRule;
import edu.vt.middleware.password.DigitCharacterRule;
import edu.vt.middleware.password.LengthRule;
import edu.vt.middleware.password.LowercaseCharacterRule;
import edu.vt.middleware.password.NonAlphanumericCharacterRule;
import edu.vt.middleware.password.NumericalSequenceRule;
import edu.vt.middleware.password.Password;
import edu.vt.middleware.password.PasswordData;
import edu.vt.middleware.password.PasswordValidator;
import edu.vt.middleware.password.QwertySequenceRule;
import edu.vt.middleware.password.Rule;
import edu.vt.middleware.password.RuleResult;
import edu.vt.middleware.password.UppercaseCharacterRule;
import java.util.ArrayList;
import java.util.List;

public class PasswordCheck {

    private static PasswordValidator getPasswordValidator(){
        /* Rule for allowed charachters */
        char chars[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
                        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                        'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                        'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
                        'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
                        'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                        'Y', 'Z', '_', '@', '#', '!'};
        AllowedCharacterRule allowedCharsRule = new AllowedCharacterRule(chars);

        /* Rule for alphabetic sequence */
        AlphabeticalSequenceRule alphabeticalSequenceRule = new AlphabeticalSequenceRule(4, true);

        /* Rule for number of digits to be in password */
        DigitCharacterRule digitCharacterRule = new DigitCharacterRule();

        /* Rule for password length */
        LengthRule lengthRule = new LengthRule(8, 15);

        /* Add lowercase character rule */
        LowercaseCharacterRule lowercaseCharacterRule = new LowercaseCharacterRule();

        /* Add non-alphanumberic character rule */
        NonAlphanumericCharacterRule nonAlphanumericCharacterRule = new NonAlphanumericCharacterRule(2);

        /* Add numerical sequence rule */
        NumericalSequenceRule numericalSequenceRule = new NumericalSequenceRule(3, true);

         /* Add qwerty sequence rule */
        QwertySequenceRule qwertySequenceRule = new QwertySequenceRule(3, true);

        /* Add uppercase character rule */
        UppercaseCharacterRule uppercaseCharacterRule = new UppercaseCharacterRule();
        
        /* Add rules to ruleList */
        List<Rule> ruleList = new ArrayList<Rule>();
        ruleList.add(allowedCharsRule);
        ruleList.add(alphabeticalSequenceRule);
        ruleList.add(digitCharacterRule);
        ruleList.add(lengthRule);
        ruleList.add(lowercaseCharacterRule);
        ruleList.add(nonAlphanumericCharacterRule);
        ruleList.add(numericalSequenceRule);
        ruleList.add(qwertySequenceRule);
        ruleList.add(uppercaseCharacterRule);

        PasswordValidator validator = new PasswordValidator(ruleList);

        return validator;
    }

    public static boolean isPasswordValid(String password){
        PasswordData passwordData = new PasswordData(new Password(password));
        RuleResult result = getPasswordValidator().validate(passwordData);
        return result.isValid();
    }
    
    public static void main(String args[]){
       System.out.println("Is password valid : " + isPasswordValid("asdfghjkl"));
       System.out.println("Is password valid : " + isPasswordValid("asdfghjkl%$"));
       System.out.println("Is password valid : " + isPasswordValid("xyzabc"));
       System.out.println("Is password valid : " + isPasswordValid("123abc"));
       System.out.println("Is password valid : " + isPasswordValid("123ABC123"));
       System.out.println("Is password valid : " + isPasswordValid("123AbC123"));
       System.out.println("Is password valid : " + isPasswordValid("asd!123@"));
       System.out.println("Is password valid : " + isPasswordValid("asd!901@"));
       System.out.println("Is password valid : " + isPasswordValid("asd!019@"));
       System.out.println("Is password valid : " + isPasswordValid("as!019@d"));

       System.out.println("Is password valid : " + isPasswordValid("as!019@D"));
    }
}


Output
Is password valid : false
Is password valid : false
Is password valid : false
Is password valid : false
Is password valid : false
Is password valid : false
Is password valid : false
Is password valid : false
Is password valid : false
Is password valid : false
Is password valid : true

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment