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

63 lines
2.5 KiB
Java
Raw Normal View History

/*
* Copyright 2018 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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) {
throw new AssertionError(e);
2018-06-10 17:12:44 +02:00
}
}
}