fencryption_lib/commands/
encrypt_text.rs

1//! Encrypt text.
2
3use base64::{engine::general_purpose, Engine};
4
5use crate::{
6    commands::{ErrorBuilder, Result},
7    crypto::Crypto,
8};
9
10/// Encrypts given text (encoded to base64).
11pub fn execute(key: &String, text: &String) -> Result<String> {
12    let crypto = Crypto::new(key).map_err(|e| {
13        ErrorBuilder::new()
14            .message("Failed to initialize encryption utils")
15            .error(e)
16            .build()
17    })?;
18
19    let plain = text.as_bytes();
20
21    let enc = crypto.encrypt(plain).map_err(|e| {
22        ErrorBuilder::new()
23            .message("Failed to encrypt text")
24            .error(e)
25            .build()
26    })?;
27
28    Ok(general_purpose::STANDARD.encode(enc))
29}