openpgp 0.2.0

Read and write OpenPGP (RFC4880) files
docs.rs failed to build openpgp-0.2.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: openpgp-0.1.0

The goal of this crate is to interact with the OpenPGP-format, i.e. parse it and produce correct PGP files. OpenPGP can describe so many things that parsing is much more pleasant to write in an event-driven way, and the resulting API is also smaller.

This version is not yet able to produce encrypted content, but signed messages should work, as demonstrated by the following example, which reads a private key (in the format output by gpg --export-secret-keys -a), signs a message and reads it back, verifying the signature.

PGP files are split into packets, with different meanings.

use openpgp::*;
use openpgp::key::*;
use std::collections::HashMap;

struct P {
    keys: HashMap<u64, key::PGPKey>,
    data: Vec<u8>,
    password: &'static [u8],
}

impl PGP for P {

    fn get_key<'a>(&'a mut self, keyid: u64) -> Option<&'a key::Key> {
        self.keys.get(&keyid).and_then(|x| Some(&x.key))
    }

    fn get_signed_data(&mut self, _: signature::Type) -> &[u8] {
        &self.data
    }
   
    fn get_password(&mut self) -> &[u8] {
        &self.password
    }
   
    fn public_key(&mut self, public_key: key::PGPKey) -> Result<(), Error> {
        let id = public_key.long_id();
        println!("public key {:?}", id);
        if !self.keys.contains_key(&id) {
            self.keys.insert(id, public_key);
        }
        Ok(())
    }

    fn secret_key(&mut self, secret_key: key::PGPKey) -> Result<(), Error> {
        self.public_key(secret_key)
    }
   
    fn literal(&mut self, file_name:&str, date:u32, literal: &[u8]) -> Result<(), Error> {
        println!("literal: {:?} {:?}", file_name, date);
        self.data.clear();
        self.data.extend(literal);
        Ok(())
    }
   
    fn signature_verified(&mut self, is_ok:bool) -> Result<(), Error> {
        println!("Verified: {:?}", is_ok);
        Ok(())
    }

}
let secret_key = "-----BEGIN PGP PRIVATE KEY BLOCK-----\nVersion: GnuPG v2\n\nlQPGBFeWBdYBCADZtx24u+o/1nN+7L/OzXY8icC72AI93U8TGMg4jEDmuDJkMThu\nWviYQpC4JbJJMBHeZcfzXragS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X9ZMgy7DgVBsgJCqTmUG\nihjoTLeKdjYQTFEIsPsePfbvRdDqdtZUXd9JCq+eMr/dLd4oJmCsvFMDPPGfW7rw\n5rPd0/9nwNu0Bst0PAiOliHYmRfSYy9l15wy8FrVIsJGqpEDotzo+sXicZWTtUuA\nIbBVag5seqXE70GhZ+pdSx+dJNE55NOlCzw4Y2yc6HbeVeguIXY=\n=USRz\n-----END PGP PRIVATE KEY BLOCK-----\n";

let contents = b"Moi non plus, Bob";
let mut p = P {
    keys: HashMap::new(),
    data: contents.to_vec(),
    password: b"blabla blibli",
};

{
    let mut sk = secret_key.as_bytes();
    let sk = read_armored(&mut sk);
    let mut sk = &sk[..];
    parse(&mut p, &mut sk).unwrap();
};

let mut s = Vec::new();
{
    let mut buf = Vec::new();

    let now = std::time::SystemTime::now();
    let now = now.duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as u32;
    if let Some(&Key::Secret(ref sk)) = p.keys.get(&1475582989415350818).and_then(|pgp| Some(&pgp.key)) {
        sk.sign(&mut buf,
                contents,
                signature::Type::Binary,
                &[signature::Subpacket::SignatureCreationTime(now)],
                &[signature::Subpacket::Issuer(1475582989415350818)])
          .unwrap();
        packet::write(&mut s, packet::Tag::Signature, &buf).unwrap();
    }
}
let mut slice = &s[..];
parse(&mut p, &mut slice).unwrap();