From 4b4126e45c0a2cd4b84a7691e07fe065bc8441ea Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Thu, 12 Jul 2018 18:57:45 +0200 Subject: [PATCH] Add convenience methods to PGPKeyRing --- .../pgpainless/key/collection/PGPKeyRing.java | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/pgpainless/pgpainless/key/collection/PGPKeyRing.java b/src/main/java/org/pgpainless/pgpainless/key/collection/PGPKeyRing.java index cd0ea5d5..68b5863a 100644 --- a/src/main/java/org/pgpainless/pgpainless/key/collection/PGPKeyRing.java +++ b/src/main/java/org/pgpainless/pgpainless/key/collection/PGPKeyRing.java @@ -15,8 +15,10 @@ */ package org.pgpainless.pgpainless.key.collection; +import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint; public class PGPKeyRing { @@ -24,10 +26,40 @@ public class PGPKeyRing { private PGPSecretKeyRing secretKeys; public PGPKeyRing(PGPPublicKeyRing publicKeys, PGPSecretKeyRing secretKeys) { + if (secretKeys == null && publicKeys == null) { + throw new IllegalArgumentException("publicKeys and secretKeys MUST NOT be both null."); + } + + if (publicKeys != null && secretKeys != null) { + if (publicKeys.getPublicKey().getKeyID() != secretKeys.getPublicKey().getKeyID()) { + throw new IllegalArgumentException("publicKeys and secretKeys must have the same master key."); + } + } + this.publicKeys = publicKeys; this.secretKeys = secretKeys; } + public long getKeyId() { + return getMasterKey().getKeyID(); + } + + public PGPPublicKey getMasterKey() { + PGPPublicKey publicKey = hasSecretKeys() ? secretKeys.getPublicKey() : publicKeys.getPublicKey(); + if (!publicKey.isMasterKey()) { + throw new IllegalStateException("Expected master key is not a master key"); + } + return publicKey; + } + + public OpenPgpV4Fingerprint getV4Fingerprint() { + return new OpenPgpV4Fingerprint(getMasterKey()); + } + + public boolean hasSecretKeys() { + return secretKeys != null; + } + public PGPPublicKeyRing getPublicKeys() { return publicKeys; } @@ -35,6 +67,4 @@ public class PGPKeyRing { public PGPSecretKeyRing getSecretKeys() { return secretKeys; } - - }