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 2021/11/09 03:28:31 UTC
[GitHub] [shardingsphere] strongduanmu opened a new issue #13513: Add generics for EncryptAlgorithm and QueryAssistedEncryptAlgorithm interfaces
strongduanmu opened a new issue #13513:
URL: https://github.com/apache/shardingsphere/issues/13513
## Feature Request
At present, the EncryptAlgorithm and QueryAssistedEncryptAlgorithm interfaces only support `String` type encryption and decryption, which meets many general requirements, but also limits the scalability of the algorithm.
```java
/**
* Encrypt algorithm for SPI.
*/
public interface EncryptAlgorithm extends ShardingSphereAlgorithm, ShardingSphereAlgorithmPostProcessor {
/**
* Encode.
*
* @param plaintext plaintext
* @return ciphertext
*/
String encrypt(Object plaintext);
/**
* Decode.
*
* @param ciphertext ciphertext
* @return plaintext
*/
Object decrypt(String ciphertext);
}
```
Therefore, we expect to introduce generics to provide users with better scalability. For example, user wants to enter the `Integer` type for encryption, and then return the encryption result of the `Integer` type. Through the generic interface, user can implement this algorithm by himself.
### Is your feature request related to a problem?
### Describe the feature you would like.
The interface after adding generics is as follows:
```java
/**
* Encrypt algorithm for SPI.
*/
public interface EncryptAlgorithm<I, O> extends ShardingSphereAlgorithm, ShardingSphereAlgorithmPostProcessor {
/**
* Encode.
*
* @param plainValue plainValue
* @return cipherValue
*/
O encrypt(I plainValue);
/**
* Decode.
*
* @param cipherValue cipherValue
* @return plainValue
*/
I decrypt(O cipherValue);
}
```
The default encryption and decryption algorithm will still maintain the original interface declaration, that is, use the `<Object, String>` generic.
```java
/**
* AES encrypt algorithm.
*/
@Getter
@Setter
public final class AESEncryptAlgorithm implements EncryptAlgorithm<Object, String> {
...
@SneakyThrows(GeneralSecurityException.class)
@Override
public String encrypt(final Object plainValue) {
if (null == plainValue) {
return null;
}
byte[] result = getCipher(Cipher.ENCRYPT_MODE).doFinal(String.valueOf(plainValue).getBytes(StandardCharsets.UTF_8));
return DatatypeConverter.printBase64Binary(result);
}
@SneakyThrows(GeneralSecurityException.class)
@Override
public Object decrypt(final String cipherValue) {
if (null == cipherValue) {
return null;
}
byte[] result = getCipher(Cipher.DECRYPT_MODE).doFinal(DatatypeConverter.parseBase64Binary(cipherValue));
return new String(result, StandardCharsets.UTF_8);
}
```
--
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
[GitHub] [shardingsphere] tristaZero closed issue #13513: Add generics for EncryptAlgorithm and QueryAssistedEncryptAlgorithm interfaces
Posted by GitBox <gi...@apache.org>.
tristaZero closed issue #13513:
URL: https://github.com/apache/shardingsphere/issues/13513
--
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