brownpaper/src/pgp.rs
shimunn 338f8f6729
Some checks reported errors
continuous-integration/drone/push Build encountered an error
only accept signed snippets
2019-09-06 23:28:28 +02:00

52 lines
1.4 KiB
Rust

use std::io;
use std::fs;
use std::fs::{File};
use std::path::Path;
use std::io::prelude::*;
use openpgp::parse::Parse;
use openpgp::*;
use openpgp::parse::stream::*;
pub struct KnownKeys {
keys: Vec<openpgp::TPK>
}
impl VerificationHelper for &KnownKeys {
fn get_public_keys(&mut self, _ids: &[KeyID]) -> Result<Vec<TPK>> {
Ok(self.keys.clone())
}
fn check(&mut self, structure: &MessageStructure) -> Result<()> {
Ok(()) // Implement your verification policy here.
}
}
impl KnownKeys {
pub fn load_dir(dir: impl AsRef<Path>) -> io::Result<KnownKeys> {
let mut keys: Vec<openpgp::TPK> = Vec::with_capacity(3);
for f in fs::read_dir(dir)? {
let f = f?;
if f.metadata()?.is_dir() {
continue;
}
let tpk = openpgp::TPK::from_file(f.path()).unwrap();
println!("Fingerprint: {}", tpk.fingerprint());
keys.push(tpk);
}
Ok(KnownKeys{
keys: keys
})
}
pub fn verify(&mut self, r: impl Read) -> io::Result<Vec<u8>> {
let mut content = Vec::with_capacity(2048);
let helper = &*self;
let mut v = Verifier::<&KnownKeys>::from_reader(r, helper, None).map_err(|e| dbg!(e)).unwrap();
if v.read_to_end(&mut content).is_err() {
return Err(io::Error::new(io::ErrorKind::InvalidData, "Signature Mismatch"));
}
Ok(content)
}
}