-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlib.rs
More file actions
35 lines (32 loc) · 745 Bytes
/
Copy pathlib.rs
File metadata and controls
35 lines (32 loc) · 745 Bytes
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
use base64::{
Engine as _,
engine::general_purpose::STANDARD as base64Engine
};
/// Encrypts a String.
///
/// Example:
/// ```
/// use cryptor::encrypt;
/// assert_eq!(encrypt("hello"), "aGVsbG8=");
/// assert_eq!(encrypt(""), "");
/// ```
pub fn encrypt(to: &str) -> String {
base64Engine.encode(String::from(to))
}
/// Decrypts a String.
///
/// Example:
/// ```
/// use cryptor::decrypt;
/// assert_eq!(decrypt("aGVsbG8="), "hello");
/// assert_eq!(decrypt(""), "");
/// ```
pub fn decrypt(from: &str) -> String {
let base64_bytes = base64Engine.decode(
String::from(from)
).unwrap_or(vec![]);
match String::from_utf8(base64_bytes) {
Ok(result) => result,
Err(_) => "".to_owned()
}
}