|
@ -15,6 +15,7 @@ public class PasswordValidator { |
|
|
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]).+$"); |
|
|
private final Pattern digitPattern = Pattern.compile("^(?=.*\\d).+$"); |
|
|
private final Pattern digitPattern = Pattern.compile("^(?=.*\\d).+$"); |
|
|
|
|
|
private static final String pwnedPasswordsApiUrl = "https://api.pwnedpasswords.com/range/"; |
|
|
|
|
|
|
|
|
public boolean validate(String password) { |
|
|
public boolean validate(String password) { |
|
|
if (password.length() < minLength) { |
|
|
if (password.length() < minLength) { |
|
@ -90,13 +91,14 @@ public class PasswordValidator { |
|
|
public static boolean isPwned(String password) { |
|
|
public static boolean isPwned(String password) { |
|
|
String sha1 = PasswordValidator.getSHA1Hash(password); |
|
|
String sha1 = PasswordValidator.getSHA1Hash(password); |
|
|
if (sha1 != null) { |
|
|
if (sha1 != null) { |
|
|
String url = "https://api.pwnedpasswords.com/range/" + sha1.substring(0, 5); |
|
|
|
|
|
|
|
|
String url = pwnedPasswordsApiUrl + sha1.substring(0, 5); |
|
|
try { |
|
|
try { |
|
|
String result = HttpApi.sendHttpGETRequest(url); |
|
|
String result = HttpApi.sendHttpGETRequest(url); |
|
|
BufferedReader bufReader = new BufferedReader(new StringReader(result)); |
|
|
BufferedReader bufReader = new BufferedReader(new StringReader(result)); |
|
|
String line = null; |
|
|
String line = null; |
|
|
while ((line = bufReader.readLine()) != null) { |
|
|
while ((line = bufReader.readLine()) != null) { |
|
|
if (sha1.toUpperCase().endsWith(line.split(":")[0])) { |
|
|
|
|
|
|
|
|
String[] lineSplit = line.split(":"); |
|
|
|
|
|
if (lineSplit.length > 0 && sha1.toUpperCase().endsWith(lineSplit[0])) { |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|