001/**
002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package com.mybatisflex.core.mask;
017
018/**
019 * 内置的数据脱敏方式
020 */
021public class Masks {
022
023    /**
024     * 手机号脱敏
025     */
026    public static final String MOBILE = "mobile";
027
028    /**
029     * 固定电话脱敏
030     */
031    public static final String FIXED_PHONE = "fixed_phone";
032
033    /**
034     * 身份证号脱敏
035     */
036    public static final String ID_CARD_NUMBER = "id_card_number";
037
038    /**
039     * 中文名脱敏
040     */
041    public static final String CHINESE_NAME = "chinese_name";
042
043    /**
044     * 地址脱敏
045     */
046    public static final String ADDRESS = "address";
047
048    /**
049     * 邮件脱敏
050     */
051    public static final String EMAIL = "email";
052
053    /**
054     * 密码脱敏
055     */
056    public static final String PASSWORD = "password";
057
058    /**
059     * 车牌号脱敏
060     */
061    public static final String CAR_LICENSE = "car_license";
062
063    /**
064     * 银行卡号脱敏
065     */
066    public static final String BANK_CARD_NUMBER = "bank_card_number";
067
068
069    private static String createMask(int count) {
070        StringBuilder mask = new StringBuilder();
071        for (int i = 0; i < count; i++) {
072            mask.append("*");
073        }
074        return mask.toString();
075    }
076
077
078    private static String mask(String needToMaskString, int keepFirstCount, int keepLastCount, int maskCount) {
079        return needToMaskString.substring(0, keepFirstCount)
080                + createMask(maskCount)
081                + needToMaskString.substring(needToMaskString.length() - keepLastCount);
082    }
083
084
085    /**
086     * 手机号脱敏处理器
087     * 保留前三后四,中间的为星号  "*"
088     */
089    static MaskProcesser MOBILE_PROCESSER = data -> {
090        if (data instanceof String && ((String) data).startsWith("1") && ((String) data).length() == 11) {
091            return mask((String) data, 3, 4, 4);
092        }
093        return data;
094    };
095
096
097    /**
098     * 固定电话脱敏
099     * 保留前三后四,中间的为星号  "*"
100     */
101    static MaskProcesser FIXED_PHONE_PROCESSER = data -> {
102        if (data instanceof String && ((String) data).length() > 5) {
103            return mask((String) data, 3, 2, ((String) data).length() - 5);
104        }
105        return data;
106    };
107
108
109    /**
110     * 身份证号脱敏处理器
111     * 身份证号的保留前三后四,中间的数为星号  "*"
112     */
113    static MaskProcesser ID_CARD_NUMBER_PROCESSER = data -> {
114        if (data instanceof String && ((String) data).length() >= 15) {
115            return mask((String) data, 3, 4, ((String) data).length() - 7);
116        }
117        return data;
118    };
119
120
121    /**
122     * 姓名脱敏
123     */
124    static MaskProcesser CHINESE_NAME_PROCESSER = data -> {
125        if (data instanceof String) {
126            String name = (String) data;
127            if (name.length() == 2) {
128                return name.charAt(0) + "*";
129            } else if (name.length() == 3) {
130                return name.charAt(0) + "*" + name.charAt(2);
131            } else if (name.length() == 4) {
132                return "**" + name.substring(2, 4);
133            } else if (name.length() > 4) {
134                return mask(name, 2, 1, name.length() - 3);
135            }
136        }
137        return data;
138    };
139
140
141    /**
142     * 地址脱敏
143     */
144    static MaskProcesser ADDRESS_PROCESSER = data -> {
145        if (data instanceof String) {
146            String address = (String) data;
147            if (address.length() > 6) {
148                return mask(address, 6, 0, 3);
149            } else if (address.length() > 3) {
150                return mask(address, 3, 0, 3);
151            }
152        }
153        return data;
154    };
155
156
157    /**
158     * email 脱敏
159     */
160    static MaskProcesser EMAIL_PROCESSER = data -> {
161        if (data instanceof String && ((String) data).contains("@")) {
162            String fullEmail = (String) data;
163            int indexOf = fullEmail.lastIndexOf("@");
164            String email = fullEmail.substring(0, indexOf);
165
166            if (email.length() == 1) {
167                return "*" + fullEmail.substring(indexOf);
168            } else if (email.length() == 2) {
169                return "**" + fullEmail.substring(indexOf);
170            } else if (email.length() < 5) {
171                return mask(email, 2, 0, email.length() - 2) + fullEmail.substring(indexOf);
172            } else {
173                return mask(email, 3, 0, email.length() - 3) + fullEmail.substring(indexOf);
174            }
175        }
176        return data;
177    };
178
179
180    /**
181     * 密码 脱敏
182     */
183    static MaskProcesser PASSWORD_PROCESSER = data -> {
184        if (data instanceof String ) {
185            return mask((String) data, 0, 0, ((String) data).length()) ;
186        }
187        return data;
188    };
189
190
191    /**
192     * 车牌号 脱敏
193     */
194    static MaskProcesser CAR_LICENSE_PROCESSER = data -> {
195        if (data instanceof String) {
196            return mask((String) data, 3, 1, ((String) data).length() - 4);
197        }
198        return data;
199    };
200
201
202    /**
203     * 银行卡号 脱敏
204     */
205    static MaskProcesser BANK_CARD_PROCESSER = data -> {
206        if (data instanceof String && ((String) data).length() >= 8) {
207            return mask((String) data, 4, 4, 4);
208        }
209        return data;
210    };
211
212
213}