Thursday, 22 January 2015

Generate secure and random password


In this post I am going to explain how to generate secure and random passwords using vtpassword library. VT Password is a Java library for verifying that a password meets specified ruleset, and also you can define your own rules and generate passwords based on the rules.

Program I am going to explain used below rules to generate password.

DigitCharacterRule : Rule for determining if a password contains the correct number of digit characters.

LowercaseCharacterRule : Rule for determining if a password contains the correct number of lowercase characters.

NonAlphanumericCharacterRule : Rule for determining if a password contains the correct number of non-alphanumeric characters.

UppercaseCharacterRule : Rule for determining if a password contains the correct number of uppercase characters.

import edu.vt.middleware.password.CharacterRule;
import edu.vt.middleware.password.DigitCharacterRule;
import edu.vt.middleware.password.LowercaseCharacterRule;
import edu.vt.middleware.password.NonAlphanumericCharacterRule;
import edu.vt.middleware.password.PasswordGenerator;
import edu.vt.middleware.password.UppercaseCharacterRule;
import java.util.ArrayList;
import java.util.List;

public class PasswordCheck {

    private static List<CharacterRule> getPasswordRules(){
        /* Rule for number of digits to be in password */
        DigitCharacterRule digitCharacterRule = new DigitCharacterRule();

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

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

        /* Add uppercase character rule */
        UppercaseCharacterRule uppercaseCharacterRule = new UppercaseCharacterRule();
        
        /* Add rules to ruleList */
        List<CharacterRule> ruleList = new ArrayList<CharacterRule>();
        ruleList.add(digitCharacterRule);
        ruleList.add(lowercaseCharacterRule);
        ruleList.add(nonAlphanumericCharacterRule);
        ruleList.add(uppercaseCharacterRule);

        return ruleList;
    }

    public static String getPasswordByRules(){
        PasswordGenerator generator = new PasswordGenerator();
        return generator.generatePassword(10, getPasswordRules());
    }

    public static void main(String args[]){
        for(int i=0; i<10; i++)
            System.out.println(getPasswordByRules());
    }
}


Sample Output
3j>r.|N&Y!
66(S*Y68oh
2;=pWl='XT
"8Y>oxrZ%(
sQ5L,n-4lt
$l4)X{ImM3
^6+(dH9@$Q
;LC"nDJ6]<
Q\4fm?j;C1
w=Fo\~Npd8


PasswordGenerator generator = new PasswordGenerator();
PasswordGeneror creates passwords that meet password character rule criteria.

generator.generatePassword(10, getPasswordRules());
Generates a password of 10 characters in length, which meets the requirements of the supplied character rules.

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment