1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-04-22 14:34:48 +02:00

Progress porting the example tests

This commit is contained in:
Paul Schaub 2025-02-17 14:36:43 +01:00
parent 760c3a9730
commit 5266fa53c8
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 38 additions and 30 deletions
pgpainless-core/src
main/kotlin/org/pgpainless
test/java/org/pgpainless/example

View file

@ -132,6 +132,8 @@ class PGPainless(
if (key is PGPSecretKeyRing) ArmorUtils.toAsciiArmoredString(key)
else ArmorUtils.toAsciiArmoredString(key as PGPPublicKeyRing)
@JvmStatic fun asciiArmor(cert: OpenPGPCertificate) = asciiArmor(cert.pgpKeyRing)
/**
* Wrap a key of certificate in ASCII armor and write the result into the given
* [OutputStream].
@ -204,6 +206,8 @@ class PGPainless(
fun inspectKeyRing(key: PGPKeyRing, referenceTime: Date = Date()) =
KeyRingInfo(key, referenceTime)
@JvmStatic
@JvmOverloads
fun inspectKeyRing(key: OpenPGPCertificate, referenceTime: Date = Date()) =
KeyRingInfo(key, getPolicy(), referenceTime)

View file

@ -9,6 +9,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.key.info.KeyRingInfo;
@ -21,11 +23,11 @@ public class ConvertKeys {
@Test
public void secretKeyToCertificate() {
String userId = "alice@wonderland.lit";
PGPSecretKeyRing secretKey = PGPainless.generateKeyRing()
.modernKeyRing(userId)
.getPGPSecretKeyRing();
OpenPGPKey secretKey = PGPainless.generateKeyRing()
.modernKeyRing(userId);
// Extract certificate (public key) from secret key
PGPPublicKeyRing certificate = PGPainless.extractCertificate(secretKey);
OpenPGPCertificate certificate = secretKey.toCertificate();
KeyRingInfo secretKeyInfo = PGPainless.inspectKeyRing(secretKey);

View file

@ -15,6 +15,9 @@ import java.nio.charset.StandardCharsets;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.openpgp.api.OpenPGPKeyReader;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
@ -133,12 +136,13 @@ public class Encrypt {
@Test
public void encryptAndSignMessage() throws PGPException, IOException {
// Prepare keys
PGPSecretKeyRing keyAlice = PGPainless.readKeyRing().secretKeyRing(ALICE_KEY);
PGPPublicKeyRing certificateAlice = PGPainless.readKeyRing().publicKeyRing(ALICE_CERT);
OpenPGPKeyReader reader = PGPainless.getInstance().readKey();
OpenPGPKey keyAlice = reader.parseKey(ALICE_KEY);
OpenPGPCertificate certificateAlice = reader.parseCertificate(ALICE_CERT);
SecretKeyRingProtector protectorAlice = SecretKeyRingProtector.unprotectedKeys();
PGPSecretKeyRing keyBob = PGPainless.readKeyRing().secretKeyRing(BOB_KEY);
PGPPublicKeyRing certificateBob = PGPainless.readKeyRing().publicKeyRing(BOB_CERT);
OpenPGPKey keyBob = reader.parseKey(BOB_KEY);
OpenPGPCertificate certificateBob = reader.parseCertificate(BOB_CERT);
SecretKeyRingProtector protectorBob = SecretKeyRingProtector.unprotectedKeys();
// plaintext message to encrypt
@ -227,10 +231,11 @@ public class Encrypt {
@Test
public void encryptWithCommentHeader() throws PGPException, IOException {
// Prepare keys
PGPPublicKeyRing certificateAlice = PGPainless.readKeyRing().publicKeyRing(ALICE_CERT);
OpenPGPKeyReader reader = PGPainless.getInstance().readKey();
OpenPGPCertificate certificateAlice = reader.parseCertificate(ALICE_CERT);
PGPSecretKeyRing keyBob = PGPainless.readKeyRing().secretKeyRing(BOB_KEY);
PGPPublicKeyRing certificateBob = PGPainless.readKeyRing().publicKeyRing(BOB_CERT);
OpenPGPKey keyBob = reader.parseKey(BOB_KEY);
OpenPGPCertificate certificateBob = reader.parseCertificate(BOB_CERT);
SecretKeyRingProtector protectorBob = SecretKeyRingProtector.unprotectedKeys();
// plaintext message to encrypt

View file

@ -9,8 +9,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Date;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.CompressionAlgorithm;
@ -58,16 +58,16 @@ public class GenerateKeys {
// Set a password to protect the secret key
String password = "ra1nb0w";
// Generate the OpenPGP key
PGPSecretKeyRing secretKey = PGPainless.generateKeyRing()
.modernKeyRing(userId, password)
.getPGPSecretKeyRing();
OpenPGPKey secretKey = PGPainless.generateKeyRing()
.modernKeyRing(userId, password);
// Extract public key
PGPPublicKeyRing publicKey = PGPainless.extractCertificate(secretKey);
OpenPGPCertificate publicKey = secretKey.toCertificate();
// Encode the public key to an ASCII armored string ready for sharing
String asciiArmoredPublicKey = PGPainless.asciiArmor(publicKey);
assertTrue(asciiArmoredPublicKey.startsWith("-----BEGIN PGP PUBLIC KEY BLOCK-----"));
KeyRingInfo keyInfo = new KeyRingInfo(secretKey);
KeyRingInfo keyInfo = PGPainless.inspectKeyRing(secretKey);
assertEquals(3, keyInfo.getSecretKeys().size());
assertEquals(userId, keyInfo.getPrimaryUserId());
assertEquals(PublicKeyAlgorithm.EDDSA_LEGACY.getAlgorithmId(),
@ -91,11 +91,10 @@ public class GenerateKeys {
// Set a password to protect the secret key
String password = "b1angl3s";
// Generate the OpenPGP key
PGPSecretKeyRing secretKey = PGPainless.generateKeyRing()
.simpleRsaKeyRing(userId, RsaLength._4096, password)
.getPGPSecretKeyRing();
OpenPGPKey secretKey = PGPainless.generateKeyRing()
.simpleRsaKeyRing(userId, RsaLength._4096, password);
KeyRingInfo keyInfo = new KeyRingInfo(secretKey);
KeyRingInfo keyInfo = PGPainless.inspectKeyRing(secretKey);
assertEquals(1, keyInfo.getSecretKeys().size());
assertEquals(userId, keyInfo.getPrimaryUserId());
assertEquals(PublicKeyAlgorithm.RSA_GENERAL.getAlgorithmId(), keyInfo.getAlgorithm().getAlgorithmId());
@ -115,12 +114,11 @@ public class GenerateKeys {
// Set a password to protect the secret key
String password = "tr4ns";
// Generate the OpenPGP key
PGPSecretKeyRing secretKey = PGPainless.generateKeyRing()
.simpleEcKeyRing(userId, password)
.getPGPSecretKeyRing();
OpenPGPKey secretKey = PGPainless.generateKeyRing()
.simpleEcKeyRing(userId, password);
KeyRingInfo keyInfo = new KeyRingInfo(secretKey);
KeyRingInfo keyInfo = PGPainless.inspectKeyRing(secretKey);
assertEquals(2, keyInfo.getSecretKeys().size());
assertEquals(userId, keyInfo.getPrimaryUserId());
}
@ -174,7 +172,7 @@ public class GenerateKeys {
// It is recommended to use the Passphrase class, as it can be used to safely invalidate passwords from memory
Passphrase passphrase = Passphrase.fromPassword("1nters3x");
PGPSecretKeyRing secretKey = PGPainless.buildKeyRing()
OpenPGPKey secretKey = PGPainless.buildKeyRing()
.setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519),
// The primary key MUST carry the CERTIFY_OTHER flag, but CAN carry additional flags
KeyFlag.CERTIFY_OTHER))
@ -204,11 +202,10 @@ public class GenerateKeys {
.addUserId(additionalUserId)
// Set passphrase. Alternatively use .withoutPassphrase() to leave key unprotected.
.setPassphrase(passphrase)
.build()
.getPGPSecretKeyRing();
.build();
KeyRingInfo keyInfo = new KeyRingInfo(secretKey);
KeyRingInfo keyInfo = PGPainless.inspectKeyRing(secretKey);
assertEquals(3, keyInfo.getSecretKeys().size());
assertEquals("Morgan Carpenter (Pride!) <mcarpenter@pgpainless.org>", keyInfo.getPrimaryUserId());
assertTrue(keyInfo.isUserIdValid(additionalUserId));