util.helpers: Add a whitespace-aware base64 codec.
This commit is contained in:
parent
b6c7a06edd
commit
1921f6819e
1 changed files with 15 additions and 1 deletions
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue