Skip to content

Commit

Permalink
add encrypt metadata for algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
tuichenchuxin committed Jan 16, 2024
1 parent 2b1a34b commit c4a72b8
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,22 @@ public final class TestQueryAssistedShardingEncryptAlgorithm implements EncryptA

@Getter
private Properties properties;


@Getter
private EncryptAlgorithmMetaData metaData;

@Override
public void init(final Properties props) {
this.properties = props;
EncryptAlgorithmMetaData algorithmMetaData = new EncryptAlgorithmMetaData();
algorithmMetaData.setSupportDecrypt(false);
metaData = algorithmMetaData;
}

@Override
public String encrypt(final Object plainValue, final EncryptContext encryptContext) {
return "assistedEncryptValue";
}

@Override
public EncryptAlgorithmMetaData getMetaData() {
EncryptAlgorithmMetaData result = new EncryptAlgorithmMetaData(1);
result.setSupportDecrypt(false);
return result;
}

@Override
public String getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
@Getter
public final class EncryptAlgorithmMetaData {

private final double expansibility;

@Setter
private boolean supportDecrypt = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.encrypt.algorithm.assisted;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shardingsphere.encrypt.api.context.EncryptContext;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
Expand All @@ -35,24 +36,23 @@ public final class MD5AssistedEncryptAlgorithm implements EncryptAlgorithm {

private String salt;

@Getter
private EncryptAlgorithmMetaData metaData;

@Override
public void init(final Properties props) {
this.salt = props.getProperty(SALT_KEY, "");
// TODO get actual expansibility
EncryptAlgorithmMetaData encryptAlgorithmMetaData = new EncryptAlgorithmMetaData();
encryptAlgorithmMetaData.setSupportDecrypt(false);
metaData = encryptAlgorithmMetaData;
}

@Override
public String encrypt(final Object plainValue, final EncryptContext encryptContext) {
return null == plainValue ? null : DigestUtils.md5Hex(plainValue + salt);
}

@Override
public EncryptAlgorithmMetaData getMetaData() {
// TODO get actual expansibility
EncryptAlgorithmMetaData result = new EncryptAlgorithmMetaData(1);
result.setSupportDecrypt(false);
return result;
}

@Override
public String getType() {
return "MD5";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.base.Strings;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.SneakyThrows;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.MessageDigestAlgorithms;
Expand Down Expand Up @@ -49,10 +50,14 @@ public final class AESEncryptAlgorithm implements EncryptAlgorithm {

private static final String DIGEST_ALGORITHM_NAME = "digest-algorithm-name";

@Getter
private EncryptAlgorithmMetaData metaData;

private byte[] secretKey;

@Override
public void init(final Properties props) {
metaData = new EncryptAlgorithmMetaData();
secretKey = createSecretKey(props);
}

Expand Down Expand Up @@ -90,12 +95,6 @@ private Cipher getCipher(final int decryptMode) throws NoSuchPaddingException, N
return result;
}

@Override
public EncryptAlgorithmMetaData getMetaData() {
// TODO get actual expansibility
return new EncryptAlgorithmMetaData(1);
}

@Override
public String getType() {
return "AES";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@

package org.apache.shardingsphere.encrypt.fixture;

import lombok.Getter;
import org.apache.shardingsphere.encrypt.api.context.EncryptContext;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;

@Getter
public final class CoreEncryptAlgorithmFixture implements EncryptAlgorithm {

private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData();

@Override
public String encrypt(final Object plainValue, final EncryptContext encryptContext) {
return "encryptValue";
Expand All @@ -33,11 +37,6 @@ public Object decrypt(final Object cipherValue, final EncryptContext encryptCont
return "decryptValue";
}

@Override
public EncryptAlgorithmMetaData getMetaData() {
return new EncryptAlgorithmMetaData(1);
}

@Override
public String getType() {
return "CORE.FIXTURE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@

package org.apache.shardingsphere.encrypt.fixture;

import lombok.Getter;
import org.apache.shardingsphere.encrypt.api.context.EncryptContext;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;

import java.util.Properties;

@Getter
public final class CoreQueryAssistedEncryptAlgorithmFixture implements EncryptAlgorithm {

private EncryptAlgorithmMetaData metaData;

@Override
public String encrypt(final Object plainValue, final EncryptContext encryptContext) {
return "assistedEncryptValue";
public void init(final Properties props) {
EncryptAlgorithmMetaData encryptAlgorithmMetaData = new EncryptAlgorithmMetaData();
encryptAlgorithmMetaData.setSupportDecrypt(false);
metaData = encryptAlgorithmMetaData;
}

@Override
public EncryptAlgorithmMetaData getMetaData() {
EncryptAlgorithmMetaData result = new EncryptAlgorithmMetaData(1);
result.setSupportDecrypt(false);
return result;
public String encrypt(final Object plainValue, final EncryptContext encryptContext) {
return "assistedEncryptValue";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,30 @@

package org.apache.shardingsphere.encrypt.fixture;

import lombok.Getter;
import org.apache.shardingsphere.encrypt.api.context.EncryptContext;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;

import java.util.Properties;

@Getter
public final class CoreQueryLikeEncryptAlgorithmFixture implements EncryptAlgorithm {

private EncryptAlgorithmMetaData metaData;

@Override
public String encrypt(final Object plainValue, final EncryptContext encryptContext) {
return "likeEncryptValue";
public void init(final Properties props) {
EncryptAlgorithmMetaData encryptAlgorithmMetaData = new EncryptAlgorithmMetaData();
encryptAlgorithmMetaData.setSupportLike(true);
encryptAlgorithmMetaData.setSupportDecrypt(false);
encryptAlgorithmMetaData.setSupportEquivalentFilter(false);
metaData = encryptAlgorithmMetaData;
}

@Override
public EncryptAlgorithmMetaData getMetaData() {
EncryptAlgorithmMetaData result = new EncryptAlgorithmMetaData(1);
result.setSupportLike(true);
result.setSupportDecrypt(false);
result.setSupportEquivalentFilter(false);
return result;
public String encrypt(final Object plainValue, final EncryptContext encryptContext) {
return "likeEncryptValue";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@

package org.apache.shardingsphere.encrypt.distsql.handler.fixture;

import lombok.Getter;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import org.apache.shardingsphere.encrypt.api.context.EncryptContext;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;

@Getter
public final class DistSQLEncryptAlgorithmFixture implements EncryptAlgorithm {

private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData();

@Override
public String encrypt(final Object plainValue, final EncryptContext encryptContext) {
return "encryptValue";
Expand All @@ -33,11 +37,6 @@ public Object decrypt(final Object cipherValue, final EncryptContext encryptCont
return "decryptValue";
}

@Override
public EncryptAlgorithmMetaData getMetaData() {
return new EncryptAlgorithmMetaData(1);
}

@Override
public String getType() {
return "DISTSQL.FIXTURE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@

package org.apache.shardingsphere.test.e2e.driver.fixture.encrypt;

import lombok.Getter;
import org.apache.shardingsphere.encrypt.api.context.EncryptContext;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;

@Getter
public final class JDBCEncryptAlgorithmFixture implements EncryptAlgorithm {

private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData();

@Override
public String encrypt(final Object plainValue, final EncryptContext encryptContext) {
return "encryptValue";
Expand All @@ -33,11 +37,6 @@ public Object decrypt(final Object cipherValue, final EncryptContext encryptCont
return "decryptValue";
}

@Override
public EncryptAlgorithmMetaData getMetaData() {
return new EncryptAlgorithmMetaData(1);
}

@Override
public String getType() {
return "JDBC.FIXTURE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,27 @@

package org.apache.shardingsphere.test.e2e.driver.fixture.encrypt;

import lombok.Getter;
import org.apache.shardingsphere.encrypt.api.context.EncryptContext;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;

import java.util.Properties;

@Getter
public final class JDBCQueryAssistedEncryptAlgorithmFixture implements EncryptAlgorithm {

private EncryptAlgorithmMetaData metaData;

@Override
public String encrypt(final Object plainValue, final EncryptContext encryptContext) {
return "assistedEncryptValue";
public void init(final Properties props) {
EncryptAlgorithmMetaData encryptAlgorithmMetaData = new EncryptAlgorithmMetaData();
encryptAlgorithmMetaData.setSupportDecrypt(false);
metaData = encryptAlgorithmMetaData;
}

@Override
public EncryptAlgorithmMetaData getMetaData() {
EncryptAlgorithmMetaData result = new EncryptAlgorithmMetaData(1);
result.setSupportDecrypt(false);
return result;
public String encrypt(final Object plainValue, final EncryptContext encryptContext) {
return "assistedEncryptValue";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.test.e2e.fixture;

import com.google.common.base.Strings;
import lombok.Getter;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import lombok.SneakyThrows;
import org.apache.shardingsphere.encrypt.api.context.EncryptContext;
Expand Down Expand Up @@ -59,12 +60,18 @@ public final class ITEncryptLikeAlgorithmFixture implements EncryptAlgorithm {

private Map<Character, Integer> charIndexes;

@Getter
private EncryptAlgorithmMetaData metaData;

@Override
public void init(final Properties props) {
delta = createDelta(props);
mask = createMask(props);
start = createStart(props);
charIndexes = createCharIndexes(props);
EncryptAlgorithmMetaData encryptAlgorithmMetaData = new EncryptAlgorithmMetaData();
encryptAlgorithmMetaData.setSupportLike(true);
metaData = encryptAlgorithmMetaData;
}

private int createDelta(final Properties props) {
Expand Down Expand Up @@ -152,13 +159,6 @@ private char getMaskedChar(final char originalChar) {
return (char) (((originalChar + delta) & mask) + start);
}

@Override
public EncryptAlgorithmMetaData getMetaData() {
EncryptAlgorithmMetaData result = new EncryptAlgorithmMetaData(1);
result.setSupportLike(true);
return result;
}

@Override
public String getType() {
return "IT.ENCRYPT.LIKE.FIXTURE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@

package org.apache.shardingsphere.test.it.rewrite.fixture.encrypt;

import lombok.Getter;
import org.apache.shardingsphere.encrypt.api.context.EncryptContext;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;

@Getter
public final class RewriteNormalEncryptAlgorithmFixture implements EncryptAlgorithm {

private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData();

@Override
public String encrypt(final Object plainValue, final EncryptContext encryptContext) {
if (null == plainValue) {
Expand All @@ -39,11 +43,6 @@ public Object decrypt(final Object cipherValue, final EncryptContext encryptCont
return cipherValue.toString().replaceAll("encrypt_", "");
}

@Override
public EncryptAlgorithmMetaData getMetaData() {
return new EncryptAlgorithmMetaData(1);
}

@Override
public String getType() {
return "REWRITE.NORMAL.FIXTURE";
Expand Down
Loading

0 comments on commit c4a72b8

Please sign in to comment.