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_r316120812
##########
File path:
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/security/util/crypto/OpenPGPPasswordBasedEncryptorTest.java
##########
@@ -63,60 +65,75 @@ 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
java.io.ByteArrayInputStream(PLAINTEXT.getBytes("UTF-8"));
- OutputStream cipherStream = new ByteArrayOutputStream();
- OutputStream recoveredStream = new ByteArrayOutputStream();
- // No file, just streams
- String filename = "tempFile.txt";
+ 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.");
- OpenPGPPasswordBasedEncryptor encryptor = new
OpenPGPPasswordBasedEncryptor(EncryptionMethod.PGP.getAlgorithm(),
EncryptionMethod.PGP.getProvider(), PASSWORD.toCharArray(), filename);
+ // Arrange
+ final String PLAINTEXT = "This is a plaintext message.";
+ logger.info("Plaintext: {}", PLAINTEXT);
+ InputStream plainStream = new
java.io.ByteArrayInputStream(PLAINTEXT.getBytes("UTF-8"));
+ OutputStream cipherStream = new ByteArrayOutputStream();
+ OutputStream recoveredStream = new ByteArrayOutputStream();
- StreamCallback encryptionCallback = encryptor.getEncryptionCallback();
- StreamCallback decryptionCallback = encryptor.getDecryptionCallback();
+ // No file, just streams
+ String filename = "tempFile.txt";
- // Act
- encryptionCallback.process(plainStream, cipherStream);
+ OpenPGPPasswordBasedEncryptor encryptor = new
OpenPGPPasswordBasedEncryptor(EncryptionMethod.PGP.getAlgorithm(),
+ cipher, EncryptionMethod.PGP.getProvider(),
PASSWORD.toCharArray(), filename);
- final byte[] cipherBytes = ((ByteArrayOutputStream)
cipherStream).toByteArray();
- logger.info("Encrypted: {}", Hex.encodeHexString(cipherBytes));
- InputStream cipherInputStream = new ByteArrayInputStream(cipherBytes);
+ StreamCallback encryptionCallback =
encryptor.getEncryptionCallback();
+ StreamCallback decryptionCallback =
encryptor.getDecryptionCallback();
- decryptionCallback.process(cipherInputStream, recoveredStream);
+ // Act
+ encryptionCallback.process(plainStream, cipherStream);
- // Assert
- byte[] recoveredBytes = ((ByteArrayOutputStream)
recoveredStream).toByteArray();
- String recovered = new String(recoveredBytes, "UTF-8");
- logger.info("Recovered: {}", recovered);
- assert PLAINTEXT.equals(recovered);
+ 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(encryptedFile);
- OutputStream recoveredStream = new ByteArrayOutputStream();
-
- // No file, just streams
- String filename = encryptedFile.getName();
-
- OpenPGPPasswordBasedEncryptor encryptor = new
OpenPGPPasswordBasedEncryptor(EncryptionMethod.PGP.getAlgorithm(),
EncryptionMethod.PGP.getProvider(), LEGACY_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