脱敏工具类add银行卡号

This commit is contained in:
duandazhi 2021-04-05 00:16:37 +08:00
parent a37f17eef6
commit e5c53efc1d
2 changed files with 37 additions and 1 deletions

View File

@ -3648,6 +3648,7 @@ public class CharSequenceUtil {
* StrUtil.desensitized("duandazhi-jack@gmail.com.cn", DesensitizedUtils.DesensitizedType.EMAIL)) = "d*************@gmail.com.cn"
* StrUtil.desensitized("1234567890", DesensitizedUtils.DesensitizedType.PASSWORD)) = "**********"
* StrUtil.desensitized("苏D40000", DesensitizedUtils.DesensitizedType.CAR_LICENSE)) = "苏D4***0"
* StrUtil.desensitized("11011111222233333256", DesensitizedUtils.DesensitizedType.CAR_LICENSE)) = "1101 **** **** **** 3256"
* </pre>
*
* @param str 字符串

View File

@ -12,6 +12,8 @@ package cn.hutool.core.util;
* <li>地址</li>
* <li>电子邮件</li>
* <li>密码</li>
* <li>车牌</li>
* <li>银行卡号</li>
* </ul>
*
* @author dazer and neusoft and qiaomu
@ -42,7 +44,8 @@ public class DesensitizedUtil {
//密码
PASSWORD,
//中国大陆车牌包含普通车辆新能源车辆
CAR_LICENSE
CAR_LICENSE,
BANK_CARD
}
/**
@ -57,6 +60,7 @@ public class DesensitizedUtil {
* DesensitizedUtil.desensitized("duandazhi-jack@gmail.com.cn", DesensitizedUtils.DesensitizedType.EMAIL)) = "d*************@gmail.com.cn"
* DesensitizedUtil.desensitized("1234567890", DesensitizedUtils.DesensitizedType.PASSWORD)) = "**********"
* DesensitizedUtil.desensitized("苏D40000", DesensitizedUtils.DesensitizedType.CAR_LICENSE)) = "苏D4***0"
* DesensitizedUtil.desensitized("11011111222233333256", DesensitizedUtils.DesensitizedType.CAR_LICENSE)) = "1101 **** **** **** 3256"
* </pre>
*
* @param str 字符串
@ -98,6 +102,9 @@ public class DesensitizedUtil {
case CAR_LICENSE:
newStr = DesensitizedUtil.carLicense(String.valueOf(str));
break;
case BANK_CARD:
newStr = DesensitizedUtil.bankCard(String.valueOf(str));
break;
default:
}
return newStr;
@ -245,4 +252,32 @@ public class DesensitizedUtil {
}
return carLicense;
}
/**
* 银行卡号脱敏
* eg: 1101 **** **** **** 3256
* @param s 银行卡号
* @return 脱敏之后的银行卡号
* @since 5.6.3
*/
public static String bankCard(String s) {
if (StrUtil.isBlank(s)) {
return StrUtil.EMPTY;
}
s = StrUtil.trim(s);
if (s.length() < 9) {
return s;
}
String s1 = s.substring(0, 4);
String s2 = s.substring(s.length() - 4, s.length());
String sMiddle = s.substring(4, s.length() - 4);
StringBuilder buf = new StringBuilder();
for (int i = 0; i < sMiddle.length(); ++i) {
buf.append("*");
if ((i + 1) % 4 == 0) {
buf.append(" ");
}
}
return s1 + " " + buf.toString() + " " + s2 + "";
}
}