1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-10 14:21:09 +01:00

Add new ProducerOption setComment() for Ascii armored EncryptionStreams. (#254)

* Add new ProducerOption setComment() for Ascii armored EncryptionStreams.
This commit is contained in:
feri 2022-02-24 00:51:16 +01:00 committed by GitHub
parent 1753cef10e
commit 928fa12b51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 106 additions and 0 deletions

View file

@ -23,6 +23,7 @@ import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.SubkeyIdentifier;
import org.pgpainless.util.ArmorUtils;
import org.pgpainless.util.ArmoredOutputStreamFactory;
import org.pgpainless.util.StreamGeneratorWrapper;
import org.slf4j.Logger;
@ -74,6 +75,9 @@ public final class EncryptionStream extends OutputStream {
LOGGER.debug("Wrap encryption output in ASCII armor");
armorOutputStream = ArmoredOutputStreamFactory.get(outermostStream);
if (options.hasComment()) {
ArmorUtils.addCommentHeader(armorOutputStream, options.getComment());
}
outermostStream = armorOutputStream;
}

View file

@ -25,6 +25,7 @@ public final class ProducerOptions {
private CompressionAlgorithm compressionAlgorithmOverride = PGPainless.getPolicy().getCompressionAlgorithmPolicy()
.defaultCompressionAlgorithm();
private boolean asciiArmor = true;
private String comment = null;
private ProducerOptions(EncryptionOptions encryptionOptions, SigningOptions signingOptions) {
this.encryptionOptions = encryptionOptions;
@ -107,6 +108,40 @@ public final class ProducerOptions {
return asciiArmor;
}
/**
* set the comment for header in ascii armored output.
* The default value is null, which means no comment header is added.
* Multiline comments are possible using '\\n'.
*
* @param comment comment header text
* @return builder
*/
public ProducerOptions setComment(String comment) {
if (!asciiArmor) {
throw new IllegalArgumentException("Comment can only be set when ASCII armoring is enabled.");
}
this.comment = comment;
return this;
}
/**
* Return comment set for header in ascii armored output.
*
* @return comment
*/
public String getComment() {
return comment;
}
/**
* Return whether a comment was set (!= null).
*
* @return comment
*/
public boolean hasComment() {
return comment != null;
}
public ProducerOptions setCleartextSigned() {
if (signingOptions == null) {
throw new IllegalArgumentException("Signing Options cannot be null if cleartext signing is enabled.");