1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-23 11:27:57 +01:00

Implement ProducerOptions.setHideArmorHeaders()

Fixes #328
This commit is contained in:
Paul Schaub 2022-10-31 11:43:24 +01:00
parent 8834d8ad10
commit 754fcf72a1
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 65 additions and 1 deletions

View file

@ -86,7 +86,7 @@ public final class EncryptionStream extends OutputStream {
outermostStream = new BufferedOutputStream(outermostStream); outermostStream = new BufferedOutputStream(outermostStream);
LOGGER.debug("Wrap encryption output in ASCII armor"); LOGGER.debug("Wrap encryption output in ASCII armor");
armorOutputStream = ArmoredOutputStreamFactory.get(outermostStream); armorOutputStream = ArmoredOutputStreamFactory.get(outermostStream, options);
if (options.hasComment()) { if (options.hasComment()) {
String[] commentLines = options.getComment().split("\n"); String[] commentLines = options.getComment().split("\n");
for (String commentLine : commentLines) { for (String commentLine : commentLines) {

View file

@ -22,6 +22,7 @@ public final class ProducerOptions {
private StreamEncoding encodingField = StreamEncoding.BINARY; private StreamEncoding encodingField = StreamEncoding.BINARY;
private boolean applyCRLFEncoding = false; private boolean applyCRLFEncoding = false;
private boolean cleartextSigned = false; private boolean cleartextSigned = false;
private boolean hideArmorHeaders = false;
private CompressionAlgorithm compressionAlgorithmOverride = PGPainless.getPolicy().getCompressionAlgorithmPolicy() private CompressionAlgorithm compressionAlgorithmOverride = PGPainless.getPolicy().getCompressionAlgorithmPolicy()
.defaultCompressionAlgorithm(); .defaultCompressionAlgorithm();
@ -302,4 +303,22 @@ public final class ProducerOptions {
public @Nullable SigningOptions getSigningOptions() { public @Nullable SigningOptions getSigningOptions() {
return signingOptions; return signingOptions;
} }
public boolean isHideArmorHeaders() {
return hideArmorHeaders;
}
/**
* If set to <pre>true</pre>, armor headers like version or comments will be omitted from armored output.
* By default, armor headers are not hidden.
* Note: If comments are added via {@link #setComment(String)}, those are not omitted, even if
* {@link #hideArmorHeaders} is set to <pre>true</pre>.
*
* @param hideArmorHeaders true or false
* @return this
*/
public ProducerOptions setHideArmorHeaders(boolean hideArmorHeaders) {
this.hideArmorHeaders = hideArmorHeaders;
return this;
}
} }

View file

@ -7,6 +7,7 @@ package org.pgpainless.util;
import java.io.OutputStream; import java.io.OutputStream;
import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.pgpainless.encryption_signing.ProducerOptions;
/** /**
* Factory to create configured {@link ArmoredOutputStream ArmoredOutputStreams}. * Factory to create configured {@link ArmoredOutputStream ArmoredOutputStreams}.
@ -37,6 +38,16 @@ public final class ArmoredOutputStreamFactory {
return armoredOutputStream; return armoredOutputStream;
} }
public static ArmoredOutputStream get(OutputStream outputStream, ProducerOptions options) {
if (options.isHideArmorHeaders()) {
ArmoredOutputStream armorOut = new ArmoredOutputStream(outputStream);
armorOut.clearHeaders();
return armorOut;
} else {
return get(outputStream);
}
}
/** /**
* Overwrite the version header of ASCII armors with a custom value. * Overwrite the version header of ASCII armors with a custom value.
* Newlines in the version info string result in multiple version header entries. * Newlines in the version info string result in multiple version header entries.

View file

@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.encryption_signing;
import org.bouncycastle.openpgp.PGPException;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.util.Passphrase;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class HideArmorHeadersTest {
@Test
public void testVersionHeaderIsOmitted() throws PGPException, IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign()
.onOutputStream(out)
.withOptions(ProducerOptions.encrypt(
EncryptionOptions.get()
.addPassphrase(Passphrase.fromPassword("sw0rdf1sh")))
.setHideArmorHeaders(true));
encryptionStream.write("Hello, World!\n".getBytes());
encryptionStream.close();
assertTrue(out.toString().startsWith("-----BEGIN PGP MESSAGE-----\n\n")); // No "Version: PGPainless"
}
}