Passay −CharacterRule

Passay – CharacterRule ”; Previous Next CharacterRule helps in defining a set of characters and minimum no. of characters required in a password. Example The below example shows the validation of a password against above policy using Passay library. import java.util.ArrayList; import java.util.List; import org.passay.CharacterRule; import org.passay.EnglishCharacterData; import org.passay.LengthRule; import org.passay.PasswordData; import org.passay.PasswordValidator; import org.passay.Rule; import org.passay.RuleResult; import org.passay.WhitespaceRule; public class PassayExample { public static void main(String[] args) { List<Rule> rules = new ArrayList<>(); //Rule 1: Password length should be in between //8 and 16 characters rules.add(new LengthRule(8, 16)); //Rule 2: No whitespace allowed rules.add(new WhitespaceRule()); //Rule 3.a: At least one Upper-case character rules.add(new CharacterRule(EnglishCharacterData.UpperCase, 1)); //Rule 3.b: At least one Lower-case character rules.add(new CharacterRule(EnglishCharacterData.LowerCase, 1)); //Rule 3.c: At least one digit rules.add(new CharacterRule(EnglishCharacterData.Digit, 1)); //Rule 3.d: At least one special character rules.add(new CharacterRule(EnglishCharacterData.Special, 1)); PasswordValidator validator = new PasswordValidator(rules); PasswordData password = new PasswordData(“Microsoft@123”); RuleResult result = validator.validate(password); if(result.isValid()){ System.out.println(“Password validated.”); } else { System.out.println(“Invalid Password: ” + validator.getMessages(result)); } } } Output Password validated. Print Page Previous Next Advertisements ”;

Passay − NumberRangeRule

Passay – NumberRangeRule ”; Previous Next NumberRangeRule allows to specify the range of numbers which are not allowed in a password. Consider the following example. Example import org.passay.IllegalCharacterRule; import org.passay.NumberRangeRule; import org.passay.PasswordData; import org.passay.PasswordValidator; import org.passay.RuleResult; import org.passay.WhitespaceRule; public class PassayExample { public static void main(String[] args) { //Rule: Special characters like &, <, > are not allowed in a password IllegalCharacterRule illegalCharacterRule = new IllegalCharacterRule(new char[] {”&”, ”<”, ”>”}); //Rule: 1 to 5 numbers are not allowed NumberRangeRule numberRangeRule = new NumberRangeRule(1, 5); //Rule: White spaces are not allowed WhitespaceRule whitespaceRule = new WhitespaceRule(); PasswordValidator validator = new PasswordValidator(illegalCharacterRule,numberRangeRule,whitespaceRule); PasswordData password = new PasswordData(“abc&4d ef6”); RuleResult result = validator.validate(password); if(result.isValid()){ System.out.println(“Password validated.”); } else { System.out.println(“Invalid Password: ” + validator.getMessages(result)); } } } Output Invalid Password: [ Password contains the illegal character ”&”., Password contains the number ”4”., Password contains a whitespace character.] Print Page Previous Next Advertisements ”;

Passay − lllegalCharacterRule

Passay – IllegalCharacterRule ”; Previous Next IllegalCharacterRule allows to specify the characters which are not allowed in a password. Consider the following example. Example import org.passay.IllegalCharacterRule; import org.passay.NumberRangeRule; import org.passay.PasswordData; import org.passay.PasswordValidator; import org.passay.RuleResult; import org.passay.WhitespaceRule; public class PassayExample { public static void main(String[] args) { //Rule: Special characters like &, <, > are not allowed in a password IllegalCharacterRule illegalCharacterRule = new IllegalCharacterRule(new char[] {”&”, ”<”, ”>”}); //Rule: 1 to 5 numbers are not allowed NumberRangeRule numberRangeRule = new NumberRangeRule(1, 5); //Rule: White spaces are not allowed WhitespaceRule whitespaceRule = new WhitespaceRule(); PasswordValidator validator = new PasswordValidator(illegalCharacterRule,numberRangeRule,whitespaceRule); PasswordData password = new PasswordData(“abc&4d ef6”); RuleResult result = validator.validate(password); if(result.isValid()){ System.out.println(“Password validated.”); }else{ System.out.println(“Invalid Password: ” + validator.getMessages(result)); } } } Output Invalid Password: [ Password contains the illegal character ”&”., Password contains the number ”4”., Password contains a whitespace character.] Print Page Previous Next Advertisements ”;

Passay − DictionarySubstringRule

Passay – DictionarySubstringRule ”; Previous Next DictionarySubstringRule allows to check if certain words are not part of a password. Consider the following example. Example import org.passay.DictionarySubstringRule; import org.passay.PasswordData; import org.passay.PasswordValidator; import org.passay.RuleResult; import org.passay.dictionary.ArrayWordList; import org.passay.dictionary.WordListDictionary; public class PassayExample { public static void main(String[] args) { WordListDictionary wordListDictionary = new WordListDictionary( new ArrayWordList(new String[] { “password”, “username” })); DictionarySubstringRule dictionaryRule = new DictionarySubstringRule(wordListDictionary); PasswordValidator validator = new PasswordValidator(dictionaryRule); PasswordData password = new PasswordData(“password@123”); RuleResult result = validator.validate(password); if(result.isValid()){ System.out.println(“Password validated.”); }else{ System.out.println(“Invalid Password: ” + validator.getMessages(result)); } } } Output Invalid Password: [Password contains the dictionary word ”password”.] Print Page Previous Next Advertisements ”;

Passay – Discussion

Discuss Passay ”; Previous Next Passay is a Java based Password generation and validation library. It provides comprehensive features list in order to validate/generate passwords and is highly configurable. Print Page Previous Next Advertisements ”;

Passay − Password Validation

Passay – Password Validation ”; Previous Next A typical Password policy contains a set of rules to check a password if is compliant with organization rules. Consider the following policy: Length of password should be in between 8 to 16 characters. A password should not contain any whitespace. A password should contains each of the following: upper, lower, digit and a symbol. Example The below example shows the validation of a password against above policy using Passay library. import java.util.ArrayList; import java.util.List; import org.passay.CharacterRule; import org.passay.EnglishCharacterData; import org.passay.LengthRule; import org.passay.PasswordData; import org.passay.PasswordValidator; import org.passay.Rule; import org.passay.RuleResult; import org.passay.WhitespaceRule; public class PassayExample { public static void main(String[] args) { List<Rule> rules = new ArrayList<>(); //Rule 1: Password length should be in between //8 and 16 characters rules.add(new LengthRule(8, 16)); //Rule 2: No whitespace allowed rules.add(new WhitespaceRule()); //Rule 3.a: At least one Upper-case character rules.add(new CharacterRule(EnglishCharacterData.UpperCase, 1)); //Rule 3.b: At least one Lower-case character rules.add(new CharacterRule(EnglishCharacterData.LowerCase, 1)); //Rule 3.c: At least one digit rules.add(new CharacterRule(EnglishCharacterData.Digit, 1)); //Rule 3.d: At least one special character rules.add(new CharacterRule(EnglishCharacterData.Special, 1)); PasswordValidator validator = new PasswordValidator(rules); PasswordData password = new PasswordData(“Microsoft@123”); RuleResult result = validator.validate(password); if(result.isValid()){ System.out.println(“Password validated.”); } else { System.out.println(“Invalid Password: ” + validator.getMessages(result)); } } } Output Password validated. Print Page Previous Next Advertisements ”;

Passay − LengthComplexityRule

Passay – LengthComplexityRule ”; Previous Next LengthComplexityRule helps in defining the applicable rule on a password based on its length. Consider the following policy. If length of password is in between 1 to 5 characters, only lower case alphabets are allowed. If length of password is in between 6 to 8 characters, then only a, b and c are allowed. Example The below example shows the validation of a password against above policy using Passay library. import org.passay.AllowedCharacterRule; import org.passay.CharacterRule; import org.passay.EnglishCharacterData; import org.passay.LengthComplexityRule; import org.passay.PasswordData; import org.passay.PasswordValidator; import org.passay.RuleResult; public class PassayExample { public static void main(String[] args) { LengthComplexityRule lengthComplexityRule = new LengthComplexityRule(); //Rule: Password of 1 to 5 characters should contains lower case alphabets only lengthComplexityRule.addRules(“[1,5]”, new CharacterRule(EnglishCharacterData.LowerCase, 5)); //8 and 16 characters lengthComplexityRule.addRules(“[6,8]”, new AllowedCharacterRule(new char[] { ”a”, ”b”, ”c” })); PasswordValidator validator = new PasswordValidator(lengthComplexityRule); PasswordData password = new PasswordData(“abcdef”); RuleResult result = validator.validate(password); if(result.isValid()){ System.out.println(“Password validated.”); }else{ System.out.println(“Invalid Password: ” + validator.getMessages(result)); } } } Output Invalid Password: [ Password contains the illegal character ”d”., Password contains the illegal character ”e”., Password contains the illegal character ”f”., Password meets 0 complexity rules, but 1 are required.] Print Page Previous Next Advertisements ”;

Passay − Customized Messages

Passay − Customized Messages ”; Previous Next Passay libary provides a MessageResolver API to override the default messages used by the validator. It can take the path to custom properties file and use the standard keys to override the required message. Example The below example shows the validation of a password and show a custom message using Passay library. messages.properties INSUFFICIENT_UPPERCASE=Password missing at least %1$s uppercase characters. PassayExample.java import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.passay.CharacterRule; import org.passay.EnglishCharacterData; import org.passay.LengthRule; import org.passay.MessageResolver; import org.passay.PasswordData; import org.passay.PasswordValidator; import org.passay.PropertiesMessageResolver; import org.passay.Rule; import org.passay.RuleResult; import org.passay.WhitespaceRule; public class PassayExample { public static void main(String[] args) throws FileNotFoundException, IOException { List<Rule> rules = new ArrayList<>(); rules.add(new LengthRule(8, 16)); rules.add(new WhitespaceRule()); rules.add(new CharacterRule(EnglishCharacterData.UpperCase, 1)); rules.add(new CharacterRule(EnglishCharacterData.LowerCase, 1)); rules.add(new CharacterRule(EnglishCharacterData.Digit, 1)); rules.add(new CharacterRule(EnglishCharacterData.Special, 1)); Properties props = new Properties(); props.load(new FileInputStream(“E:/Test/messages.properties”)); MessageResolver resolver = new PropertiesMessageResolver(props); PasswordValidator validator = new PasswordValidator(resolver, rules); PasswordData password = new PasswordData(“microsoft@123”); RuleResult result = validator.validate(password); if(result.isValid()){ System.out.println(“Password validated.”); }else{ System.out.println(“Invalid Password: ” + validator.getMessages(result)); } } } Output Invalid Password: [Password missing at least 1 uppercase characters.] Print Page Previous Next Advertisements ”;

Passay − usernameRule

Passay – UsernameRule ”; Previous Next UsernameRule ensures that password is not containing the username. Consider the following example. Example import org.passay.PasswordData; import org.passay.PasswordValidator; import org.passay.Rule; import org.passay.RuleResult; import org.passay.UsernameRule; public class PassayExample { public static void main(String[] args) { //Rule: Password should not contain user-name Rule rule = new UsernameRule(); PasswordValidator validator = new PasswordValidator(rule); PasswordData password = new PasswordData(“microsoft”); password.setUsername(“micro”); RuleResult result = validator.validate(password); if(result.isValid()){ System.out.println(“Password validated.”); }else{ System.out.println(“Invalid Password: ” + validator.getMessages(result)); } } } Output Invalid Password: [Password contains the user id ”micro”.] Print Page Previous Next Advertisements ”;

Passay − Home

Passay Tutorial PDF Version Quick Guide Resources Job Search Discussion Passay is a Java based Password generation and validation library. It provides comprehensive features list in order to validate/generate passwords and is highly configurable. Audience This tutorial is designed for Software Professionals who are willing to validate/generate passwords in Java in simple and easy steps. This tutorial will give you an understanding of the Passay concepts and after completing this tutorial you will be at an intermediate level of expertise from where you can take yourself to a higher level of expertise. Prerequisites Before proceeding with this tutorial, you should have a basic understanding of Java and execution of programs, etc. Print Page Previous Next Advertisements ”;