From 7e9f3d179f83b50f24d5bbc520fb7008376b5dbf Mon Sep 17 00:00:00 2001 From: binsky Date: Thu, 17 Feb 2022 18:11:58 +0100 Subject: [PATCH] implement generating passwords with uppercase letters --- src/main/java/PasswordGenerator.java | 7 ++++++- src/test/java/PasswordGeneratorTest.java | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/PasswordGenerator.java b/src/main/java/PasswordGenerator.java index 1a092a8..bab7e6f 100644 --- a/src/main/java/PasswordGenerator.java +++ b/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(); } diff --git a/src/test/java/PasswordGeneratorTest.java b/src/test/java/PasswordGeneratorTest.java index 8c14fe6..57192f3 100644 --- a/src/test/java/PasswordGeneratorTest.java +++ b/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()); + } }