Thursday, 22 January 2015

AlphabeticalSequenceRule


AlphabeticalSequenceRule checks, whether password contains alphabetic sequence like (abcde, VWXYZ etc.). The default sequence length is 5 characters. It checks both upper case and lowercase letters.

AlphabeticalSequenceRule()
Matches default sequence of length 5.

AlphabeticalSequenceRule(int sequenceLength, boolean wrap)
Matches sequence, parameter "sequenceLength" specifies sequence length and "wrap" specifies whether to wrap search sequences.
If "wrap" is set to true, then it match sequences like "yzabc".

import edu.vt.middleware.password.AllowedCharacterRule;
import edu.vt.middleware.password.AlphabeticalSequenceRule;
import edu.vt.middleware.password.Password;
import edu.vt.middleware.password.PasswordData;
import edu.vt.middleware.password.PasswordValidator;
import edu.vt.middleware.password.Rule;
import edu.vt.middleware.password.RuleResult;
import java.util.ArrayList;
import java.util.List;

public class PasswordCheck {

    private static PasswordValidator getPasswordValidator(){
        /* Rules 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);

        /* Rules for alphabetic sequence */
        AlphabeticalSequenceRule alphabeticalSequenceRule = new AlphabeticalSequenceRule(6, true);

        

        List<Rule> ruleList = new ArrayList<Rule>();
        ruleList.add(allowedCharsRule);
        ruleList.add(alphabeticalSequenceRule);

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


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

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment