You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/12/31 11:34:26 UTC
[shardingsphere] branch master updated: implements PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE mask algorithm (#23220)
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 9f6dc8b7988 implements PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE mask algorithm (#23220)
9f6dc8b7988 is described below
commit 9f6dc8b798823905f6212d275649b33b244ac509
Author: liushangqing9803 <30...@users.noreply.github.com>
AuthorDate: Sat Dec 31 19:34:19 2022 +0800
implements PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE mask algorithm (#23220)
Co-authored-by: sq.liu05 <sq...@zuche.com>
Co-authored-by: Zhengqiang Duan <du...@apache.org>
---
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 | 10 +++
.../common-config/builtin-algorithm/mask.en.md | 10 +++
...rsonalIdentityNumberRandomReplaceAlgorithm.java | 85 ++++++++++++++++++++++
...rg.apache.shardingsphere.mask.spi.MaskAlgorithm | 1 +
...alIdentityNumberRandomReplaceAlgorithmTest.java | 53 ++++++++++++++
7 files changed, 161 insertions(+)
diff --git a/docs/document/content/dev-manual/mask.cn.md b/docs/document/content/dev-manual/mask.cn.md
index daeeb21f274..e601e5c39e7 100644
--- a/docs/document/content/dev-manual/mask.cn.md
+++ b/docs/document/content/dev-manual/mask.cn.md
@@ -27,3 +27,4 @@ chapter = true
| MASK_BEFORE_SPECIAL_CHARS | 特殊字符前遮盖数据脱敏算法 | [`org.apache.shardingsphere.mask.algorithm.cover.MASK_BEFORE_SPECIAL_CHARS`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithm.java) |
| MASK_AFTER_SPECIAL_CHARS | 特殊字符后遮盖数据脱敏算法 | [`org.apache.shardingsphere.mask.algorithm.cover.MASK_AFTER_SPECIAL_CHARS`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithm.java) |
| TELEPHONE_RANDOM_REPLACE | ⼿机号随机替换数据脱敏算法 | [`org.apache.shardingsphere.mask.algorithm.replace.TELEPHONE_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/TelephoneRandomReplaceAlgorithm.java) |
+| PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE | 身份证号随机替换数据脱敏算法 | [`org.apache.shardingsphere.mask.algorithm.replace.PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithm.java) |
diff --git a/docs/document/content/dev-manual/mask.en.md b/docs/document/content/dev-manual/mask.en.md
index 2d06d7a4003..4333136c13f 100644
--- a/docs/document/content/dev-manual/mask.en.md
+++ b/docs/document/content/dev-manual/mask.en.md
@@ -26,4 +26,5 @@ Data masking algorithm definition
| MASK_FROM_X_TO_Y | Mask from x to y data masking algorithm | [`org.apache.shardingsphere.mask.algorithm.cover.MASK_FROM_X_TO_Y`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java) |
| MASK_BEFORE_SPECIAL_CHARS | Mask before special chars data masking algorithm | [`org.apache.shardingsphere.mask.algorithm.cover.MASK_BEFORE_SPECIAL_CHARS`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithm.java) |
| MASK_AFTER_SPECIAL_CHARS | Mask after special chars data masking algorithm | [`org.apache.shardingsphere.mask.algorithm.cover.MASK_AFTER_SPECIAL_CHARS`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithm.java) |
+| PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE | Random replacement of personal identity number data masking algorithm | [`org.apache.shardingsphere.mask.algorithm.replace.PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithm.java) |
| TELEPHONE_RANDOM_REPLACE | Telephone random replace data masking algorithm | [`org.apache.shardingsphere.mask.algorithm.replace.TELEPHONE_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/TelephoneRandomReplaceAlgorithm.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 b3b9b74d52d..2939ba279a5 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
@@ -105,6 +105,16 @@ weight = 9
|-----------------|----------|-------------|
| network-numbers | String | ⽹号(以英文逗号分隔) |
+#### PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE 脱敏算法
+
+类型:PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE
+
+可配置属性:
+
+| *名称* | *数据类型* | *说明* |
+|----------------|----------|----------------|
+| area | 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 f49b530c3d7..2ce54d7ee80 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
@@ -105,6 +105,16 @@ Attributes:
|-----------------|--------|---------------------------------------|
| network-numbers | String | Network numbers (separate with comma) |
+#### PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE Data Masking Algorithm
+
+Type: PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE
+
+Attributes:
+
+| *Name* | *DataType* | *Description* |
+|----------------|--------|----------------------------------------|
+| area | String | area (optional,default:CN) |
+
## 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/PersonalIdentityNumberRandomReplaceAlgorithm.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithm.java
new file mode 100644
index 00000000000..b8e00f22402
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithm.java
@@ -0,0 +1,85 @@
+/*
+ * 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.Strings;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Properties;
+import java.util.Random;
+
+/**
+ * Personal identity number random replace.
+ */
+public final class PersonalIdentityNumberRandomReplaceAlgorithm implements MaskAlgorithm<Object, String> {
+
+ private static final String AREA = "area";
+
+ private String area;
+
+ private final Random random = new Random();
+
+ @Getter
+ private Properties props;
+
+ @Override
+ public void init(final Properties props) {
+ this.props = props;
+ this.area = props.getProperty(AREA, "CN");
+ }
+
+ @Override
+ public String mask(final Object plainValue) {
+ String result = null == plainValue ? null : String.valueOf(plainValue);
+ if (Strings.isNullOrEmpty(result)) {
+ return result;
+ }
+ switch (area) {
+ case "CN":
+ return randomReplaceForCN(result);
+ default:
+ }
+ return result;
+ }
+
+ private String randomReplaceForCN(final String result) {
+ int length = result.length();
+ switch (length) {
+ case 15:
+ return randomReplaceNumber(result, 6, 12);
+ case 18:
+ return randomReplaceNumber(result, 6, 14);
+ default:
+ }
+ return result;
+ }
+
+ private String randomReplaceNumber(final String result, final int from, final int to) {
+ char[] chars = result.toCharArray();
+ for (int i = from; i < to; i++) {
+ chars[i] = Character.forDigit(random.nextInt(10), 10);
+ }
+ return new String(chars);
+ }
+
+ @Override
+ public String getType() {
+ return "PERSONAL_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 819502bc85f..aac67ec8d2d 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
@@ -23,3 +23,4 @@ org.apache.shardingsphere.mask.algorithm.cover.MaskBeforeSpecialCharsAlgorithm
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
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithmTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithmTest.java
new file mode 100644
index 00000000000..05c741eb373
--- /dev/null
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithmTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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 PersonalIdentityNumberRandomReplaceAlgorithmTest {
+
+ private PersonalIdentityNumberRandomReplaceAlgorithm maskAlgorithm;
+
+ @Before
+ public void setUp() {
+ maskAlgorithm = new PersonalIdentityNumberRandomReplaceAlgorithm();
+ maskAlgorithm.init(createProperties("CN"));
+ }
+
+ @Test
+ public void assertMask() {
+ assertThat(maskAlgorithm.mask("372928198312103215"), not("372928198312103215"));
+ assertThat(maskAlgorithm.mask("372928231210321"), not("372928231210321"));
+ assertThat(maskAlgorithm.mask("1234567891011121314"), is("1234567891011121314"));
+ assertThat(maskAlgorithm.mask("123456"), is("123456"));
+ assertThat(maskAlgorithm.mask(""), is(""));
+ }
+
+ private Properties createProperties(final String area) {
+ Properties result = new Properties();
+ result.setProperty("area", area);
+ return result;
+ }
+}