【工具类】数据脱敏工具类

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;

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值