1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-10 06:11:08 +01:00

Improve support for PGP[Secret|Public]KeyRingCollections

This commit is contained in:
Paul Schaub 2021-05-29 13:52:29 +02:00
parent 1a5baa0fa4
commit 82536eaa77
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
5 changed files with 217 additions and 34 deletions

View file

@ -136,26 +136,6 @@ public class KeyRingReader {
isSilent);
}
private static void validateStreamsNotBothNull(InputStream publicIn, InputStream secretIn) {
if (publicIn == null && secretIn == null) {
throw new NullPointerException("publicIn and secretIn cannot be BOTH null.");
}
}
private static PGPPublicKeyRing maybeReadPublicKeys(InputStream publicIn) throws IOException {
if (publicIn != null) {
return readPublicKeyRing(publicIn);
}
return null;
}
private static PGPSecretKeyRing maybeReadSecretKeys(InputStream secretIn) throws IOException, PGPException {
if (secretIn != null) {
return readSecretKeyRing(secretIn);
}
return null;
}
/**
* Hacky workaround for #96.
* For {@link PGPPublicKeyRingCollection#PGPPublicKeyRingCollection(InputStream, KeyFingerPrintCalculator)}

View file

@ -28,7 +28,9 @@ import org.bouncycastle.bcpg.ArmoredInputStream;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.util.io.Streams;
import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.key.OpenPgpV4Fingerprint;
@ -54,6 +56,30 @@ public class ArmorUtils {
return toAsciiArmoredString(publicKeys.getEncoded(), header);
}
public static String toAsciiArmoredString(PGPSecretKeyRingCollection secretKeyRings) throws IOException {
StringBuilder sb = new StringBuilder();
for (Iterator<PGPSecretKeyRing> iterator = secretKeyRings.iterator(); iterator.hasNext(); ) {
PGPSecretKeyRing secretKeyRing = iterator.next();
sb.append(toAsciiArmoredString(secretKeyRing));
if (iterator.hasNext()) {
sb.append('\n');
}
}
return sb.toString();
}
public static String toAsciiArmoredString(PGPPublicKeyRingCollection publicKeyRings) throws IOException {
StringBuilder sb = new StringBuilder();
for (Iterator<PGPPublicKeyRing> iterator = publicKeyRings.iterator(); iterator.hasNext(); ) {
PGPPublicKeyRing publicKeyRing = iterator.next();
sb.append(toAsciiArmoredString(publicKeyRing));
if (iterator.hasNext()) {
sb.append('\n');
}
}
return sb.toString();
}
private static MultiMap<String, String> keyToHeader(PGPKeyRing keyRing) {
MultiMap<String, String> header = new MultiMap<>();
OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(keyRing);