Guest User

Untitled

a guest
Jun 22nd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. public class PasswordStrengthValidator {
  2.  
  3. public static void main(String[] args) {
  4. System.out.println(calculate("demoPassword!@", 6, 25, 2));
  5. // print true
  6. }
  7.  
  8. /**
  9. * @param passwordString Password to be evaluated
  10. * @param minLengthRequired Password min length
  11. * @param maxLengthRequired Max length of password
  12. * @param requiredMatches Number of complexities to be matched. Recommended 2
  13. * @return true, if strong password
  14. */
  15. protected static boolean calculate(String passwordString, int minLengthRequired, int maxLengthRequired, int requiredMatches) {
  16. if (passwordString == null) return false;
  17. passwordString = passwordString.replaceAll("[^a-zA-Z0-9 $!@#%&*/+-:?.,;]", "");
  18. int length = passwordString.length(), uppercase = 0, lowercase = 0, digits = 0, symbols, requirements = 0;
  19. if (length < minLengthRequired || length > maxLengthRequired) return false;
  20. for (int i = 0; i < passwordString.length(); i++) {
  21. if (Character.isUpperCase(passwordString.charAt(i))) uppercase++;
  22. else if (Character.isLowerCase(passwordString.charAt(i))) lowercase++;
  23. else if (Character.isDigit(passwordString.charAt(i))) digits++;
  24. }
  25. symbols = length - uppercase - lowercase - digits;
  26.  
  27. if (uppercase > 0) requirements++;
  28. if (lowercase > 0) requirements++;
  29. if (digits > 0) requirements++;
  30. if (symbols > 0) requirements++;
  31. return requirements >= requiredMatches;
  32. }
  33. }
Add Comment
Please, Sign In to add comment