Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PasswordStrengthValidator {
- public static void main(String[] args) {
- System.out.println(calculate("demoPassword!@", 6, 25, 2));
- // print true
- }
- /**
- * @param passwordString Password to be evaluated
- * @param minLengthRequired Password min length
- * @param maxLengthRequired Max length of password
- * @param requiredMatches Number of complexities to be matched. Recommended 2
- * @return true, if strong password
- */
- protected static boolean calculate(String passwordString, int minLengthRequired, int maxLengthRequired, int requiredMatches) {
- if (passwordString == null) return false;
- passwordString = passwordString.replaceAll("[^a-zA-Z0-9 $!@#%&*/+-:?.,;]", "");
- int length = passwordString.length(), uppercase = 0, lowercase = 0, digits = 0, symbols, requirements = 0;
- if (length < minLengthRequired || length > maxLengthRequired) return false;
- for (int i = 0; i < passwordString.length(); i++) {
- if (Character.isUpperCase(passwordString.charAt(i))) uppercase++;
- else if (Character.isLowerCase(passwordString.charAt(i))) lowercase++;
- else if (Character.isDigit(passwordString.charAt(i))) digits++;
- }
- symbols = length - uppercase - lowercase - digits;
- if (uppercase > 0) requirements++;
- if (lowercase > 0) requirements++;
- if (digits > 0) requirements++;
- if (symbols > 0) requirements++;
- return requirements >= requiredMatches;
- }
- }
Add Comment
Please, Sign In to add comment