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

Emmanuel Gil Peyrot created

Change summary

src/util/helpers.rs | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)

Detailed changes

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<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))
+    }
+}