use std::io::{self, Read};
extern crate sequoia_openpgp as openpgp;
use self::openpgp::types::SymmetricAlgorithm;
use self::openpgp::conversions::hex;
use self::openpgp::crypto::mpis;
use self::openpgp::{Packet, Result};
use self::openpgp::packet::prelude::*;
use self::openpgp::packet::header::CTB;
use self::openpgp::packet::{Header, header::BodyLength, Signature};
use self::openpgp::packet::signature::subpacket::{Subpacket, SubpacketValue};
use self::openpgp::crypto::{SessionKey, s2k::S2K};
use self::openpgp::parse::{map::Map, Parse, PacketParserResult};
#[derive(Debug)]
pub enum Kind {
Message {
encrypted: bool,
},
Keyring,
TPK,
Unknown,
}
/// Converts sequoia_openpgp types for rendering.
pub trait Convert<T> {
/// Performs the conversion.
fn convert(self) -> T;
}
impl Convert<chrono::Duration> for std::time::Duration {
fn convert(self) -> chrono::Duration {
chrono::Duration::seconds(self.as_secs() as i64)
}
}
impl Convert<chrono::DateTime<chrono::offset::Utc>> for std::time::SystemTime {
fn convert(self) -> chrono::DateTime<chrono::offset::Utc> {
chrono::DateTime::<chrono::offset::Utc>::from(self)
}
}
pub fn dump<W>(input: &mut dyn io::Read, output: &mut dyn io::Write,
mpis: bool, hex: bool, sk: Option<&SessionKey>,
width: W)
-> Result<Kind>
where W: Into<Option<usize>>
{
let mut ppr
= self::openpgp::parse::PacketParserBuilder::from_reader(input)?
.map(hex).finalize()?;
let mut message_encrypted = false;
let width = width.into().unwrap_or(80);
let mut dumper = PacketDumper::new(width, mpis);
while let PacketParserResult::Some(mut pp) = ppr {
let additional_fields = match pp.packet {
Packet::Literal(_) => {
let mut prefix = vec![0; 40];
let n = pp.read(&mut prefix)?;
Some(vec![
format!("Content: {:?}{}",
String::from_utf8_lossy(&prefix[..n]),
if n == prefix.len() { "..." } else { "" }),
])
},
Packet::SEIP