package org.dromara.common.encrypt.utils;
import org.apache.commons.lang3.StringUtils;

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

/**
 * 敏感数据脱敏工具类
 */
public class Desensitization {
    /**
     * 身份证号脱敏
     *
     * @param idCard
     * @return
     */
    public static String idCardDesensitization(String idCard) {
        if (StringUtils.isNotEmpty(idCard)) {
            // 身份证号脱敏规则一：保留前六后三
            if (idCard.length() == 15) {
                idCard = idCard.replaceAll("(\\w{6})\\w*(\\w{3})", "$1******$2");
            } else if (idCard.length() == 18) {
                idCard = idCard.replaceAll("(\\w{6})\\w*(\\w{3})", "$1*********$2");
            }
            // 身份证号脱敏规则二：保留前三后四
            // idCard = idCard.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
        }
        return idCard;
    }

    /**
     * 手机号码脱敏
     *
     * @param mobilePhone
     * @return
     */
    public static String mobilePhoneDesensitization(String mobilePhone) {
        // 手机号码保留前三后四
        if (StringUtils.isNotEmpty(mobilePhone)) {
            mobilePhone = mobilePhone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }
        return mobilePhone;
    }

}
