26 lines
960 B
Rust
26 lines
960 B
Rust
use pgp::crypto::SymmetricKeyAlgorithm;
|
|
use pgp::{Deserializable, Message, SignedPublicKey};
|
|
use std::io::Write;
|
|
|
|
pub fn encrypt(msg: String) -> String {
|
|
// err: Encryption is done twice
|
|
let pub_key =
|
|
SignedPublicKey::from_string(&*std::fs::read_to_string("/config/pub.key").unwrap())
|
|
.unwrap()
|
|
.0;
|
|
let mut rng = rand::thread_rng();
|
|
let c = Message::new_literal("msg", &msg)
|
|
.encrypt_to_keys(&mut rng, SymmetricKeyAlgorithm::AES128, &[&pub_key])
|
|
.unwrap()
|
|
.to_armored_string(None)
|
|
.unwrap();
|
|
return c;
|
|
}
|
|
|
|
pub fn save_msg(msg: String, name: &str) {
|
|
std::fs::create_dir_all("/data/messages").expect("couldn't create msg dir");
|
|
let time = chrono::offset::Utc::now();
|
|
let time = time.format("%Y-%m-%d.%H-%M").to_string();
|
|
let mut f = std::fs::File::create(format!("/data/messages/{name}-{time}.asc")).unwrap();
|
|
f.write_all(encrypt(msg).as_bytes()).unwrap();
|
|
}
|