-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencrypt_decrypt_armor.rs
40 lines (34 loc) · 1.2 KB
/
encrypt_decrypt_armor.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
40
use drand_core::HttpClient;
fn main() {
let msg = b"Hello world! I'm encrypting a message using timelock encryption.".to_vec();
// Use a drand client to retrieve beacon information
let client: HttpClient =
"https://api.drand.sh/dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493"
.try_into()
.unwrap();
let info = client.chain_info().unwrap();
let round = 1000;
// Encryption with armoring, making encrypted message ASCII printable
let mut armored = tlock_age::armor::ArmoredWriter::wrap_output(vec![]).unwrap();
tlock_age::encrypt(
&mut armored,
msg.as_slice(),
&info.hash(),
&info.public_key(),
round,
)
.unwrap();
let encrypted = armored.finish().unwrap();
// Decrypting the message. It requires the round signature, here retrieved from the beacon above.
let mut decrypted = vec![];
let signature = client.get(round).unwrap().signature();
tlock_age::decrypt(
&mut decrypted,
encrypted.as_slice(),
&info.hash(),
&signature,
)
.unwrap();
let decrypted = std::str::from_utf8(&decrypted).unwrap();
println!("{decrypted}");
}