-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace key files in test with keys generated on the spot
- Loading branch information
1 parent
4c7eb72
commit a64a641
Showing
3 changed files
with
36 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 33 additions & 33 deletions
66
src/test/java/io/github/rabobank/shadow_tool/EncryptionServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
package io.github.rabobank.shadow_tool; | ||
|
||
import org.bouncycastle.util.encoders.Base64; | ||
import org.bouncycastle.util.io.pem.PemReader; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.crypto.Cipher; | ||
import java.io.File; | ||
import java.io.StringReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.security.KeyFactory; | ||
import java.security.KeyPair; | ||
import java.security.KeyPairGenerator; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivateKey; | ||
import java.security.PublicKey; | ||
import java.security.spec.PKCS8EncodedKeySpec; | ||
import java.security.spec.X509EncodedKeySpec; | ||
import java.util.Objects; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class EncryptionServiceTest { | ||
private static final PrivateKey PRIVATE_KEY; | ||
private static final PublicKey PUBLIC_KEY; | ||
|
||
static { | ||
try { | ||
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); | ||
keyPairGen.initialize(2048); | ||
KeyPair pair = keyPairGen.generateKeyPair(); | ||
PRIVATE_KEY = pair.getPrivate(); | ||
PUBLIC_KEY = pair.getPublic(); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Test | ||
void encryptAndDecrypt() throws Exception { | ||
final var encryptionService = new EncryptionService(publicKey()); | ||
final var encryptCipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"); | ||
encryptCipher.init(Cipher.ENCRYPT_MODE, PUBLIC_KEY); | ||
final var encryptionService = new DefaultEncryptionService(encryptCipher); | ||
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 | ||
var privateKey = privateKey(); | ||
final var cipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"); | ||
cipher.init(Cipher.DECRYPT_MODE, privateKey); | ||
final var cipherText = cipher.doFinal(Base64.decode(encryptedDifferences)); | ||
final var decryptCipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"); | ||
decryptCipher.init(Cipher.DECRYPT_MODE, PRIVATE_KEY); | ||
final var cipherText = decryptCipher.doFinal(Base64.decode(encryptedDifferences)); | ||
final var expectedUnencryptedResult = new String(cipherText, StandardCharsets.UTF_8); | ||
|
||
assertEquals(expectedUnencryptedResult, plainDifferences); | ||
} | ||
|
||
private static PrivateKey privateKey() throws Exception { | ||
final var privateKeyFile = new File(Objects.requireNonNull(EncryptionServiceTest.class.getClassLoader().getResource("private.key")).getFile()); | ||
final var reader = new StringReader(Files.readString(privateKeyFile.toPath())); | ||
final var pemReader = new PemReader(reader); | ||
final var factory = KeyFactory.getInstance("RSA"); | ||
final var pemObject = pemReader.readPemObject(); | ||
final var keyContentAsBytesFromBC = pemObject.getContent(); | ||
final var privKeySpec = new PKCS8EncodedKeySpec(keyContentAsBytesFromBC); | ||
return factory.generatePrivate(privKeySpec); | ||
} | ||
|
||
private static PublicKey publicKey() throws Exception { | ||
final var publicKeyFile = new File(Objects.requireNonNull(EncryptionServiceTest.class.getClassLoader().getResource("public.key")).getFile()); | ||
final var reader = new StringReader(Files.readString(publicKeyFile.toPath())); | ||
final var pemReader = new PemReader(reader); | ||
final var factory = KeyFactory.getInstance("RSA"); | ||
final var pemObject = pemReader.readPemObject(); | ||
final var keyContentAsBytesFromBC = pemObject.getContent(); | ||
final var pubKeySpec = new X509EncodedKeySpec(keyContentAsBytesFromBC); | ||
return factory.generatePublic(pubKeySpec); | ||
@Test | ||
void encryptAndForgotToInitCipher() throws Exception { | ||
final var encryptCipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"); | ||
final var encryptionService = new DefaultEncryptionService(encryptCipher); | ||
final var plainDifferences = "'place' changed: 'Dintelooord' -> 'Dinteloord'\n" + | ||
"'madrigals' collection changes :\n" + | ||
" 1. 'Bruno' changed to 'Mirabel'\n" + | ||
" 0. 'Bruno' added"; | ||
final var exception = assertThrows(SecurityException.class, () -> encryptionService.encrypt(plainDifferences)); | ||
assertEquals("java.lang.IllegalStateException: Cipher not initialized", exception.getMessage()); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.