1
0
Fork 0
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:
Paul Schaub 2022-03-15 16:51:56 +01:00
parent f155768539
commit ecfa3823fb

View file

@ -4,6 +4,7 @@
package org.pgpainless.key.util; package org.pgpainless.key.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -25,6 +26,7 @@ import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector; import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.exception.NotYetImplementedException; import org.pgpainless.exception.NotYetImplementedException;
import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.protection.UnlockSecretKey; import org.pgpainless.key.protection.UnlockSecretKey;
@ -290,4 +292,24 @@ public final class KeyRingUtils {
return newSecretKey; 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());
}
} }