mirror of
https://github.com/chatmail/core.git
synced 2026-05-05 14:26:30 +03:00
DcKey::to_base64 can not fail
As this can't fail, we don't have to expose Result on this and can keep the API simpler.
This commit is contained in:
committed by
Floris Bruynooghe
parent
c41bdaa2b7
commit
4d0a08d858
@@ -106,19 +106,16 @@ impl fmt::Display for Aheader {
|
|||||||
// adds a whitespace every 78 characters, this allows
|
// adds a whitespace every 78 characters, this allows
|
||||||
// email crate to wrap the lines according to RFC 5322
|
// email crate to wrap the lines according to RFC 5322
|
||||||
// (which may insert a linebreak before every whitespace)
|
// (which may insert a linebreak before every whitespace)
|
||||||
let keydata = self
|
let keydata = self.public_key.to_base64().chars().enumerate().fold(
|
||||||
.public_key
|
String::new(),
|
||||||
.to_base64()
|
|mut res, (i, c)| {
|
||||||
.unwrap_or_default()
|
|
||||||
.chars()
|
|
||||||
.enumerate()
|
|
||||||
.fold(String::new(), |mut res, (i, c)| {
|
|
||||||
if i % 78 == 78 - "keydata=".len() {
|
if i % 78 == 78 - "keydata=".len() {
|
||||||
res.push(' ')
|
res.push(' ')
|
||||||
}
|
}
|
||||||
res.push(c);
|
res.push(c);
|
||||||
res
|
res
|
||||||
});
|
},
|
||||||
|
);
|
||||||
write!(fmt, " keydata={}", keydata)
|
write!(fmt, " keydata={}", keydata)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
src/key.rs
18
src/key.rs
@@ -48,12 +48,14 @@ pub type Result<T> = std::result::Result<T, Error>;
|
|||||||
pub trait DcKey: Serialize + Deserializable {
|
pub trait DcKey: Serialize + Deserializable {
|
||||||
type KeyType: Serialize + Deserializable;
|
type KeyType: Serialize + Deserializable;
|
||||||
|
|
||||||
|
/// Create a key from some bytes.
|
||||||
fn from_slice(bytes: &[u8]) -> Result<Self::KeyType> {
|
fn from_slice(bytes: &[u8]) -> Result<Self::KeyType> {
|
||||||
Ok(<Self::KeyType as Deserializable>::from_bytes(Cursor::new(
|
Ok(<Self::KeyType as Deserializable>::from_bytes(Cursor::new(
|
||||||
bytes,
|
bytes,
|
||||||
))?)
|
))?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a key from a base64 string.
|
||||||
fn from_base64(data: &str) -> Result<Self::KeyType> {
|
fn from_base64(data: &str) -> Result<Self::KeyType> {
|
||||||
// strip newlines and other whitespace
|
// strip newlines and other whitespace
|
||||||
let cleaned: String = data.trim().split_whitespace().collect();
|
let cleaned: String = data.trim().split_whitespace().collect();
|
||||||
@@ -61,14 +63,16 @@ pub trait DcKey: Serialize + Deserializable {
|
|||||||
Self::from_slice(&bytes)
|
Self::from_slice(&bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_base64(&self) -> Result<String> {
|
/// Serialise the key to a base64 string.
|
||||||
let bytes = self.to_bytes()?;
|
fn to_base64(&self) -> String {
|
||||||
Ok(base64::encode(&bytes))
|
// Not using Serialize::to_bytes() to make clear *why* it is
|
||||||
|
// safe to ignore this error.
|
||||||
|
// Because we write to a Vec<u8> 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<Self::KeyType> {
|
|
||||||
// <Self as Self::KeyType>::verify(self)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DcKey for SignedPublicKey {
|
impl DcKey for SignedPublicKey {
|
||||||
|
|||||||
Reference in New Issue
Block a user