From 5bccc1960e51a1cdcc085a780a3bd663e0b7897f Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 27 Sep 2022 16:11:45 +0200 Subject: [PATCH] Add PGPainless.asciiArmor(key, outputStream) --- .../main/java/org/pgpainless/PGPainless.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/PGPainless.java b/pgpainless-core/src/main/java/org/pgpainless/PGPainless.java index 9ff2a3b0..e41304b7 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/PGPainless.java +++ b/pgpainless-core/src/main/java/org/pgpainless/PGPainless.java @@ -5,9 +5,11 @@ package org.pgpainless; import java.io.IOException; +import java.io.OutputStream; import java.util.Date; import javax.annotation.Nonnull; +import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing; @@ -89,9 +91,10 @@ public final class PGPainless { * @param key key or certificate * @return ascii armored string * - * @throws IOException in case of an error in the {@link org.bouncycastle.bcpg.ArmoredOutputStream} + * @throws IOException in case of an error in the {@link ArmoredOutputStream} */ - public static String asciiArmor(@Nonnull PGPKeyRing key) throws IOException { + public static String asciiArmor(@Nonnull PGPKeyRing key) + throws IOException { if (key instanceof PGPSecretKeyRing) { return ArmorUtils.toAsciiArmoredString((PGPSecretKeyRing) key); } else { @@ -99,6 +102,21 @@ public final class PGPainless { } } + /** + * Wrap a key of certificate in ASCII armor and write the result into the given {@link OutputStream}. + * + * @param key key or certificate + * @param outputStream output stream + * + * @throws IOException in case of an error ion the {@link ArmoredOutputStream} + */ + public static void asciiArmor(@Nonnull PGPKeyRing key, @Nonnull OutputStream outputStream) + throws IOException { + ArmoredOutputStream armorOut = ArmorUtils.toAsciiArmoredStream(key, outputStream); + key.encode(armorOut); + armorOut.close(); + } + /** * Create an {@link EncryptionStream}, which can be used to encrypt and/or sign data using OpenPGP. *