Browse Source

implement method to shuffle password characters

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

15
src/main/java/PasswordGenerator.java

@ -6,6 +6,7 @@ public class PasswordGenerator {
final String uppercaseCharacters = "ABCDEFGHJKMNPQRSTUVWXYZ";
final String digits = "0123456789";
final String specialCharacters = ".!?=@#$()%^&/*_-+";
final SecureRandom rand = new SecureRandom();
private int length = 12;
private boolean useUppercase = true;
@ -17,7 +18,6 @@ public class PasswordGenerator {
public String generateRandomPassword() {
StringBuilder generatedPassword = new StringBuilder();
SecureRandom rand = new SecureRandom();
String characterPool = "";
if (isUseLowercase()) {
@ -55,6 +55,19 @@ public class PasswordGenerator {
return generatedPassword.toString();
}
public String shufflePassword(String password) {
StringBuilder shuffledPassword = new StringBuilder();
StringBuilder passwordCopy = new StringBuilder(password);
while (passwordCopy.length() != 0) {
int index = rand.nextInt(passwordCopy.length());
char c = passwordCopy.charAt(index);
shuffledPassword.append(c);
passwordCopy.deleteCharAt(index);
}
return shuffledPassword.toString();
}
public boolean isUseUppercase() {
return useUppercase;
}

7
src/test/java/PasswordGeneratorTest.java

@ -62,4 +62,11 @@ class PasswordGeneratorTest {
PasswordValidator passwordValidator = new PasswordValidator();
assertTrue(passwordValidator.validate(passwordGenerator.generateRandomPassword()));
}
@Test
void testShufflePassword() {
String testInput = "ABcdefgh123";
assertNotEquals(passwordGenerator.shufflePassword(testInput), testInput);
assertEquals(passwordGenerator.shufflePassword(testInput).length(), testInput.length());
}
}
Loading…
Cancel
Save