Skip to content

Commit

Permalink
Support multiline comments in ProducerOption.setComment().
Browse files Browse the repository at this point in the history
  • Loading branch information
ferenc-hechler authored and vanitasvitae committed Feb 24, 2022
1 parent 53f7815 commit 7a77d08
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ private void prepareArmor() {
LOGGER.debug("Wrap encryption output in ASCII armor");
armorOutputStream = ArmoredOutputStreamFactory.get(outermostStream);
if (options.hasComment()) {
ArmorUtils.addCommentHeader(armorOutputStream, options.getComment());
String[] commentLines = options.getComment().split("\n");
for (String commentLine : commentLines) {
if (!commentLine.trim().isEmpty()) {
ArmorUtils.addCommentHeader(armorOutputStream, commentLine);
}
}
}
outermostStream = armorOutputStream;
}
Expand Down
15 changes: 11 additions & 4 deletions pgpainless-core/src/test/java/org/pgpainless/example/Encrypt.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ public void encryptUsingPassphrase() throws PGPException, IOException {
/**
* In this example, Alice is sending a signed and encrypted message to Bob.
* She encrypts the message to both bobs certificate and her own.
* A comment header with the text "This comment was added using options." is added
* using the fluent ProducerOption syntax.
* A multiline comment header is added using the fluent ProducerOption syntax.
*
* Bob subsequently decrypts the message using his key.
*/
Expand All @@ -152,7 +151,13 @@ public void encryptWithCommentHeader() throws PGPException, InvalidAlgorithmPara

// plaintext message to encrypt
String message = "Hello, World!\n";
String comment = "This comment was added using options.";
String[] comments = {
"This comment was added using options.",
"And it has three lines.",
" ",
"Empty lines are skipped."
};
String comment = comments[0] + "\n" + comments[1] + "\n" + comments[2] + "\n" + comments[3];
ByteArrayOutputStream ciphertext = new ByteArrayOutputStream();
// Encrypt and sign
EncryptionStream encryptor = PGPainless.encryptAndOrSign()
Expand All @@ -172,7 +177,9 @@ public void encryptWithCommentHeader() throws PGPException, InvalidAlgorithmPara
String encryptedMessage = ciphertext.toString();

// check that comment header was added after "BEGIN PGP" and "Version:"
assertEquals(encryptedMessage.split("\n")[2].trim(), "Comment: " + comment);
assertEquals(encryptedMessage.split("\n")[2].trim(), "Comment: " + comments[0]);
assertEquals(encryptedMessage.split("\n")[3].trim(), "Comment: " + comments[1]);
assertEquals(encryptedMessage.split("\n")[4].trim(), "Comment: " + comments[3]);

// also test, that decryption still works...

Expand Down

0 comments on commit 7a77d08

Please sign in to comment.