Skip to content

Commit

Permalink
org.example.PasswordGenerator.generatePassword refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlo-bystrytskyi committed Sep 3, 2024
1 parent 33fb085 commit 7c318af
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/main/java/org/example/PasswordGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@
public class PasswordGenerator {
public static void main(String[] args) {
for (int i = 0; i < 50; i++) {
System.out.println(generatePassword());
System.out.println(generatePassword(20));
}
}

public static String generatePassword() {
return generatePassword(MINIMUM_PASSWORD_LENGTH);
}

public static String generatePassword(int length) {
while (true) {
String password = generateNonValidatedPassword();
String password = generateNonValidatedPassword(length);
if (PasswordValidator.isValid(password)) {

return password;
}
}
}

private static String generateNonValidatedPassword() {
private static String generateNonValidatedPassword(int length) {
Random random = new Random();
char[] passwordElements = new char[MINIMUM_PASSWORD_LENGTH];
for (int i = 0; i < MINIMUM_PASSWORD_LENGTH; i++) {
char[] passwordElements = new char[length];
for (int i = 0; i < length; i++) {
// Magic!
passwordElements[i] = (char) (33 + random.nextInt(94));
}
Expand Down

0 comments on commit 7c318af

Please sign in to comment.