zoukankan      html  css  js  c++  java
  • 终于有了,史上最强大的数据脱敏处理算法

    哈哈,标题党了,向你道歉!

    言归正传。

    我们的应用系统里,为保护用户隐私,用户的敏感信息经常要做脱敏显示或脱敏存储,比如用户的身份证号、手机号、银行卡,等等。在支付系统或金融系统,数据安全是第一要务,数据的脱敏处理更是必选项。

    【身份证号脱敏示例】120115201406180712 脱敏后:120115********0712
    【银行卡号脱敏示例】9558820200019833888 脱敏后:955882*********3888
    【手机号脱敏示例】18810754438 脱敏后:188******38

    算法实现原理很简单,就是保留头尾字符,把中间的部分用特殊字符如星号“*”作为掩码来表示。

    网上类似算法很多。这里提供一个可供参考:

    import java.util.Arrays;
    import java.util.concurrent.atomic.AtomicInteger;
        public static String tuoMin(String str, int headCharCount, int tailCharCount) {
            if(str.length()<headCharCount+tailCharCount){
                throw new IllegalArgumentException("明文过短,无法脱敏");
            }
            String repeat = "";
    
            int len = str.length() - headCharCount - tailCharCount;
            if (len > 0) {
                char[] buf = new char[len];
                AtomicInteger integer = new AtomicInteger(0);
                Arrays.asList(new Integer[len]).stream().forEach(b -> buf[integer.getAndIncrement()] = '*');
                repeat = new String(buf);
            }
            return str.substring(0, headCharCount) + repeat + str.substring(str.length() - tailCharCount);
        }

    测试案例:

        public static void main(String[] args) {
            System.out.println(tuoMin("120115201406180712", 6, 4));
            System.out.println(tuoMin("9558820200019833888", 6, 4));
            System.out.println(tuoMin("18810754438", 3, 2));
        }

    结果输出:

    120115********0712
    955882*********3888
    188******38
  • 相关阅读:
    初始化Winsock库
    memset与初始化
    老板不在,嚣张的正则
    教研室的下午,取快递的一天
    教研室的夜晚
    真不知道起什么名字了
    任性就是没长大咯
    难得起得早,难得周六上班
    工欲学其语,必先装软件
    151008-JS初级完成,PHP入门(变量常量等)-没假放了
  • 原文地址:https://www.cnblogs.com/buguge/p/11930553.html
Copyright © 2011-2022 走看看