Update examples with new MessageMetadata class

This commit is contained in:
Paul Schaub 2022-11-22 15:52:15 +01:00
parent f005885318
commit 27fd15a012
2 changed files with 30 additions and 33 deletions

View File

@ -24,7 +24,7 @@ import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.DocumentSignatureType; import org.pgpainless.algorithm.DocumentSignatureType;
import org.pgpainless.decryption_verification.ConsumerOptions; import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream; import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.OpenPgpMetadata; import org.pgpainless.decryption_verification.MessageMetadata;
import org.pgpainless.encryption_signing.EncryptionStream; import org.pgpainless.encryption_signing.EncryptionStream;
import org.pgpainless.encryption_signing.ProducerOptions; import org.pgpainless.encryption_signing.ProducerOptions;
import org.pgpainless.encryption_signing.SigningOptions; import org.pgpainless.encryption_signing.SigningOptions;
@ -168,9 +168,9 @@ public class DecryptOrVerify {
decryptionStream.close(); // remember to close the stream! decryptionStream.close(); // remember to close the stream!
// The metadata object contains information about the message // The metadata object contains information about the message
OpenPgpMetadata metadata = decryptionStream.getResult(); MessageMetadata metadata = decryptionStream.getMetadata();
assertTrue(metadata.isEncrypted()); // message was encrypted assertTrue(metadata.isEncrypted()); // message was encrypted
assertFalse(metadata.isVerified()); // We did not do any signature verification assertFalse(metadata.isVerifiedSigned()); // We did not do any signature verification
// The output stream now contains the decrypted message // The output stream now contains the decrypted message
assertEquals(PLAINTEXT, plaintextOut.toString()); assertEquals(PLAINTEXT, plaintextOut.toString());
@ -200,11 +200,10 @@ public class DecryptOrVerify {
decryptionStream.close(); // remember to close the stream to finish signature verification decryptionStream.close(); // remember to close the stream to finish signature verification
// metadata with information on the message, like signatures // metadata with information on the message, like signatures
OpenPgpMetadata metadata = decryptionStream.getResult(); MessageMetadata metadata = decryptionStream.getMetadata();
assertTrue(metadata.isEncrypted()); // messages was in fact encrypted assertTrue(metadata.isEncrypted()); // messages was in fact encrypted
assertTrue(metadata.isSigned()); // message contained some signatures assertTrue(metadata.isVerifiedSigned()); // the signatures were actually correct
assertTrue(metadata.isVerified()); // the signatures were actually correct assertTrue(metadata.isVerifiedSignedBy(certificate)); // the signatures could be verified using the certificate
assertTrue(metadata.containsVerifiedSignatureFrom(certificate)); // the signatures could be verified using the certificate
assertEquals(PLAINTEXT, plaintextOut.toString()); assertEquals(PLAINTEXT, plaintextOut.toString());
} }
@ -232,8 +231,8 @@ public class DecryptOrVerify {
verificationStream.close(); // remember to close the stream to finish sig verification verificationStream.close(); // remember to close the stream to finish sig verification
// Get the metadata object for information about the message // Get the metadata object for information about the message
OpenPgpMetadata metadata = verificationStream.getResult(); MessageMetadata metadata = verificationStream.getMetadata();
assertTrue(metadata.isVerified()); // signatures were verified successfully assertTrue(metadata.isVerifiedSigned()); // signatures were verified successfully
// The output stream we piped to now contains the message // The output stream we piped to now contains the message
assertEquals(PLAINTEXT, out.toString()); assertEquals(PLAINTEXT, out.toString());
} }
@ -286,8 +285,8 @@ public class DecryptOrVerify {
verificationStream.close(); // as always, remember to close the stream verificationStream.close(); // as always, remember to close the stream
// Metadata will confirm that the message was in fact signed // Metadata will confirm that the message was in fact signed
OpenPgpMetadata metadata = verificationStream.getResult(); MessageMetadata metadata = verificationStream.getMetadata();
assertTrue(metadata.isVerified()); assertTrue(metadata.isVerifiedSigned());
// compare the plaintext to what we originally signed // compare the plaintext to what we originally signed
assertArrayEquals(msg.getBytes(StandardCharsets.UTF_8), plain.toByteArray()); assertArrayEquals(msg.getBytes(StandardCharsets.UTF_8), plain.toByteArray());
} }

View File

