You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/12/30 01:11:42 UTC

[GitHub] [shardingsphere] strongduanmu commented on a diff in pull request #23171: Implement TELEPHONE_RANDOM_REPLACE Algorithm

strongduanmu commented on code in PR #23171:
URL: https://github.com/apache/shardingsphere/pull/23171#discussion_r1059205583


##########
features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/TelephoneRandomReplaceAlgorithm.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.cover;
+
+import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Random;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Telephone random replace algorithm.
+ */
+public final class TelephoneRandomReplaceAlgorithm implements MaskAlgorithm<Object, String> {
+    
+    private static final String NETWORK_NUMBER = "network-number";
+    
+    private Set<String> networkNumbers;
+    
+    private List<Integer> networkNumberLength;
+    
+    @Getter
+    private Properties props;
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+        createNetworkNumbers(props);
+    }
+    
+    private void createNetworkNumbers(final Properties props) {
+        MaskAlgorithmUtil.checkAtLeastOneCharConfig(props, NETWORK_NUMBER, getType());
+        networkNumbers = Splitter.on(",").trimResults().splitToStream(props.getProperty(NETWORK_NUMBER)).map(networkNumber -> isNumeric(networkNumber)).collect(Collectors.toSet());
+        networkNumberLength = networkNumbers.stream().map(networkNumber -> String.valueOf(networkNumber).length()).distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
+    }
+    
+    private String isNumeric(final String networkNumber) {
+        try {
+            Integer.parseInt(networkNumber);
+            return networkNumber;
+        } catch (NumberFormatException e) {
+            throw new MaskAlgorithmInitializationException(getType(), "network-number can only be numbers and ,");
+        }
+    }
+    
+    @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();
+        for (Integer length : networkNumberLength) {

Review Comment:
   Please rename length to each.



##########
features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/TelephoneRandomReplaceAlgorithm.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.cover;

Review Comment:
   Please move this algorithm to replace package.



##########
features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/TelephoneRandomReplaceAlgorithm.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.cover;
+
+import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Random;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Telephone random replace algorithm.
+ */
+public final class TelephoneRandomReplaceAlgorithm implements MaskAlgorithm<Object, String> {
+    
+    private static final String NETWORK_NUMBER = "network-number";
+    
+    private Set<String> networkNumbers;
+    
+    private List<Integer> networkNumberLength;
+    
+    @Getter
+    private Properties props;
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+        createNetworkNumbers(props);
+    }
+    
+    private void createNetworkNumbers(final Properties props) {
+        MaskAlgorithmUtil.checkAtLeastOneCharConfig(props, NETWORK_NUMBER, getType());
+        networkNumbers = Splitter.on(",").trimResults().splitToStream(props.getProperty(NETWORK_NUMBER)).map(networkNumber -> isNumeric(networkNumber)).collect(Collectors.toSet());
+        networkNumberLength = networkNumbers.stream().map(networkNumber -> String.valueOf(networkNumber).length()).distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
+    }
+    
+    private String isNumeric(final String networkNumber) {
+        try {
+            Integer.parseInt(networkNumber);
+            return networkNumber;
+        } catch (NumberFormatException e) {
+            throw new MaskAlgorithmInitializationException(getType(), "network-number can only be numbers and ,");
+        }
+    }
+    
+    @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();
+        for (Integer length : networkNumberLength) {
+            if (networkNumbers.contains(result.substring(0, length))) {
+                for (int i = length; i != -1 && i < chars.length; i++) {
+                    chars[i] = Character.forDigit(random.nextInt(10), 10);
+                }
+                continue;
+            }
+        }
+        return new String(chars);
+    }
+    
+    @Override
+    public String getType() {
+        return "TELEPHONE_RANDOM_REPLACE";

Review Comment:
   Hi @gxxiong, can we add this new algorithm to mask built-in algorithm document and dev-manual document?
   
   - https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/mask/
   - https://shardingsphere.apache.org/document/current/cn/dev-manual/mask/



##########
features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/TelephoneRandomReplaceAlgorithm.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.cover;
+
+import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Random;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Telephone random replace algorithm.
+ */
+public final class TelephoneRandomReplaceAlgorithm implements MaskAlgorithm<Object, String> {
+    
+    private static final String NETWORK_NUMBER = "network-number";
+    
+    private Set<String> networkNumbers;
+    
+    private List<Integer> networkNumberLength;
+    
+    @Getter
+    private Properties props;
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+        createNetworkNumbers(props);
+    }
+    
+    private void createNetworkNumbers(final Properties props) {
+        MaskAlgorithmUtil.checkAtLeastOneCharConfig(props, NETWORK_NUMBER, getType());
+        networkNumbers = Splitter.on(",").trimResults().splitToStream(props.getProperty(NETWORK_NUMBER)).map(networkNumber -> isNumeric(networkNumber)).collect(Collectors.toSet());
+        networkNumberLength = networkNumbers.stream().map(networkNumber -> String.valueOf(networkNumber).length()).distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

Review Comment:
   Why we need networkNumberLength param?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org