1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-22 19:08:00 +01:00

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.decryption_verification.ConsumerOptions;
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.ProducerOptions;
import org.pgpainless.encryption_signing.SigningOptions;
@ -168,9 +168,9 @@ public class DecryptOrVerify {
decryptionStream.close(); // remember to close the stream!
// The metadata object contains information about the message
OpenPgpMetadata metadata = decryptionStream.getResult();
MessageMetadata metadata = decryptionStream.getMetadata();
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
assertEquals(PLAINTEXT, plaintextOut.toString());
@ -200,11 +200,10 @@ public class DecryptOrVerify {
decryptionStream.close(); // remember to close the stream to finish signature verification
// 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.isSigned()); // message contained some signatures
assertTrue(metadata.isVerified()); // the signatures were actually correct
assertTrue(metadata.containsVerifiedSignatureFrom(certificate)); // the signatures could be verified using the certificate
assertTrue(metadata.isVerifiedSigned()); // the signatures were actually correct
assertTrue(metadata.isVerifiedSignedBy(certificate)); // the signatures could be verified using the certificate
assertEquals(PLAINTEXT, plaintextOut.toString());
}
@ -232,8 +231,8 @@ public class DecryptOrVerify {
verificationStream.close(); // remember to close the stream to finish sig verification
// Get the metadata object for information about the message
OpenPgpMetadata metadata = verificationStream.getResult();
assertTrue(metadata.isVerified()); // signatures were verified successfully
MessageMetadata metadata = verificationStream.getMetadata();
assertTrue(metadata.isVerifiedSigned()); // signatures were verified successfully
// The output stream we piped to now contains the message
assertEquals(PLAINTEXT, out.toString());
}
@ -286,8 +285,8 @@ public class DecryptOrVerify {
verificationStream.close(); // as always, remember to close the stream
// Metadata will confirm that the message was in fact signed
OpenPgpMetadata metadata = verificationStream.getResult();
assertTrue(metadata.isVerified());
MessageMetadata metadata = verificationStream.getMetadata();
assertTrue(metadata.isVerifiedSigned());
// compare the plaintext to what we originally signed
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.decryption_verification.ConsumerOptions;
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.EncryptionStream;
import org.pgpainless.encryption_signing.ProducerOptions;
@ -148,12 +148,12 @@ public class Encrypt {
EncryptionStream encryptor = PGPainless.encryptAndOrSign()
.onOutputStream(ciphertext)
.withOptions(ProducerOptions.signAndEncrypt(
// we want to encrypt communication (affects key selection based on key flags)
EncryptionOptions.encryptCommunications()
.addRecipient(certificateBob)
.addRecipient(certificateAlice),
new SigningOptions()
.addInlineSignature(protectorAlice, keyAlice, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT)
// we want to encrypt communication (affects key selection based on key flags)
EncryptionOptions.encryptCommunications()
.addRecipient(certificateBob)
.addRecipient(certificateAlice),
new SigningOptions()
.addInlineSignature(protectorAlice, keyAlice, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT)
).setAsciiArmor(true)
);
@ -176,9 +176,9 @@ public class Encrypt {
decryptor.close();
// Check the metadata to see how the message was encrypted/signed
OpenPgpMetadata metadata = decryptor.getResult();
MessageMetadata metadata = decryptor.getMetadata();
assertTrue(metadata.isEncrypted());
assertTrue(metadata.containsVerifiedSignatureFrom(certificateAlice));
assertTrue(metadata.isVerifiedSignedBy(certificateAlice));
assertEquals(message, plaintext.toString());
}
@ -236,10 +236,10 @@ public class Encrypt {
// plaintext message to encrypt
String message = "Hello, World!\n";
String[] comments = {
"This comment was added using options.",
"And it has three lines.",
" ",
"Empty lines are skipped."
"This comment was added using options.",
"And it has three lines.",
" ",
"Empty lines are skipped."
};
String comment = comments[0] + "\n" + comments[1] + "\n" + comments[2] + "\n" + comments[3];
ByteArrayOutputStream ciphertext = new ByteArrayOutputStream();
@ -247,12 +247,12 @@ public class Encrypt {
EncryptionStream encryptor = PGPainless.encryptAndOrSign()
.onOutputStream(ciphertext)
.withOptions(ProducerOptions.encrypt(
// we want to encrypt communication (affects key selection based on key flags)
EncryptionOptions.encryptCommunications()
.addRecipient(certificateBob)
.addRecipient(certificateAlice)
).setAsciiArmor(true)
.setComment(comment)
// we want to encrypt communication (affects key selection based on key flags)
EncryptionOptions.encryptCommunications()
.addRecipient(certificateBob)
.addRecipient(certificateAlice)
).setAsciiArmor(true)
.setComment(comment)
);
// Pipe data trough and CLOSE the stream (important)
@ -281,10 +281,8 @@ public class Encrypt {
decryptor.close();
// Check the metadata to see how the message was encrypted/signed
OpenPgpMetadata metadata = decryptor.getResult();
MessageMetadata metadata = decryptor.getMetadata();
assertTrue(metadata.isEncrypted());
assertEquals(message, plaintext.toString());
}
}