mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-23 04:42:06 +01:00
Add support for deleting user-ids (untested)
This commit is contained in:
parent
92e2828885
commit
6159428c9a
2 changed files with 42 additions and 1 deletions
|
@ -147,7 +147,42 @@ public class KeyRingEditor implements KeyRingEditorInterface {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public KeyRingEditorInterface deleteUserId(String userId, SecretKeyRingProtector protector) {
|
public KeyRingEditorInterface deleteUserId(String userId, SecretKeyRingProtector protector) {
|
||||||
throw new NotYetImplementedException();
|
PGPPublicKey publicKey = secretKeyRing.getPublicKey();
|
||||||
|
return deleteUserId(publicKey.getKeyID(), userId, protector);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public KeyRingEditorInterface deleteUserId(long keyId, String userId, SecretKeyRingProtector secretKeyRingProtector) {
|
||||||
|
List<PGPPublicKey> publicKeys = new ArrayList<>();
|
||||||
|
Iterator<PGPPublicKey> publicKeyIterator = secretKeyRing.getPublicKeys();
|
||||||
|
boolean foundKey = false;
|
||||||
|
while (publicKeyIterator.hasNext()) {
|
||||||
|
PGPPublicKey publicKey = publicKeyIterator.next();
|
||||||
|
if (publicKey.getKeyID() == keyId) {
|
||||||
|
foundKey = true;
|
||||||
|
if (!hasUserId(userId, publicKey)) {
|
||||||
|
throw new NoSuchElementException("Key " + Long.toHexString(keyId) + " does not have a user-id attribute of value '" + userId + "'");
|
||||||
|
}
|
||||||
|
publicKey = PGPPublicKey.removeCertification(publicKey, userId);
|
||||||
|
}
|
||||||
|
publicKeys.add(publicKey);
|
||||||
|
}
|
||||||
|
if (!foundKey) {
|
||||||
|
throw new NoSuchElementException("Cannot find public key with id " + Long.toHexString(keyId));
|
||||||
|
}
|
||||||
|
PGPPublicKeyRing publicKeyRing = new PGPPublicKeyRing(publicKeys);
|
||||||
|
secretKeyRing = PGPSecretKeyRing.replacePublicKeys(secretKeyRing, publicKeyRing);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean hasUserId(String userId, PGPPublicKey publicKey) {
|
||||||
|
boolean hasUserId = false;
|
||||||
|
Iterator<String> userIdIterator = publicKey.getUserIDs();
|
||||||
|
while (userIdIterator.hasNext()) {
|
||||||
|
hasUserId = userId.equals(userIdIterator.next());
|
||||||
|
if (hasUserId) break;
|
||||||
|
}
|
||||||
|
return hasUserId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -50,6 +50,12 @@ public interface KeyRingEditorInterface {
|
||||||
*/
|
*/
|
||||||
KeyRingEditorInterface deleteUserId(String userId, SecretKeyRingProtector secretKeyRingProtector);
|
KeyRingEditorInterface deleteUserId(String userId, SecretKeyRingProtector secretKeyRingProtector);
|
||||||
|
|
||||||
|
default KeyRingEditorInterface deleteUserId(OpenPgpV4Fingerprint fingerprint, String userId, SecretKeyRingProtector secretKeyRingProtector) {
|
||||||
|
return deleteUserId(fingerprint.getKeyId(), userId, secretKeyRingProtector);
|
||||||
|
}
|
||||||
|
|
||||||
|
KeyRingEditorInterface deleteUserId(long keyId, String userId, SecretKeyRingProtector secretKeyRingProtector);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a subkey to the key ring.
|
* Add a subkey to the key ring.
|
||||||
* The subkey will be generated from the provided {@link KeySpec}.
|
* The subkey will be generated from the provided {@link KeySpec}.
|
||||||
|
|
Loading…
Reference in a new issue