This is an automated email from the ASF dual-hosted git repository.
panjuan 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 76c6ca30d09 Revise MilitaryIdentityNumberRandomReplaceAlgorithm and
update document (#23313)
76c6ca30d09 is described below
commit 76c6ca30d09cc25aa0179e62b9ccc9bff9d2555f
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Wed Jan 4 09:50:53 2023 +0800
Revise MilitaryIdentityNumberRandomReplaceAlgorithm and update document
(#23313)
---
docs/document/content/dev-manual/mask.cn.md | 3 ++-
docs/document/content/dev-manual/mask.en.md | 21 +++++++--------
.../common-config/builtin-algorithm/mask.cn.md | 26 +++++++++++++------
.../common-config/builtin-algorithm/mask.en.md | 26 +++++++++++++------
...litaryIdentityNumberRandomReplaceAlgorithm.java | 30 ++++++++++------------
...ryIdentityNumberRandomReplaceAlgorithmTest.java | 17 +++++-------
6 files changed, 69 insertions(+), 54 deletions(-)
diff --git a/docs/document/content/dev-manual/mask.cn.md
b/docs/document/content/dev-manual/mask.cn.md
index 301398910bf..77b2da728d9 100644
--- a/docs/document/content/dev-manual/mask.cn.md
+++ b/docs/document/content/dev-manual/mask.cn.md
@@ -27,4 +27,5 @@ 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)
|
| 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)
|
-| 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)
|
+| MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE | 军官证随机替换数据脱敏算法 |
[`org.apache.shardingsphere.mask.algorithm.replace.MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithm.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)
|
diff --git a/docs/document/content/dev-manual/mask.en.md
b/docs/document/content/dev-manual/mask.en.md
index 00684f333be..00c00b466a8 100644
--- a/docs/document/content/dev-manual/mask.en.md
+++ b/docs/document/content/dev-manual/mask.en.md
@@ -17,14 +17,15 @@ Data masking algorithm definition
### Implementation classes
-| *Configuration Type* | *Description*
| *Fully-qualified class name*
|
-|-----------------------------------------|---------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| MD5 | Data masking algorithm based on
MD5 |
[`org.apache.shardingsphere.mask.algorithm.hash.MD5MaskAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/hash/MD5MaskAlgorithm.java)
|
-| KEEP_FIRST_N_LAST_M | Keep first n last m data masking
algorithm |
[`org.apache.shardingsphere.mask.algorithm.cover.KEEP_FIRST_N_LAST_M`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithm.java)
|
-| KEEP_FROM_X_TO_Y | Keep from x to y data masking
algorithm |
[`org.apache.shardingsphere.mask.algorithm.cover.KEEP_FROM_X_TO_Y`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java)
|
-| MASK_FIRST_N_LAST_M | Mask first n last m data masking
algorithm |
[`org.apache.shardingsphere.mask.algorithm.cover.MASK_FIRST_N_LAST_M`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithm.java)
|
-| 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)
|
+| *Configuration Type* | *Description*
| *Fully-qualified class name*
|
+|-----------------------------------------|----------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| MD5 | Data masking algorithm based on
MD5 |
[`org.apache.shardingsphere.mask.algorithm.hash.MD5MaskAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/hash/MD5MaskAlgorithm.java)
|
+| KEEP_FIRST_N_LAST_M | Keep first n last m data masking
algorithm |
[`org.apache.shardingsphere.mask.algorithm.cover.KEEP_FIRST_N_LAST_M`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithm.java)
|
+| KEEP_FROM_X_TO_Y | Keep from x to y data masking
algorithm |
[`org.apache.shardingsphere.mask.algorithm.cover.KEEP_FROM_X_TO_Y`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java)
|
+| MASK_FIRST_N_LAST_M | Mask first n last m data masking
algorithm |
[`org.apache.shardingsphere.mask.algorithm.cover.MASK_FIRST_N_LAST_M`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithm.java)
|
+| 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 | Personal identity number random
replace 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)
|
+| MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE | Military identity number random
replace data masking algorithm |
[`org.apache.shardingsphere.mask.algorithm.replace.MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithm.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 79f0df0d8a7..435e566db16 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
@@ -23,7 +23,7 @@ weight = 9
### 遮盖脱敏算法
-#### KEEP_FIRST_N_LAST_M 脱敏算法
+#### 保留前 N 后 M 脱敏算法
类型:KEEP_FIRST_N_LAST_M
@@ -35,7 +35,7 @@ weight = 9
| last-m | int | 后 n 位 |
| replace-char | String | 替换字符 |
-#### KEEP_FROM_X_TO_Y 脱敏算法
+#### 保留自 X 至 Y 脱敏算法
类型:KEEP_FROM_X_TO_Y
@@ -47,7 +47,7 @@ weight = 9
| to-y | int | 结束位置(从 0 开始) |
| replace-char | String | 替换字符 |
-#### MASK_FIRST_N_LAST_M 脱敏算法
+#### 遮盖前 N 后 M 脱敏算法
类型:MASK_FIRST_N_LAST_M
@@ -59,7 +59,7 @@ weight = 9
| last-m | int | 后 n 位 |
| replace-char | String | 替换字符 |
-#### MASK_FROM_X_TO_Y 脱敏算法
+#### 遮盖自 X 至 Y 脱敏算法
类型:MASK_FROM_X_TO_Y
@@ -71,7 +71,7 @@ weight = 9
| to-y | int | 结束位置(从 0 开始) |
| replace-char | String | 替换字符 |
-#### MASK_BEFORE_SPECIAL_CHARS 脱敏算法
+#### 特殊字符前遮盖脱敏算法
类型:MASK_BEFORE_SPECIAL_CHARS
@@ -82,7 +82,7 @@ weight = 9
| special-chars | String | 特殊字符(首次出现) |
| replace-char | String | 替换字符 |
-#### MASK_AFTER_SPECIAL_CHARS 脱敏算法
+#### 特殊字符后遮盖脱敏算法
类型:MASK_AFTER_SPECIAL_CHARS
@@ -95,7 +95,7 @@ weight = 9
### 替换脱敏算法
-#### PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE 脱敏算法
+#### 身份证随机替换脱敏算法
类型:PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE
@@ -105,7 +105,17 @@ weight = 9
|------------------------------|----------|----------------------|
| alpha-two-country-area-code | String | 两位字母国家/地区编码(可选,默认:CN)|
-#### TELEPHONE_RANDOM_REPLACE 脱敏算法
+#### 军官证随机替换脱敏算法
+
+类型:MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE
+
+可配置属性:
+
+| *名称* | *数据类型* | *说明* |
+|------------------------------|----------|----------------------|
+| type-codes | String | 军官证种类编码(以英文逗号分隔,例如:军,人,士,文,职) |
+
+#### 手机号随机替换脱敏算法
类型:TELEPHONE_RANDOM_REPLACE
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 0dd68083bc2..55729daa47a 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
@@ -23,7 +23,7 @@ Attributes:
### Mask Data Masking Algorithm
-#### KEEP_FIRST_N_LAST_M Data Masking Algorithm
+#### Keep First N Last M Data Masking Algorithm
Type: KEEP_FIRST_N_LAST_M
@@ -35,7 +35,7 @@ Attributes:
| last-m | int | last m substring |
| replace-char | String | replace char |
-#### KEEP_FROM_X_TO_Y Data Masking Algorithm
+#### Keep From X To Y Data Masking Algorithm
Type: KEEP_FROM_X_TO_Y
@@ -47,7 +47,7 @@ Attributes:
| to-y | int | end position (from 0) |
| replace-char | String | replace char |
-#### MASK_FIRST_N_LAST_M Data Masking Algorithm
+#### Mask First N Last M Data Masking Algorithm
Type: MASK_FIRST_N_LAST_M
@@ -59,7 +59,7 @@ Attributes:
| last-m | int | last m substring |
| replace-char | String | replace char |
-#### MASK_FROM_X_TO_Y Data Masking Algorithm
+#### Mask From X To Y Data Masking Algorithm
Type: MASK_FROM_X_TO_Y
@@ -71,7 +71,7 @@ Attributes:
| to-y | int | end position (from 0) |
| replace-char | String | replace char |
-#### MASK_BEFORE_SPECIAL_CHARS Data Masking Algorithm
+#### Mask Before Special Chars Data Masking Algorithm
Type: MASK_BEFORE_SPECIAL_CHARS
@@ -82,7 +82,7 @@ Attributes:
| special-chars | String | Special chars (first appearance) |
| replace-char | String | replace char |
-#### MASK_AFTER_SPECIAL_CHARS Data Masking Algorithm
+#### Mask After Special Chars Data Masking Algorithm
Type: MASK_AFTER_SPECIAL_CHARS
@@ -95,7 +95,7 @@ Attributes:
### Replace Data Masking Algorithm
-#### PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE Data Masking Algorithm
+#### Personal Identity Number Random Replace Data Masking Algorithm
Type: PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE
@@ -105,7 +105,17 @@ Attributes:
|-----------------------------|--------|-----------------------------------------------------------|
| alpha-two-country-area-code | String | alpha two country area code
(Optional, default value: CN) |
-#### TELEPHONE_RANDOM_REPLACE Data Masking Algorithm
+#### Military Identity Number Random Replace Data Masking Algorithm
+
+类型:MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE
+
+可配置属性:
+
+| *Name* | *DataType* | *Description*
|
+|-------------------------------|------------|------------------------------------------------------------|
+| type-codes | String | military identity number type
codes (separate with comma) |
+
+#### Telephone Random Replace Data Masking Algorithm
Type: TELEPHONE_RANDOM_REPLACE
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
index d1148090d43..08a62d9974d 100644
---
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
@@ -19,12 +19,11 @@ 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.List;
import java.util.Properties;
import java.util.Random;
import java.util.stream.Collectors;
@@ -36,11 +35,22 @@ public final class
MilitaryIdentityNumberRandomReplaceAlgorithm implements MaskA
private static final String TYPE_CODE = "type-codes";
- private Collection<Character> typeCodes;
+ private List<Character> typeCodes;
@Getter
private Properties props;
+ @Override
+ public void init(final Properties props) {
+ this.props = props;
+ this.typeCodes = createTypeCodes(props);
+ }
+
+ private List<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 mask(final Object plainValue) {
String result = null == plainValue ? null : String.valueOf(plainValue);
@@ -49,8 +59,7 @@ public final class
MilitaryIdentityNumberRandomReplaceAlgorithm implements MaskA
}
Random random = new Random();
char[] chars = result.toCharArray();
- int randomIndex = random.nextInt(typeCodes.size());
- chars[0] = Lists.newArrayList(typeCodes).get(randomIndex);
+ chars[0] = typeCodes.get(random.nextInt(typeCodes.size()));
for (int i = 1; i < chars.length; i++) {
if (Character.isDigit(chars[i])) {
chars[i] = Character.forDigit(random.nextInt(10), 10);
@@ -59,17 +68,6 @@ public final class
MilitaryIdentityNumberRandomReplaceAlgorithm implements MaskA
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/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
index 6e05555ae9c..5eee8214dba 100644
---
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
@@ -17,14 +17,15 @@
package org.apache.shardingsphere.mask.algorithm.replace;
+import org.apache.shardingsphere.test.util.PropertiesBuilder;
+import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
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;
+import static org.junit.Assert.assertNull;
public final class MilitaryIdentityNumberRandomReplaceAlgorithmTest {
@@ -33,19 +34,13 @@ public final class
MilitaryIdentityNumberRandomReplaceAlgorithmTest {
@Before
public void setUp() {
maskAlgorithm = new MilitaryIdentityNumberRandomReplaceAlgorithm();
- maskAlgorithm.init(createProperties());
- }
-
- private Properties createProperties() {
- Properties result = new Properties();
- result.setProperty("type-codes", "军,人,士,文,职");
- return result;
+ maskAlgorithm.init(PropertiesBuilder.build(new Property("type-codes",
"军,人,士,文,职")));
}
@Test
public void assertMask() {
- assertThat(maskAlgorithm.mask("军字第1234567号".charAt(0)), not('军'));
- assertThat(maskAlgorithm.mask("军字第1234567号".substring(3, 10)),
not("1234567"));
+ assertThat(maskAlgorithm.mask("军字第1234567号"), not("军字第1234567号"));
assertThat(maskAlgorithm.mask(""), is(""));
+ assertNull(maskAlgorithm.mask(null));
}
}