import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.regex.Pattern; public class PasswordValidator { int minLength = 6; boolean requireUppercase = true; boolean requireLowercase = true; boolean requireDigit = true; private final Pattern uppercasePattern = Pattern.compile("^(?=.*[A-Z]).+$"); private final Pattern lowercasePattern = Pattern.compile("^(?=.*[a-z]).+$"); private final Pattern digitPattern = Pattern.compile("^(?=.*\\d).+$"); public boolean validate(String password) { if (password.length() < minLength) { return false; } else if (requireUppercase && !uppercasePattern.matcher(password).matches()) { return false; } else if (requireLowercase && !lowercasePattern.matcher(password).matches()) { return false; } else if (requireDigit && !digitPattern.matcher(password).matches()) { return false; } return true; } public int getMinLength() { return minLength; } public void setMinLength(int minLength) { this.minLength = minLength; } public boolean isRequireUppercase() { return requireUppercase; } public void setRequireUppercase(boolean requireUppercase) { this.requireUppercase = requireUppercase; } public boolean isRequireLowercase() { return requireLowercase; } public void setRequireLowercase(boolean requireLowercase) { this.requireLowercase = requireLowercase; } public boolean isRequireDigit() { return requireDigit; } public void setRequireDigit(boolean requireDigit) { this.requireDigit = requireDigit; } public static String getSHA1Hash(String input) { if (input.length() > 0) { try { MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] messageDigest = md.digest(input.getBytes()); // Convert byte array into signum representation BigInteger no = new BigInteger(1, messageDigest); // Convert message digest into hex value StringBuilder hashtext = new StringBuilder(); hashtext.append(no.toString(16)); // Add preceding 0s to make it 32 bit while (hashtext.length() < 32) { hashtext.insert(0, "0"); } return hashtext.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } return null; } }