1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-06-17 17:14:51 +02:00
pgpainless/src/main/java/de/vanitasvitae/crypto/pgpainless/util/BCUtil.java

49 lines
1.9 KiB
Java
Raw Normal View History

2018-06-10 17:12:44 +02:00
package de.vanitasvitae.crypto.pgpainless.util;
import java.io.IOException;
2018-06-11 01:33:49 +02:00
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
2018-06-10 17:12:44 +02:00
import java.util.Arrays;
import java.util.Iterator;
2018-06-11 01:33:49 +02:00
import java.util.List;
2018-06-10 17:12:44 +02:00
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
public class BCUtil {
public static PGPPublicKeyRingCollection keyRingsToKeyRingCollection(PGPPublicKeyRing... rings)
throws IOException, PGPException {
return new PGPPublicKeyRingCollection(Arrays.asList(rings));
}
public static PGPSecretKeyRingCollection keyRingsToKeyRingCollection(PGPSecretKeyRing... rings)
throws IOException, PGPException {
return new PGPSecretKeyRingCollection(Arrays.asList(rings));
}
2018-06-11 01:33:49 +02:00
public static PGPPublicKeyRing publicKeyRingFromSecretKeyRing(PGPSecretKeyRing secring) {
List<PGPPublicKey> list = new ArrayList<>();
for (Iterator<PGPPublicKey> i = secring.getPublicKeys(); i.hasNext(); ) {
2018-06-10 17:12:44 +02:00
PGPPublicKey k = i.next();
2018-06-11 01:33:49 +02:00
list.add(k);
}
// TODO: Change to simply using the List constructor once BC 1.60 gets released.
try {
Constructor<PGPPublicKeyRing> constructor;
constructor = PGPPublicKeyRing.class.getDeclaredConstructor(List.class);
constructor.setAccessible(true);
PGPPublicKeyRing pubring = constructor.newInstance(list);
return pubring;
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
e.printStackTrace();
return null;
2018-06-10 17:12:44 +02:00
}
}
}