Browse Source

make password generator requirements configurable

feature-password-generator
binsky 3 years ago
parent
commit
408d189ac4
  1. 56
      src/main/java/PasswordGenerator.java
  2. 9
      src/test/java/PasswordGeneratorTest.java

56
src/main/java/PasswordGenerator.java

@ -6,28 +6,62 @@ public class PasswordGenerator {
final String uppercaseCharacters = "ABCDEFGHJKMNPQRSTUVWXYZ"; final String uppercaseCharacters = "ABCDEFGHJKMNPQRSTUVWXYZ";
final String digits = "0123456789"; final String digits = "0123456789";
int length = 12;
private int length = 12;
private boolean useUppercase = true;
private boolean useLowercase = true;
private boolean useDigits = true;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public String generateRandomPassword() { public String generateRandomPassword() {
StringBuilder generatedPassword = new StringBuilder(); StringBuilder generatedPassword = new StringBuilder();
SecureRandom rand = new SecureRandom(); SecureRandom rand = new SecureRandom();
String characterPool = ""; String characterPool = "";
characterPool += lowercaseCharacters;
characterPool += uppercaseCharacters;
characterPool += digits;
if (isUseLowercase()) {
characterPool += lowercaseCharacters;
}
if (isUseUppercase()) {
characterPool += uppercaseCharacters;
}
if (isUseDigits()) {
characterPool += digits;
}
for (int generatorPosition = 0; generatorPosition < getLength(); generatorPosition++) { for (int generatorPosition = 0; generatorPosition < getLength(); generatorPosition++) {
generatedPassword.append(characterPool.charAt(rand.nextInt(characterPool.length()))); generatedPassword.append(characterPool.charAt(rand.nextInt(characterPool.length())));
} }
return generatedPassword.toString(); return generatedPassword.toString();
} }
public boolean isUseUppercase() {
return useUppercase;
}
public void setUseUppercase(boolean useUppercase) {
this.useUppercase = useUppercase;
}
public boolean isUseLowercase() {
return useLowercase;
}
public void setUseLowercase(boolean useLowercase) {
this.useLowercase = useLowercase;
}
public boolean isUseDigits() {
return useDigits;
}
public void setUseDigits(boolean useDigits) {
this.useDigits = useDigits;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
} }

9
src/test/java/PasswordGeneratorTest.java

@ -26,16 +26,25 @@ class PasswordGeneratorTest {
@Test @Test
void testPasswordLowercaseRequirement() { void testPasswordLowercaseRequirement() {
passwordGenerator.setUseLowercase(false);
assertFalse(Pattern.compile("^(?=.*[a-z]).+$").matcher(passwordGenerator.generateRandomPassword()).matches());
passwordGenerator.setUseLowercase(true);
assertTrue(Pattern.compile("^(?=.*[a-z]).+$").matcher(passwordGenerator.generateRandomPassword()).matches()); assertTrue(Pattern.compile("^(?=.*[a-z]).+$").matcher(passwordGenerator.generateRandomPassword()).matches());
} }
@Test @Test
void testPasswordUppercaseRequirement() { void testPasswordUppercaseRequirement() {
passwordGenerator.setUseUppercase(false);
assertFalse(Pattern.compile("^(?=.*[A-Z]).+$").matcher(passwordGenerator.generateRandomPassword()).matches());
passwordGenerator.setUseUppercase(true);
assertTrue(Pattern.compile("^(?=.*[A-Z]).+$").matcher(passwordGenerator.generateRandomPassword()).matches()); assertTrue(Pattern.compile("^(?=.*[A-Z]).+$").matcher(passwordGenerator.generateRandomPassword()).matches());
} }
@Test @Test
void testPasswordDigitsRequirement() { void testPasswordDigitsRequirement() {
passwordGenerator.setUseDigits(false);
assertFalse(Pattern.compile("^(?=.*\\d).+$").matcher(passwordGenerator.generateRandomPassword()).matches());
passwordGenerator.setUseDigits(true);
assertTrue(Pattern.compile("^(?=.*\\d).+$").matcher(passwordGenerator.generateRandomPassword()).matches()); assertTrue(Pattern.compile("^(?=.*\\d).+$").matcher(passwordGenerator.generateRandomPassword()).matches());
} }
} }
Loading…
Cancel
Save