summaryrefslogtreecommitdiffstats
path: root/guide
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-11-20 14:11:29 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-11-20 14:11:53 +0100
commit13c437470cc7377d7b761b5bb9b8d4efb0ba385e (patch)
treedce09461d71932949024eae22d23b7189ab203ab /guide
parentefe767fc4e48ef2a53e7f8cb7e44d46d884b9694 (diff)
openpgp: Use the builder pattern for stream::Encryptor.
- Fixes #375.
Diffstat (limited to 'guide')
-rw-r--r--guide/src/chapter_02.md48
1 files changed, 28 insertions, 20 deletions
diff --git a/guide/src/chapter_02.md b/guide/src/chapter_02.md
index d00fa669..acc2cb2e 100644
--- a/guide/src/chapter_02.md
+++ b/guide/src/chapter_02.md
@@ -50,7 +50,7 @@ fn main() {
# fn encrypt(sink: &mut 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)
@@ -62,10 +62,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)?;
+# 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()?;
@@ -191,7 +193,7 @@ fn generate() -> openpgp::Result<openpgp::TPK> {
# fn encrypt(sink: &mut 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)
@@ -203,10 +205,12 @@ fn generate() -> openpgp::Result<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()?;
@@ -332,7 +336,7 @@ implements [`io::Write`], and we simply write the plaintext to it.
fn encrypt(sink: &mut 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)
@@ -344,10 +348,12 @@ fn encrypt(sink: &mut 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()?;
@@ -487,7 +493,7 @@ Decrypted data can be read from this using [`io::Read`].
# fn encrypt(sink: &mut 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)
@@ -499,10 +505,12 @@ Decrypted data can be read from this using [`io::Read`].
# 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()?;