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
34
35
36
37
38
use base64::{engine::general_purpose, Engine};

use crate::{
    commands::{ErrorBuilder, Result},
    crypto::Crypto,
};

pub fn execute(key: &String, encrypted: &String) -> Result<String> {
    let crypto = Crypto::new(key).map_err(|e| {
        ErrorBuilder::new()
            .message("Failed to initialize encryption utils")
            .error(e)
            .build()
    })?;

    let enc = general_purpose::STANDARD
        .decode(encrypted.to_owned())
        .map_err(|e| {
            ErrorBuilder::new()
                .message("Failed to decode base64")
                .error(e)
                .build()
        })?;

    let dec = crypto.decrypt(enc).map_err(|e| {
        ErrorBuilder::new()
            .message("Failed to encrypt text")
            .error(e)
            .build()
    })?;

    Ok(String::from_utf8(dec).map_err(|e| {
        ErrorBuilder::new()
            .message("Failed to decode decrypted bytes")
            .error(e)
            .build()
    })?)
}