From 0a8aa98e54e76ba7306eee635c3f3ef3d397173e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9gory=20Burri?= Date: Wed, 2 Sep 2020 16:19:47 +0200 Subject: [PATCH] Automatically generate a key if the key file isn't found. --- Cargo.lock | 8 ++++---- src/crypto.rs | 6 ++++++ src/main.rs | 23 +++++++++++++++++++---- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ec1bb08..0542cca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -81,7 +81,7 @@ dependencies = [ "h2 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "http 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", - "indexmap 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "indexmap 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -698,7 +698,7 @@ dependencies = [ "futures-sink 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "futures-util 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "http 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "indexmap 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "indexmap 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-util 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -771,7 +771,7 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.5.1" +version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "autocfg 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1717,7 +1717,7 @@ dependencies = [ "checksum httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" "checksum humansize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" "checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" -"checksum indexmap 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "86b45e59b16c76b11bf9738fd5d38879d3bd28ad292d7b313608becb17ae2df9" +"checksum indexmap 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4e47a3566dd4fd4eec714ae6ceabdee0caec795be835c223d92c2d40f1e8cf1c" "checksum instant 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "5b141fdc7836c525d4d594027d318c84161ca17aaf8113ab1f81ab93ae897485" "checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" "checksum ipconfig 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f7e2f18aece9709094573a9f24f483c4f65caa4298e2f7ae1b71cc65d853fad7" diff --git a/src/crypto.rs b/src/crypto.rs index 26cbe68..16fbaa0 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -33,6 +33,12 @@ fn decode_key(key: &str) -> Result, KeyError> { } } +/// Return a random key encoded in base64. +pub fn generate_key() -> String { + let key = rand::thread_rng().gen::<[u8; 16]>(); + base64::encode(key) +} + /// Encrypt the given text with the given key (first version). The key length must be 128 bits encoded in base64. /// Ouput formats: /// * 'version' = 1: "1" + base_64( + hash(message) + aes(message)) diff --git a/src/main.rs b/src/main.rs index 8d0a5c9..f385a0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,10 +8,9 @@ use actix_files as fs; use actix_web::{web, middleware, App, HttpServer, HttpResponse, web::Query}; use askama::Template; -use std::io::prelude::*; +use std::{fs::File, path::Path, env::args, io::prelude::*}; use ron::de::from_reader; use serde::Deserialize; -use std::{fs::File, env::args}; use itertools::Itertools; @@ -71,9 +70,25 @@ fn read_key() -> String { ) } +fn write_key(key : &str) { + let percent_encoded = percent_encoding::utf8_percent_encode(key, percent_encoding::NON_ALPHANUMERIC).to_string(); + let mut file = File::create(consts::FILE_KEY).unwrap(); + file.write_all(percent_encoded.as_bytes()).unwrap(); +} + #[actix_rt::main] async fn main() -> std::io::Result<()> { - let key = read_key(); + let key = { + // If the key file doesn't exist then create a new one with a random key in it. + if !Path::new(consts::FILE_KEY).exists() { + let new_key = crypto::generate_key(); + write_key(&new_key); + println!("A key has been generated here: {}", consts::FILE_KEY); + new_key + } else { + read_key() + } + }; if process_args(&key) { return Ok(()) } @@ -116,7 +131,7 @@ async fn main() -> std::io::Result<()> { fn process_args(key: &str) -> bool { fn print_usage() { println!("Usage:"); - println!(" {} [--help] [--encrypt |--decrypt ]", get_exe_name()); + println!(" {} [--help] [--encrypt | --decrypt ]", get_exe_name()); } let args: Vec = args().collect(); -- 2.45.2