import java.io.Serializable;
/**
* 脱敏工具类
* 脱敏规则:可显示字符
* 姓名:前1
* 电话:前3后4
* 身份证:前6后3
* 地址:省、市
* 银行账号:后4
*/
public class DesensitizeUtils implements Serializable {
public static final String desensitize(String value,String type){
if(null == value || "".equals(value)){
return value;
}
String res = "";
if(Type.NAME.name().equals(type)){
res = desensitizeName(value);
}else if(Type.MOBILE.name().equals(type)){
res = desensitizeMobile(value);
}else if(Type.IDNO.name().equals(type)){
res = desensitizeIdno(value);
}else if(Type.BANKNO.name().equals(type)){
res = desensitizeBankNo(value);
}else{
throw new RuntimeException("Unrecognized mask type!");
}
return res;
}
private static String desensitizeName(String value){
return value.substring(0,1)+generateStar(value.length()-1);
}
private static String desensitizeMobile(String value){
if(value.length()<11){
throw new RuntimeException("Illegal Mobile Number.");
}
return value.substring(0,3)+generateStar(value.length()-7)+value.substring(value.length()-4);
}
private static String desensitizeIdno(String value) {
if(value.length()<15){
throw new RuntimeException("Illegal ID number");
}
return value.substring(0,6)+generateStar(value.length()-9)+value.substring(value.length()-3);
}
private static String desensitizeBankNo(String value){
if(value.length()<4){
throw new RuntimeException("Illegal bank account number");
}
return generateStar(value.length()-4)+value.substring(value.length()-4);
}
private static String generateStar(int cnt){
StringBuilder sb = new StringBuilder();
for(int i=0;i<cnt;i++){
sb.append("*");
}
return sb.toString();
}
public enum Type{
NAME,MOBILE,IDNO,BANKNO
}
}
标签:Type,String,res,equals,value,工具,type,脱敏
From: https://www.cnblogs.com/lovedaodao/p/16621010.html