Android examples for java.security:Password
check Password Strong Level
import android.support.annotation.IntRange; import java.util.HashSet; import java.util.Objects; import java.util.Set; import java.util.regex.Pattern; public class Main{ private static final Set<String> dictionary = new HashSet<>(); private static final Pattern numPattern = Pattern.compile("^[0-9]+$"); public static int checkPwdStrong(String pwd, int minLen, int maxLen) { maxLen = Math.max(maxLen, minLen); int level = 1; String lowerPwd = pwd.toLowerCase(); if (containsSmallAToZ(lowerPwd)) level++;//w w w.ja va 2 s . c o m if (containsBiggerAToZ(pwd)) level++; if (containsNumber(lowerPwd)) level++; if (contains_(lowerPwd)) level++; if ((containsSmallAToZ(lowerPwd) && containsNumber(lowerPwd)) || (containsSmallAToZ(lowerPwd) && contains_(lowerPwd)) || (containsNumber(lowerPwd) && contains_(lowerPwd))) level++; if (containsSmallAToZ(lowerPwd) && containsNumber(lowerPwd) && contains_(lowerPwd)) level++; // if(lowerPwd.length() < (minLen + maxLen) / 2 && lowerPwd.length() >= minLen) if (lowerPwd.length() < minLen) level--; if (lowerPwd.length() > 1) { boolean allEquals = true; char firstChar = pwd.charAt(0); for (char c : pwd.toCharArray()) if (c != firstChar) { allEquals = false; break; } if (allEquals) return 0; } if (isNumberSequence(lowerPwd)) level--; if ("abcdefghijklmnopqrstuvwxyz".contains(lowerPwd)) level--; if ("qwertyuiop".contains(pwd) || "asdfghjkl".contains(pwd) || "zxcvbnm".contains(pwd)) level--; if (lowerPwd.length() % 2 == 0) { String part1 = pwd.substring(0, pwd.length() / 2); String part2 = pwd.substring(pwd.length() / 2); if (part1.equals(part2)) level--; } if (pwd.length() % 3 == 0) { String part1 = pwd.substring(0, pwd.length() / 3); String part2 = pwd.substring(pwd.length() / 3, pwd.length() / 3 * 2); String part3 = pwd.substring(pwd.length() / 3 * 2); if (part1.equals(part2) && part2.equals(part3)) level--; } if (numPattern.matcher(pwd).matches()) { if (pwd.length() == 8) { int year = Integer.valueOf(pwd.substring(0, 4)); int month = Integer.valueOf(pwd.substring(4, 6)); int day = Integer.valueOf(pwd.substring(5, 7)); if (year >= 1000 && year < 2100 && month >= 1 && month <= 12 && day >= 1 && day <= 31) { level--; } } } if (dictionary.contains(lowerPwd)) level--; level = Math.min(level, 5); level = Math.max(level, 0); return level; } public static boolean containsSmallAToZ(String str) { for (char c : str.toCharArray()) if (c >= 'a' && c <= 'z') return true; return false; } public static boolean containsBiggerAToZ(String str) { for (char c : str.toCharArray()) if (c >= 'A' && c <= 'Z') return true; return false; } public static boolean containsNumber(String str) { for (char c : str.toCharArray()) if (c >= '0' && c <= '9') return true; return false; } public static boolean contains_(String str) { for (char c : str.toCharArray()) for (char special : Constants.SPECIAL_CHARS) if (c == special) return true; return false; } public static boolean isNumberSequence(String str) { if (str.length() < 2) return false; if ("01234567890".contains(str) || "09876543210".contains(str)) return true; return false; } }