欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

身份证数据脱敏Utils

程序员文章站 2024-03-19 15:02:22
...
                                身份证数据脱敏Utils

身份证,等信息脱敏


import org.apache.commons.lang3.StringUtils;

/**
 * Created by huangquanwei on 2019/5/8 9:31
 */
public class MaskUtils {

    public static String maskCellphone(String cellphoneNo) {
        if ((cellphoneNo == null) || (cellphoneNo.trim().length() != 11)) {
            return cellphoneNo;
        }
        return cellphoneNo.substring(0, 3) + "****" + cellphoneNo.substring(cellphoneNo.length() - 4);
    }

    public static String maskEmail(String email) {
        if (StringUtils.isBlank(email)) {
            return "";
        }
        int index = StringUtils.indexOf(email, "@");
        if (index <= 1) {
            return email;
        } else {
            return StringUtils.rightPad(StringUtils.left(email, 1), index, "*")
                    .concat(StringUtils.mid(email, index, StringUtils.length(email)));
        }
    }

    private static String maskCardNo(String cardNo) {
        if ((cardNo == null) || (cardNo.trim().length() <= 8)) {
            return cardNo;
        }
        cardNo = cardNo.trim();
        int length = cardNo.length();
        String firstFourNo = cardNo.substring(0, 4);
        String lastFourNo = cardNo.substring(length - 4);
        String mask = "";
        for (int i = 0; i < length - 8; i++) {
            mask = mask + "*";
        }
        return firstFourNo + mask + lastFourNo;
    }

    private static String maskNameNos(String cardNo) {
        if ((cardNo == null)) {
            return cardNo;
        }
        cardNo = cardNo.trim();
        int length = cardNo.length();
        String firstFourNo = null;
        String lastFourNo  = null;
        String mask = "";
        if (length>8){
             firstFourNo = cardNo.substring(0, 4);
             lastFourNo = cardNo.substring(length - 4);
            for (int i = 0; i < length - 8; i++) {
                mask = mask + "*";
            }
        }else if (length<=8 && length>4){
            firstFourNo = cardNo.substring(0, 2);
            lastFourNo = cardNo.substring(length - 3);
            for (int i = 0; i < length - 5 ; i++) {
                mask = mask + "*";
            }
        }else if (length <= 4){
            firstFourNo = cardNo.substring(0, 1);
            lastFourNo = cardNo.substring(length - 1);
            for (int i = 0; i < length - 2 ; i++) {
                mask = mask + "*";
            }
        }
        return firstFourNo + mask + lastFourNo;
    }

    private static String maskRealNameNo(String cardNo) {
        if ((cardNo == null)) {
            return cardNo;
        }
        cardNo = cardNo.trim();
        int length = cardNo.length();
        String firstFourNo = null;
        String lastFourNo  = null;
        String mask = "";
            firstFourNo = cardNo.substring(length-1, length);
            for (int i = 0; i < length - 1; i++) {
                mask = mask + "*";
            }
        return mask+firstFourNo ;
    }


    public static String maskIDCardNo(String idCardNo) {
        return maskCardNo(idCardNo);
    }

    public static String maskUsernameNo(String bankCardNo) {
        return maskNameNos(bankCardNo);
    }

    public static String maskNameNo(String bankCardNo) {
        return maskRealNameNo(bankCardNo);
    }
}

相关标签: 身份证脱敏