Skip to content

Commit

Permalink
Noop encryption should encode to base64
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvisser committed Nov 15, 2023
1 parent e10d67d commit d600f12
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package io.github.rabobank.shadow_tool;

import org.bouncycastle.util.encoders.Base64;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* A version of the Encryption Service that doesn't perform any encryption.
* A version of the Encryption Service that doesn't perform any encryption,
* it only encodes the differences as a Base 64 String.
* <p>
* This might be useful for simple use-case where encryption of differences is not required,
* for example with public data.
*
* @see Base64#toBase64String(byte[])
*/
class NoopEncryptionService implements EncryptionService {
public class NoopEncryptionService implements EncryptionService {
public static final NoopEncryptionService INSTANCE = new NoopEncryptionService();

NoopEncryptionService() {
}

@Override
public String encrypt(final String value) {
return value;
return Base64.toBase64String(value.getBytes(UTF_8));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import org.junit.jupiter.api.Test;

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import static java.nio.charset.StandardCharsets.UTF_8;
import static javax.crypto.Cipher.DECRYPT_MODE;
import static javax.crypto.Cipher.ENCRYPT_MODE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand All @@ -19,9 +21,9 @@ class EncryptionServiceTest {

static {
try {
var keyPairGen = KeyPairGenerator.getInstance("RSA");
final var keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048);
var pair = keyPairGen.generateKeyPair();
final var pair = keyPairGen.generateKeyPair();
PRIVATE_KEY = pair.getPrivate();
PUBLIC_KEY = pair.getPublic();
} catch (NoSuchAlgorithmException e) {
Expand All @@ -32,7 +34,7 @@ class EncryptionServiceTest {
@Test
void encryptAndDecrypt() throws Exception {
final var encryptCipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");
encryptCipher.init(Cipher.ENCRYPT_MODE, PUBLIC_KEY);
encryptCipher.init(ENCRYPT_MODE, PUBLIC_KEY);
final var encryptionService = new DefaultEncryptionService(encryptCipher);
final var plainDifferences = "'place' changed: 'Dintelooord' -> 'Dinteloord'\n" +
"'madrigals' collection changes :\n" +
Expand All @@ -41,11 +43,11 @@ void encryptAndDecrypt() throws Exception {
final var encryptedDifferences = encryptionService.encrypt(plainDifferences);
//Decrypt and verify
final var decryptCipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");
decryptCipher.init(Cipher.DECRYPT_MODE, PRIVATE_KEY);
decryptCipher.init(DECRYPT_MODE, PRIVATE_KEY);
final var cipherText = decryptCipher.doFinal(Base64.decode(encryptedDifferences));
final var expectedUnencryptedResult = new String(cipherText, StandardCharsets.UTF_8);
final var result = new String(cipherText, UTF_8);

assertEquals(plainDifferences, expectedUnencryptedResult);
assertEquals(plainDifferences, result);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package io.github.rabobank.shadow_tool;

import org.bouncycastle.util.encoders.Base64;
import org.junit.jupiter.api.Test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;

class NoopEncryptionServiceTest {
@Test
void valuesAreNotEncrypted() {
void encryptAndDecrypt() {
final var encryptionService = NoopEncryptionService.INSTANCE;
final var plainDifferences = "'place' changed: 'Dintelooord' -> 'Dinteloord'\n" +
"'madrigals' collection changes :\n" +
" 1. 'Bruno' changed to 'Mirabel'\n" +
" 0. 'Bruno' added";
final var encryptedDifferences = encryptionService.encrypt(plainDifferences);
//Decrypt and verify
final var decoded = Base64.decode(encryptedDifferences);
final var result = new String(decoded, UTF_8);

assertEquals(plainDifferences, encryptedDifferences);
assertEquals(plainDifferences, result);
}
}

0 comments on commit d600f12

Please sign in to comment.