@ -21,7 +21,7 @@ import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.DocumentSignatureType; import org.pgpainless.algorithm.DocumentSignatureType;
import org.pgpainless.decryption_verification.ConsumerOptions; import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream; import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.OpenPgpMetadata; import org.pgpainless.decryption_verification.MessageMetadata;
import org.pgpainless.encryption_signing.EncryptionOptions; import org.pgpainless.encryption_signing.EncryptionOptions;
import org.pgpainless.encryption_signing.EncryptionStream; import org.pgpainless.encryption_signing.EncryptionStream;
import org.pgpainless.encryption_signing.ProducerOptions; import org.pgpainless.encryption_signing.ProducerOptions;
@ -148,12 +148,12 @@ public class Encrypt {
EncryptionStream encryptor = PGPainless.encryptAndOrSign() EncryptionStream encryptor = PGPainless.encryptAndOrSign()
.onOutputStream(ciphertext) .onOutputStream(ciphertext)
.withOptions(ProducerOptions.signAndEncrypt( .withOptions(ProducerOptions.signAndEncrypt(
// we want to encrypt communication (affects key selection based on key flags) // we want to encrypt communication (affects key selection based on key flags)
EncryptionOptions.encryptCommunications() EncryptionOptions.encryptCommunications()
.addRecipient(certificateBob) .addRecipient(certificateBob)
.addRecipient(certificateAlice), .addRecipient(certificateAlice),
new SigningOptions() new SigningOptions()
.addInlineSignature(protectorAlice, keyAlice, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT) .addInlineSignature(protectorAlice, keyAlice, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT)
).setAsciiArmor(true) ).setAsciiArmor(true)
); );
@ -176,9 +176,9 @@ public class Encrypt {
decryptor.close(); decryptor.close();
// Check the metadata to see how the message was encrypted/signed // Check the metadata to see how the message was encrypted/signed
OpenPgpMetadata metadata = decryptor.getResult(); MessageMetadata metadata = decryptor.getMetadata();
assertTrue(metadata.isEncrypted()); assertTrue(metadata.isEncrypted());
assertTrue(metadata.containsVerifiedSignatureFrom(certificateAlice)); assertTrue(metadata.isVerifiedSignedBy(certificateAlice));
assertEquals(message, plaintext.toString()); assertEquals(message, plaintext.toString());
} }
@ -236,10 +236,10 @@ public class Encrypt {
// plaintext message to encrypt // plaintext message to encrypt
String message = "Hello, World!\n"; String message = "Hello, World!\n";
String[] comments = { String[] comments = {
"This comment was added using options.", "This comment was added using options.",
"And it has three lines.", "And it has three lines.",
" ", " ",
"Empty lines are skipped." "Empty lines are skipped."
}; };
String comment = comments[0] + "\n" + comments[1] + "\n" + comments[2] + "\n" + comments[3]; String comment = comments[0] + "\n" + comments[1] + "\n" + comments[2] + "\n" + comments[3];
ByteArrayOutputStream ciphertext = new ByteArrayOutputStream(); ByteArrayOutputStream ciphertext = new ByteArrayOutputStream();
@ -247,12 +247,12 @@ public class Encrypt {
EncryptionStream encryptor = PGPainless.encryptAndOrSign() EncryptionStream encryptor = PGPainless.encryptAndOrSign()
.onOutputStream(ciphertext) .onOutputStream(ciphertext)
.withOptions(ProducerOptions.encrypt( .withOptions(ProducerOptions.encrypt(
// we want to encrypt communication (affects key selection based on key flags) // we want to encrypt communication (affects key selection based on key flags)
EncryptionOptions.encryptCommunications() EncryptionOptions.encryptCommunications()
.addRecipient(certificateBob) .addRecipient(certificateBob)
.addRecipient(certificateAlice) .addRecipient(certificateAlice)
).setAsciiArmor(true) ).setAsciiArmor(true)
.setComment(comment) .setComment(comment)
); );
// Pipe data trough and CLOSE the stream (important) // Pipe data trough and CLOSE the stream (important)
@ -281,10 +281,8 @@ public class Encrypt {
decryptor.close(); decryptor.close();
// Check the metadata to see how the message was encrypted/signed // Check the metadata to see how the message was encrypted/signed
OpenPgpMetadata metadata = decryptor.getResult(); MessageMetadata metadata = decryptor.getMetadata();
assertTrue(metadata.isEncrypted()); assertTrue(metadata.isEncrypted());
assertEquals(message, plaintext.toString()); assertEquals(message, plaintext.toString());
} }
} }