Random Password Generator in Java(using Passey)

By Dhiraj Ray, 02 May,2017   1068

This article is about random password generator in java. The password generated will be alphanumeric and special characters with configurable no of characters limit. Here we will be using an exisiting open source password validation and generation library for Java called passay to generate and validate passwords by providing different configurable constraints.

We will be creating a simple maven based java application and implement passey to generate and validate passwords policies.So let us get started.

Maven Dependencies for Passey

At first we require to include following maven dependency in our pom.xml to get started with passey.

<dependencies> <dependency> <groupId>org.passay</groupId> <artifactId>passay</artifactId> <version>1.2.0</version> </dependency> </dependencies>

Random Password Generator

Following is method that generates random password in java. The random password generation is configurable. We can easily configure the generated password to contain alphanumeric or special characters.

Following code will generate password having at least one uppercase character, one lowercase character, one special character and a length of 8 characters.

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

Validating Password Policy

Following code will validate the input password. If the password matches with the restrictions we have configured, it will flag as a valid password else it will append error message for any missing characters.

public boolean isPasswordValid(String password) { boolean isPasswordValid = false; PasswordValidator validator = new PasswordValidator(Arrays.asList(new LengthRule(8, 16), new CharacterRule(EnglishCharacterData.UpperCase, 1), new CharacterRule(EnglishCharacterData.LowerCase, 1), new CharacterRule(EnglishCharacterData.Digit, 1), new CharacterRule(EnglishCharacterData.Special, 1), new WhitespaceRule())); RuleResult result = validator.validate(new PasswordData(password)); if (result.isValid()) { isPasswordValid = true; System.out.println("The supplied password - " + password + " is valid."); } else { isPasswordValid = false; final StringBuilder message = new StringBuilder(); validator.getMessages(result).stream().forEach(message::append); System.out.println("The supplied password - " + password + " is invalid." + message); } return isPasswordValid; }

Testing Random password Generator

The following tester class will execute the method defined above and validate our password generator.You can check the console to verify it.

public class PasswordPolicyTest { public static void main(String[] args) { PasswordPolicy passpolicy = new PasswordPolicy(); String generatedPassword = passpolicy.generateRandomPassword(); System.out.println(String.format("%s '%s'", "Generated password according to password policy - ", generatedPassword)); String validPassword = getPassword(true); System.out.println(String.format("%s '%s'", "valid password to test - ", validPassword)); passpolicy.isPasswordValid(validPassword); String inValidPassword = getPassword(false); System.out.println(String.format("%s '%s'", "invalid password to test - ", inValidPassword)); passpolicy.isPasswordValid(inValidPassword); } private static String getPassword(boolean validPassword) { String password; if(validPassword) { password = "Dafg123!j"; }else { password = "1234asfg"; } return password; } }

Follwing is the result.

random password generator


This demonstrates a simple random password generator in java. In next article we will take a look into hashing these random genrated passwords and save to db.

I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.

Download the source

Further Reading on Core Java

Suggest more topics in suggestion section or write your own article and share with your colleagues.