java实现的密码强度检测功能完整示例实现的密码强度检测功能完整示例
主要介绍了java实现的密码强度检测功能,结合完整实例形式分析了java针对密码强度检测相关的字符串遍历、
判断,以及输出密码强度等级相关操作技巧,需要的朋友可以参考下
本文实例讲述了java实现的密码强度检测功能。分享给大家供大家参考,具体如下:
CheckStrength.java文件:
package com.wx.pwd;
/**
* 检测密码强度
*
* @author venshine
*/
public class CheckStrength {
public enum LEVEL {
EASY, MIDIUM, STRONG, VERY_STRONG, EXTREMELY_STRONG
}
/**
* 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;
/**
* 简单的密码字典
*/
private final static String[] DICTIONARY = {"password", "abc123", "iloveyou", "adobe123", "123123", "sunshine",
"1314520", "a1b2c3", "123qwe", "aaa111", "qweasd", "admin", "passwd"};
/**
*检查字符类型,包括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;
}
/**
* 按不同类型计算密码的数量
*
* @param passwd
* @param type
* @return
*/
private static int countLetter(String passwd, int type) {
int count = 0;
if (null != passwd && passwd.length() > 0) {
for (char c : passwd.toCharArray()) {
if (checkCharacterType(c) == type) {
count++;
}
}
}
return count;
}
/**
* 检查密码的强度
*
* @param passwd
* @return strength level
*/
public static int checkPasswordStrength(String passwd) {
if (StringUtils.equalsNull(passwd)) {
评论5