Package org.pgpainless.key.util
Class KeyRingUtils
- java.lang.Object
-
- org.pgpainless.key.util.KeyRingUtils
-
public final class KeyRingUtils extends java.lang.Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static org.bouncycastle.openpgp.PGPPublicKeygetPrimaryPublicKeyFrom(org.bouncycastle.openpgp.PGPKeyRing keyRing)Return the primaryPGPPublicKeyfrom the provided key ring or null if it has none.static org.bouncycastle.openpgp.PGPSecretKeygetPrimarySecretKeyFrom(org.bouncycastle.openpgp.PGPSecretKeyRing secretKeys)Return the primaryPGPSecretKeyfrom the providedPGPSecretKeyRingor null if it has none.static org.bouncycastle.openpgp.PGPPublicKeygetPublicKeyFrom(org.bouncycastle.openpgp.PGPKeyRing keyRing, long subKeyId)Return the public key with the given subKeyId from the keyRing.static <T extends org.bouncycastle.openpgp.PGPKeyRing>
TinjectCertification(T keyRing, java.lang.String userId, org.bouncycastle.openpgp.PGPSignature certification)Inject a user-id certification into the given key ring.static <T extends org.bouncycastle.openpgp.PGPKeyRing>
TinjectCertification(T keyRing, org.bouncycastle.openpgp.PGPPublicKey certifiedKey, org.bouncycastle.openpgp.PGPSignature certification)Inject a key certification into the given key ring.static <T extends org.bouncycastle.openpgp.PGPKeyRing>
TinjectCertification(T keyRing, org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector userAttributes, org.bouncycastle.openpgp.PGPSignature certification)Inject a user-attribute vector certification into the given key ring.static booleankeyRingContainsKeyWithId(org.bouncycastle.openpgp.PGPPublicKeyRing ring, long keyId)Return true, if the givenPGPPublicKeyRingcontains aPGPPublicKeyfor the given key id.static org.bouncycastle.openpgp.PGPPublicKeyRingCollectionkeyRingsToKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRing... rings)Create a newPGPPublicKeyRingCollectionfrom an array ofPGPPublicKeyRings.static org.bouncycastle.openpgp.PGPSecretKeyRingCollectionkeyRingsToKeyRingCollection(org.bouncycastle.openpgp.PGPSecretKeyRing... rings)Create a newPGPSecretKeyRingCollectionfrom an array ofPGPSecretKeyRings.static <T extends org.bouncycastle.openpgp.PGPKeyRing>
TkeysPlusPublicKey(T keyRing, org.bouncycastle.openpgp.PGPPublicKey publicKey)Inject aPGPPublicKeyinto the given key ring.static org.bouncycastle.openpgp.PGPSecretKeyRingkeysPlusSecretKey(org.bouncycastle.openpgp.PGPSecretKeyRing secretKeys, org.bouncycastle.openpgp.PGPSecretKey secretKey)Inject aPGPSecretKeyinto aPGPSecretKeyRing.static org.bouncycastle.openpgp.PGPPublicKeyRingCollectionpublicKeyRingCollectionFrom(org.bouncycastle.openpgp.PGPSecretKeyRingCollection secretKeyRings)ExtractPGPPublicKeyRingsfrom allPGPSecretKeyRingsin the givenPGPSecretKeyRingCollectionand return them as aPGPPublicKeyRingCollection.static org.bouncycastle.openpgp.PGPPublicKeyRingpublicKeyRingFrom(org.bouncycastle.openpgp.PGPSecretKeyRing secretKeys)Extract aPGPPublicKeyRingcontaining all public keys from the providedPGPSecretKeyRing.static org.bouncycastle.openpgp.PGPSecretKeyRingremoveSecretKey(org.bouncycastle.openpgp.PGPSecretKeyRing secretKeys, long secretKeyId)Deprecated.usestripSecretKey(PGPSecretKeyRing, long)instead.static org.bouncycastle.openpgp.PGPPublicKeyrequirePrimaryPublicKeyFrom(org.bouncycastle.openpgp.PGPKeyRing keyRing)Return the primaryPGPPublicKeyfrom the provided key ring.static org.bouncycastle.openpgp.PGPSecretKeyrequirePrimarySecretKeyFrom(org.bouncycastle.openpgp.PGPSecretKeyRing secretKeys)Return the primaryPGPSecretKeyfrom the providedPGPSecretKeyRing.static org.bouncycastle.openpgp.PGPPublicKeyrequirePublicKeyFrom(org.bouncycastle.openpgp.PGPKeyRing keyRing, long subKeyId)Require the public key with the given subKeyId from the keyRing.static org.bouncycastle.openpgp.PGPSecretKeyrequireSecretKeyFrom(org.bouncycastle.openpgp.PGPSecretKeyRing keyRing, long subKeyId)Require the secret key with the given secret subKeyId from the secret keyRing.static org.bouncycastle.openpgp.PGPSecretKeysecretKeyPlusSignature(org.bouncycastle.openpgp.PGPSecretKey secretKey, org.bouncycastle.openpgp.PGPSignature signature)Inject the given signature into the public part of the given secret key.static org.bouncycastle.openpgp.PGPSecretKeyRingstripSecretKey(org.bouncycastle.openpgp.PGPSecretKeyRing secretKeys, long secretKeyId)Remove the secret key of the subkey identified by the given secret key id from the key ring.static org.bouncycastle.openpgp.PGPPrivateKeyunlockSecretKey(org.bouncycastle.openpgp.PGPSecretKey secretKey, SecretKeyRingProtector protector)Unlock aPGPSecretKeyand return the resultingPGPPrivateKey.
-
-
-
Method Detail
-
requirePrimarySecretKeyFrom
@Nonnull public static org.bouncycastle.openpgp.PGPSecretKey requirePrimarySecretKeyFrom(@Nonnull org.bouncycastle.openpgp.PGPSecretKeyRing secretKeys)Return the primaryPGPSecretKeyfrom the providedPGPSecretKeyRing. If it has no primary secret key, throw aNoSuchElementException.- Parameters:
secretKeys- secret keys- Returns:
- primary secret key
-
getPrimarySecretKeyFrom
@Nullable public static org.bouncycastle.openpgp.PGPSecretKey getPrimarySecretKeyFrom(@Nonnull org.bouncycastle.openpgp.PGPSecretKeyRing secretKeys)Return the primaryPGPSecretKeyfrom the providedPGPSecretKeyRingor null if it has none.- Parameters:
secretKeys- secret key ring- Returns:
- primary secret key
-
requirePrimaryPublicKeyFrom
@Nonnull public static org.bouncycastle.openpgp.PGPPublicKey requirePrimaryPublicKeyFrom(@Nonnull org.bouncycastle.openpgp.PGPKeyRing keyRing)Return the primaryPGPPublicKeyfrom the provided key ring. Throws aNoSuchElementExceptionif the key ring has no primary public key.- Parameters:
keyRing- key ring- Returns:
- primary public key
-
getPrimaryPublicKeyFrom
@Nullable public static org.bouncycastle.openpgp.PGPPublicKey getPrimaryPublicKeyFrom(@Nonnull org.bouncycastle.openpgp.PGPKeyRing keyRing)Return the primaryPGPPublicKeyfrom the provided key ring or null if it has none.- Parameters:
keyRing- key ring- Returns:
- primary public key
-
getPublicKeyFrom
@Nullable public static org.bouncycastle.openpgp.PGPPublicKey getPublicKeyFrom(@Nonnull org.bouncycastle.openpgp.PGPKeyRing keyRing, long subKeyId)Return the public key with the given subKeyId from the keyRing. If no such subkey exists, return null.- Parameters:
keyRing- key ringsubKeyId- subkey id- Returns:
- subkey or null
-
requirePublicKeyFrom
@Nonnull public static org.bouncycastle.openpgp.PGPPublicKey requirePublicKeyFrom(@Nonnull org.bouncycastle.openpgp.PGPKeyRing keyRing, long subKeyId)Require the public key with the given subKeyId from the keyRing. If no such subkey exists, throw anNoSuchElementException.- Parameters:
keyRing- key ringsubKeyId- subkey id- Returns:
- subkey
-
requireSecretKeyFrom
@Nonnull public static org.bouncycastle.openpgp.PGPSecretKey requireSecretKeyFrom(@Nonnull org.bouncycastle.openpgp.PGPSecretKeyRing keyRing, long subKeyId)Require the secret key with the given secret subKeyId from the secret keyRing. If no such subkey exists, throw anNoSuchElementException.- Parameters:
keyRing- secret key ringsubKeyId- subkey id- Returns:
- secret subkey
-
publicKeyRingFrom
@Nonnull public static org.bouncycastle.openpgp.PGPPublicKeyRing publicKeyRingFrom(@Nonnull org.bouncycastle.openpgp.PGPSecretKeyRing secretKeys)Extract aPGPPublicKeyRingcontaining all public keys from the providedPGPSecretKeyRing.- Parameters:
secretKeys- secret key ring- Returns:
- public key ring
-
publicKeyRingCollectionFrom
@Nonnull public static org.bouncycastle.openpgp.PGPPublicKeyRingCollection publicKeyRingCollectionFrom(@Nonnull org.bouncycastle.openpgp.PGPSecretKeyRingCollection secretKeyRings) throws org.bouncycastle.openpgp.PGPException, java.io.IOExceptionExtractPGPPublicKeyRingsfrom allPGPSecretKeyRingsin the givenPGPSecretKeyRingCollectionand return them as aPGPPublicKeyRingCollection.- Parameters:
secretKeyRings- secret key ring collection- Returns:
- public key ring collection
- Throws:
org.bouncycastle.openpgp.PGPException- TODO: removejava.io.IOException- TODO: remove
-
unlockSecretKey
@Nonnull public static org.bouncycastle.openpgp.PGPPrivateKey unlockSecretKey(@Nonnull org.bouncycastle.openpgp.PGPSecretKey secretKey, @Nonnull SecretKeyRingProtector protector) throws org.bouncycastle.openpgp.PGPExceptionUnlock aPGPSecretKeyand return the resultingPGPPrivateKey.- Parameters:
secretKey- secret keyprotector- protector to unlock the secret key- Returns:
- private key
- Throws:
org.bouncycastle.openpgp.PGPException- if something goes wrong (e.g. wrong passphrase)
-
keyRingsToKeyRingCollection
@Nonnull public static org.bouncycastle.openpgp.PGPPublicKeyRingCollection keyRingsToKeyRingCollection(@Nonnull org.bouncycastle.openpgp.PGPPublicKeyRing... rings) throws java.io.IOException, org.bouncycastle.openpgp.PGPExceptionCreate a newPGPPublicKeyRingCollectionfrom an array ofPGPPublicKeyRings.- Parameters:
rings- array of public key rings- Returns:
- key ring collection
- Throws:
java.io.IOException- in case of an io errororg.bouncycastle.openpgp.PGPException- in case of a broken key
-
keyRingsToKeyRingCollection
@Nonnull public static org.bouncycastle.openpgp.PGPSecretKeyRingCollection keyRingsToKeyRingCollection(@Nonnull org.bouncycastle.openpgp.PGPSecretKeyRing... rings) throws java.io.IOException, org.bouncycastle.openpgp.PGPExceptionCreate a newPGPSecretKeyRingCollectionfrom an array ofPGPSecretKeyRings.- Parameters:
rings- array of secret key rings- Returns:
- secret key ring collection
- Throws:
java.io.IOException- in case of an io errororg.bouncycastle.openpgp.PGPException- in case of a broken key
-
keyRingContainsKeyWithId
public static boolean keyRingContainsKeyWithId(@Nonnull org.bouncycastle.openpgp.PGPPublicKeyRing ring, long keyId)Return true, if the givenPGPPublicKeyRingcontains aPGPPublicKeyfor the given key id.- Parameters:
ring- public key ringkeyId- id of the key in question- Returns:
- true if ring contains said key, false otherwise
-
injectCertification
@Nonnull public static <T extends org.bouncycastle.openpgp.PGPKeyRing> T injectCertification(@Nonnull T keyRing, @Nonnull org.bouncycastle.openpgp.PGPPublicKey certifiedKey, @Nonnull org.bouncycastle.openpgp.PGPSignature certification)Inject a key certification into the given key ring.- Type Parameters:
T- eitherPGPPublicKeyRingorPGPSecretKeyRing- Parameters:
keyRing- key ringcertifiedKey- signed public keycertification- key signature- Returns:
- key ring with injected signature
- Throws:
java.util.NoSuchElementException- in case that the signed key is not part of the key ring
-
injectCertification
@Nonnull public static <T extends org.bouncycastle.openpgp.PGPKeyRing> T injectCertification(@Nonnull T keyRing, @Nonnull java.lang.String userId, @Nonnull org.bouncycastle.openpgp.PGPSignature certification)Inject a user-id certification into the given key ring.- Type Parameters:
T- eitherPGPPublicKeyRingorPGPSecretKeyRing- Parameters:
keyRing- key ringuserId- signed user-idcertification- signature- Returns:
- key ring with injected certification
-
injectCertification
@Nonnull public static <T extends org.bouncycastle.openpgp.PGPKeyRing> T injectCertification(@Nonnull T keyRing, @Nonnull org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector userAttributes, @Nonnull org.bouncycastle.openpgp.PGPSignature certification)Inject a user-attribute vector certification into the given key ring.- Type Parameters:
T- eitherPGPPublicKeyRingorPGPSecretKeyRing- Parameters:
keyRing- key ringuserAttributes- certified user attributescertification- certification signature- Returns:
- key ring with injected user-attribute certification
-
keysPlusPublicKey
@Nonnull public static <T extends org.bouncycastle.openpgp.PGPKeyRing> T keysPlusPublicKey(@Nonnull T keyRing, @Nonnull org.bouncycastle.openpgp.PGPPublicKey publicKey)Inject aPGPPublicKeyinto the given key ring.- Type Parameters:
T- eitherPGPPublicKeyRingorPGPSecretKeyRing- Parameters:
keyRing- key ringpublicKey- public key- Returns:
- key ring with injected public key
-
keysPlusSecretKey
@Nonnull public static org.bouncycastle.openpgp.PGPSecretKeyRing keysPlusSecretKey(@Nonnull org.bouncycastle.openpgp.PGPSecretKeyRing secretKeys, @Nonnull org.bouncycastle.openpgp.PGPSecretKey secretKey)Inject aPGPSecretKeyinto aPGPSecretKeyRing.- Parameters:
secretKeys- secret key ringsecretKey- secret key- Returns:
- secret key ring with injected secret key
-
secretKeyPlusSignature
@Nonnull public static org.bouncycastle.openpgp.PGPSecretKey secretKeyPlusSignature(@Nonnull org.bouncycastle.openpgp.PGPSecretKey secretKey, @Nonnull org.bouncycastle.openpgp.PGPSignature signature)Inject the given signature into the public part of the given secret key.- Parameters:
secretKey- secret keysignature- signature- Returns:
- secret key with the signature injected in its public key
-
removeSecretKey
@Nonnull @Deprecated public static org.bouncycastle.openpgp.PGPSecretKeyRing removeSecretKey(@Nonnull org.bouncycastle.openpgp.PGPSecretKeyRing secretKeys, long secretKeyId) throws java.io.IOException, org.bouncycastle.openpgp.PGPExceptionDeprecated.usestripSecretKey(PGPSecretKeyRing, long)instead. TODO: Remove in 1.2.XRemove the secret key of the subkey identified by the given secret key id from the key ring. The public part stays attached to the key ring, so that it can still be used for encryption / verification of signatures. This method is intended to be used to remove secret primary keys from live keys when those are kept in offline storage.- Parameters:
secretKeys- secret key ringsecretKeyId- id of the secret key to remove- Returns:
- secret key ring with removed secret key
- Throws:
java.io.IOException- in case of an error during serialization / deserialization of the keyorg.bouncycastle.openpgp.PGPException- in case of a broken key
-
stripSecretKey
@Nonnull public static org.bouncycastle.openpgp.PGPSecretKeyRing stripSecretKey(@Nonnull org.bouncycastle.openpgp.PGPSecretKeyRing secretKeys, long secretKeyId) throws java.io.IOException, org.bouncycastle.openpgp.PGPExceptionRemove the secret key of the subkey identified by the given secret key id from the key ring. The public part stays attached to the key ring, so that it can still be used for encryption / verification of signatures. This method is intended to be used to remove secret primary keys from live keys when those are kept in offline storage.- Parameters:
secretKeys- secret key ringsecretKeyId- id of the secret key to remove- Returns:
- secret key ring with removed secret key
- Throws:
java.io.IOException- in case of an error during serialization / deserialization of the keyorg.bouncycastle.openpgp.PGPException- in case of a broken key
-
-