Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 增加一种秘文类型正则脱敏规则 #309

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ballcat.desensitize.rule.regex;

/**
* 【密文】脱敏,前3后2,中间替换为 4个 * eg. 3950587458326514452641976780061 -> 395****61
*
* @author evil0th
* @since 2.0.0
*/
public class KeyRegexDesensitizeRule implements RegexDesensitizeRule {

@Override
public String getRegex() {
return "(.{3}).*(.{2}$)";
}

@Override
public String getReplacement() {
return "$1****$2";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.ballcat.desensitize.handler.PhoneNumberDesensitizationHandler;
import org.ballcat.desensitize.rule.regex.EmailRegexDesensitizeRule;
import org.ballcat.desensitize.rule.regex.KeyRegexDesensitizeRule;
import org.ballcat.desensitize.rule.slide.IdCardNoSlideDesensitizeRule;
import org.ballcat.desensitize.util.DesensitizationUtil;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -53,6 +54,8 @@ void test() {
assertEquals("t****@qq.com", DesensitizationUtil.maskEmail("test.demo@qq.com"));
assertEquals("622260**********1234", DesensitizationUtil.maskBankCardNo("62226000000043211234"));
assertEquals("******", DesensitizationUtil.maskPassword(UUID.randomUUID().toString()));
assertEquals("395****61",
DesensitizationUtil.maskByRegex("3950587458326514452641976780061", new KeyRegexDesensitizeRule()));
assertEquals("192.*.*.*", DesensitizationUtil.maskIP("192.168.2.1"));
assertEquals("2001:*:*:*:*:*:*:*", DesensitizationUtil.maskIP("2001:0db8:02de:0000:0000:0000:0000:0e13"));
assertEquals("2001:*:*:*:*:*:*:*", DesensitizationUtil.maskIP("2001:db8:2de:0:0:0:0:e13"));
Expand Down
Loading