From e3c586e182b57d81b05205f567b0bbcdd21a7804 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 18 Feb 2025 14:08:34 +0100 Subject: [PATCH] Port Sign and UnlockSecretKeys examples --- .../java/org/pgpainless/example/Sign.java | 10 ++++---- .../pgpainless/example/UnlockSecretKeys.java | 23 ++++++++++--------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/pgpainless-core/src/test/java/org/pgpainless/example/Sign.java b/pgpainless-core/src/test/java/org/pgpainless/example/Sign.java index 0ae2ab93..cef4850a 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/example/Sign.java +++ b/pgpainless-core/src/test/java/org/pgpainless/example/Sign.java @@ -14,9 +14,9 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import org.bouncycastle.openpgp.PGPException; -import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.api.OpenPGPCertificate; +import org.bouncycastle.openpgp.api.OpenPGPKey; import org.bouncycastle.util.io.Streams; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -32,13 +32,13 @@ import org.pgpainless.util.ArmorUtils; public class Sign { - private static PGPSecretKeyRing secretKey; + private static OpenPGPKey secretKey; private static SecretKeyRingProtector protector; @BeforeAll public static void prepare() { - secretKey = PGPainless.generateKeyRing().modernKeyRing("Emilia Example ") - .getPGPSecretKeyRing(); + secretKey = PGPainless.generateKeyRing() + .modernKeyRing("Emilia Example "); protector = SecretKeyRingProtector.unprotectedKeys(); // no password } @@ -94,7 +94,7 @@ public class Sign { EncryptionResult result = signingStream.getResult(); OpenPGPCertificate.OpenPGPComponentKey signingKey = PGPainless.inspectKeyRing(secretKey).getSigningSubkeys().get(0); - PGPSignature signature = result.getDetachedSignatures().get(new SubkeyIdentifier(secretKey, signingKey.getKeyIdentifier())).iterator().next(); + PGPSignature signature = result.getDetachedSignatures().get(new SubkeyIdentifier(signingKey)).iterator().next(); String detachedSignature = ArmorUtils.toAsciiArmoredString(signature.getEncoded()); assertTrue(detachedSignature.startsWith("-----BEGIN PGP SIGNATURE-----")); diff --git a/pgpainless-core/src/test/java/org/pgpainless/example/UnlockSecretKeys.java b/pgpainless-core/src/test/java/org/pgpainless/example/UnlockSecretKeys.java index 92387978..cb2908dd 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/example/UnlockSecretKeys.java +++ b/pgpainless-core/src/test/java/org/pgpainless/example/UnlockSecretKeys.java @@ -6,9 +6,10 @@ package org.pgpainless.example; import java.io.IOException; +import org.bouncycastle.bcpg.KeyIdentifier; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPSecretKey; -import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.bouncycastle.openpgp.api.OpenPGPKey; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.key.OpenPgpV4Fingerprint; @@ -22,11 +23,11 @@ import org.pgpainless.util.Passphrase; * {@link PGPSecretKey PGPSecretKeys} are often password protected to prevent unauthorized access. * To perform certain actions with secret keys, such as creating signatures or decrypting encrypted messages, * the secret key needs to be unlocked to access the underlying {@link org.bouncycastle.openpgp.PGPPrivateKey}. - * + *

* Providing the required {@link org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor}/{@link org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor} * is a task that needs to be performed by the {@link SecretKeyRingProtector}. * There are different implementations available that implement this interface. - * + *

* Below are some examples of how to use these implementations in different scenarios. */ public class UnlockSecretKeys { @@ -36,7 +37,7 @@ public class UnlockSecretKeys { */ @Test public void unlockUnprotectedKeys() throws PGPException, IOException { - PGPSecretKeyRing unprotectedKey = TestKeys.getJulietSecretKeyRing(); + OpenPGPKey unprotectedKey = PGPainless.getInstance().toKey(TestKeys.getJulietSecretKeyRing()); // This protector will only unlock unprotected keys SecretKeyRingProtector protector = SecretKeyRingProtector.unprotectedKeys(); @@ -49,7 +50,7 @@ public class UnlockSecretKeys { */ @Test public void unlockWholeKeyWithSamePassphrase() throws PGPException, IOException { - PGPSecretKeyRing secretKey = TestKeys.getCryptieSecretKeyRing(); + OpenPGPKey secretKey = PGPainless.getInstance().toKey(TestKeys.getCryptieSecretKeyRing()); Passphrase passphrase = TestKeys.CRYPTIE_PASSPHRASE; // Unlock all subkeys in the secret key with the same passphrase @@ -91,14 +92,14 @@ public class UnlockSecretKeys { "UPPI6jsYqxEHzRGex8t971atnDAjvDiS31YN\n" + "=fTmB\n" + "-----END PGP PRIVATE KEY BLOCK-----"; - PGPSecretKeyRing secretKey = PGPainless.readKeyRing().secretKeyRing(pgpPrivateKeyBlock); + OpenPGPKey secretKey = PGPainless.getInstance().readKey().parseKey(pgpPrivateKeyBlock); CachingSecretKeyRingProtector protector = SecretKeyRingProtector.defaultSecretKeyRingProtector(null); // Add passphrases for subkeys via public key - protector.addPassphrase(secretKey.getPublicKey(), + protector.addPassphrase(secretKey.getPrimaryKey().getKeyIdentifier(), Passphrase.fromPassword("pr1maryK3y")); // or via subkey-id - protector.addPassphrase(3907509425258753406L, + protector.addPassphrase(new KeyIdentifier(3907509425258753406L), Passphrase.fromPassword("f1rs7subk3y")); // or via fingerprint protector.addPassphrase(new OpenPgpV4Fingerprint("DD8E1195E4B1720E7FB10EF7F60402708E75D941"), @@ -107,10 +108,10 @@ public class UnlockSecretKeys { assertProtectorUnlocksAllSecretKeys(secretKey, protector); } - private void assertProtectorUnlocksAllSecretKeys(PGPSecretKeyRing secretKey, SecretKeyRingProtector protector) + private void assertProtectorUnlocksAllSecretKeys(OpenPGPKey key, SecretKeyRingProtector protector) throws PGPException { - for (PGPSecretKey key : secretKey) { - UnlockSecretKey.unlockSecretKey(key, protector); + for (OpenPGPKey.OpenPGPSecretKey componentKey : key.getSecretKeys().values()) { + UnlockSecretKey.unlockSecretKey(componentKey, protector); } } }