当前位置:网站首页>字符脱敏工具
字符脱敏工具
2022-08-07 04:15:00 【熟透的蜗牛】
public class DesensitizationUtil {
/**
* 只显示第一个汉字,其他隐藏为2个星号<例子:李**>
*
* @param fullName
* @param index 1 为第index位开始脱敏
* @return
*/
public static String left(String fullName, int index) {
if (StringUtils.isBlank(fullName)) {
return "";
}
String name = StringUtils.left(fullName, index);
return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
}
/**
* 110****58,前面保留3位明文,后面保留2位明文
*
* @param name
* @param index 3
* @param end 2
* @return
*/
public static String around(String name, int index, int end) {
if (StringUtils.isBlank(name)) {
return "";
}
return StringUtils.left(name, index).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(name, end), StringUtils.length(name), "*"), "***"));
}
/**
* 后四位,其他隐藏<例子:****1234>
*
* @param num
* @return
*/
public static String right(String num, int end) {
if (StringUtils.isBlank(num)) {
return "";
}
return StringUtils.leftPad(StringUtils.right(num, end), StringUtils.length(num), "*");
}
// 手机号码前三后四脱敏
public static String mobileEncrypt(String mobile) {
if (StringUtils.isEmpty(mobile) || (mobile.length() != 11)) {
return mobile;
}
return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
}
//身份证前三后四脱敏
public static String idEncrypt(String id) {
if (StringUtils.isEmpty(id) || (id.length() < 8)) {
return id;
}
return id.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
}
//护照前2后3位脱敏,护照一般为8或9位
public static String idPassport(String id) {
if (StringUtils.isEmpty(id) || (id.length() < 8)) {
return id;
}
return id.substring(0, 2) + new String(new char[id.length() - 5]).replace("\0", "*") + id.substring(id.length() - 3);
}
/**
* 证件后几位脱敏
*
* @param id
* @param sensitiveSize
* @return
*/
public static String idPassport(String id, int sensitiveSize) {
if (StringUtils.isBlank(id)) {
return "";
}
int length = StringUtils.length(id);
return StringUtils.rightPad(StringUtils.left(id, length - sensitiveSize), length, "*");
}
}边栏推荐
猜你喜欢
随机推荐
聊聊字典那些事
多线程之原子整数和原子引用
“go1.15.3“ does not match go tool version “go1.13.8“
工业5g路由器厂家
From the rejection of my resume to the harvest of 8 big company offers, it took me 3 months to successfully break through the cocoon and become a butterfly
2022 Niu Ke Duo School Six B-Eezie and Pie (dfs)
Industrial 5g router manufacturers
Golang map结构体成员不能修改的问题
Equal scaling
Golang 作用域的坑
SaaS服务平台软件是什么?
How to adjust the game settings of Ark Survival Evolved
Polygon Ventures或有意与Genesis合作
5G/4G水资源遥控终端
BI skills丨Filter reset
golang server proxy
Collections and Iterators
@RequestBody
wpf中TreeView的滚动条
Intelligent street lamp intelligent remote control



![[transfer] swig](/img/b0/9bc0aae8fc4eef7834be559eaf1d6c.png)





