diff --git a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java index 79a0a24e..36d14fdb 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java +++ b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java @@ -51,6 +51,7 @@ import org.pgpainless.decryption_verification.DetachedSignature; import org.pgpainless.decryption_verification.OpenPgpMetadata; import org.pgpainless.implementation.ImplementationFactory; import org.pgpainless.key.OpenPgpV4Fingerprint; +import org.pgpainless.util.ArmoredOutputStreamFactory; import org.pgpainless.util.Passphrase; /** @@ -146,7 +147,7 @@ public final class EncryptionStream extends OutputStream { } LOGGER.log(LEVEL, "Wrap encryption output in ASCII armor"); - armorOutputStream = new ArmoredOutputStream(outermostStream); + armorOutputStream = ArmoredOutputStreamFactory.get(outermostStream); outermostStream = armorOutputStream; } diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/ArmorUtils.java b/pgpainless-core/src/main/java/org/pgpainless/util/ArmorUtils.java index 7bae1324..6807e37f 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/ArmorUtils.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/ArmorUtils.java @@ -41,7 +41,7 @@ public class ArmorUtils { public static String toAsciiArmoredString(InputStream inputStream) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); - ArmoredOutputStream armor = new ArmoredOutputStream(out); + ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(out); Streams.pipeAll(inputStream, armor); armor.close(); diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/ArmoredOutputStreamFactory.java b/pgpainless-core/src/main/java/org/pgpainless/util/ArmoredOutputStreamFactory.java new file mode 100644 index 00000000..57deb8bc --- /dev/null +++ b/pgpainless-core/src/main/java/org/pgpainless/util/ArmoredOutputStreamFactory.java @@ -0,0 +1,34 @@ +/* + * Copyright 2021 Paul Schaub. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.pgpainless.util; + +import java.io.OutputStream; + +import org.bouncycastle.bcpg.ArmoredOutputStream; + +/** + * Factory to create configured {@link ArmoredOutputStream ArmoredOutputStreams}. + */ +public class ArmoredOutputStreamFactory { + + public static final String VERSION = "PGPainless"; + + public static ArmoredOutputStream get(OutputStream outputStream) { + ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream); + armoredOutputStream.setHeader(ArmoredOutputStream.VERSION_HDR, VERSION); + return armoredOutputStream; + } +} diff --git a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptDecryptTest.java b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptDecryptTest.java index dcb25efb..a5028d99 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptDecryptTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptDecryptTest.java @@ -52,6 +52,7 @@ import org.pgpainless.key.generation.type.rsa.RsaLength; import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.UnprotectedKeysProtector; import org.pgpainless.key.util.KeyRingUtils; +import org.pgpainless.util.ArmoredOutputStreamFactory; import org.pgpainless.util.BCUtil; public class EncryptDecryptTest { @@ -205,7 +206,7 @@ public class EncryptDecryptTest { Set signatureSet = metadata.getSignatures(); ByteArrayOutputStream sigOut = new ByteArrayOutputStream(); - ArmoredOutputStream armorOut = new ArmoredOutputStream(sigOut); + ArmoredOutputStream armorOut = ArmoredOutputStreamFactory.get(sigOut); signatureSet.iterator().next().encode(armorOut); armorOut.close(); String armorSig = sigOut.toString(); diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyTest.java index 531cb89e..41c82992 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyTest.java @@ -30,6 +30,7 @@ import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.key.util.KeyRingUtils; +import org.pgpainless.util.ArmoredOutputStreamFactory; public class GenerateKeyTest { @@ -41,13 +42,13 @@ public class GenerateKeyTest { PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - ArmoredOutputStream armor = new ArmoredOutputStream(bytes); + ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(bytes); secretKeys.encode(armor); armor.close(); String publicKey = new String(bytes.toByteArray()); bytes = new ByteArrayOutputStream(); - armor = new ArmoredOutputStream(bytes); + armor = ArmoredOutputStreamFactory.get(bytes); secretKeys.encode(armor); armor.close(); String privateKey = new String(bytes.toByteArray()); diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java index 67937f74..2eb77269 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/generation/GenerateKeyWithAdditionalUserIdTest.java @@ -35,6 +35,7 @@ import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.key.generation.type.KeyType; import org.pgpainless.key.generation.type.rsa.RsaLength; import org.pgpainless.key.util.KeyRingUtils; +import org.pgpainless.util.ArmoredOutputStreamFactory; public class GenerateKeyWithAdditionalUserIdTest { @@ -65,7 +66,7 @@ public class GenerateKeyWithAdditionalUserIdTest { assertFalse(userIds.hasNext()); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); - ArmoredOutputStream armor = new ArmoredOutputStream(byteOut); + ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(byteOut); secretKeys.encode(armor); armor.close(); diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/modification/RevokeKeyWithGenericCertificationSignatureTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/modification/RevokeKeyWithGenericCertificationSignatureTest.java index bf841111..850b957a 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/modification/RevokeKeyWithGenericCertificationSignatureTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/modification/RevokeKeyWithGenericCertificationSignatureTest.java @@ -28,6 +28,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.key.protection.UnprotectedKeysProtector; +import org.pgpainless.util.ArmoredOutputStreamFactory; /** * Test that makes sure that PGPainless can deal with keys that carry a key @@ -88,14 +89,14 @@ public class RevokeKeyWithGenericCertificationSignatureTest { PGPPublicKey pkr = secretKeyRing.getPublicKeys().next(); ByteArrayOutputStream pubOutBytes = new ByteArrayOutputStream(); - try (ArmoredOutputStream pubOut = new ArmoredOutputStream(pubOutBytes)) { + try (ArmoredOutputStream pubOut = ArmoredOutputStreamFactory.get(pubOutBytes)) { pkr.encode(pubOut); } pubOutBytes.close(); PGPSecretKey skr = secretKeyRing.getSecretKeys().next(); ByteArrayOutputStream secOutBytes = new ByteArrayOutputStream(); - try (ArmoredOutputStream privOut = new ArmoredOutputStream(secOutBytes)) { + try (ArmoredOutputStream privOut = ArmoredOutputStreamFactory.get(secOutBytes)) { skr.encode(privOut); } secOutBytes.close(); diff --git a/pgpainless-core/src/test/java/org/pgpainless/symmetric_encryption/LegacySymmetricEncryptionTest.java b/pgpainless-core/src/test/java/org/pgpainless/symmetric_encryption/LegacySymmetricEncryptionTest.java index c8b44c7b..c31b31b5 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/symmetric_encryption/LegacySymmetricEncryptionTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/symmetric_encryption/LegacySymmetricEncryptionTest.java @@ -27,6 +27,7 @@ import org.bouncycastle.openpgp.PGPException; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.algorithm.SymmetricKeyAlgorithm; +import org.pgpainless.util.ArmoredOutputStreamFactory; import org.pgpainless.util.Passphrase; public class LegacySymmetricEncryptionTest { @@ -52,7 +53,7 @@ public class LegacySymmetricEncryptionTest { Passphrase passphrase = new Passphrase(password.toCharArray()); byte[] enc = PGPainless.encryptWithPassword(plain, passphrase, SymmetricKeyAlgorithm.AES_128); ByteArrayOutputStream out = new ByteArrayOutputStream(); - ArmoredOutputStream armor = new ArmoredOutputStream(out); + ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(out); armor.write(enc); armor.flush(); armor.close(); diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/Armor.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/Armor.java index 7ddf047c..c93998ed 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/Armor.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/commands/Armor.java @@ -17,6 +17,7 @@ package org.pgpainless.sop.commands; import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.util.io.Streams; +import org.pgpainless.util.ArmoredOutputStreamFactory; import picocli.CommandLine; import java.io.IOException; @@ -49,7 +50,7 @@ public class Armor implements Runnable { @Override public void run() { - try (PushbackInputStream pbIn = new PushbackInputStream(System.in); ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(System.out)) { + try (PushbackInputStream pbIn = new PushbackInputStream(System.in); ArmoredOutputStream armoredOutputStream = ArmoredOutputStreamFactory.get(System.out)) { byte[] start = new byte[14]; int read = pbIn.read(start); pbIn.unread(read);