1use base64::{
2 alphabet::Alphabet,
3 engine::{general_purpose::PAD, GeneralPurpose},
4 Engine,
5};
6
7use super::Encoding;
8use crate::NcrError;
9
10#[derive(Debug)]
17pub struct Base64Encoding;
18
19impl Encoding for Base64Encoding {
20 fn encode(text: &[u8]) -> String {
21 BASE64_ENGINE.encode(text)
22 }
23
24 fn decode(text: &str) -> Result<Vec<u8>, NcrError> {
25 BASE64_ENGINE
26 .decode(text)
27 .map_err(|_| NcrError::DecodeError)
28 }
29}
30
31const BASE64_ALPHABET: Alphabet =
32 match Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\\") {
33 Ok(alphabet) => alphabet,
34 Err(_) => panic!(),
35 };
36const BASE64_ENGINE: GeneralPurpose = GeneralPurpose::new(&BASE64_ALPHABET, PAD);