zoukankan      html  css  js  c++  java
  • 脱敏工具类

    /**
     * 通用脱敏工具类
     * 可用于:
     *      用户名
     *      手机号
     *      邮箱
     *      地址等
     */
    public class DesensitizationUtil {
    
        private static final int SIZE = 6;
        private static final String SYMBOL = "*";
    
        public static void main(String[] args) {
            String name = commonDisplay("赵镇啊");
            String mobile = commonDisplay("13900000000");
            String mail = commonDisplay("admin@imooc.com");
            String address = commonDisplay("北京大运河东路888号");
    
            System.out.println(name);
            System.out.println(mobile);
            System.out.println(mail);
            System.out.println(address);
        }
    
        /**
         * 通用脱敏方法
         * @param value
         * @return
         */
        public static String commonDisplay(String value) {
            if (null == value || "".equals(value)) {
                return value;
            }
            int len = value.length();
            int pamaone = len / 2;
            int pamatwo = pamaone - 1;
            int pamathree = len % 2;
            StringBuilder stringBuilder = new StringBuilder();
            if (len <= 2) {
                if (pamathree == 1) {
                    return SYMBOL;
                }
                stringBuilder.append(SYMBOL);
                stringBuilder.append(value.charAt(len - 1));
            } else {
                if (pamatwo <= 0) {
                    stringBuilder.append(value.substring(0, 1));
                    stringBuilder.append(SYMBOL);
                    stringBuilder.append(value.substring(len - 1, len));
    
                } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) {
                    int pamafive = (len - SIZE) / 2;
                    stringBuilder.append(value.substring(0, pamafive));
                    for (int i = 0; i < SIZE; i++) {
                        stringBuilder.append(SYMBOL);
                    }
                    if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) {
                        stringBuilder.append(value.substring(len - pamafive, len));
                    } else {
                        stringBuilder.append(value.substring(len - (pamafive + 1), len));
                    }
                } else {
                    int pamafour = len - 2;
                    stringBuilder.append(value.substring(0, 1));
                    for (int i = 0; i < pamafour; i++) {
                        stringBuilder.append(SYMBOL);
                    }
                    stringBuilder.append(value.substring(len - 1, len));
                }
            }
            return stringBuilder.toString();
        }
    
    }
    
    

    脱敏工具类,用于手机号昵称等脱敏

    欢迎搜索关注本人与朋友共同开发的微信面经小程序【大厂面试助手】和公众号【微瞰技术】

    file
    file

  • 相关阅读:
    mongo批量删除js示例
    Mongo压测介绍
    线性代数学习笔记(1)--二维向量点积本质(仅供自己理解)
    墨菲定律的正确解读
    spring cloud 2020.0.1踩坑记录-bootstrap不生效等
    记一次网络质量原因导致接口调用超时的调查过程
    .NET Debugging Demos Lab 2: Crash
    .NET Debugging Demos Lab 3: Memory
    Hang caused by GC
    .NET Debugging Demos Lab 4: High CPU hang
  • 原文地址:https://www.cnblogs.com/zhendiao/p/14159525.html
Copyright © 2011-2022 走看看