1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-23 04:42:06 +01:00

Add convenience methods to PGPKeyRing

This commit is contained in:
Paul Schaub 2018-07-12 18:57:45 +02:00
parent ded635b354
commit 4b4126e45c
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -15,8 +15,10 @@
*/ */
package org.pgpainless.pgpainless.key.collection; package org.pgpainless.pgpainless.key.collection;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
public class PGPKeyRing { public class PGPKeyRing {
@ -24,10 +26,40 @@ public class PGPKeyRing {
private PGPSecretKeyRing secretKeys; private PGPSecretKeyRing secretKeys;
public PGPKeyRing(PGPPublicKeyRing publicKeys, 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.publicKeys = publicKeys;
this.secretKeys = secretKeys; 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() { public PGPPublicKeyRing getPublicKeys() {
return publicKeys; return publicKeys;
} }
@ -35,6 +67,4 @@ public class PGPKeyRing {
public PGPSecretKeyRing getSecretKeys() { public PGPSecretKeyRing getSecretKeys() {
return secretKeys; return secretKeys;
} }
} }