diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/util/KeyRingUtils.java b/pgpainless-core/src/main/java/org/pgpainless/key/util/KeyRingUtils.java index a85ea446..4d5dc834 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/util/KeyRingUtils.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/util/KeyRingUtils.java @@ -154,4 +154,50 @@ public final class KeyRingUtils { long keyId) { return ring.getPublicKey(keyId) != null; } + + /** + * Delete the given user-id and its certification signatures from the given key. + * + * @deprecated Deleting user-ids is highly discouraged, since it might lead to all sorts of problems + * (e.g. lost key properties). + * Instead, user-ids should only be revoked. + * + * @param secretKeys secret keys + * @param userId user-id + * @return modified secret keys + */ + @Deprecated + public PGPSecretKeyRing deleteUserIdFromSecretKeyRing(PGPSecretKeyRing secretKeys, String userId) { + PGPSecretKey secretKey = secretKeys.getSecretKey(); // user-ids are located on primary key only + PGPPublicKey publicKey = secretKey.getPublicKey(); // user-ids are placed on the public key part + publicKey = PGPPublicKey.removeCertification(publicKey, userId); + if (publicKey == null) { + throw new NoSuchElementException("User-ID " + userId + " not found on the key."); + } + secretKey = PGPSecretKey.replacePublicKey(secretKey, publicKey); + secretKeys = PGPSecretKeyRing.insertSecretKey(secretKeys, secretKey); + return secretKeys; + } + + /** + * Delete the given user-id and its certification signatures from the given certificate. + * + * @deprecated Deleting user-ids is highly discouraged, since it might lead to all sorts of problems + * (e.g. lost key properties). + * Instead, user-ids should only be revoked. + * + * @param publicKeys certificate + * @param userId user-id + * @return modified secret keys + */ + @Deprecated + public PGPPublicKeyRing deleteUserIdFromPublicKeyRing(PGPPublicKeyRing publicKeys, String userId) { + PGPPublicKey publicKey = publicKeys.getPublicKey(); // user-ids are located on primary key only + publicKey = PGPPublicKey.removeCertification(publicKey, userId); + if (publicKey == null) { + throw new NoSuchElementException("User-ID " + userId + " not found on the key."); + } + publicKeys = PGPPublicKeyRing.insertPublicKey(publicKeys, publicKey); + return publicKeys; + } }