From 1921f6819e5342c0502a737e9080fae22be4cc92 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 27 Jan 2019 17:16:23 +0100 Subject: [PATCH] util.helpers: Add a whitespace-aware base64 codec. --- src/util/helpers.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/util/helpers.rs b/src/util/helpers.rs index fad0b67..2aa3701 100644 --- a/src/util/helpers.rs +++ b/src/util/helpers.rs @@ -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, 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) -> Option { + Some(base64::encode(b)) + } +}