mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-23 04:42:06 +01:00
Add utility method to remove secret subkey from key ring
This might be useful for offline primary keys
This commit is contained in:
parent
f155768539
commit
ecfa3823fb
1 changed files with 22 additions and 0 deletions
|
@ -4,6 +4,7 @@
|
|||
|
||||
package org.pgpainless.key.util;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
@ -25,6 +26,7 @@ import org.bouncycastle.openpgp.PGPSignature;
|
|||
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.exception.NotYetImplementedException;
|
||||
import org.pgpainless.implementation.ImplementationFactory;
|
||||
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
||||
import org.pgpainless.key.protection.UnlockSecretKey;
|
||||
|
||||
|
@ -290,4 +292,24 @@ public final class KeyRingUtils {
|
|||
return newSecretKey;
|
||||
}
|
||||
|
||||
public static PGPSecretKeyRing removeSecretKey(PGPSecretKeyRing secretKeys, long secretKeyId)
|
||||
throws IOException, PGPException {
|
||||
if (secretKeys.getSecretKey(secretKeyId) == null) {
|
||||
throw new NoSuchElementException("PGPSecretKeyRing does not contain secret key " + Long.toHexString(secretKeyId));
|
||||
}
|
||||
|
||||
ByteArrayOutputStream encoded = new ByteArrayOutputStream();
|
||||
for (PGPSecretKey secretKey : secretKeys) {
|
||||
if (secretKey.getKeyID() == secretKeyId) {
|
||||
secretKey.getPublicKey().encode(encoded);
|
||||
} else {
|
||||
secretKey.encode(encoded);
|
||||
}
|
||||
}
|
||||
for (Iterator<PGPPublicKey> it = secretKeys.getExtraPublicKeys(); it.hasNext(); ) {
|
||||
PGPPublicKey extra = it.next();
|
||||
extra.encode(encoded);
|
||||
}
|
||||
return new PGPSecretKeyRing(encoded.toByteArray(), ImplementationFactory.getInstance().getKeyFingerprintCalculator());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue