From c234e38ae22e84da82c09bebe0aea3c3dfda0f4b Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Thu, 1 Jul 2021 17:37:30 +0200 Subject: [PATCH] Add key extraction and ascii armor examples --- .../org/pgpainless/example/ModifyKeys.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pgpainless-core/src/test/java/org/pgpainless/example/ModifyKeys.java b/pgpainless-core/src/test/java/org/pgpainless/example/ModifyKeys.java index ec087482..5daf5df3 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/example/ModifyKeys.java +++ b/pgpainless-core/src/test/java/org/pgpainless/example/ModifyKeys.java @@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.IOException; import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; import java.util.Date; @@ -28,6 +29,7 @@ import java.util.List; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPPublicKey; +import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -69,6 +71,35 @@ public class ModifyKeys { signingSubkeyId = info.getSigningSubkeys().get(0).getKeyID(); } + /** + * This example demonstrates how to extract a certificate (public key) from a secret key. + */ + @Test + public void extractPublicKey() { + // the certificate consists of only the public keys + PGPPublicKeyRing certificate = PGPainless.extractCertificate(secretKey); + + + KeyRingInfo info = PGPainless.inspectKeyRing(certificate); + assertFalse(info.isSecretKey()); + } + + /** + * This example demonstrates how to export a secret key or certificate to an ASCII armored string. + */ + @Test + public void toAsciiArmoredString() throws IOException { + PGPPublicKeyRing certificate = PGPainless.extractCertificate(secretKey); + + + String asciiArmoredSecretKey = PGPainless.asciiArmor(secretKey); + String asciiArmoredCertificate = PGPainless.asciiArmor(certificate); + + + assertTrue(asciiArmoredSecretKey.startsWith("-----BEGIN PGP PRIVATE KEY BLOCK-----")); + assertTrue(asciiArmoredCertificate.startsWith("-----BEGIN PGP PUBLIC KEY BLOCK-----")); + } + /** * This example demonstrates how to change the passphrase of a secret key and all its subkeys. *