package com.testjdbc.util;
import org.apache.commons.lang3.StringUtils;
/**
* 信息脱敏工具类
**/
public class DataMaskingUtils {
public static void main(String[] args) {
//中文名脱敏
String s1 = DataMaskingUtils.chineseName("欧阳娜娜"); //欧***
System.out.println("中文名脱敏1:"+s1);
String s2 = DataMaskingUtils.chineseName("欧阳", "娜娜");//欧阳**
System.out.println("中文名脱敏2:"+s2);
//字符脱敏
String s3 = DataMaskingUtils.strMasking("信息脱敏工具类测试字符脱敏", 4, 2);
String s4 = DataMaskingUtils.strMasking("字符脱敏", 5, 2);
String s5 = DataMaskingUtils.strMasking("字符脱敏", 2, 3);
String s6 = DataMaskingUtils.strMasking("字符脱敏", 3, 3);
System.out.println("字符脱敏前4位, 后2位:"+s3);
System.out.println("字符脱敏前5位, 后2位:"+s4);
System.out.println("字符脱敏前2位, 后3位:"+s5);
System.out.println("字符脱敏前3位, 后3位:"+s6);
//[身份证号] 脱敏 后四位其他隐藏
String s7 = DataMaskingUtils.idCardNum("430903199909093412");
System.out.println("[身份证号] 脱敏 后四位其他隐藏:"+s7);
//[身份证号]脱敏 前六位,后四位,其他用隐藏
String s8 = DataMaskingUtils.idCard("430903199909093412");
System.out.println("[身份证号]脱敏 前六位,后四位,其他用隐藏:"+s8);
//[固定电话] 后四位,其他隐藏
String s9 = DataMaskingUtils.fixedPhone("13077098909");
System.out.println("[固定电话] 后四位,其他隐藏:"+s9);
//[手机号码] 前三位,后四位,其他隐藏<例子:138******1234>
String s10 = DataMaskingUtils.mobilePhone("13879081234");
System.out.println("[手机号码] 前三位,后四位,其他隐藏:"+s10);
//[地址] 只显示到地区,不显示详细地址
String s11 = DataMaskingUtils.address("北京市丰台区莲花池东路118号", 10);
System.out.println("[地址] 只显示到地区,不显示详细地址:"+s11);
//[电子邮箱] 邮箱前缀仅显示第一个字母
String s12 = DataMaskingUtils.email("zhangsan123@163.com");
System.out.println("[电子邮箱] 邮箱前缀仅显示第一个字母:"+s12);
//[银行卡号] 前六位,后四位,其他用星号隐藏每位1个星号
String s13 = DataMaskingUtils.bankCard("622260011111111111234");
System.out.println("[银行卡号] 前六位,后四位,其他用星号隐藏每位1个星号:"+s13);
//[公司开户银行联号] 公司开户银行联行号,显示前两位,其他用星号隐藏,每位1个星号
String s14 = DataMaskingUtils.cnapsCode("6222600111111111234");
System.out.println("[公司开户银行联号] 公司开户银行联行号,显示前两位,其他用星号隐藏,每位1个星号:"+s14);
}
/**
* 字符脱敏.
*
* @param str the str
* @param pre the pre 前几位
* @param suf the suf 后几位
* @return the string
*/
public static String strMasking(String str, int pre, int suf) {
if (StringUtils.isBlank(str)) {
return str;
}
if (pre <= 0 && suf <= 0) {
return str;
}
int len = str.length();
if (len > (pre + suf)) {
return subPre(str, pre) +
fill("", '*', len - pre - suf, false) +
subSufByLength(str, suf);
} else if (len > pre && pre > 0) {
return subPre(str, pre) +
fill("", '*', len - pre, false);
} else if (len > suf && suf > 0) {
return fill("", '*', len - suf, false) +
subSufByLength(str, suf);
} else {
return fill("", '*', len, false);
}
}
public static String subSufByLength(CharSequence string, int length) {
if (isEmpty(string)) {
return null;
} else {
return length <= 0 ? "" : sub(string, -length, string.length());
}
}
public static String fill(String str, char filledChar, int len, boolean isPre) {
int strLen = str.length();
if (strLen > len) {
return str;
} else {
String filledStr = repeat(filledChar, len - strLen);
return isPre ? filledStr.concat(str) : str.concat(filledStr);
}
}
public static String repeat(char c, int count) {
if (count <= 0) {
return "";
} else {
char[] result = new char[count];
for(int i = 0; i < count; ++i) {
result[i] = c;
}
return new String(result);
}
}
public static String repeat(CharSequence str, int count) {
if (null == str) {
return null;
} else if (count > 0 && str.length() != 0) {
if (count == 1) {
return str.toString();
} else {
int len = str.length();
long longSize = (long) len * (long) count;
int size = (int) longSize;
if ((long) size != longSize) {
throw new ArrayIndexOutOfBoundsException("Required String length is too large: " + longSize);
} else {
char[] array = new char[size];
str.toString().getChars(0, len, array, 0);
int n;
for (n = len; n < size - n; n <<= 1) {
System.arraycopy(array, 0, array, n, n);
}
System.arraycopy(array, 0, array, n, size - n);
return new String(array);
}
}
} else {
return "";
}
}
public static String hide(CharSequence str, int startInclude, int endExclude) {
return replace(str, startInclude, endExclude, '*');
}
public static String replace(CharSequence str, int startInclude, int endExclude, char replacedChar) {
if (isEmpty(str)) {
return str(str);
} else {
String originalStr = str(str);
int[] strCodePoints = originalStr.codePoints().toArray();
int strLength = strCodePoints.length;
if (startInclude > strLength) {
return originalStr;
} else {
if (endExclude > strLength) {
endExclude = strLength;
}
if (startInclude > endExclude) {
return originalStr;
} else {
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < strLength; ++i) {
if (i >= startInclude && i < endExclude) {
stringBuilder.append(replacedChar);
} else {
stringBuilder.append(new String(strCodePoints, i, 1));
}
}
return stringBuilder.toString();
}
}
}
}
public static String replace(CharSequence str, int startInclude, int endExclude, CharSequence replacedStr) {
if (isEmpty(str)) {
return str(str);
} else {
String originalStr = str(str);
int[] strCodePoints = originalStr.codePoints().toArray();
int strLength = strCodePoints.length;
if (startInclude > strLength) {
return originalStr;
} else {
if (endExclude > strLength) {
endExclude = strLength;
}
if (startInclude > endExclude) {
return originalStr;
} else {
StringBuilder stringBuilder = new StringBuilder();
int i;
for(i = 0; i < startInclude; ++i) {
stringBuilder.append(new String(strCodePoints, i, 1));
}
stringBuilder.append(replacedStr);
for(i = endExclude; i < strLength; ++i) {
stringBuilder.append(new String(strCodePoints, i, 1));
}
return stringBuilder.toString();
}
}
}
}
public static String subPre(CharSequence string, int toIndexExclude) {
return sub(string, 0, toIndexExclude);
}
public static String sub(CharSequence str, int fromIndexInclude, int toIndexExclude) {
if (isEmpty(str)) {
return str(str);
} else {
int len = str.length();
if (fromIndexInclude < 0) {
fromIndexInclude += len;
if (fromIndexInclude < 0) {
fromIndexInclude = 0;
}
} else if (fromIndexInclude > len) {
fromIndexInclude = len;
}
if (toIndexExclude < 0) {
toIndexExclude += len;
if (toIndexExclude < 0) {
toIndexExclude = len;
}
} else if (toIndexExclude > len) {
toIndexExclude = len;
}
if (toIndexExclude < fromIndexInclude) {
int tmp = fromIndexInclude;
fromIndexInclude = toIndexExclude;
toIndexExclude = tmp;
}
return fromIndexInclude == toIndexExclude ? "" : str.toString().substring(fromIndexInclude, toIndexExclude);
}
}
public static boolean isEmpty(CharSequence str) {
return str == null || str.length() == 0;
}
public static String str(CharSequence cs) {
return null == cs ? null : cs.toString();
}
/**
* [中文姓名] 只显示第一个汉字,其他隐藏为星号<例子:李**>
*
* @param fullName 姓名
* @return
*/
public static String chineseName(String fullName) {
if (StringUtils.isBlank(fullName)) {
return "";
}
String name = StringUtils.left(fullName, 1);
return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
}
/**
* [中文姓名] 只显示姓氏,其他隐藏为星号<例子:欧阳娜娜 : 欧阳**>
*
* @param familyName 姓氏
* @param givenName 名字
* @return
*/
public static String chineseName(String familyName, String givenName) {
if (StringUtils.isBlank(familyName) || StringUtils.isBlank(givenName)) {
return "";
}
if (familyName.length() > 1) {
String name = StringUtils.left(familyName, familyName.length());
return StringUtils.rightPad(name, StringUtils.length(familyName + givenName), "*");
}
return chineseName(familyName + givenName);
}
/**
* [身份证号] 显示最后四位,其他隐藏。共计18位或者15位。<例子:*************5762>
*
* @param id
* @return
*/
public static String idCardNum(String id) {
if (StringUtils.isBlank(id)) {
return "";
}
String num = StringUtils.right(id, 4);
return StringUtils.leftPad(num, StringUtils.length(id), "*");
}
/**
* [身份证号] 前六位,后四位,其他用星号隐藏每位1个星号<例子:451002********1647>
*
* @param carId
* @return
*/
public static String idCard(String carId) {
if (StringUtils.isBlank(carId)) {
return "";
}
return StringUtils.left(carId, 6).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(carId, 4), StringUtils.length(carId), "*"), "******"));
}
/**
* [固定电话] 后四位,其他隐藏<例子:****1234>
*
* @param num
* @return
*/
public static String fixedPhone(String num) {
if (StringUtils.isBlank(num)) {
return "";
}
return StringUtils.leftPad(StringUtils.right(num, 4), StringUtils.length(num), "*");
}
/**
* [手机号码] 前三位,后四位,其他隐藏<例子:138******1234>
*
* @param num
* @return
*/
public static String mobilePhone(String num) {
if (StringUtils.isBlank(num)) {
return "";
}
return StringUtils.left(num, 3).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(num, 4), StringUtils.length(num), "*"), "***"));
}
/**
* [地址] 只显示到地区,不显示详细地址;我们要对个人信息增强保护<例子:北京市海淀区****>
*
* @param address
* @param sensitiveSize 敏感信息长度
* @return
*/
public static String address(String address, int sensitiveSize) {
if (StringUtils.isBlank(address)) {
return "";
}
int length = StringUtils.length(address);
return StringUtils.rightPad(StringUtils.left(address, length - sensitiveSize), length, "*");
}
/**
* [电子邮箱] 邮箱前缀仅显示第一个字母,前缀其他隐藏,用星号代替,@及后面的地址显示<例子:g**@163.com>
*
* @param email
* @return
*/
public static String email(String email) {
if (StringUtils.isBlank(email)) {
return "";
}
int index = StringUtils.indexOf(email, "@");
if (index <= 1) {
return email;
}
return StringUtils.rightPad(StringUtils.left(email, 1), index, "*").concat(StringUtils.mid(email, index, StringUtils.length(email)));
}
/**
* [银行卡号] 前六位,后四位,其他用星号隐藏每位1个星号<例子:6222600**********1234>
*
* @param cardNum
* @return
*/
public static String bankCard(String cardNum) {
if (StringUtils.isBlank(cardNum)) {
return "";
}
return StringUtils.left(cardNum, 6).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(cardNum, 4), StringUtils.length(cardNum), "*"), "******"));
}
/**
* [公司开户银行联号] 公司开户银行联行号,显示前两位,其他用星号隐藏,每位1个星号<例子:12********>
*
* @param code
* @return
*/
public static String cnapsCode(String code) {
if (StringUtils.isBlank(code)) {
return "";
}
return StringUtils.rightPad(StringUtils.left(code, 2), StringUtils.length(code), "*");
}
}
java脱敏处理
最新推荐文章于 2024-07-26 03:18:41 发布