diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-09-06 13:47:50 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-09-06 16:42:12 +0200 |
commit | 38a4d2b4ff4fc4512b31a4ff4e4ddd8a6b3c7503 (patch) | |
tree | 469d667b6bab8333df02c7a2402b9edabbe08419 /openpgp/examples/generate-encrypt-decrypt.rs | |
parent | 4d642187f1df0c9a4c60dc2355c797ebac6fcd4f (diff) |
openpgp: Rework streaming encryptor.
- Instead of giving a set of TPKs to the encryptor, hand in a set of
recipients, which are (keyid, key)-tuples, conveniently created
from key queries over TPKs. This simplifies the encryptor, and
makes the key selection explicit.
- Drop the EncryptionMode type.
- As a nice side effect, we can now generate encrypted messages with
wildcard recipient addresses.
Diffstat (limited to 'openpgp/examples/generate-encrypt-decrypt.rs')
-rw-r--r-- | openpgp/examples/generate-encrypt-decrypt.rs | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/openpgp/examples/generate-encrypt-decrypt.rs b/openpgp/examples/generate-encrypt-decrypt.rs index 301a4340..c0c4c4ed 100644 --- a/openpgp/examples/generate-encrypt-decrypt.rs +++ b/openpgp/examples/generate-encrypt-decrypt.rs @@ -6,6 +6,7 @@ extern crate sequoia_openpgp as openpgp; use crate::openpgp::crypto::SessionKey; use crate::openpgp::constants::SymmetricAlgorithm; use crate::openpgp::serialize::stream::*; +use crate::openpgp::packet::KeyFlags; use crate::openpgp::parse::stream::*; const MESSAGE: &'static str = "дружба"; @@ -40,14 +41,22 @@ fn generate() -> openpgp::Result<openpgp::TPK> { /// Encrypts the given message. fn encrypt(sink: &mut Write, plaintext: &str, recipient: &openpgp::TPK) -> openpgp::Result<()> { + // Build a vector of recipients to hand to Encryptor. + let recipients = + recipient.keys_valid() + .key_flags(KeyFlags::default() + .set_encrypt_at_rest(true) + .set_encrypt_for_transport(true)) + .map(|(_, _, key)| key.into()) + .collect::<Vec<_>>(); + // Start streaming an OpenPGP message. let message = Message::new(sink); // We want to encrypt a literal data packet. let encryptor = Encryptor::new(message, &[], // No symmetric encryption. - &[recipient], - EncryptionMode::ForTransport, + &recipients, None, None)?; // Emit a literal data packet. |