diff --git a/src/aheader.rs b/src/aheader.rs index 326c1a21e..609f70ee9 100644 --- a/src/aheader.rs +++ b/src/aheader.rs @@ -106,19 +106,16 @@ impl fmt::Display for Aheader { // adds a whitespace every 78 characters, this allows // email crate to wrap the lines according to RFC 5322 // (which may insert a linebreak before every whitespace) - let keydata = self - .public_key - .to_base64() - .unwrap_or_default() - .chars() - .enumerate() - .fold(String::new(), |mut res, (i, c)| { + let keydata = self.public_key.to_base64().chars().enumerate().fold( + String::new(), + |mut res, (i, c)| { if i % 78 == 78 - "keydata=".len() { res.push(' ') } res.push(c); res - }); + }, + ); write!(fmt, " keydata={}", keydata) } } diff --git a/src/key.rs b/src/key.rs index 93612cee4..fd0473c08 100644 --- a/src/key.rs +++ b/src/key.rs @@ -48,12 +48,14 @@ pub type Result = std::result::Result; pub trait DcKey: Serialize + Deserializable { type KeyType: Serialize + Deserializable; + /// Create a key from some bytes. fn from_slice(bytes: &[u8]) -> Result { Ok(::from_bytes(Cursor::new( bytes, ))?) } + /// Create a key from a base64 string. fn from_base64(data: &str) -> Result { // strip newlines and other whitespace let cleaned: String = data.trim().split_whitespace().collect(); @@ -61,14 +63,16 @@ pub trait DcKey: Serialize + Deserializable { Self::from_slice(&bytes) } - fn to_base64(&self) -> Result { - let bytes = self.to_bytes()?; - Ok(base64::encode(&bytes)) + /// Serialise the key to a base64 string. + fn to_base64(&self) -> String { + // Not using Serialize::to_bytes() to make clear *why* it is + // safe to ignore this error. + // Because we write to a Vec the io::Write impls never + // fail and we can hide this error. + let mut buf = Vec::new(); + self.to_writer(&mut buf).unwrap(); + base64::encode(&buf) } - - // fn verify(&self) -> Result { - // ::verify(self) - // } } impl DcKey for SignedPublicKey {