Passay - Password Generation



PasswordGenerator class helps in generating password using given policy. Consider the following policy:

  • Length of password should be 8 characters.

  • A password should contains each of the following: upper, lower, digit and a symbol.

Example - Generating Password using Rules array

The below example shows the generation of a password against above policy using Passay library.

PassayDemo.java

package com.tutorialspoint;

import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.PasswordGenerator;
import org.passay.Rule;

public class PassayDemo {
   public static void main(String[] args) {
	  Rule[] rules = new Rule[3];
	  rules[0] = new CharacterRule(EnglishCharacterData.Alphabetical);
	  rules[1] = new CharacterRule(EnglishCharacterData.Digit);
	  rules[2] = new CharacterRule(EnglishCharacterData.Special);

      PasswordGenerator passwordGenerator = new PasswordGenerator();
      String password = passwordGenerator.generatePassword(8, rules);
      System.out.println(password);
   }
}

Output

Compile and run the code to verify the result −

–·”yv2^±

Example - Generating Password using Rules List

The below example shows the generation of a password against above policy using Passay library.

PassayDemo.java

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;

import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.PasswordGenerator;
import org.passay.Rule;

public class PassayDemo {
   public static void main(String[] args) {
	  List<Rule> rules = new ArrayList<>();
	  rules.add(new CharacterRule(EnglishCharacterData.Alphabetical));
	  rules.add(new CharacterRule(EnglishCharacterData.Digit));
	  rules.add(new CharacterRule(EnglishCharacterData.Special));

      PasswordGenerator passwordGenerator = new PasswordGenerator();
      String password = passwordGenerator.generatePassword(8, rules);
      System.out.println(password);
   }
}

Output

Compile and run the code to verify the result −

;d”9dv₶6
Advertisements