1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 02:09:38 +02:00

Port DecryptOrVerify example

This commit is contained in:
Paul Schaub 2025-04-07 16:25:10 +02:00
parent cb7c27751a
commit a973e84d83
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -66,7 +66,7 @@ public class DecryptOrVerify {
/**
* Protector to unlock the secret key.
* Since the key is not protected, it is enough to use an unprotectedKeys implementation.
*
* <p>
* For more info on how to use the {@link SecretKeyRingProtector}, see {@link UnlockSecretKeys}.
*/
private static final SecretKeyRingProtector keyProtector = SecretKeyRingProtector.unprotectedKeys();
@ -149,9 +149,6 @@ public class DecryptOrVerify {
/**
* This example demonstrates how to decrypt an encrypted message using a secret key.
*
* @throws PGPException
* @throws IOException
*/
@Test
public void decryptMessage() throws PGPException, IOException {
@ -182,9 +179,6 @@ public class DecryptOrVerify {
/**
* In this example, an encrypted and signed message is processed.
* The message gets decrypted using the secret key and the signatures are verified using the certificate.
*
* @throws PGPException
* @throws IOException
*/
@Test
public void decryptMessageAndVerifySignatures() throws PGPException, IOException {
@ -215,8 +209,6 @@ public class DecryptOrVerify {
/**
* In this example, signed messages are verified.
* The example shows that verification of inband signed, and cleartext signed messages works the same.
* @throws PGPException
* @throws IOException
*/
@Test
public void verifySignatures() throws PGPException, IOException {
@ -245,11 +237,10 @@ public class DecryptOrVerify {
/**
* This example shows how to create - and verify - cleartext signed messages.
* @throws PGPException
* @throws IOException
*/
@Test
public void createAndVerifyCleartextSignedMessage() throws PGPException, IOException {
PGPainless api = PGPainless.getInstance();
// In this example we sign and verify a number of different messages one after the other
for (String msg : new String[] {"Hello World!", "- Hello - World -", "Hello, World!\n", "Hello\nWorld!"}) {
// we need to read the plaintext message from somewhere
@ -257,14 +248,14 @@ public class DecryptOrVerify {
// and write the signed message to an output stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
SigningOptions signingOptions = SigningOptions.get();
SigningOptions signingOptions = SigningOptions.get(api);
// for cleartext signed messages, we need to add a detached signature...
signingOptions.addDetachedSignature(keyProtector, secretKey.getPGPSecretKeyRing(), DocumentSignatureType.CANONICAL_TEXT_DOCUMENT);
signingOptions.addDetachedSignature(keyProtector, secretKey, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT);
ProducerOptions producerOptions = ProducerOptions.sign(signingOptions)
.setCleartextSigned(); // and declare that the message will be cleartext signed
// Create the signing stream
EncryptionStream signingStream = PGPainless.encryptAndOrSign()
EncryptionStream signingStream = api.generateMessage()
.onOutputStream(out) // on the output stream
.withOptions(producerOptions); // with the options
@ -281,7 +272,7 @@ public class DecryptOrVerify {
// and pass it to the decryption stream
DecryptionStream verificationStream = PGPainless.decryptAndOrVerify()
.onInputStream(signedIn)
.withOptions(ConsumerOptions.get().addVerificationCert(certificate));
.withOptions(ConsumerOptions.get(api).addVerificationCert(certificate));
// plain will receive the plaintext message
ByteArrayOutputStream plain = new ByteArrayOutputStream();