Move keydata= wrapping from key.rs to aheader.rs

This commit is contained in:
Alexander Krotov
2019-12-06 20:33:25 +03:00
committed by holger krekel
parent f1aba8115b
commit 38cddff6e9
2 changed files with 16 additions and 18 deletions

View File

@@ -95,15 +95,24 @@ impl Aheader {
impl fmt::Display for Aheader {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
// TODO replace 78 with enum /rtn
// adds a whitespace every 78 characters, this allows libEtPan to
// wrap the lines according to RFC 5322
// (which may insert a linebreak before every whitespace)
let keydata = self.public_key.to_base64(78);
write!(fmt, "addr={};", self.addr)?;
if self.prefer_encrypt == EncryptPreference::Mutual {
write!(fmt, " prefer-encrypt=mutual;")?;
}
// adds a whitespace every 78 characters, this allows libEtPan to
// wrap the lines according to RFC 5322
// (which may insert a linebreak before every whitespace)
let keydata = self.public_key.to_base64().chars().enumerate().fold(
String::new(),
|mut res, (i, c)| {
if i > 0 && i % 78 == 0 {
res.push(' ')
}
res.push(c);
res
},
);
write!(fmt, " keydata={}", keydata)
}
}

View File

@@ -178,20 +178,9 @@ impl Key {
}
}
pub fn to_base64(&self, break_every: usize) -> String {
pub fn to_base64(&self) -> String {
let buf = self.to_bytes();
let encoded = base64::encode(&buf);
encoded
.chars()
.enumerate()
.fold(String::new(), |mut res, (i, c)| {
if i > 0 && i % break_every == 0 {
res.push(' ')
}
res.push(c);
res
})
base64::encode(&buf)
}
pub fn to_armored_string(