From 211330fa781ab404800565808c3c15b5c40df7c9 Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期三, 27 十月 2021 11:35:43 +0800
Subject: [PATCH] 敏感数据处理工具

---
 src/main/java/com/hx/util/BlurDataUtil.java |  119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 119 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/hx/util/BlurDataUtil.java b/src/main/java/com/hx/util/BlurDataUtil.java
new file mode 100644
index 0000000..bef9fd0
--- /dev/null
+++ b/src/main/java/com/hx/util/BlurDataUtil.java
@@ -0,0 +1,119 @@
+package com.hx.util;
+
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * 鏁版嵁鑴辨晱宸ュ叿绫�
+ *
+ */
+public class BlurDataUtil {
+
+    /**
+     * 鎵嬫満鍙疯劚鏁忓鐞�
+     * 鑴辨晱瑙勫垯: 淇濈暀鍓嶄笁鍚庡洓, 姣斿 18738291234 缃崲涓� 187****1234
+     * @param phone
+     * @return
+     */
+    public static final String blurPhone(String phone) {
+        if (StringUtils.isEmpty(phone) || (phone.length() != 11)) {
+            return phone;
+        }
+        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
+    }
+
+    /**
+     * 韬唤璇佸彿鑴辨晱澶勭悊
+     * 鍘熻韩浠借瘉鍙凤細500222202110275699锛岃劚鏁忓悗锛�132****99308084911
+     * @param idCard
+     * @return
+     */
+    public static String blurIdCard(String idCard) {
+        if (StringUtils.isEmpty(idCard)) {
+            return "";
+        }
+        /*
+         * 鍙傛暟1锛氳瘉浠跺彿锛屽弬鏁�2锛圤VERLAY锛夛細鏇挎崲鍚庣殑瀛楃涓诧紝
+         * 鍙傛暟3锛圫TART锛夛細鏇挎崲鐨勮捣濮嬩笅鏍囷紝鍙傛暟4锛圗ND锛夛細鏇挎崲鐨勭粨鏉熶笅鏍囷紙涓嶅寘鍚級
+         */
+        return StringUtils.overlay(idCard, "****", 3, 7);
+    }
+
+    /**
+     * 韬唤璇佸彿鑴辨晱澶勭悊
+     * 灞曠ず 鍓�6浣嶅拰鍚�6浣�
+     * @param idCard
+     * @return
+     */
+    public static String hiddenIdCard(String idCard) {//韬唤璇�
+        if (StringUtils.isBlank(idCard)) {
+            return "";
+        }
+        return StringUtils.left(idCard, 6).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(idCard, 4), StringUtils.length(idCard), "*"), "***"));
+    }
+
+    /**
+     * 閭鑴辨晱澶勭悊
+     * 鍘熼偖绠憋細zhangsan@qq.com锛岃劚鏁忓悗锛歾hang***@qq.com
+     * @param email
+     * @return
+     */
+    public static String blurEmail(String email) {
+        if (StringUtils.isEmpty(email)) {
+            return email;
+        }
+        String encrypt = email.replaceAll("(\\w+)\\w{3}@(\\w+)", "$1***@$2");
+        if(StringUtils.equalsIgnoreCase(email, encrypt)){
+            encrypt = email.replaceAll("(\\w*)\\w{1}@(\\w+)", "$1*@$2");
+        }
+        return encrypt;
+    }
+
+    /**
+     * 鎶ょ収鑴辨晱澶勭悊
+     * 鑴辨晱瑙勫垯锛氭姢鐓у墠2鍚�3浣嶈劚鏁忥紝鎶ょ収涓�鑸负8鎴�9浣�
+     * @param passport
+     * @return
+     */
+    public static String blurPassport(String passport) {
+        if (StringUtils.isEmpty(passport) || (passport.length() < 8)) {
+            return passport;
+        }
+        return passport.substring(0, 2) + new String(new char[passport.length() - 5]).replace("\0", "*") + passport.substring(passport.length() - 3);
+    }
+
+    /**
+     * 瀛楁淇℃伅鑴辨晱
+     * 鑴辨晱瑙勫垯:濡傛灉瀛楃闀垮害澶т簬3浣嶏紝鍒欓殣钘忔渶鍚庝笁浣嶏紝鍚﹀垯闅愯棌鏈�鍚�1浣�
+     * @param field
+     * @return
+     */
+    public static String blurField(String field) {
+        if (StringUtils.isEmpty(field)) {
+            return field;
+        }
+        String encrypt = field.replaceAll("(\\w+)\\w{3}", "$1***");
+        if(StringUtils.equalsIgnoreCase(field, encrypt)){
+            encrypt = field.replaceAll("(\\w*)\\w{1}", "$1*");
+        }
+        return encrypt;
+    }
+
+    public static void main(String[] args) {
+        System.out.println(blurPhone("18738291234"));  // 187****1234
+        System.out.println(blurIdCard("500222202110275699"));  // 500****99410275467
+        System.out.println(blurEmail("zhangsan@qq.com"));  // zhang***@qq.com
+        System.out.println(blurPassport("12345678"));  // 12***678
+        System.out.println(blurField("I feel so good"));  // I f*** so g***
+    }
+
+    /*
+     * 澶囨敞锛�
+     * 1銆丼tring.replaceAll(绗�1涓弬鏁版槸鑴辨晱绛涢�夌殑姝e垯锛岀2涓弬鏁版槸鑴辨晱鏇挎崲鐨勬鍒�)
+     * 2銆侀渶瑕佸紩鍏ommons-lang3锛岃繖涓熀鏈瘡涓」鐩兘鐢ㄥ埌
+     * <dependency>
+     *     <groupId>org.apache.commons</groupId>
+     *     <artifactId>commons-lang3</artifactId>
+     *     <version>3.7</version>
+     * </dependency>
+     */
+}
\ No newline at end of file

--
Gitblit v1.8.0