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

Print fingerprint+user-id in comment headers of Armor

This commit is contained in:
Paul Schaub 2021-05-08 14:02:44 +02:00
parent ec611d7c5f
commit 892f452da8
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 38 additions and 7 deletions

View file

@ -19,30 +19,61 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.io.Streams;
import org.pgpainless.key.OpenPgpV4Fingerprint;
public class ArmorUtils {
public static String toAsciiArmoredString(PGPSecretKeyRing secretKeys) throws IOException {
return toAsciiArmoredString(secretKeys.getEncoded());
MultiMap<String, String> header = keyToHeader(secretKeys);
return toAsciiArmoredString(secretKeys.getEncoded(), header);
}
public static String toAsciiArmoredString(PGPPublicKeyRing publicKeys) throws IOException {
return toAsciiArmoredString(publicKeys.getEncoded());
MultiMap<String, String> header = keyToHeader(publicKeys);
return toAsciiArmoredString(publicKeys.getEncoded(), header);
}
private static MultiMap<String, String> keyToHeader(PGPKeyRing keyRing) {
MultiMap<String, String> header = new MultiMap<>();
OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(keyRing);
Iterator<String> userIds = keyRing.getPublicKey().getUserIDs();
header.put("Comment", fingerprint.prettyPrint());
if (userIds.hasNext()) {
header.put("Comment", userIds.next());
}
return header;
}
public static String toAsciiArmoredString(byte[] bytes) throws IOException {
return toAsciiArmoredString(new ByteArrayInputStream(bytes));
return toAsciiArmoredString(bytes, null);
}
public static String toAsciiArmoredString(byte[] bytes, MultiMap<String, String> additionalHeaderValues) throws IOException {
return toAsciiArmoredString(new ByteArrayInputStream(bytes), additionalHeaderValues);
}
public static String toAsciiArmoredString(InputStream inputStream) throws IOException {
return toAsciiArmoredString(inputStream, null);
}
public static String toAsciiArmoredString(InputStream inputStream, MultiMap<String, String> additionalHeaderValues) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(out);
if (additionalHeaderValues != null) {
for (String header : additionalHeaderValues.keySet()) {
for (String value : additionalHeaderValues.get(header)) {
armor.addHeader(header, value);
}
}
}
Streams.pipeAll(inputStream, armor);
armor.close();

View file

@ -18,7 +18,7 @@ package org.pgpainless.util;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
@ -33,7 +33,7 @@ public class MultiMap<K, V> {
public MultiMap(@Nonnull MultiMap<K, V> other) {
this.map = new HashMap<>();
for (K k : other.map.keySet()) {
map.put(k, new HashSet<>(other.map.get(k)));
map.put(k, new LinkedHashSet<>(other.map.get(k)));
}
}
@ -67,7 +67,7 @@ public class MultiMap<K, V> {
public void put(K k, V v) {
Set<V> values = map.get(k);
if (values == null) {
values = new HashSet<>();
values = new LinkedHashSet<>();
map.put(k, values);
}
values.add(v);