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

Add some more utility methods

This commit is contained in:
Paul Schaub 2020-11-29 19:06:49 +01:00
parent e1be34e007
commit 935af80d14
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -18,8 +18,10 @@ package org.pgpainless.key.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.openpgp.PGPPrivateKey; import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
@ -30,6 +32,38 @@ import org.pgpainless.key.protection.SecretKeyRingProtector;
public class KeyRingUtils { public class KeyRingUtils {
public static PGPSecretKey requirePrimarySecretKeyFrom(PGPSecretKeyRing secretKeys) {
PGPSecretKey primarySecretKey = getPrimarySecretKeyFrom(secretKeys);
if (primarySecretKey == null) {
throw new NoSuchElementException("Provided PGPSecretKeyRing has no primary secret key.");
}
return primarySecretKey;
}
public static PGPSecretKey getPrimarySecretKeyFrom(PGPSecretKeyRing secretKeys) {
PGPSecretKey secretKey = secretKeys.getSecretKey();
if (secretKey.isMasterKey()) {
return secretKey;
}
return null;
}
public static PGPPublicKey requirePrimaryPublicKeyFrom(PGPKeyRing keyRing) {
PGPPublicKey primaryPublicKey = getPrimaryPublicKeyFrom(keyRing);
if (primaryPublicKey == null) {
throw new NoSuchElementException("Provided PGPKeyRing has no primary public key.");
}
return primaryPublicKey;
}
public static PGPPublicKey getPrimaryPublicKeyFrom(PGPKeyRing keyRing) {
PGPPublicKey primaryPublicKey = keyRing.getPublicKey();
if (primaryPublicKey.isMasterKey()) {
return primaryPublicKey;
}
return null;
}
public static PGPPublicKeyRing publicKeyRingFrom(PGPSecretKeyRing secretKeys) { public static PGPPublicKeyRing publicKeyRingFrom(PGPSecretKeyRing secretKeys) {
List<PGPPublicKey> publicKeyList = new ArrayList<>(); List<PGPPublicKey> publicKeyList = new ArrayList<>();
Iterator<PGPPublicKey> publicKeyIterator = secretKeys.getPublicKeys(); Iterator<PGPPublicKey> publicKeyIterator = secretKeys.getPublicKeys();