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 42fd87ea9f8 implements MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE mask 
algorithm (#23247)
42fd87ea9f8 is described below

commit 42fd87ea9f86300d8d2ac3dcad7525344cff1186
Author: lushaorong <[email protected]>
AuthorDate: Wed Jan 4 08:44:42 2023 +0800

    implements MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE mask algorithm (#23247)
    
    * implements MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE mask algorithm
    
    Signed-off-by: lushaorong <[email protected]>
    
    * implements MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE mask algorithm
    
    Signed-off-by: lushaorong <[email protected]>
    
    Signed-off-by: lushaorong <[email protected]>
---
 ...litaryIdentityNumberRandomReplaceAlgorithm.java | 77 ++++++++++++++++++++++
 ...rg.apache.shardingsphere.mask.spi.MaskAlgorithm |  1 +
 ...ryIdentityNumberRandomReplaceAlgorithmTest.java | 51 ++++++++++++++
 3 files changed, 129 insertions(+)

diff --git 
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithm.java
 
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithm.java
new file mode 100644
index 00000000000..d1148090d43
--- /dev/null
+++ 
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithm.java
@@ -0,0 +1,77 @@
+/*
+ * 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 com.google.common.collect.Lists;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Collection;
+import java.util.Properties;
+import java.util.Random;
+import java.util.stream.Collectors;
+
+/**
+ * Military identity number random replace algorithm.
+ */
+public final class MilitaryIdentityNumberRandomReplaceAlgorithm implements 
MaskAlgorithm<Object, String> {
+    
+    private static final String TYPE_CODE = "type-codes";
+    
+    private Collection<Character> typeCodes;
+    
+    @Getter
+    private Properties props;
+    
+    @Override
+    public String mask(final Object plainValue) {
+        String result = null == plainValue ? null : String.valueOf(plainValue);
+        if (Strings.isNullOrEmpty(result)) {
+            return result;
+        }
+        Random random = new Random();
+        char[] chars = result.toCharArray();
+        int randomIndex = random.nextInt(typeCodes.size());
+        chars[0] = Lists.newArrayList(typeCodes).get(randomIndex);
+        for (int i = 1; i < chars.length; i++) {
+            if (Character.isDigit(chars[i])) {
+                chars[i] = Character.forDigit(random.nextInt(10), 10);
+            }
+        }
+        return new String(chars);
+    }
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+        this.typeCodes = createTypeCodes(props);
+    }
+    
+    private Collection<Character> createTypeCodes(final Properties props) {
+        MaskAlgorithmUtil.checkAtLeastOneCharConfig(props, TYPE_CODE, 
getType());
+        return 
Splitter.on(",").trimResults().splitToList(props.getProperty(TYPE_CODE)).stream().map(each
 -> each.charAt(0)).collect(Collectors.toList());
+    }
+    
+    @Override
+    public String getType() {
+        return "MILITARY_IDENTITY_NUMBER_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 aac67ec8d2d..22178d046a5 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
@@ -24,3 +24,4 @@ 
org.apache.shardingsphere.mask.algorithm.cover.MaskFirstNLastMMaskAlgorithm
 org.apache.shardingsphere.mask.algorithm.cover.MaskFromXToYMaskAlgorithm
 
org.apache.shardingsphere.mask.algorithm.replace.TelephoneRandomReplaceAlgorithm
 
org.apache.shardingsphere.mask.algorithm.replace.PersonalIdentityNumberRandomReplaceAlgorithm
+org.apache.shardingsphere.mask.algorithm.replace.MilitaryIdentityNumberRandomReplaceAlgorithm
diff --git 
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithmTest.java
 
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithmTest.java
new file mode 100644
index 00000000000..6e05555ae9c
--- /dev/null
+++ 
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithmTest.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.junit.Before;
+import org.junit.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public final class MilitaryIdentityNumberRandomReplaceAlgorithmTest {
+    
+    private MilitaryIdentityNumberRandomReplaceAlgorithm maskAlgorithm;
+    
+    @Before
+    public void setUp() {
+        maskAlgorithm = new MilitaryIdentityNumberRandomReplaceAlgorithm();
+        maskAlgorithm.init(createProperties());
+    }
+    
+    private Properties createProperties() {
+        Properties result = new Properties();
+        result.setProperty("type-codes", "军,人,士,文,职");
+        return result;
+    }
+    
+    @Test
+    public void assertMask() {
+        assertThat(maskAlgorithm.mask("军字第1234567号".charAt(0)), not('军'));
+        assertThat(maskAlgorithm.mask("军字第1234567号".substring(3, 10)), 
not("1234567"));
+        assertThat(maskAlgorithm.mask(""), is(""));
+    }
+}

Reply via email to