33 lines
773 B
Rust
33 lines
773 B
Rust
use std::io::Write;
|
|
|
|
use base64::engine::general_purpose;
|
|
use magic_crypt::{new_magic_crypt, MagicCryptTrait};
|
|
|
|
pub fn decryt<I>(value: I, secret_key: String) -> anyhow::Result<String>
|
|
where
|
|
I: AsRef<str>,
|
|
{
|
|
let mc = new_magic_crypt!(&secret_key, 256);
|
|
Ok(mc.decrypt_base64_to_string(value)?)
|
|
}
|
|
|
|
pub fn compute_key(from: String) -> String {
|
|
let mut enc = base64::write::EncoderStringWriter::new(&general_purpose::URL_SAFE_NO_PAD);
|
|
enc.write_all(from.as_bytes()).unwrap();
|
|
|
|
enc.into_inner()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[tokio::test]
|
|
async fn test_compute_key_async() {
|
|
let input = "test string".to_string();
|
|
|
|
let encoded = compute_key(input);
|
|
|
|
assert_eq!(encoded, "base64_encoded_string");
|
|
}
|
|
}
|