java密码强度_java实现的密码强度检测功能完整示例

本文介绍了使用Java实现的密码强度检测功能,包括一个CheckStrength类,用于检查密码的长度、字符类型组合以及避免常见弱密码。通过检测数字、小写字母、大写字母和特殊字符的出现情况,以及对特定模式的排除,来评估密码的安全级别。
摘要由CSDN通过智能技术生成

本文实例讲述了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)) {

throw new IllegalArgumentException("password is empty");

}

int len = passwd.length();

int level = 0;

// 增加点

//判断密码是否含有数字有level++

if (countLetter(passwd, NUM) > 0) {

level

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值