mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-10 02:39:39 +02:00
Port DecryptOrVerify example
This commit is contained in:
parent
cb7c27751a
commit
a973e84d83
1 changed files with 6 additions and 15 deletions
|
@ -66,7 +66,7 @@ public class DecryptOrVerify {
|
||||||
/**
|
/**
|
||||||
* Protector to unlock the secret key.
|
* Protector to unlock the secret key.
|
||||||
* Since the key is not protected, it is enough to use an unprotectedKeys implementation.
|
* 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}.
|
* For more info on how to use the {@link SecretKeyRingProtector}, see {@link UnlockSecretKeys}.
|
||||||
*/
|
*/
|
||||||
private static final SecretKeyRingProtector keyProtector = SecretKeyRingProtector.unprotectedKeys();
|
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.
|
* This example demonstrates how to decrypt an encrypted message using a secret key.
|
||||||
*
|
|
||||||
* @throws PGPException
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void decryptMessage() throws PGPException, IOException {
|
public void decryptMessage() throws PGPException, IOException {
|
||||||
|
@ -182,9 +179,6 @@ public class DecryptOrVerify {
|
||||||
/**
|
/**
|
||||||
* In this example, an encrypted and signed message is processed.
|
* 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.
|
* The message gets decrypted using the secret key and the signatures are verified using the certificate.
|
||||||
*
|
|
||||||
* @throws PGPException
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void decryptMessageAndVerifySignatures() throws PGPException, IOException {
|
public void decryptMessageAndVerifySignatures() throws PGPException, IOException {
|
||||||
|
@ -215,8 +209,6 @@ public class DecryptOrVerify {
|
||||||
/**
|
/**
|
||||||
* In this example, signed messages are verified.
|
* In this example, signed messages are verified.
|
||||||
* The example shows that verification of inband signed, and cleartext signed messages works the same.
|
* The example shows that verification of inband signed, and cleartext signed messages works the same.
|
||||||
* @throws PGPException
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void verifySignatures() throws PGPException, IOException {
|
public void verifySignatures() throws PGPException, IOException {
|
||||||
|
@ -245,11 +237,10 @@ public class DecryptOrVerify {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This example shows how to create - and verify - cleartext signed messages.
|
* This example shows how to create - and verify - cleartext signed messages.
|
||||||
* @throws PGPException
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void createAndVerifyCleartextSignedMessage() throws PGPException, IOException {
|
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
|
// 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!"}) {
|
for (String msg : new String[] {"Hello World!", "- Hello - World -", "Hello, World!\n", "Hello\nWorld!"}) {
|
||||||
// we need to read the plaintext message from somewhere
|
// 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
|
// and write the signed message to an output stream
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
|
||||||
SigningOptions signingOptions = SigningOptions.get();
|
SigningOptions signingOptions = SigningOptions.get(api);
|
||||||
// for cleartext signed messages, we need to add a detached signature...
|
// 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)
|
ProducerOptions producerOptions = ProducerOptions.sign(signingOptions)
|
||||||
.setCleartextSigned(); // and declare that the message will be cleartext signed
|
.setCleartextSigned(); // and declare that the message will be cleartext signed
|
||||||
|
|
||||||
// Create the signing stream
|
// Create the signing stream
|
||||||
EncryptionStream signingStream = PGPainless.encryptAndOrSign()
|
EncryptionStream signingStream = api.generateMessage()
|
||||||
.onOutputStream(out) // on the output stream
|
.onOutputStream(out) // on the output stream
|
||||||
.withOptions(producerOptions); // with the options
|
.withOptions(producerOptions); // with the options
|
||||||
|
|
||||||
|
@ -281,7 +272,7 @@ public class DecryptOrVerify {
|
||||||
// and pass it to the decryption stream
|
// and pass it to the decryption stream
|
||||||
DecryptionStream verificationStream = PGPainless.decryptAndOrVerify()
|
DecryptionStream verificationStream = PGPainless.decryptAndOrVerify()
|
||||||
.onInputStream(signedIn)
|
.onInputStream(signedIn)
|
||||||
.withOptions(ConsumerOptions.get().addVerificationCert(certificate));
|
.withOptions(ConsumerOptions.get(api).addVerificationCert(certificate));
|
||||||
|
|
||||||
// plain will receive the plaintext message
|
// plain will receive the plaintext message
|
||||||
ByteArrayOutputStream plain = new ByteArrayOutputStream();
|
ByteArrayOutputStream plain = new ByteArrayOutputStream();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue