mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-09 18:29:39 +02:00
Port Sign and UnlockSecretKeys examples
This commit is contained in:
parent
bac71bb137
commit
c9b80315ed
2 changed files with 17 additions and 16 deletions
|
@ -14,9 +14,9 @@ import java.io.InputStream;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPException;
|
import org.bouncycastle.openpgp.PGPException;
|
||||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|
||||||
import org.bouncycastle.openpgp.PGPSignature;
|
import org.bouncycastle.openpgp.PGPSignature;
|
||||||
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
|
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
|
||||||
|
import org.bouncycastle.openpgp.api.OpenPGPKey;
|
||||||
import org.bouncycastle.util.io.Streams;
|
import org.bouncycastle.util.io.Streams;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
@ -32,13 +32,13 @@ import org.pgpainless.util.ArmorUtils;
|
||||||
|
|
||||||
public class Sign {
|
public class Sign {
|
||||||
|
|
||||||
private static PGPSecretKeyRing secretKey;
|
private static OpenPGPKey secretKey;
|
||||||
private static SecretKeyRingProtector protector;
|
private static SecretKeyRingProtector protector;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void prepare() {
|
public static void prepare() {
|
||||||
secretKey = PGPainless.generateKeyRing().modernKeyRing("Emilia Example <emilia@example.org>")
|
secretKey = PGPainless.generateKeyRing()
|
||||||
.getPGPSecretKeyRing();
|
.modernKeyRing("Emilia Example <emilia@example.org>");
|
||||||
protector = SecretKeyRingProtector.unprotectedKeys(); // no password
|
protector = SecretKeyRingProtector.unprotectedKeys(); // no password
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ public class Sign {
|
||||||
EncryptionResult result = signingStream.getResult();
|
EncryptionResult result = signingStream.getResult();
|
||||||
|
|
||||||
OpenPGPCertificate.OpenPGPComponentKey signingKey = PGPainless.inspectKeyRing(secretKey).getSigningSubkeys().get(0);
|
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());
|
String detachedSignature = ArmorUtils.toAsciiArmoredString(signature.getEncoded());
|
||||||
|
|
||||||
assertTrue(detachedSignature.startsWith("-----BEGIN PGP SIGNATURE-----"));
|
assertTrue(detachedSignature.startsWith("-----BEGIN PGP SIGNATURE-----"));
|
||||||
|
|
|
@ -6,9 +6,10 @@ package org.pgpainless.example;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.bouncycastle.bcpg.KeyIdentifier;
|
||||||
import org.bouncycastle.openpgp.PGPException;
|
import org.bouncycastle.openpgp.PGPException;
|
||||||
import org.bouncycastle.openpgp.PGPSecretKey;
|
import org.bouncycastle.openpgp.PGPSecretKey;
|
||||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
import org.bouncycastle.openpgp.api.OpenPGPKey;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.pgpainless.PGPainless;
|
import org.pgpainless.PGPainless;
|
||||||
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
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.
|
* {@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,
|
* 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}.
|
* the secret key needs to be unlocked to access the underlying {@link org.bouncycastle.openpgp.PGPPrivateKey}.
|
||||||
*
|
* <p>
|
||||||
* Providing the required {@link org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor}/{@link org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor}
|
* 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}.
|
* is a task that needs to be performed by the {@link SecretKeyRingProtector}.
|
||||||
* There are different implementations available that implement this interface.
|
* There are different implementations available that implement this interface.
|
||||||
*
|
* <p>
|
||||||
* Below are some examples of how to use these implementations in different scenarios.
|
* Below are some examples of how to use these implementations in different scenarios.
|
||||||
*/
|
*/
|
||||||
public class UnlockSecretKeys {
|
public class UnlockSecretKeys {
|
||||||
|
@ -36,7 +37,7 @@ public class UnlockSecretKeys {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void unlockUnprotectedKeys() throws PGPException, IOException {
|
public void unlockUnprotectedKeys() throws PGPException, IOException {
|
||||||
PGPSecretKeyRing unprotectedKey = TestKeys.getJulietSecretKeyRing();
|
OpenPGPKey unprotectedKey = PGPainless.getInstance().toKey(TestKeys.getJulietSecretKeyRing());
|
||||||
// This protector will only unlock unprotected keys
|
// This protector will only unlock unprotected keys
|
||||||
SecretKeyRingProtector protector = SecretKeyRingProtector.unprotectedKeys();
|
SecretKeyRingProtector protector = SecretKeyRingProtector.unprotectedKeys();
|
||||||
|
|
||||||
|
@ -49,7 +50,7 @@ public class UnlockSecretKeys {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void unlockWholeKeyWithSamePassphrase() throws PGPException, IOException {
|
public void unlockWholeKeyWithSamePassphrase() throws PGPException, IOException {
|
||||||
PGPSecretKeyRing secretKey = TestKeys.getCryptieSecretKeyRing();
|
OpenPGPKey secretKey = PGPainless.getInstance().toKey(TestKeys.getCryptieSecretKeyRing());
|
||||||
Passphrase passphrase = TestKeys.CRYPTIE_PASSPHRASE;
|
Passphrase passphrase = TestKeys.CRYPTIE_PASSPHRASE;
|
||||||
|
|
||||||
// Unlock all subkeys in the secret key with the same passphrase
|
// Unlock all subkeys in the secret key with the same passphrase
|
||||||
|
@ -91,14 +92,14 @@ public class UnlockSecretKeys {
|
||||||
"UPPI6jsYqxEHzRGex8t971atnDAjvDiS31YN\n" +
|
"UPPI6jsYqxEHzRGex8t971atnDAjvDiS31YN\n" +
|
||||||
"=fTmB\n" +
|
"=fTmB\n" +
|
||||||
"-----END PGP PRIVATE KEY BLOCK-----";
|
"-----END PGP PRIVATE KEY BLOCK-----";
|
||||||
PGPSecretKeyRing secretKey = PGPainless.readKeyRing().secretKeyRing(pgpPrivateKeyBlock);
|
OpenPGPKey secretKey = PGPainless.getInstance().readKey().parseKey(pgpPrivateKeyBlock);
|
||||||
|
|
||||||
CachingSecretKeyRingProtector protector = SecretKeyRingProtector.defaultSecretKeyRingProtector(null);
|
CachingSecretKeyRingProtector protector = SecretKeyRingProtector.defaultSecretKeyRingProtector(null);
|
||||||
// Add passphrases for subkeys via public key
|
// Add passphrases for subkeys via public key
|
||||||
protector.addPassphrase(secretKey.getPublicKey(),
|
protector.addPassphrase(secretKey.getPrimaryKey().getKeyIdentifier(),
|
||||||
Passphrase.fromPassword("pr1maryK3y"));
|
Passphrase.fromPassword("pr1maryK3y"));
|
||||||
// or via subkey-id
|
// or via subkey-id
|
||||||
protector.addPassphrase(3907509425258753406L,
|
protector.addPassphrase(new KeyIdentifier(3907509425258753406L),
|
||||||
Passphrase.fromPassword("f1rs7subk3y"));
|
Passphrase.fromPassword("f1rs7subk3y"));
|
||||||
// or via fingerprint
|
// or via fingerprint
|
||||||
protector.addPassphrase(new OpenPgpV4Fingerprint("DD8E1195E4B1720E7FB10EF7F60402708E75D941"),
|
protector.addPassphrase(new OpenPgpV4Fingerprint("DD8E1195E4B1720E7FB10EF7F60402708E75D941"),
|
||||||
|
@ -107,10 +108,10 @@ public class UnlockSecretKeys {
|
||||||
assertProtectorUnlocksAllSecretKeys(secretKey, protector);
|
assertProtectorUnlocksAllSecretKeys(secretKey, protector);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertProtectorUnlocksAllSecretKeys(PGPSecretKeyRing secretKey, SecretKeyRingProtector protector)
|
private void assertProtectorUnlocksAllSecretKeys(OpenPGPKey key, SecretKeyRingProtector protector)
|
||||||
throws PGPException {
|
throws PGPException {
|
||||||
for (PGPSecretKey key : secretKey) {
|
for (OpenPGPKey.OpenPGPSecretKey componentKey : key.getSecretKeys().values()) {
|
||||||
UnlockSecretKey.unlockSecretKey(key, protector);
|
UnlockSecretKey.unlockSecretKey(componentKey, protector);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue