1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use crate::prelude::*;
use crate::Method;

use std::sync::Arc;
use std::str::FromStr;
use async_trait::async_trait;

#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(featue = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Base64;

impl FromStr for Base64 {
    type Err = Error;

    fn from_str(input: &str) -> Result<Self> {
        if input.to_uppercase() == "BASE64" || input.to_uppercase() == "B64" {
            Ok(Base64)
        } else {
            Err(Error::InvalidMethodError(input.to_string()))
        }
    }
}

#[async_trait]
impl Method for Base64 {
    async fn encrypt(&self, uw: Arc<str>, _vw: Arc<str>) -> Result<String> {
        use base64::{engine::general_purpose, Engine as _};

        let encoded: String = general_purpose::STANDARD.encode(uw.as_bytes());

        Ok(encoded)
    }
}