From 13c437470cc7377d7b761b5bb9b8d4efb0ba385e Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Wed, 20 Nov 2019 14:11:29 +0100 Subject: openpgp: Use the builder pattern for stream::Encryptor. - Fixes #375. --- openpgp/examples/encrypt-for.rs | 14 ++++++++------ openpgp/examples/generate-encrypt-decrypt.rs | 12 +++++++----- openpgp/examples/pad.rs | 13 +++++++------ 3 files changed, 22 insertions(+), 17 deletions(-) (limited to 'openpgp/examples') diff --git a/openpgp/examples/encrypt-for.rs b/openpgp/examples/encrypt-for.rs index 3076c7a4..82f636d9 100644 --- a/openpgp/examples/encrypt-for.rs +++ b/openpgp/examples/encrypt-for.rs @@ -35,7 +35,7 @@ fn main() { }).collect(); // Build a vector of recipients to hand to Encryptor. - let recipients = + let mut recipients = tpks.iter() .flat_map(|tpk| tpk.keys_valid().key_flags(mode.clone())) .map(|(_, _, key)| key.into()) @@ -51,11 +51,13 @@ fn main() { let message = Message::new(sink); // We want to encrypt a literal data packet. - let encryptor = Encryptor::new(message, - &[], // No symmetric encryption. - &recipients, - None, None) - .expect("Failed to create encryptor"); + let mut encryptor = Encryptor::for_recipient( + message, recipients.pop().expect("No encryption key found")); + for r in recipients { + encryptor = encryptor.add_recipient(r) + } + let encryptor = encryptor.build().expect("Failed to create encryptor"); + let mut literal_writer = LiteralWriter::new(encryptor).build() .expect("Failed to create literal writer"); diff --git a/openpgp/examples/generate-encrypt-decrypt.rs b/openpgp/examples/generate-encrypt-decrypt.rs index f8eeb05f..d20219ab 100644 --- a/openpgp/examples/generate-encrypt-decrypt.rs +++ b/openpgp/examples/generate-encrypt-decrypt.rs @@ -41,7 +41,7 @@ fn generate() -> openpgp::Result { fn encrypt(sink: &mut dyn Write, plaintext: &str, recipient: &openpgp::TPK) -> openpgp::Result<()> { // Build a vector of recipients to hand to Encryptor. - let recipients = + let mut recipients = recipient.keys_valid() .key_flags(KeyFlags::default() .set_encrypt_at_rest(true) @@ -53,10 +53,12 @@ fn encrypt(sink: &mut dyn Write, plaintext: &str, recipient: &openpgp::TPK) let message = Message::new(sink); // We want to encrypt a literal data packet. - let encryptor = Encryptor::new(message, - &[], // No symmetric encryption. - &recipients, - None, None)?; + let mut encryptor = Encryptor::for_recipient( + message, recipients.pop().expect("No encryption key found")); + for r in recipients { + encryptor = encryptor.add_recipient(r) + } + let encryptor = encryptor.build().expect("Failed to create encryptor"); // Emit a literal data packet. let mut literal_writer = LiteralWriter::new(encryptor).build()?; diff --git a/openpgp/examples/pad.rs b/openpgp/examples/pad.rs index 76d4ebc0..6a3d7c96 100644 --- a/openpgp/examples/pad.rs +++ b/openpgp/examples/pad.rs @@ -37,7 +37,7 @@ fn main() { }).collect(); // Build a vector of recipients to hand to Encryptor. - let recipients = + let mut recipients = tpks.iter() .flat_map(|tpk| tpk.keys_valid().key_flags(mode.clone())) .map(|(_, _, key)| Recipient::new(KeyID::wildcard(), key)) @@ -53,11 +53,12 @@ fn main() { let message = Message::new(sink); // We want to encrypt a literal data packet. - let encryptor = Encryptor::new(message, - &[], // No symmetric encryption. - &recipients, - None, None) - .expect("Failed to create encryptor"); + let mut encryptor = Encryptor::for_recipient( + message, recipients.pop().expect("No encryption key found")); + for r in recipients { + encryptor = encryptor.add_recipient(r) + } + let encryptor = encryptor.build().expect("Failed to create encryptor"); let padder = Padder::new(encryptor, padme) .expect("Failed to create padder"); -- cgit v1.2.3