脱敏工具类

脱敏工具类

脱敏工具类

package com.xzxzxz.util;

import cn.hutool.core.lang.Console;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 脱敏字段范围:手机号、固话号码、身份证号码、电子邮箱、银行卡号、家庭住址
 * 数据脱敏的标准:
 * 身份证号码: 保留前6位和后4位,其余隐藏。示例:312104********3221
 * 电话号码:
 * 手机:11位手机号码,保留前3位和后4位,其余隐藏。示例:137****4321;12位手机号码,保留前4位和后4位,其余隐藏。示例:1348****6523;13位手机号码,保留前4位和后5位,其余隐藏。示例:0235****59621,均隐藏中间4位,以此类推。
 * 固话:不论几位区号,统一用3个*替代,区号后的电话号码保留后4位,其余隐藏。示例:原号码,021-62878783,脱敏后 ***-****8783;
 * 电子邮箱: 邮箱前缀仅显示第一个字母,前缀其他隐藏,@及后面的地址显示,示例:d**@126.com。
 * 银行卡号: 保留前2位和后4位。示例:62**********4821
 * 个人家庭住址:除国家以外的省、市、区、县等信息均用2个*替代,详细地址中的弄、号、门牌等数字信息均用两个*替代。示例:原地址:中国江苏省南京市栖霞区敏行路201号1502室,脱敏后:中国**省**市**区敏行路**号**室
 * 注:家庭住址仅需脱敏供内部人员使用的业务系统,快递小哥使用的前端系统不用脱敏
 * @data 2021-04-21 上午 10:13
 */
public class DesensitizationUtil {





/**
     * 身份证号前六后四脱敏
     *
     * @param id 身份证号
     * @return
     */

    public static String idEncrypt(String id) {
        if (StringUtils.isEmpty(id) || (id.length() < 8)) {
            return id;
        }
        return id.replaceAll("(?<=\\w{6})\\w(?=\\w{4})", "*");
    }


    /*
     * 邮箱脱敏
     *
     * @param email 邮箱
     * @return
     */

    public static String email(String email) {
        if (StringUtils.isEmpty(email)) {
            return "";
        }
        return email.replaceAll("(^\\w)[^@]*(@.*$)", "$1****$2");
    }


/**
     * 银行账户号码脱敏:显示前二后四,范例:62**********4568
     *
     * @param bankAcct 银行账户号码
     * @return
     */

    public static String encryptBankAcct(String bankAcct) {
        if (bankAcct == null) {
            return "";
        }
        return replaceBetween(bankAcct, 2, bankAcct.length() - 4, null);
    }


    private static String replaceBetween(String sourceStr, int begin, int end, String replacement) {
        if (sourceStr == null) {
            return "";
        }
        if (replacement == null) {
            replacement = "*";
        }
        int replaceLength = end - begin;
        if (StringUtils.isNotBlank(sourceStr) && replaceLength > 0) {
            StringBuilder sb = new StringBuilder(sourceStr);
            sb.replace(begin, end, StringUtils.repeat(replacement, replaceLength));
            return sb.toString();
        } else {
            return sourceStr;
        }
    }


/**
     *      * 脱敏地址字符串中的数字
     *      * @param address
     *      * @return
     *      
     */

    public static String addressTM(String address) {
        if (address == null) {
            return "";
        }
        char[] aa = address.toCharArray();
        String newAddr = "";
        String temp = "";
        for (int a = aa.length - 1; a >= 0; a--) {
            if ((int) aa[a] >= '0' && (int) aa[a] <= '9') {
                temp = aa[a] + temp;
            } else {
                if (temp.length() > 0) {
                    int l = 2;
                    newAddr = temp.substring((temp.length() < l ? l : temp.length()), temp.length()) + newAddr;
                    if (temp.length() - 1 < 1) {
                        newAddr = "*" + newAddr;
                    } else {
                        for (int b = 0; b < temp.length() - 1; b++) {
                            newAddr = "*" + newAddr;
                        }
                    }
                }
                temp = "";
                newAddr = aa[a] + newAddr;
            }
        }
        //武胜路333号1层
        Pattern p = Pattern.compile(".*\\d+.*");
        Matcher m = p.matcher(temp);
        if (m.matches()) {
            if (temp.length() > 1) {
                newAddr = "*" + temp.substring(temp.length() - 1, temp.length()) + newAddr;
                for (int i = 0; i < temp.length() - 2; i++) {
                    newAddr = "*" + newAddr;
                }
            } else {
                newAddr = "*" + newAddr;
            }
        }
        return newAddr;
    }



    public static String mobileEncrypt(String mobile) {
        if (StringUtils.isEmpty(mobile)) {
            return "";
        }
        //固话
        if (mobile.indexOf("-") != -1){
            String s="***-";
            String[] split = mobile.split("-");
            String str = split[1];
            return s + str.replaceAll("(\\d{4})(\\d{4})", "****$2");
        }
        Console.log("手机号的长度{}", mobile.length());

        switch (mobile.length()) {
            case 11:
                return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
            case 12:
                return mobile.replaceAll("(\\d{4})\\d{4}(\\d{4})", "$1****$2");
            case 13:
                return mobile.replaceAll("(\\d{4})\\d{4}(\\d{5})", "$1****$2");
        }
        return "";
    }
 
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值