Browse Source

add password wordlist check to password validator

feature-password-validator-2
binsky 3 years ago
parent
commit
8cfd748197
  1. 11
      src/main/java/PasswordValidator.java
  2. 10
      src/test/java/PasswordValidatorTest.java

11
src/main/java/PasswordValidator.java

@ -14,6 +14,7 @@ public class PasswordValidator {
boolean requireDigit = true; boolean requireDigit = true;
boolean requireSpecialChar = true; boolean requireSpecialChar = true;
boolean checkPwned = true; boolean checkPwned = true;
boolean checkWordlist = false;
private final Pattern uppercasePattern = Pattern.compile("^(?=.*[A-Z]).+$"); private final Pattern uppercasePattern = Pattern.compile("^(?=.*[A-Z]).+$");
private final Pattern lowercasePattern = Pattern.compile("^(?=.*[a-z]).+$"); private final Pattern lowercasePattern = Pattern.compile("^(?=.*[a-z]).+$");
@ -32,6 +33,8 @@ public class PasswordValidator {
return false; return false;
} else if (requireSpecialChar && !specialCharPattern.matcher(password).matches()) { } else if (requireSpecialChar && !specialCharPattern.matcher(password).matches()) {
return false; return false;
} else if (checkWordlist && isInWordlist(password)) {
return false;
} else if (checkPwned && isPwned(password)) { } else if (checkPwned && isPwned(password)) {
return false; return false;
} }
@ -86,6 +89,14 @@ public class PasswordValidator {
this.checkPwned = checkPwned; this.checkPwned = checkPwned;
} }
public boolean isCheckWordlist() {
return checkWordlist;
}
public void setCheckWordlist(boolean checkWordlist) {
this.checkWordlist = checkWordlist;
}
public static String getSHA1Hash(String input) { public static String getSHA1Hash(String input) {
if (input.length() > 0) { if (input.length() > 0) {
try { try {

10
src/test/java/PasswordValidatorTest.java

@ -74,6 +74,16 @@ class PasswordValidatorTest {
assertFalse(passwordValidator.validate("8")); assertFalse(passwordValidator.validate("8"));
assertFalse(passwordValidator.validate("asdf12")); assertFalse(passwordValidator.validate("asdf12"));
assertTrue(passwordValidator.validate("=phan0johB4aisae6Mie0jeip9Saejahc0iuvuth7ahv9uoni6o*_.+")); assertTrue(passwordValidator.validate("=phan0johB4aisae6Mie0jeip9Saejahc0iuvuth7ahv9uoni6o*_.+"));
// test password wordlist check
passwordValidator.setRequireUppercase(true);
passwordValidator.setRequireLowercase(true);
passwordValidator.setRequireDigit(true);
passwordValidator.setCheckPwned(true);
passwordValidator.setCheckWordlist(true);
assertFalse(passwordValidator.validate("8"));
assertFalse(passwordValidator.validate("_Sonnenblume123"));
assertTrue(passwordValidator.validate("=phan0johB4aisae6Mie0jeip9Saejahc0iuvuth7ahv9uoni6o*_.+"));
} }
@Test @Test

Loading…
Cancel
Save