package common.basemodule.baseutil;
import java.util.Objects;
/**
* 敏感信息脱敏工具类
*/
public class BaseMaskUtil {
/**
* 手机号显示首3末4位,中间用*号隐藏代替,如:111****1111
*/
public static String maskPhone(String phone) {
if (Objects.isNull(phone) || phone.length() <= 8) {
return phone;
}
return wordMask(phone, 3, 4, "*");
}
/**
* 固定电话脱敏
*/
public static String maskFixedPhone(String telephone) {
if (BaseStringUtil.isBlank(telephone)) {
return telephone;
}
String result;
if (telephone.length() > 8) {
if (telephone.contains("-")) {
String[] temp = telephone.split("-");
result = temp[0] + "****" + temp[1].substring(temp[1].length() - 4, temp[1].length());
} else {
result = telephone.substring(0, 3) + "****" + telephone.substring(telephone.length() - 4, telephone.length());
}
} else {
result = "****" + telephone.substring(telephone.length() - 4, telephone.length());
}
return result;
}
/**
* 身份证号显示首6末4位,中间用4个*号隐藏代替,如:111111****1111
*/
public static String maskIdCard(String idCard) {
if (BaseStringUtil.isBlank(idCard)) {
return idCard;
}
return wordMask(idCard, 3, 4, "*");
}
/**
* 银行卡显示首6末4位,中间用4个*号隐藏代替,如:111111****1111
*/
public static String maskBankCard(String cardNo) {
if (BaseStringUtil.isBlank(cardNo) || cardNo.length() < 10) {
return cardNo;
}
return wordMask(cardNo, 6, 4, "*");
}
/**
* 邮箱脱敏 前两位及最后一位字符,及@后邮箱域名信息,如:XX****X@163.com
*/
public static String maskEmail(String email) {
if (BaseStringUtil.isBlank(email)) {
return email;
}
String[] temp = email.split("@");
return wordMask(temp[0], 2, 1, "*") + "@" + temp[1];
}
/**
* 汉字掩码
* 0-1字,如:用(用)
* 2字,如:用于(*于)
* 3-4字,如:用于掩(用*掩)、用于掩码(用**码)
* 5-6字,如:用于掩码测(用于*码测)、用于掩码测试(用于**测试)
* 大于6字,如:用于掩码测试的字符串(用于掩****字符串)
*/
public static String maskChineseName(String name) {
int lenth = BaseStringUtil.length(name);
switch (lenth) {
case 0:
case 1:
return name;
case 2:
return "*" + name.substring(1, 2);
case 3:
case 4:
return wordMask(name, 1, 1, "*");
case 5:
case 6:
return wordMask(name, 2, 2, "*");
default:
return wordMask(name, 3, 3, "*");
}
}
/**
* 全隐藏,如: ***
*/
public static String maskAll(String str) {
if (BaseStringUtil.isBlank(str)) {
return str;
}
return "******";
}
/**
* 家庭住址脱敏
* sensitiveSize 敏感信息长度
*/
public static String maskAddress(String address, int sensitiveSize) {
if (BaseStringUtil.isBlank(address)) {
return address;
}
int length = BaseStringUtil.length(address);
return BaseStringUtil.rightPad(BaseStringUtil.left(address, length - sensitiveSize), length - sensitiveSize + 4, "*");
}
/**
* 自定义脱敏内容
* maskCustom("11111111111",3, 4) returns "111****1111"
*/
public static String maskCustom(String value, int from, int length) {
if (BaseStringUtil.isBlank(value) || from < 0 || length < 0
|| (value.length() < from + length)) {
return value;
}
return BaseStringUtil.left(value, from)
.concat(BaseStringUtil.rightPad("", length, '*'))
.concat(value.substring(from + length, value.length()));
}
public static String wordMask(String word, int startLength, int endLength, String pad) {
if (startLength + endLength > word.length()) {
return BaseStringUtil.leftPad("", word.length() - 1, pad);
}
String startStr = word.substring(0, startLength);
String endStr = word.substring(word.length() - endLength, word.length());
return startStr + BaseStringUtil.leftPad("", word.length() - startLength - endLength, pad) + endStr;
}
}
【工具类】数据脱敏工具类
于 2022-05-25 21:46:45 首次发布