sequoia-openpgp 0.12.0 -> 0.16.0

This commit is contained in:
2020-04-12 22:04:52 +08:00
parent e16648a218
commit 32aa27ab1a
3 changed files with 64 additions and 36 deletions

View File

@@ -12,15 +12,15 @@ use std::{
};
use rust_util::{ XResult, new_box_error, };
use openpgp::{
types::KeyFlags,
TPK,
Cert,
parse::Parse,
types::KeyFlags,
serialize::stream::{
Recipient,
Message,
Encryptor,
LiteralWriter,
},
policy::StandardPolicy as P,
};
use indicatif::{ ProgressBar, ProgressStyle, };
@@ -29,20 +29,20 @@ const PB_PROGRESS: &str = "#-";
const PB_TEMPLATE: &str = "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})";
pub struct OpenPGPTool {
pub tpk: TPK,
pub cert: Cert,
}
impl OpenPGPTool {
pub fn from_file(file: &str) -> XResult<OpenPGPTool> {
Ok(OpenPGPTool{
tpk: TPK::from_file(std::path::Path::new(file))?,
cert: Cert::from_file(Path::new(file))?,
})
}
#[allow(dead_code)]
pub fn from_bytes(bs: &[u8]) -> XResult<OpenPGPTool> {
Ok(OpenPGPTool{
tpk: TPK::from_bytes(&bs)?,
cert: Cert::from_bytes(&bs)?,
})
}
@@ -54,18 +54,23 @@ impl OpenPGPTool {
return Err(new_box_error(&format!("To file exists: {}", to_file)));
}
let recipient: Recipient = self.tpk.keys_valid()
.key_flags(KeyFlags::default().set_encrypt_at_rest(true).set_encrypt_for_transport(true))
.map(|(_, _, key)| key.into())
.nth(0)
.ok_or_else(|| new_box_error("Encryption key not found in TPK"))?;
// https://gitlab.com/sequoia-pgp/sequoia/-/blob/master/openpgp/examples/encrypt-for.rs
let p = &P::new();
let mode = KeyFlags::default().set_storage_encryption(true);
let recipients = self.cert.keys()
.with_policy(p, None).alive().revoked(false).key_flags(&mode)
.map(|ka| ka.key().into())
.collect::<Vec<_>>();
if recipients.is_empty() {
return Err(new_box_error("Cannot find any encrypt key in pgp key file."));
}
let bw = BufWriter::new(File::create(to_file)?);
let message = if armor {
Message::new(armor::Writer::new(bw, armor::Kind::Message, &[])?)
} else {
Message::new(bw)
};
let encryptor = Encryptor::for_recipient(message, recipient).build()?;
let encryptor = Encryptor::for_recipients(message, recipients).build()?;
let mut pgp_encrypt_writer = LiteralWriter::new(encryptor).build()?;
let mut from = File::open(from_file)?;
encrypt_read_write(&mut from, &mut pgp_encrypt_writer)?;