blob: 0b5c94d78f4c2714a1b36b309cd860ed927b538f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
use sequoia_openpgp as openpgp;
use openpgp::cert::Cert;
use openpgp::policy::StandardPolicy;
use openpgp::serialize::stream::{Encryptor, LiteralWriter, Message};
use std::io::Write;
// naively encrypt, without caring for revocation or expiration
pub fn encrypt_to_cert(
bytes: &[u8],
cert: &Cert,
) -> openpgp::Result<Vec<u8>> {
let mut sink = vec![];
let p = &StandardPolicy::new();
let recipients = cert
.keys()
.with_policy(p, None)
.supported()
.for_transport_encryption()
.for_storage_encryption();
let message =
Encryptor::for_recipients(Message::new(&mut sink), recipients)
.build()?;
let mut w = LiteralWriter::new(message).build()?;
w.write_all(bytes)?;
w.finalize()?;
Ok(sink)
}
pub fn encrypt_with_password(
bytes: &[u8],
password: &str,
) -> openpgp::Result<Vec<u8>> {
let mut sink = vec![];
let message =
Encryptor::with_passwords(Message::new(&mut sink), Some(password))
.build()?;
let mut w = LiteralWriter::new(message).build()?;
w.write_all(bytes)?;
w.finalize()?;
Ok(sink)
}
|