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 37265b9 commit 33fb085
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/main/java/org/example/PasswordGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ public static void main(String[] args) {
}

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

return password;
}
}
}

private static String generateNonValidatedPassword() {
Random random = new Random();
char[] passwordElements = new char[MINIMUM_PASSWORD_LENGTH];
String password = "";
do {
for (int i = 0; i < MINIMUM_PASSWORD_LENGTH; i++) {
// Magic!
passwordElements[i] = (char) (33 + random.nextInt(94));
}
password = new String(passwordElements);
} while (!PasswordValidator.isValid(password));
for (int i = 0; i < MINIMUM_PASSWORD_LENGTH; i++) {
// Magic!
passwordElements[i] = (char) (33 + random.nextInt(94));
}

return password;
return new String(passwordElements);
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/example/PasswordValidator.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.example;

public class PasswordValidator {
static int MINIMUM_PASSWORD_LENGTH = 8;
private static final String[] COMMONLY_USED = {"Password1!", "Aa345678"};
static int MINIMUM_PASSWORD_LENGTH = 8;

public static boolean isValid(String password) {
return isLengthCorrect(password)
Expand All @@ -27,6 +27,7 @@ public static boolean containsUppercaseLowercase(String password) {
public static boolean isNotCommonlyUsed(String password) {
for (String commonlyUsedPassword : COMMONLY_USED) {
if (commonlyUsedPassword.equals(password)) {

return false;
}
}
Expand Down

0 comments on commit 33fb085

Please sign in to comment.