nodb/crypto.rs
1//! # Crypto
2//!
3//! This module contains the `B64` struct which is used to encrypt and decrypt data using the `base64` algorithm.
4
5use anyhow::Result;
6use base64::{
7 engine::{general_purpose::STANDARD, GeneralPurpose},
8 Engine,
9};
10
11const STD: GeneralPurpose = STANDARD;
12
13/// The `B64` struct is used to encrypt and decrypt data using the `base64` algorithm.
14pub struct B64 {}
15
16impl B64 {
17 /// Creates a new `B64` instance.
18
19 pub const fn new() -> Self {
20 Self {}
21 }
22
23 /// Encrypts the given data using the `base64` algorithm.
24
25 pub fn encrypt<T: AsRef<[u8]>>(&self, data: T) -> String {
26 STD.encode(data)
27 }
28
29 /// Decrypts the given data using the `base64` algorithm.
30
31 pub fn decrypt<T: AsRef<[u8]>>(&self, data: T) -> Result<Vec<u8>> {
32 Ok(STD.decode(data)?)
33 }
34}