package com.se.system.utils; @SuppressWarnings("ALL") public class CheckPwdUtils { /** * NUM 数字 * SMALL_LETTER 小写字母 * CAPITAL_LETTER 大写字母 * OTHER_CHAR 特殊字符 */ private static final int NUM = 1; private static final int SMALL_LETTER = 2; private static final int CAPITAL_LETTER = 3; private static final int OTHER_CHAR = 4; /** * 检查密码的强度 * * @param passwd 密码 * @return 密码等级 */ public static int checkPwdLevel(String passwd) { if (null == passwd) { throw new IllegalArgumentException("密码不能为空"); } if (passwd.length() < 8) { return 1; } int level = 0; // 判断密码长度是否大于等于8 是level++ if (passwd.length() >= 8) { level++; } // 判断密码是否含有数字 有level++ if (countLetter(passwd, NUM) > 0) { level++; } // 判断密码是否含有小写字母 有level++ if (countLetter(passwd, SMALL_LETTER) > 0) { level++; } // 判断密码是否还有大写字母 有level++ if (countLetter(passwd, CAPITAL_LETTER) > 0) { level++; } // 判断密码是否还有特殊字符 有level++ if (countLetter(passwd, OTHER_CHAR) > 0) { level++; } return level; } /** * 计算密码中指定字符类型的数量 * * @param passwd 密码 * @param type 类型 * @return 数量 */ private static int countLetter(String passwd, int type) { int count = 0; if (null != passwd && !passwd.isEmpty()) { for (char c : passwd.toCharArray()) { if (checkCharacterType(c) == type) { count++; } } } return count; } /** * 检查字符类型,包括num、大写字母、小写字母和其他字符。 * * @param c – 字符 * @return 类型 */ private static int checkCharacterType(char c) { if (c >= 48 && c <= 57) { return NUM; } if (c >= 65 && c <= 90) { return CAPITAL_LETTER; } if (c >= 97 && c <= 122) { return SMALL_LETTER; } return OTHER_CHAR; } }