Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion features/encrypt/like/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-plugin-features-encrypt</artifactId>
<version>5.4.2-SNAPSHOT</version>
<version>${revision}</version>
</parent>
<artifactId>shardingsphere-plugin-features-encrypt-like</artifactId>
<name>${project.artifactId}</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import lombok.SneakyThrows;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;

Expand Down Expand Up @@ -68,13 +69,16 @@ public final class CharDigestLikeEncryptAlgorithm implements EncryptAlgorithm {
@Getter
private EncryptAlgorithmMetaData metaData;

private Properties props;

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

private int createDelta(final Properties props) {
Expand Down Expand Up @@ -141,6 +145,11 @@ public Object decrypt(final Object cipherValue, final AlgorithmSQLContext encryp
throw new UnsupportedOperationException(String.format("Algorithm `%s` is unsupported to decrypt", getType()));
}

@Override
public AlgorithmConfiguration toConfiguration() {
return new AlgorithmConfiguration(getType(), props);
}

private String digest(final String plainValue) {
StringBuilder result = new StringBuilder(plainValue.length());
for (char each : plainValue.toCharArray()) {
Expand Down
2 changes: 1 addition & 1 deletion features/encrypt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-plugin-features</artifactId>
<version>5.4.2-SNAPSHOT</version>
<version>${revision}</version>
</parent>
<artifactId>shardingsphere-plugin-features-encrypt</artifactId>
<packaging>pom</packaging>
Expand Down
2 changes: 1 addition & 1 deletion features/encrypt/rc4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-plugin-features-encrypt</artifactId>
<version>5.4.2-SNAPSHOT</version>
<version>${revision}</version>
</parent>
<artifactId>shardingsphere-plugin-features-encrypt-rc4</artifactId>
<name>${project.artifactId}</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.codec.binary.Base64;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
Expand All @@ -42,14 +43,17 @@ public final class RC4EncryptAlgorithm implements EncryptAlgorithm {
private static final int SBOX_LENGTH = 256;

private byte[] key;

@Getter
private EncryptAlgorithmMetaData metaData;

private Properties props;

@Override
public void init(final Properties props) {
this.props = props;
key = getKey(props);
metaData = new EncryptAlgorithmMetaData(false, false, false, new Properties());
metaData = new EncryptAlgorithmMetaData(false, false, false);
}

private byte[] getKey(final Properties props) {
Expand All @@ -69,6 +73,11 @@ public Object decrypt(final Object cipherValue, final AlgorithmSQLContext encryp
return null == cipherValue ? null : new String(crypt(Base64.decodeBase64(cipherValue.toString())), StandardCharsets.UTF_8);
}

@Override
public AlgorithmConfiguration toConfiguration() {
return new AlgorithmConfiguration(getType(), props);
}

/*
* @see <a href="http://en.wikipedia.org/wiki/RC4#Pseudo-random_generation_algorithm_.28PRGA.29">Pseudo-random generation algorithm</a>
*/
Expand Down
11 changes: 9 additions & 2 deletions features/encrypt/sm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-plugin-features-encrypt</artifactId>
<version>5.4.2-SNAPSHOT</version>
<version>${revision}</version>
</parent>
<artifactId>shardingsphere-plugin-features-encrypt-sm</artifactId>
<name>${project.artifactId}</name>
Expand All @@ -39,9 +39,16 @@
<version>${shardingsphere.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-plugin-infra-algorithm-message-digest-sm3</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<artifactId>bcprov-jdk18on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.Getter;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
import org.apache.shardingsphere.infra.algorithm.messagedigest.core.MessageDigestAlgorithm;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
Expand All @@ -36,16 +37,19 @@
public final class SM3AssistedEncryptAlgorithm implements EncryptAlgorithm {

@Getter
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, false, false, new Properties());
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, false, false);

private MessageDigestAlgorithm digestAlgorithm;

private Properties props;

static {
Security.addProvider(new BouncyCastleProvider());
}

@Override
public void init(final Properties props) {
this.props = props;
digestAlgorithm = TypedSPILoader.getService(MessageDigestAlgorithm.class, getType(), props);
}

Expand All @@ -58,7 +62,12 @@ public String encrypt(final Object plainValue, final AlgorithmSQLContext algorit
public Object decrypt(final Object cipherValue, final AlgorithmSQLContext algorithmSQLContext) {
throw new UnsupportedOperationException(String.format("Algorithm `%s` is unsupported to decrypt", getType()));
}


@Override
public AlgorithmConfiguration toConfiguration() {
return new AlgorithmConfiguration(getType(), props);
}

@Override
public String getType() {
return "SM3";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.commons.codec.binary.Hex;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
Expand Down Expand Up @@ -75,14 +76,17 @@ public final class SM4EncryptAlgorithm implements EncryptAlgorithm {
@Getter
private EncryptAlgorithmMetaData metaData;

private Properties props;

@Override
public void init(final Properties props) {
this.props = props;
String sm4Mode = createSm4Mode(props);
String sm4Padding = createSm4Padding(props);
sm4ModePadding = "SM4/" + sm4Mode + "/" + sm4Padding;
sm4Key = createSm4Key(props);
sm4Iv = createSm4Iv(props, sm4Mode);
metaData = new EncryptAlgorithmMetaData(false, false, false, new Properties());
metaData = new EncryptAlgorithmMetaData(false, false, false);
}

private String createSm4Mode(final Properties props) {
Expand Down Expand Up @@ -132,6 +136,11 @@ public Object decrypt(final Object cipherValue, final AlgorithmSQLContext encryp
return null == cipherValue ? null : new String(decrypt(fromHexString(String.valueOf(cipherValue))), StandardCharsets.UTF_8);
}

@Override
public AlgorithmConfiguration toConfiguration() {
return new AlgorithmConfiguration(getType(), props);
}

private byte[] decrypt(final byte[] cipherValue) {
return handle(cipherValue, Cipher.DECRYPT_MODE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.util.Properties;
Expand Down Expand Up @@ -59,19 +60,22 @@ void assertEncryptWithNullPlaintext() {
}

@Test
@Disabled //Algorithm `SM3` is unsupported to decrypt
void assertDecrypt() {
Object actual = encryptAlgorithm.decrypt("ab847c6f2f6a53be88808c5221bd6ee0762e1af1def82b21d2061599b6cf5c79", mock(AlgorithmSQLContext.class));
assertThat(actual.toString(), is("ab847c6f2f6a53be88808c5221bd6ee0762e1af1def82b21d2061599b6cf5c79"));
}

@Test
@Disabled //Algorithm `SM3` is unsupported to decrypt
void assertDecryptWithoutSalt() {
encryptAlgorithm.init(new Properties());
Object actual = encryptAlgorithm.decrypt("ab847c6f2f6a53be88808c5221bd6ee0762e1af1def82b21d2061599b6cf5c79", mock(AlgorithmSQLContext.class));
assertThat(actual.toString(), is("ab847c6f2f6a53be88808c5221bd6ee0762e1af1def82b21d2061599b6cf5c79"));
}

@Test
@Disabled //Algorithm `SM3` is unsupported to decrypt
void assertDecryptWithNullCiphertext() {
assertNull(encryptAlgorithm.decrypt(null, mock(AlgorithmSQLContext.class)));
}
Expand Down
2 changes: 1 addition & 1 deletion features/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-plugin</artifactId>
<version>5.4.2-SNAPSHOT</version>
<version>${revision}</version>
</parent>
<artifactId>shardingsphere-plugin-features</artifactId>
<packaging>pom</packaging>
Expand Down
6 changes: 1 addition & 5 deletions features/sharding/cosid/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-plugin-features-sharding</artifactId>
<version>5.4.2-SNAPSHOT</version>
<version>${revision}</version>
</parent>
<artifactId>shardingsphere-plugin-features-sharding-cosid</artifactId>
<name>${project.artifactId}</name>

<properties>
<cosid.version>1.18.5</cosid.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
Expand Down
2 changes: 1 addition & 1 deletion features/sharding/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-plugin-features</artifactId>
<version>5.4.2-SNAPSHOT</version>
<version>${revision}</version>
</parent>
<artifactId>shardingsphere-plugin-features-sharding</artifactId>
<packaging>pom</packaging>
Expand Down
6 changes: 1 addition & 5 deletions infra/algorithm/key-generator/cosid/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-plugin-infra-algorithm-key-generator</artifactId>
<version>5.4.2-SNAPSHOT</version>
<version>${revision}</version>
</parent>
<artifactId>shardingsphere-plugin-infra-algorithm-key-generator-cosid</artifactId>
<name>${project.artifactId}</name>

<properties>
<cosid.version>1.18.5</cosid.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.apache.shardingsphere.infra.algorithm.keygen.core.KeyGenerateAlgorithm;
import org.apache.shardingsphere.infra.algorithm.keygen.cosid.constant.CosIdKeyGenerateConstants;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.instance.InstanceContextAware;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContextAware;

import java.time.Instant;
import java.time.LocalDateTime;
Expand All @@ -41,7 +41,7 @@
/**
* CosId snowflake key generate algorithm.
*/
public final class CosIdSnowflakeKeyGenerateAlgorithm implements KeyGenerateAlgorithm, InstanceContextAware {
public final class CosIdSnowflakeKeyGenerateAlgorithm implements KeyGenerateAlgorithm, ComputeNodeInstanceContextAware {

public static final long DEFAULT_EPOCH;

Expand Down Expand Up @@ -79,7 +79,7 @@ private long getEpoch(final Properties props) {
}

@Override
public void setInstanceContext(final InstanceContext instanceContext) {
public void setComputeNodeInstanceContext(final ComputeNodeInstanceContext instanceContext) {
int workerId = instanceContext.generateWorkerId(props);
MillisecondSnowflakeId millisecondSnowflakeId =
new MillisecondSnowflakeId(epoch, MillisecondSnowflakeId.DEFAULT_TIMESTAMP_BIT, MillisecondSnowflakeId.DEFAULT_MACHINE_BIT, MillisecondSnowflakeId.DEFAULT_SEQUENCE_BIT, workerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import org.apache.shardingsphere.infra.algorithm.keygen.core.KeyGenerateAlgorithm;
import org.apache.shardingsphere.infra.algorithm.keygen.cosid.constant.CosIdKeyGenerateConstants;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.util.Properties;
Expand All @@ -45,17 +44,19 @@
import static org.mockito.Mockito.mock;

class CosIdKeyGenerateAlgorithmTest {

@Test
void assertGenerateKey() {
String idName = "test-cosid";
DefaultSegmentId defaultSegmentId = new DefaultSegmentId(new IdSegmentDistributor.Mock());
DefaultIdGeneratorProvider.INSTANCE.set(idName, defaultSegmentId);
KeyGenerateAlgorithm algorithm = TypedSPILoader.getService(KeyGenerateAlgorithm.class, "COSID", PropertiesBuilder.build(new Property(CosIdKeyGenerateConstants.ID_NAME_KEY, idName)));
Properties props = new Properties();
props.setProperty(CosIdKeyGenerateConstants.ID_NAME_KEY, idName);
KeyGenerateAlgorithm algorithm = TypedSPILoader.getService(KeyGenerateAlgorithm.class, "COSID", props);
assertThat(algorithm.generateKeys(mock(AlgorithmSQLContext.class), 1).iterator().next(), is(1L));
assertThat(algorithm.generateKeys(mock(AlgorithmSQLContext.class), 1).iterator().next(), is(2L));
}

@Test
void assertGenerateKeyWhenNotSetIdName() {
DefaultSegmentId defaultSegmentId = new DefaultSegmentId(new IdSegmentDistributor.Mock());
Expand All @@ -64,20 +65,23 @@ void assertGenerateKeyWhenNotSetIdName() {
assertThat(algorithm.generateKeys(mock(AlgorithmSQLContext.class), 1).iterator().next(), is(1L));
assertThat(algorithm.generateKeys(mock(AlgorithmSQLContext.class), 1).iterator().next(), is(2L));
}

@Test
void assertGenerateKeyWhenIdProviderIsEmpty() {
DefaultIdGeneratorProvider.INSTANCE.clear();
assertThrows(NotFoundIdGeneratorException.class, () -> TypedSPILoader.getService(KeyGenerateAlgorithm.class, "COSID").generateKeys(mock(AlgorithmSQLContext.class), 1).iterator().next());
}


@Disabled
// TODO fix this test, IdGenerator name:[__share__] not found.
@Test
void assertGenerateKeyAsString() {
String idName = "test-cosid-as-string";
String prefix = "test_";
IdGenerator idGeneratorDecorator = new StringIdGeneratorDecorator(new MillisecondSnowflakeId(1, 0), new PrefixIdConverter(prefix, Radix62IdConverter.INSTANCE));
DefaultIdGeneratorProvider.INSTANCE.set(idName, idGeneratorDecorator);
Properties props = PropertiesBuilder.build(new Property(CosIdKeyGenerateConstants.ID_NAME_KEY, idName), new Property("as-string", Boolean.TRUE.toString()));
Properties props = new Properties();
props.setProperty("as-string", Boolean.TRUE.toString());
KeyGenerateAlgorithm algorithm = TypedSPILoader.getService(KeyGenerateAlgorithm.class, "COSID", props);
Comparable<?> actual = algorithm.generateKeys(mock(AlgorithmSQLContext.class), 1).iterator().next();
assertThat(actual, instanceOf(String.class));
Expand Down
Loading