【Java工具类】密码增强工具类(支持八位&必选其三(数字,大写字母,小写字母,特殊字符)

public class PasswordUtils {
 
 public static boolean isStrongPassword(String password) {
 	 return !password.equals("123456") && checkPassRule(password);
 }

 /**
  * 密码长度大于等于8位,包含4种字符(数字、大写字母、小写字母和特殊字符)必选其三
  * <p/>
  * 返回true表示满足上述条件,否则返回false
  */
 private static boolean checkPassRule(String password) {
	  if (StringUtils.isBlank(password) || password.length() < 8) {
	   return false;
	  }
	  final String ds = "0123456789";
	  final String lcs = "abcdefghijklmnopqrstuvwxyz";
	  final String ucs = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	  String init_bits = "00000000";// 取末四位由0和1组成的字符串,依次表示是否使用了数字、小写字母、大写字母、特殊数字,0表示未使用,1表示使用,
	
	  int bit_counter = Integer.parseInt(init_bits, 2);
	
	  char[] input_chars = password.toCharArray();
	
	  for (char ch : input_chars) {
	   if (ds.indexOf(ch) != -1) {// 表示使用了数字
	    bit_counter = bit_counter | Integer.parseInt("00001000", 2);
	   } else if (lcs.indexOf(ch) != -1) {// 表示使用了小写字母
	    bit_counter = bit_counter | Integer.parseInt("00000100", 2);
	   } else if (ucs.indexOf(ch) != -1) {// 表示使用了大写字母
	    bit_counter = bit_counter | Integer.parseInt("00000010", 2);
	   } else {// 表示使用了特殊字符
	    bit_counter = bit_counter | Integer.parseInt("00000001", 2);
	   }
	  }
	
	  String bit_counter_Str = Integer.toBinaryString(bit_counter);
	
	  // 取1的个数,表示使用了多少种规则
	  int rule_use_count = StringUtils.countMatches(bit_counter_Str, "1");
	
	  return rule_use_count >= 3;
	  
	 }
}

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值