mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-27 06:42:05 +01:00
Print fingerprint+user-id in comment headers of Armor
This commit is contained in:
parent
ec611d7c5f
commit
892f452da8
2 changed files with 38 additions and 7 deletions
|
@ -19,30 +19,61 @@ import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
||||||
|
import org.bouncycastle.openpgp.PGPKeyRing;
|
||||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||||
import org.bouncycastle.util.io.Streams;
|
import org.bouncycastle.util.io.Streams;
|
||||||
|
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
||||||
|
|
||||||
public class ArmorUtils {
|
public class ArmorUtils {
|
||||||
|
|
||||||
public static String toAsciiArmoredString(PGPSecretKeyRing secretKeys) throws IOException {
|
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 {
|
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 {
|
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 {
|
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();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(out);
|
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);
|
Streams.pipeAll(inputStream, armor);
|
||||||
armor.close();
|
armor.close();
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ package org.pgpainless.util;
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ public class MultiMap<K, V> {
|
||||||
public MultiMap(@Nonnull MultiMap<K, V> other) {
|
public MultiMap(@Nonnull MultiMap<K, V> other) {
|
||||||
this.map = new HashMap<>();
|
this.map = new HashMap<>();
|
||||||
for (K k : other.map.keySet()) {
|
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) {
|
public void put(K k, V v) {
|
||||||
Set<V> values = map.get(k);
|
Set<V> values = map.get(k);
|
||||||
if (values == null) {
|
if (values == null) {
|
||||||
values = new HashSet<>();
|
values = new LinkedHashSet<>();
|
||||||
map.put(k, values);
|
map.put(k, values);
|
||||||
}
|
}
|
||||||
values.add(v);
|
values.add(v);
|
||||||
|
|
Loading…
Reference in a new issue