|  | @ -0,0 +1,143 @@
 | 
	
		
			
				|  |  | package com.yihu.iot.util.conceal;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * 数据脱敏工具
 | 
	
		
			
				|  |  |  * Created by zdm on 2019/4/2.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | public class ConcealUtil {
 | 
	
		
			
				|  |  |     private static final int SIZE = 6;
 | 
	
		
			
				|  |  |     private static final String SYMBOL = "*";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 电话号码脱敏,达到保密效果
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param userName 用户名
 | 
	
		
			
				|  |  |      * @return 替换后的用户名
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static String phoneConceal(String userName) {
 | 
	
		
			
				|  |  |         String userNameAfterReplaced = "";
 | 
	
		
			
				|  |  |         if (userName == null) {
 | 
	
		
			
				|  |  |             userName = "";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         int nameLength = userName.length();
 | 
	
		
			
				|  |  |         if (nameLength <= 1) {
 | 
	
		
			
				|  |  |             userNameAfterReplaced = "*";
 | 
	
		
			
				|  |  |         } else if (nameLength == 2) {
 | 
	
		
			
				|  |  |             userNameAfterReplaced = replaceAction(userName, "(?<=\\d{0})\\d(?=\\d{1})");
 | 
	
		
			
				|  |  |         } else if (nameLength >= 3 && nameLength <= 6) {
 | 
	
		
			
				|  |  |             userNameAfterReplaced = replaceAction(userName, "(?<=\\d{1})\\d(?=\\d{1})");
 | 
	
		
			
				|  |  |         } else if (nameLength >= 7 && nameLength <= 10) {
 | 
	
		
			
				|  |  |             userNameAfterReplaced = replaceAction(userName, "(?<=\\d{3})\\d(?=\\d{3})");
 | 
	
		
			
				|  |  |         } else if (nameLength >= 11) {
 | 
	
		
			
				|  |  |             userNameAfterReplaced = replaceAction(userName, "(?<=\\d{3})\\d(?=\\d{4})");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return userNameAfterReplaced;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 实际替换动作
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param username username
 | 
	
		
			
				|  |  |      * @param regular  正则
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     private static String replaceAction(String username, String regular) {
 | 
	
		
			
				|  |  |         return username.replaceAll(regular, "*");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 身份证号替换,保留前四位和后四位
 | 
	
		
			
				|  |  |      * <p>
 | 
	
		
			
				|  |  |      * 如果身份证号为空 或者 null ,返回null ;否则,返回替换后的字符串;
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param idCard 身份证号
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static String idCardConceal(String idCard) {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if (idCard.isEmpty() || idCard == null) {
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |             return replaceAction(idCard, "(?<=\\d{6})\\d(?=\\d{4})");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 银行卡替换,保留后四位
 | 
	
		
			
				|  |  |      * <p>
 | 
	
		
			
				|  |  |      * 如果银行卡号为空 或者 null ,返回null ;否则,返回替换后的字符串;
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param bankCard 银行卡号
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static String bankCardConceal(String bankCard) {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if (bankCard.isEmpty() || bankCard == null) {
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |             return replaceAction(bankCard, "(?<=\\d{0})\\d(?=\\d{4})");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 姓名及地址脱敏
 | 
	
		
			
				|  |  |      * @param value
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public static String nameOrAddrConceal(String value) {
 | 
	
		
			
				|  |  |         if (null == value || "".equals(value)) {
 | 
	
		
			
				|  |  |             return value;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         int len = value.length();
 | 
	
		
			
				|  |  |         int pamaone = len / 2;
 | 
	
		
			
				|  |  |         int pamatwo = pamaone - 1;
 | 
	
		
			
				|  |  |         int pamathree = len % 2;
 | 
	
		
			
				|  |  |         StringBuilder stringBuilder = new StringBuilder();
 | 
	
		
			
				|  |  |         if (len <= 2) {
 | 
	
		
			
				|  |  |             if (pamathree == 1) {
 | 
	
		
			
				|  |  |                 return SYMBOL;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             stringBuilder.append(value.charAt(0));
 | 
	
		
			
				|  |  |             stringBuilder.append(SYMBOL);
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |             if (pamatwo <= 0) {
 | 
	
		
			
				|  |  |                 stringBuilder.append(value.substring(0, 1));
 | 
	
		
			
				|  |  |                 stringBuilder.append(SYMBOL);
 | 
	
		
			
				|  |  |                 stringBuilder.append(value.substring(len - 1, len));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) {
 | 
	
		
			
				|  |  |                 int pamafive = (len - SIZE) / 2;
 | 
	
		
			
				|  |  |                 stringBuilder.append(value.substring(0, pamafive));
 | 
	
		
			
				|  |  |                 for (int i = 0; i < SIZE; i++) {
 | 
	
		
			
				|  |  |                     stringBuilder.append(SYMBOL);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) {
 | 
	
		
			
				|  |  |                     stringBuilder.append(value.substring(len - pamafive, len));
 | 
	
		
			
				|  |  |                 } else {
 | 
	
		
			
				|  |  |                     stringBuilder.append(value.substring(len - (pamafive + 1), len));
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 int pamafour = len - 2;
 | 
	
		
			
				|  |  |                 stringBuilder.append(value.substring(0, 1));
 | 
	
		
			
				|  |  |                 for (int i = 0; i < pamafour; i++) {
 | 
	
		
			
				|  |  |                     stringBuilder.append(SYMBOL);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 stringBuilder.append(value.substring(len - 1, len));
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return stringBuilder.toString();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /*    public static void main(String[] args) throws IOException {
 | 
	
		
			
				|  |  |         String strName= nameOrAddrConceal("张三");
 | 
	
		
			
				|  |  |         System.out.println(strName);
 | 
	
		
			
				|  |  |         String addr= nameOrAddrConceal("厦门市");
 | 
	
		
			
				|  |  |         System.out.println(addr);
 | 
	
		
			
				|  |  |         String idcard= idCardConceal("350825199012033283");
 | 
	
		
			
				|  |  |         System.out.println(idcard);
 | 
	
		
			
				|  |  |         String phone= phoneConceal("17689202624");
 | 
	
		
			
				|  |  |         System.out.println(phone);
 | 
	
		
			
				|  |  |     }*/
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |