[Java] Password generation (Pasay)

Use Pasay, which is a password policy enforcement library. Passwords can be generated using configurable rulesets.

Settings for using Pasay in Maven projects

pom.xml


<dependency>
    <groupId>org.passay</groupId>
    <artifactId>passay</artifactId>
    <version>1.6.0</version>
</dependency>

Code to generate a mixed case alphanumeric 8-digit password

Password.java


public class Password {

    public static String generate() {
        List<CharacterRule> rules = Arrays.asList(
                  new CharacterRule(EnglishCharacterData.UpperCase, 1),
                  new CharacterRule(EnglishCharacterData.LowerCase, 1),
                  new CharacterRule(EnglishCharacterData.Digit, 1)
            );
        PasswordGenerator generator = new PasswordGenerator();
        String password = generator.generatePassword(8, rules);
        return password;
    }
}

Test code

PasswordTest.java



public class PasswordTest {

    @Test
    public void testGenerate() {
        String password = Password.generate();
        assertTrue(password.length() == 8);
        assertTrue(password.matches(".*[a-z].*"));
        assertTrue(password.matches(".*[0-9].*"));
        assertTrue(password.matches(".*[A-Z].*"));
    }
}

Recommended Posts

[Java] Password generation (Pasay)
Java permutation generation
[Java] Stream API-Stream generation
[Java] New Thread generation method (2)
[Java] Random number generation method (Random)
[Java Silver] Array generation method
[Java] New Thread generation method (1)
Recent Java API specification generation
Java PowerPoint password setting and cancellation
Java
Java
Introduction of New Generation Java Programming Guide (Java 10)
Introduction of New Generation Java Programming Guide (Java 11)
Introduction of New Generation Java Programming Guide (Java 12)