util.helpers: Add a whitespace-aware base64 codec.

This commit is contained in:
Emmanuel Gil Peyrot 2019-01-27 17:16:23 +01:00
parent b6c7a06edd
commit 1921f6819e

View file

@ -39,7 +39,7 @@ impl TrimmedPlainText {
}
}
/// Codec wrapping base64 encode/decode
/// Codec wrapping base64 encode/decode.
pub struct Base64;
impl Base64 {
@ -51,3 +51,17 @@ impl Base64 {
Some(base64::encode(b))
}
}
/// Codec wrapping base64 encode/decode, while ignoring whitespace characters.
pub struct WhitespaceAwareBase64;
impl WhitespaceAwareBase64 {
pub fn decode(s: &str) -> Result<Vec<u8>, Error> {
let s: String = s.chars().into_iter().filter(|ch| *ch != ' ' && *ch != '\n' && *ch != '\t').collect();
Ok(base64::decode(&s)?)
}
pub fn encode(b: &Vec<u8>) -> Option<String> {
Some(base64::encode(b))
}
}