Browse Source

implement generating passwords with uppercase letters

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

7
src/main/java/PasswordGenerator.java

@ -3,6 +3,7 @@ import java.security.SecureRandom;
public class PasswordGenerator {
final String lowercaseCharacters = "abcdefghjkmnpqrstuvwxyz";
final String uppercaseCharacters = "ABCDEFGHJKMNPQRSTUVWXYZ";
int length = 12;
@ -17,9 +18,13 @@ public class PasswordGenerator {
public String generateRandomPassword() {
StringBuilder generatedPassword = new StringBuilder();
SecureRandom rand = new SecureRandom();
String characterPool = "";
characterPool += lowercaseCharacters;
characterPool += uppercaseCharacters;
for (int generatorPosition = 0; generatorPosition < getLength(); generatorPosition++) {
generatedPassword.append(lowercaseCharacters.charAt(rand.nextInt(lowercaseCharacters.length())));
generatedPassword.append(characterPool.charAt(rand.nextInt(characterPool.length())));
}
return generatedPassword.toString();
}

5
src/test/java/PasswordGeneratorTest.java

@ -28,4 +28,9 @@ class PasswordGeneratorTest {
void testPasswordLowercaseRequirement() {
assertTrue(Pattern.compile("^(?=.*[a-z]).+$").matcher(passwordGenerator.generateRandomPassword()).matches());
}
@Test
void testPasswordUppercaseRequirement() {
assertTrue(Pattern.compile("^(?=.*[A-Z]).+$").matcher(passwordGenerator.generateRandomPassword()).matches());
}
}
Loading…
Cancel
Save