Browse Source

implement requiring every configured character type in password generator

feature-password-generator
binsky 3 years ago
parent
commit
fd4e570204
  1. 27
      src/main/java/PasswordGenerator.java
  2. 7
      src/test/java/PasswordGeneratorTest.java

27
src/main/java/PasswordGenerator.java

@ -12,6 +12,7 @@ public class PasswordGenerator {
private boolean useLowercase = true; private boolean useLowercase = true;
private boolean useDigits = true; private boolean useDigits = true;
private boolean useSpecialCharacters = true; private boolean useSpecialCharacters = true;
private boolean requireEveryConfiguredCharacterType = true;
public String generateRandomPassword() { public String generateRandomPassword() {
@ -33,7 +34,23 @@ public class PasswordGenerator {
} }
for (int generatorPosition = 0; generatorPosition < getLength(); generatorPosition++) { for (int generatorPosition = 0; generatorPosition < getLength(); generatorPosition++) {
generatedPassword.append(characterPool.charAt(rand.nextInt(characterPool.length())));
String customCharacterPool = characterPool;
if (isRequireEveryConfiguredCharacterType()) {
if (generatorPosition == 0 && isUseLowercase()) {
customCharacterPool += lowercaseCharacters;
} else if (generatorPosition == 1 && isUseUppercase()) {
customCharacterPool += uppercaseCharacters;
} else if (generatorPosition == 2 && isUseDigits()) {
customCharacterPool += digits;
} else if (generatorPosition == 3 && isUseSpecialCharacters()) {
customCharacterPool += specialCharacters;
} else {
customCharacterPool = characterPool;
}
}
generatedPassword.append(customCharacterPool.charAt(rand.nextInt(customCharacterPool.length())));
} }
return generatedPassword.toString(); return generatedPassword.toString();
} }
@ -70,6 +87,14 @@ public class PasswordGenerator {
this.useSpecialCharacters = useSpecialCharacters; this.useSpecialCharacters = useSpecialCharacters;
} }
public boolean isRequireEveryConfiguredCharacterType() {
return requireEveryConfiguredCharacterType;
}
public void setRequireEveryConfiguredCharacterType(boolean requireEveryConfiguredCharacterType) {
this.requireEveryConfiguredCharacterType = requireEveryConfiguredCharacterType;
}
public int getLength() { public int getLength() {
return length; return length;
} }

7
src/test/java/PasswordGeneratorTest.java

@ -55,4 +55,11 @@ class PasswordGeneratorTest {
passwordGenerator.setUseSpecialCharacters(true); passwordGenerator.setUseSpecialCharacters(true);
assertTrue(Pattern.compile("^(?=.*[.!?=@#$()%^&/*_\\-+]).+$").matcher(passwordGenerator.generateRandomPassword()).matches()); assertTrue(Pattern.compile("^(?=.*[.!?=@#$()%^&/*_\\-+]).+$").matcher(passwordGenerator.generateRandomPassword()).matches());
} }
@Test
void testPasswordForEveryRequiredCharacter() {
passwordGenerator.setRequireEveryConfiguredCharacterType(true);
PasswordValidator passwordValidator = new PasswordValidator();
assertTrue(passwordValidator.validate(passwordGenerator.generateRandomPassword()));
}
} }
Loading…
Cancel
Save