View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.encrypt.rule.column.item;
19  
20  import lombok.Getter;
21  import lombok.RequiredArgsConstructor;
22  import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
23  import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
24  
25  import java.util.LinkedList;
26  import java.util.List;
27  
28  /**
29   * Cipher column item.
30   */
31  @RequiredArgsConstructor
32  @Getter
33  public final class CipherColumnItem {
34      
35      private final String name;
36      
37      private final EncryptAlgorithm encryptor;
38      
39      /**
40       * Encrypt.
41       *
42       * @param databaseName database name
43       * @param schemaName schema name
44       * @param tableName table name
45       * @param logicColumnName logic column name
46       * @param originalValue original value
47       * @return encrypted value
48       */
49      public Object encrypt(final String databaseName, final String schemaName, final String tableName, final String logicColumnName, final Object originalValue) {
50          if (null == originalValue) {
51              return null;
52          }
53          return encryptor.encrypt(originalValue, new AlgorithmSQLContext(databaseName, schemaName, tableName, logicColumnName));
54      }
55      
56      /**
57       * Encrypt.
58       *
59       * @param databaseName database name
60       * @param schemaName schema name
61       * @param tableName table name
62       * @param logicColumnName logic column name
63       * @param originalValues original values
64       * @return encrypted values
65       */
66      public List<Object> encrypt(final String databaseName, final String schemaName, final String tableName, final String logicColumnName, final List<Object> originalValues) {
67          AlgorithmSQLContext algorithmSQLContext = new AlgorithmSQLContext(databaseName, schemaName, tableName, logicColumnName);
68          List<Object> result = new LinkedList<>();
69          for (Object each : originalValues) {
70              result.add(null == each ? null : encryptor.encrypt(each, algorithmSQLContext));
71          }
72          return result;
73      }
74      
75      /**
76       * Decrypt.
77       *
78       * @param databaseName database name
79       * @param schemaName schema name
80       * @param tableName table name
81       * @param logicColumnName logic column name
82       * @param cipherValue cipher value
83       * @return decrypted value
84       */
85      public Object decrypt(final String databaseName, final String schemaName, final String tableName, final String logicColumnName, final Object cipherValue) {
86          if (null == cipherValue) {
87              return null;
88          }
89          return encryptor.decrypt(cipherValue, new AlgorithmSQLContext(databaseName, schemaName, tableName, logicColumnName));
90      }
91  }