This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 7c5d17de7df Implements UNIFIED_CREDIT_CODE_RANDOM_REPLACE mask 
algorithm (#23683)
7c5d17de7df is described below

commit 7c5d17de7df82b9227898019094283f320966368
Author: lushaorong <[email protected]>
AuthorDate: Thu Feb 9 12:48:16 2023 +0800

    Implements UNIFIED_CREDIT_CODE_RANDOM_REPLACE mask algorithm (#23683)
    
    * Implements UNIFIED_CREDIT_CODE_RANDOM_REPLACE mask algorithm
    
    Signed-off-by: lushaorong <[email protected]>
    
    * update mask document
    
    Signed-off-by: lushaorong <[email protected]>
    
    * Update UnifiedCreditCodeRandomReplaceAlgorithmTest.java
    
    * Update mask.cn.md
    
    * Refactor UnifiedCreditCodeRandomReplaceAlgorithm
    
    Signed-off-by: lushaorong <[email protected]>
    
    ---------
    
    Signed-off-by: lushaorong <[email protected]>
    Co-authored-by: lushr <[email protected]>
---
 docs/document/content/dev-manual/mask.cn.md        |  1 +
 docs/document/content/dev-manual/mask.en.md        |  1 +
 .../common-config/builtin-algorithm/mask.cn.md     | 12 +++
 .../common-config/builtin-algorithm/mask.en.md     | 12 +++
 .../UnifiedCreditCodeRandomReplaceAlgorithm.java   | 99 ++++++++++++++++++++++
 ...rg.apache.shardingsphere.mask.spi.MaskAlgorithm |  1 +
 ...nifiedCreditCodeRandomReplaceAlgorithmTest.java | 51 +++++++++++
 7 files changed, 177 insertions(+)

diff --git a/docs/document/content/dev-manual/mask.cn.md 
b/docs/document/content/dev-manual/mask.cn.md
index 3273414376e..4e886ab9a9f 100644
--- a/docs/document/content/dev-manual/mask.cn.md
+++ b/docs/document/content/dev-manual/mask.cn.md
@@ -31,3 +31,4 @@ chapter = true
 | TELEPHONE_RANDOM_REPLACE                | ⼿机号随机替换数据脱敏算法  | 
[`org.apache.shardingsphere.mask.algorithm.replace.TelephoneRandomReplaceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/TelephoneRandomReplaceAlgorithm.java)
 |
 | LANDLINE_NUMBER_RANDOM_REPLACE          | 座机号码随机替换          | 
[`org.apache.shardingsphere.mask.algorithm.replace.LandlineNumberRandomAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithm.java)
 |
 | GENERIC_TABLE_RANDOM_REPLACE          | 通⽤表格随机替换            | 
[`org.apache.shardingsphere.mask.algorithm.replace.GenericTableRandomReplaceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/GenericTableRandomReplaceAlgorithm.java)
                 |
+| UNIFIED_CREDIT_CODE_RANDOM_REPLACE      | 统⼀信⽤码随机替换            | 
[`org.apache.shardingsphere.mask.algorithm.replace.UnifiedCreditCodeRandomReplaceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/UnifiedCreditCodeRandomReplaceAlgorithm.java)
                 |
diff --git a/docs/document/content/dev-manual/mask.en.md 
b/docs/document/content/dev-manual/mask.en.md
index 025712c3fb8..96267dab734 100644
--- a/docs/document/content/dev-manual/mask.en.md
+++ b/docs/document/content/dev-manual/mask.en.md
@@ -31,3 +31,4 @@ Data masking algorithm definition
 | TELEPHONE_RANDOM_REPLACE                | Telephone random replace data 
masking algorithm                | 
[`org.apache.shardingsphere.mask.algorithm.replace.TelephoneRandomReplaceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/TelephoneRandomReplaceAlgorithm.java)
 |
 | LANDLINE_NUMBER_RANDOM_REPLACE          | Landline number random replace 
data masking algorithm            | 
[`org.apache.shardingsphere.mask.algorithm.replace.LandlineNumberRandomAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithm.java)
 |
 | GENERIC_TABLE_RANDOM_REPLACE            |  Generic table random replace 
algorithm                        | 
[`org.apache.shardingsphere.mask.algorithm.replace.GenericTableRandomReplaceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/GenericTableRandomReplaceAlgorithm.java)
                 |
+| UNIFIED_CREDIT_CODE_RANDOM_REPLACE      | Unified credit code random replace 
algorithm                   | 
[`org.apache.shardingsphere.mask.algorithm.replace.UnifiedCreditCodeRandomReplaceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/UnifiedCreditCodeRandomReplaceAlgorithm.java)
                 |
diff --git 
a/docs/document/content/user-manual/common-config/builtin-algorithm/mask.cn.md 
b/docs/document/content/user-manual/common-config/builtin-algorithm/mask.cn.md
index bb23d1f4f21..9d87919ed01 100644
--- 
a/docs/document/content/user-manual/common-config/builtin-algorithm/mask.cn.md
+++ 
b/docs/document/content/user-manual/common-config/builtin-algorithm/mask.cn.md
@@ -148,6 +148,18 @@ weight = 9
 | digital-codes          | String   | 数字码表(以英文逗号分隔,默认值:0,1,2,3,4,5,6,7,8,9)    
                              |
 | special-codes          | String   | 
特殊字符码表(以英文逗号分隔,默认值:~,!,@,#,$,%,^,&,*,:,&lt;,&gt;,&#166;)|
 
+#### 统⼀信⽤码随机替换
+
+类型:UNIFIED_CREDIT_CODE_RANDOM_REPLACE
+
+可配置属性:
+
+| *名称*            | *数据类型* | *说明*        |
+|-----------------|----------|-------------|
+| registration-department-codes | String   | 登记管理部门代码(以英文逗号分隔) |
+| category-codes | String   | 机构类别代码(以英文逗号分隔) |
+| administrative-division-codes | String   | 行政区划随机码表(以英文逗号分隔) |
+
 ## 操作步骤
 1. 在脱敏规则中配置脱敏算法;
 2. 为脱敏算法指定脱敏算法类型。
diff --git 
a/docs/document/content/user-manual/common-config/builtin-algorithm/mask.en.md 
b/docs/document/content/user-manual/common-config/builtin-algorithm/mask.en.md
index 8dc72f3937d..dc15bee5224 100644
--- 
a/docs/document/content/user-manual/common-config/builtin-algorithm/mask.en.md
+++ 
b/docs/document/content/user-manual/common-config/builtin-algorithm/mask.en.md
@@ -148,6 +148,18 @@ Attributes:
 | digital-random-codes   | String   | Numbers (separate with comma, default 
value: 0,1,2,3,4,5,6,7,8,9)                                                |
 | special-codes          | String   | Special codes (separate with comma, 
default value: ~,!,@,#,$,%,^,&,*,:,&lt;,&gt;,&#166;)                         |
 
+#### Unified credit code random replace algorithm
+
+Type: UNIFIED_CREDIT_CODE_RANDOM_REPLACE
+
+Attributes:
+
+| *Name*          | *DataType* | *Description*                         |
+|-----------------|----------|-------------|
+| registration-department-codes | String   | Registration department code 
(separate with comma) |
+| category-codes | String   | Category code (separate with comma) |
+| administrative-division-codes | String  |   Administrative division code 
(separate with comma) |
+
 ## Operating Procedure
 1. Configure maskAlgorithms in a mask rule.
 2. Use relevant algorithm types in maskAlgorithms.
diff --git 
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/UnifiedCreditCodeRandomReplaceAlgorithm.java
 
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/UnifiedCreditCodeRandomReplaceAlgorithm.java
new file mode 100644
index 00000000000..d9e175db5fc
--- /dev/null
+++ 
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/UnifiedCreditCodeRandomReplaceAlgorithm.java
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *     http://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.apache.shardingsphere.mask.algorithm.replace;
+
+import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmPropsChecker;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.List;
+import java.util.Properties;
+import java.util.Random;
+import java.util.stream.Collectors;
+
+/**
+ * Unified credit code random replace algorithm.
+ */
+public final class UnifiedCreditCodeRandomReplaceAlgorithm implements 
MaskAlgorithm<Object, String> {
+    
+    private static final String REGISTRATION_DEPARTMENT_CODES = 
"registration-department-codes";
+    
+    private static final String CATEGORY_CODES = "category-codes";
+    
+    private static final String ADMINISTRATIVE_DIVISION_CODES = 
"administrative-division-codes";
+    
+    private List<Character> registrationDepartmentCodes;
+    
+    private List<Character> categoryCodes;
+    
+    private List<String> administrativeDivisionCodes;
+    
+    @Getter
+    private Properties props;
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+        this.registrationDepartmentCodes = 
createRegistrationDepartmentCodes(props);
+        this.categoryCodes = createCategoryCodes(props);
+        this.administrativeDivisionCodes = 
createAdministrativeDivisionCodes(props);
+    }
+    
+    private List<Character> createRegistrationDepartmentCodes(final Properties 
props) {
+        MaskAlgorithmPropsChecker.checkAtLeastOneCharConfig(props, 
REGISTRATION_DEPARTMENT_CODES, getType());
+        return 
Splitter.on(",").trimResults().splitToList(props.getProperty(REGISTRATION_DEPARTMENT_CODES)).stream().map(each
 -> each.charAt(0)).collect(Collectors.toList());
+    }
+    
+    private List<Character> createCategoryCodes(final Properties props) {
+        MaskAlgorithmPropsChecker.checkAtLeastOneCharConfig(props, 
CATEGORY_CODES, getType());
+        return 
Splitter.on(",").trimResults().splitToList(props.getProperty(CATEGORY_CODES)).stream().map(each
 -> each.charAt(0)).collect(Collectors.toList());
+    }
+    
+    private List<String> createAdministrativeDivisionCodes(final Properties 
props) {
+        MaskAlgorithmPropsChecker.checkAtLeastOneCharConfig(props, 
ADMINISTRATIVE_DIVISION_CODES, getType());
+        return 
Splitter.on(",").trimResults().splitToList(props.getProperty(ADMINISTRATIVE_DIVISION_CODES));
+    }
+    
+    @Override
+    public String mask(final Object plainValue) {
+        String result = null == plainValue ? null : String.valueOf(plainValue);
+        if (Strings.isNullOrEmpty(result)) {
+            return result;
+        }
+        return randomReplace();
+    }
+    
+    private String randomReplace() {
+        Random random = new Random();
+        StringBuilder result = new StringBuilder();
+        
result.append(registrationDepartmentCodes.get(random.nextInt(registrationDepartmentCodes.size())));
+        result.append(categoryCodes.get(random.nextInt(categoryCodes.size())));
+        
result.append(administrativeDivisionCodes.get(random.nextInt(administrativeDivisionCodes.size())));
+        for (int i = 0; i < 10; i++) {
+            result.append(Character.forDigit(random.nextInt(10), 10));
+        }
+        return result.toString();
+    }
+    
+    @Override
+    public String getType() {
+        return "UNIFIED_CREDIT_CODE_RANDOM_REPLACE";
+    }
+}
diff --git 
a/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
 
b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
index fa34cf4c2fd..8653596044b 100644
--- 
a/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
+++ 
b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
@@ -27,3 +27,4 @@ 
org.apache.shardingsphere.mask.algorithm.replace.PersonalIdentityNumberRandomRep
 
org.apache.shardingsphere.mask.algorithm.replace.MilitaryIdentityNumberRandomReplaceAlgorithm
 org.apache.shardingsphere.mask.algorithm.replace.LandlineNumberRandomAlgorithm
 
org.apache.shardingsphere.mask.algorithm.replace.GenericTableRandomReplaceAlgorithm
+org.apache.shardingsphere.mask.algorithm.replace.UnifiedCreditCodeRandomReplaceAlgorithm
diff --git 
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/UnifiedCreditCodeRandomReplaceAlgorithmTest.java
 
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/UnifiedCreditCodeRandomReplaceAlgorithmTest.java
new file mode 100644
index 00000000000..892278dc366
--- /dev/null
+++ 
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/UnifiedCreditCodeRandomReplaceAlgorithmTest.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *     http://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.apache.shardingsphere.mask.algorithm.replace;
+
+import 
org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
+import org.apache.shardingsphere.test.util.PropertiesBuilder;
+import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public final class UnifiedCreditCodeRandomReplaceAlgorithmTest {
+    
+    private UnifiedCreditCodeRandomReplaceAlgorithm maskAlgorithm;
+    
+    @Before
+    public void setUp() {
+        maskAlgorithm = new UnifiedCreditCodeRandomReplaceAlgorithm();
+    }
+    
+    @Test
+    public void assertMask() {
+        maskAlgorithm.init(PropertiesBuilder.build(new 
Property("registration-department-codes", "1,2,3,4"), new 
Property("category-codes", "1,2,3,4"),
+                new Property("administrative-division-codes", 
"100000,200000,300000")));
+        assertThat(maskAlgorithm.mask("123456781234567890"), 
not("123456781234567890"));
+        assertThat(maskAlgorithm.mask("123456781234567890").length(), is(18));
+    }
+    
+    @Test(expected = MaskAlgorithmInitializationException.class)
+    public void assertInitWhenConfigIsNull() {
+        maskAlgorithm.init(PropertiesBuilder.build(new 
Property("registration-department-codes", "1,2,3,4")));
+    }
+}

Reply via email to