-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.rs
39 lines (33 loc) · 1.16 KB
/
basic.rs
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
39
//! This example demonstrates how to use `encrypted-message`
//! to encrypt & decrypt a payload.
use encrypted_message::{
EncryptedMessage,
strategy::Randomized,
config::{Config, Secret, ExposeSecret as _},
};
/// NOTE: Never hardcode your keys like this, obviously.
#[derive(Debug, Default)]
struct EncryptionConfig;
impl Config for EncryptionConfig {
type Strategy = Randomized;
fn keys(&self) -> Vec<Secret<[u8; 32]>> {
let encoded_keys = [Secret::new("75754f7866705767526749456f33644972646f30686e484a484631686e747657".to_string())];
encoded_keys.iter()
.map(|hex_key| {
let mut key = [0; 32];
hex::decode_to_slice(hex_key.expose_secret(), &mut key).unwrap();
key.into()
})
.collect()
}
}
fn main() {
// Encrypt a user's diary.
let diary: EncryptedMessage::<String, EncryptionConfig> = {
EncryptedMessage::encrypt("Very personal stuff".to_string()).unwrap()
};
println!("Encrypted diary: {diary:#?}");
// Decrypt the user's diary.
let decrypted = diary.decrypt().unwrap();
println!("Decrypted diary: {decrypted}");
}