From 04724808b5a3d1d65249c170273a80151ab524d4 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 4 Jan 2022 17:22:02 +0100 Subject: [PATCH] ArmorUtils: Add method to print single public keys --- .../java/org/pgpainless/util/ArmorUtils.java | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) 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 38527997..9d73635e 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/ArmorUtils.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/ArmorUtils.java @@ -18,8 +18,10 @@ import java.util.regex.Pattern; import org.bouncycastle.bcpg.ArmoredInputStream; import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.openpgp.PGPKeyRing; +import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; +import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.bouncycastle.openpgp.PGPUtil; @@ -43,13 +45,23 @@ public final class ArmorUtils { } + public static String toAsciiArmoredString(PGPSecretKey secretKey) throws IOException { + MultiMap header = keyToHeader(secretKey.getPublicKey()); + return toAsciiArmoredString(secretKey.getEncoded(), header); + } + + public static String toAsciiArmoredString(PGPPublicKey publicKey) throws IOException { + MultiMap header = keyToHeader(publicKey); + return toAsciiArmoredString(publicKey.getEncoded(), header); + } + public static String toAsciiArmoredString(PGPSecretKeyRing secretKeys) throws IOException { - MultiMap header = keyToHeader(secretKeys); + MultiMap header = keysToHeader(secretKeys); return toAsciiArmoredString(secretKeys.getEncoded(), header); } public static String toAsciiArmoredString(PGPPublicKeyRing publicKeys) throws IOException { - MultiMap header = keyToHeader(publicKeys); + MultiMap header = keysToHeader(publicKeys); return toAsciiArmoredString(publicKeys.getEncoded(), header); } @@ -66,7 +78,7 @@ public final class ArmorUtils { } public static ArmoredOutputStream toAsciiArmoredStream(PGPKeyRing keyRing, OutputStream outputStream) { - MultiMap header = keyToHeader(keyRing); + MultiMap header = keysToHeader(keyRing); return toAsciiArmoredStream(outputStream, header); } @@ -94,10 +106,10 @@ public final class ArmorUtils { return sb.toString(); } - private static MultiMap keyToHeader(PGPKeyRing keyRing) { + private static MultiMap keyToHeader(PGPPublicKey publicKey) { MultiMap header = new MultiMap<>(); - OpenPgpFingerprint fingerprint = OpenPgpFingerprint.of(keyRing); - Iterator userIds = keyRing.getPublicKey().getUserIDs(); + OpenPgpFingerprint fingerprint = OpenPgpFingerprint.of(publicKey); + Iterator userIds = publicKey.getUserIDs(); header.put(HEADER_COMMENT, fingerprint.prettyPrint()); if (userIds.hasNext()) { @@ -106,6 +118,11 @@ public final class ArmorUtils { return header; } + private static MultiMap keysToHeader(PGPKeyRing keyRing) { + PGPPublicKey publicKey = keyRing.getPublicKey(); + return keyToHeader(publicKey); + } + public static String toAsciiArmoredString(byte[] bytes) throws IOException { return toAsciiArmoredString(bytes, null); } @@ -147,7 +164,7 @@ public final class ArmorUtils { public static ArmoredOutputStream createArmoredOutputStreamFor(PGPKeyRing keyRing, OutputStream outputStream) { ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(outputStream); - MultiMap headerMap = keyToHeader(keyRing); + MultiMap headerMap = keysToHeader(keyRing); for (String header : headerMap.keySet()) { for (String value : headerMap.get(header)) { armor.addHeader(header, value);