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

Add documentation to KeyRingUtils

This commit is contained in:
Paul Schaub 2020-12-03 22:46:59 +01:00
parent 1b9bce8da7
commit 1be3d4d7d8
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -32,6 +32,13 @@ import org.pgpainless.key.protection.SecretKeyRingProtector;
public class KeyRingUtils { public class KeyRingUtils {
/**
* Return the primary {@link PGPSecretKey} from the provided {@link PGPSecretKeyRing}.
* If it has no primary secret key, throw a {@link NoSuchElementException}.
*
* @param secretKeys secret keys
* @return primary secret key
*/
public static PGPSecretKey requirePrimarySecretKeyFrom(PGPSecretKeyRing secretKeys) { public static PGPSecretKey requirePrimarySecretKeyFrom(PGPSecretKeyRing secretKeys) {
PGPSecretKey primarySecretKey = getPrimarySecretKeyFrom(secretKeys); PGPSecretKey primarySecretKey = getPrimarySecretKeyFrom(secretKeys);
if (primarySecretKey == null) { if (primarySecretKey == null) {
@ -40,6 +47,12 @@ public class KeyRingUtils {
return primarySecretKey; return primarySecretKey;
} }
/**
* Return the primary {@link PGPSecretKey} from the provided {@link PGPSecretKeyRing} or null if it has none.
*
* @param secretKeys secret key ring
* @return primary secret key
*/
public static PGPSecretKey getPrimarySecretKeyFrom(PGPSecretKeyRing secretKeys) { public static PGPSecretKey getPrimarySecretKeyFrom(PGPSecretKeyRing secretKeys) {
PGPSecretKey secretKey = secretKeys.getSecretKey(); PGPSecretKey secretKey = secretKeys.getSecretKey();
if (secretKey.isMasterKey()) { if (secretKey.isMasterKey()) {
@ -48,6 +61,13 @@ public class KeyRingUtils {
return null; return null;
} }
/**
* Return the primary {@link PGPPublicKey} from the provided key ring.
* Throws a {@link NoSuchElementException} if the key ring has no primary public key.
*
* @param keyRing key ring
* @return primary public key
*/
public static PGPPublicKey requirePrimaryPublicKeyFrom(PGPKeyRing keyRing) { public static PGPPublicKey requirePrimaryPublicKeyFrom(PGPKeyRing keyRing) {
PGPPublicKey primaryPublicKey = getPrimaryPublicKeyFrom(keyRing); PGPPublicKey primaryPublicKey = getPrimaryPublicKeyFrom(keyRing);
if (primaryPublicKey == null) { if (primaryPublicKey == null) {
@ -56,6 +76,12 @@ public class KeyRingUtils {
return primaryPublicKey; return primaryPublicKey;
} }
/**
* Return the primary {@link PGPPublicKey} from the provided key ring or null if it has none.
*
* @param keyRing key ring
* @return primary public key
*/
public static PGPPublicKey getPrimaryPublicKeyFrom(PGPKeyRing keyRing) { public static PGPPublicKey getPrimaryPublicKeyFrom(PGPKeyRing keyRing) {
PGPPublicKey primaryPublicKey = keyRing.getPublicKey(); PGPPublicKey primaryPublicKey = keyRing.getPublicKey();
if (primaryPublicKey.isMasterKey()) { if (primaryPublicKey.isMasterKey()) {
@ -64,6 +90,12 @@ public class KeyRingUtils {
return null; return null;
} }
/**
* Extract a {@link PGPPublicKeyRing} containing all public keys from the provided {@link PGPSecretKeyRing}.
*
* @param secretKeys secret key ring
* @return public key ring
*/
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();
@ -74,6 +106,15 @@ public class KeyRingUtils {
return publicKeyRing; return publicKeyRing;
} }
/**
* Unlock a {@link PGPSecretKey} and return the resulting {@link PGPPrivateKey}.
*
* @param secretKey secret key
* @param protector protector to unlock the secret key
* @return private key
*
* @throws PGPException if something goes wrong (eg. wrong passphrase)
*/
public static PGPPrivateKey unlockSecretKey(PGPSecretKey secretKey, SecretKeyRingProtector protector) throws PGPException { public static PGPPrivateKey unlockSecretKey(PGPSecretKey secretKey, SecretKeyRingProtector protector) throws PGPException {
PBESecretKeyDecryptor secretKeyDecryptor = protector.getDecryptor(secretKey.getKeyID()); PBESecretKeyDecryptor secretKeyDecryptor = protector.getDecryptor(secretKey.getKeyID());
PGPPrivateKey privateKey = secretKey.extractPrivateKey(secretKeyDecryptor); PGPPrivateKey privateKey = secretKey.extractPrivateKey(secretKeyDecryptor);