adarmiento commented on a change in pull request #3664: NIFI-6536
EncryptContent accepts configurable PGP symmetric cipher
URL: https://github.com/apache/nifi/pull/3664#discussion_r316120580
##########
File path:
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/security/util/crypto/OpenPGPKeyBasedEncryptorTest.java
##########
@@ -67,65 +69,78 @@ public void tearDown() throws Exception {
@Test
public void testShouldEncryptAndDecrypt() throws Exception {
- // Arrange
- final String PLAINTEXT = "This is a plaintext message.";
- logger.info("Plaintext: {}", PLAINTEXT);
- InputStream plainStream = new
ByteArrayInputStream(PLAINTEXT.getBytes("UTF-8"));
- OutputStream cipherStream = new ByteArrayOutputStream();
- OutputStream recoveredStream = new ByteArrayOutputStream();
-
- // No file, just streams
- String filename = "tempFile.txt";
-
- // Encryptor does not require password
- OpenPGPKeyBasedEncryptor encryptor = new OpenPGPKeyBasedEncryptor(
- EncryptionMethod.PGP.getAlgorithm(),
EncryptionMethod.PGP.getProvider(), PUBLIC_KEYRING_PATH, USER_ID, new char[0],
filename);
- StreamCallback encryptionCallback = encryptor.getEncryptionCallback();
-
- OpenPGPKeyBasedEncryptor decryptor = new OpenPGPKeyBasedEncryptor(
- EncryptionMethod.PGP.getAlgorithm(),
EncryptionMethod.PGP.getProvider(), SECRET_KEYRING_PATH, USER_ID,
PASSWORD.toCharArray(), filename);
- StreamCallback decryptionCallback = decryptor.getDecryptionCallback();
-
- // Act
- encryptionCallback.process(plainStream, cipherStream);
-
- final byte[] cipherBytes = ((ByteArrayOutputStream)
cipherStream).toByteArray();
- logger.info("Encrypted: {}", Hex.encodeHexString(cipherBytes));
- InputStream cipherInputStream = new ByteArrayInputStream(cipherBytes);
-
- decryptionCallback.process(cipherInputStream, recoveredStream);
-
- // Assert
- byte[] recoveredBytes = ((ByteArrayOutputStream)
recoveredStream).toByteArray();
- String recovered = new String(recoveredBytes, "UTF-8");
- logger.info("Recovered: {}", recovered);
- assert PLAINTEXT.equals(recovered);
+
+ for(int i = 1; i<14; i++) {
+ if (PGPEncryptedData.SAFER != i) {
+ Integer cipher = i;
+ System.out.println("Testing PGP encryption with " +
PGPUtil.getSymmetricCipherName(cipher) + " chiper.");
+ // Arrange
+ final String PLAINTEXT = "This is a plaintext message.";
+ logger.info("Plaintext: {}", PLAINTEXT);
+ InputStream plainStream = new
ByteArrayInputStream(PLAINTEXT.getBytes("UTF-8"));
+ OutputStream cipherStream = new ByteArrayOutputStream();
+ OutputStream recoveredStream = new ByteArrayOutputStream();
+
+ // No file, just streams
+ String filename = "tempFile.txt";
+
+
+ // Encryptor does not require password
+ OpenPGPKeyBasedEncryptor encryptor = new
OpenPGPKeyBasedEncryptor(
+ EncryptionMethod.PGP.getAlgorithm(), cipher,
EncryptionMethod.PGP.getProvider(), PUBLIC_KEYRING_PATH, USER_ID, new char[0],
filename);
+ StreamCallback encryptionCallback =
encryptor.getEncryptionCallback();
+
+ OpenPGPKeyBasedEncryptor decryptor = new
OpenPGPKeyBasedEncryptor(
+ EncryptionMethod.PGP.getAlgorithm(), cipher,
EncryptionMethod.PGP.getProvider(), SECRET_KEYRING_PATH, USER_ID,
PASSWORD.toCharArray(), filename);
+ StreamCallback decryptionCallback =
decryptor.getDecryptionCallback();
+
+ // Act
+ encryptionCallback.process(plainStream, cipherStream);
+
+ final byte[] cipherBytes = ((ByteArrayOutputStream)
cipherStream).toByteArray();
+ logger.info("Encrypted: {}", Hex.encodeHexString(cipherBytes));
+ InputStream cipherInputStream = new
ByteArrayInputStream(cipherBytes);
+
+ decryptionCallback.process(cipherInputStream, recoveredStream);
+
+ // Assert
+ byte[] recoveredBytes = ((ByteArrayOutputStream)
recoveredStream).toByteArray();
+ String recovered = new String(recoveredBytes, "UTF-8");
+ logger.info("Recovered: {}", recovered);
+ assert PLAINTEXT.equals(recovered);
+ }
+ }
}
@Test
public void testShouldDecryptExternalFile() throws Exception {
- // Arrange
- byte[] plainBytes = Files.readAllBytes(Paths.get(plainFile.getPath()));
- final String PLAINTEXT = new String(plainBytes, "UTF-8");
-
- InputStream cipherStream = new FileInputStream(unsignedFile);
- OutputStream recoveredStream = new ByteArrayOutputStream();
-
- // No file, just streams
- String filename = unsignedFile.getName();
-
- OpenPGPKeyBasedEncryptor encryptor = new OpenPGPKeyBasedEncryptor(
- EncryptionMethod.PGP.getAlgorithm(),
EncryptionMethod.PGP.getProvider(), SECRET_KEYRING_PATH, USER_ID,
PASSWORD.toCharArray(), filename);
-
- StreamCallback decryptionCallback = encryptor.getDecryptionCallback();
-
- // Act
- decryptionCallback.process(cipherStream, recoveredStream);
-
- // Assert
- byte[] recoveredBytes = ((ByteArrayOutputStream)
recoveredStream).toByteArray();
- String recovered = new String(recoveredBytes, "UTF-8");
- logger.info("Recovered: {}", recovered);
- Assert.assertEquals("Recovered text", PLAINTEXT, recovered);
+ for(int i = 1; i<14; i++) {
Review comment:
```suggestion
for (int i = 1; i<14; i++) {
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services