数据脱敏工具类

用于电话、身份证、IP地址等数据脱敏处理

package com.mhi.pocp.server.system.utils;

import com.google.common.base.Strings;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.http.util.TextUtils;

import java.util.List;
import java.util.regex.Pattern;


/**
 * 数据脱敏工具类
 * 
 * @author Duncino
 * @date 2022/03/28
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

	private static Pattern telephonePattern = Pattern.compile("^(0[0-9]{2,3}\\\\-)?([1-9][0-9]{6,7})$");
	private static Pattern mobilePhonePattern = Pattern
			.compile("^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$");

	private static String IP_REGEX = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
	/**
	 * 比较值
	 * 
	 * @param str1
	 * @param str2
	 * @return -1:str1 < str2,0:str1 = str2,1:str1 > str2
	 */
	public static int compare(String str1, String str2) {
		if (str1 == null) {
			return -1;
		}
		if (str2 == null) {
			return 1;
		}
		int c = str1.compareTo(str2);
		return c == 0 ? 0 : (c < 0 ? -1 : 1);
	}

	public static boolean checkPhone(String phone) {
		return !checkPattern(mobilePhonePattern, phone) && !checkPattern(telephonePattern, phone);
	}

	private static boolean checkPattern(Pattern pattern, String valueStr) {
		return pattern.matcher(valueStr).matches();
	}

	public static String joinNotEmpty(String... string) {
		StringBuffer sb = new StringBuffer();
		for (String s : string) {
			if (s != null) {
				sb.append(s);
			}
		}
		return sb.toString();
	}

	/**
	 * 名字脱敏
	 *
	 * @param name
	 * @return
	 */
	public static String nameEncrypt(String name) {
		if (org.apache.commons.lang.StringUtils.isNotEmpty(name)) {
			if (name.length() >= 4) {
				name = name.substring(0, name.length() - 2) + "**";
			} else {
				name = name.substring(0, name.length() - 1) + "*";
			}
		}
		return name;
	}

	public static String maskStr(String str) {
		if (TextUtils.isBlank(str)) {
			return str;
		}

		int length = str.length();
		int prefixLength = length / 3;
		int start = prefixLength;
		int end = length - prefixLength - 1;

		StringBuilder sbMask = new StringBuilder();

		for (int i = 0; i < length; i++) {
			if (i >= start && i <= end) {
				sbMask.append("*");
			} else {
				sbMask.append(str.charAt(i));
			}
		}

		return sbMask.toString();
	}
	
	/**
	 * 脱敏IP地址
	 * @param str
	 * @return
	 */
	public static String maskIp(String str) {
		if (Strings.isNullOrEmpty(str)) {
			return str;
		}
		return str.replaceAll(IP_REGEX, "*.*.*.*");
	}

	public static Boolean isNumberic(String str) {
		Pattern p = Pattern.compile("(^[\\-0-9][0-9]*(.[0-9]+)?)$");
		return p.matcher(str).find();
	}

	public static Boolean anyMatch(String inputStr, List<String> matchList) {
		Boolean result = false;
		if (isNotEmpty(inputStr)) {
			result = matchList.stream().anyMatch(inputStr::contains);
		}
		return result;
	}

	/**
	 * 以英文逗号分割字符串
	 * 
	 * @param s
	 * @return
	 */
	public static String[] splitIncludeModule(String s) {
		if (StringUtils.isNotEmpty(s)) {
			return s.split(",");
		}
		return new String[0];
	}

	/**
	 * 判断是否为数字或者小数
	 * @param str
	 * @return
	 **/
	public static boolean isNumeric(String str){
		Pattern pattern = Pattern.compile("[0-9]*");
		if (str.indexOf(".") > 0) {
			if (str.indexOf(".") == str.lastIndexOf(".") && str.split("\\.").length == 2) {
				return pattern.matcher(str.replace(".", "")).matches();
			} else {
				return false;
			}
		} else {
			return pattern.matcher(str).matches();
		}
	}

	/**
	 * 手机号码脱敏,只显示前三后四
	 *
	 * @param mobile
	 * @return
	 */
	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");
	}

	//护照脱敏,显示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);
	}

	//身份证脱敏,显示前三后四
	public static String idEncrypt(String id) {
		if (StringUtils.isEmpty(id) || (id.length() < 8)) {
			return id;
		}
		return id.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
	}

    /**
     * 检查字符串 @sourceData 中是否包含关键字 @keywords
     *
     * @param sourceData
     * @param keywords
     * @return true-包含,else false
     **/
    public static boolean checkDataContains(String sourceData, List<String> keywords) {
        if (StringUtils.isNotEmpty(sourceData) && CollectionUtils.isNotEmpty(keywords)) {
            for (String keyword : keywords) {
                if (sourceData.contains(keyword)) {
                    return true;
                }
            }
        }
        return false;
    }

	/**
	 * 二进制转化为16进制
	 * @param bytes
	 * @return
	 */
	public static String bytes2hex(byte[] bytes) {
		byte[] copyBytes = new byte[28];
		System.arraycopy(bytes, 0, copyBytes, 0, 28);
		StringBuilder hex = new StringBuilder();
		for (int i = 0; i < copyBytes.length; i++) {
			String temp = Integer.toHexString(copyBytes[i] & 0xFF);
			if (temp.length() == 1) {
				hex.append("0");
			}
			hex.append(temp.toLowerCase());
		}
		return hex.toString();
	}

	public static String listDescToString(List<String> list){
		StringBuilder builder = new StringBuilder();
		if (null != list && list.size() > 0){
			for (int j = 0; j < list.size(); j++) {
				builder.append(j + 1);
				builder.append("、");
				builder.append(list.get(j));
				builder.append("。");
				builder.append(" ");
				if (j != list.size() - 1){
					builder.append("\n");
				}
			}
		}
		return builder.toString();
	}
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值