From a2d9b44b27798e2c8a759b2b78d64fc3c6fd9910 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sun, 21 Jul 2019 13:04:15 +0200 Subject: [PATCH] Read and decode key file --- Cargo.lock | 1 + Cargo.toml | 3 ++- src/crypto.rs | 3 +++ src/main.rs | 23 +++++++++++++++++++---- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 59f442f..f16cc63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1295,6 +1295,7 @@ dependencies = [ "openssl 0.10.23 (registry+https://github.com/rust-lang/crates.io-index)", "ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 3d708a0..0b617e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,5 @@ openssl = "0.10" listenfd = "0.3" # To watch file modifications and automatically launch a build process (only used in dev/debug). ron = "0.5.1" # Rust object notation, to load configuration files. -itertools = "0.8.0" \ No newline at end of file +itertools = "0.8.0" +url = "1.7.2" \ No newline at end of file diff --git a/src/crypto.rs b/src/crypto.rs index 4190552..3a83d8c 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -1,5 +1,8 @@ + + pub fn encrypt(key: &str, plain_text: &str) -> String { + dbg!(key); String::new() } diff --git a/src/main.rs b/src/main.rs index 09965c1..c5a7dc1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use actix_files as fs; use actix_web::{web, middleware, App, HttpServer, HttpResponse, Responder, Result, web::Query}; use askama::Template; +use std::io::prelude::*; use ron::de::from_reader; use serde::Deserialize; use std::{fs::File, env::args}; @@ -64,17 +65,33 @@ fn print_usage() { println!(" {} [--help] [--encrypt ]", get_exe_name()); } +fn read_key() -> String { + use url::percent_encoding::percent_decode; + let mut key = String::new(); + File::open(consts::FILE_KEY) + .expect(&format!("Failed to open key file: {}", consts::FILE_KEY)) + .read_to_string(&mut key) + .expect(&format!("Failed to read key file: {}", consts::FILE_KEY)); + + String::from( + percent_decode(key.as_bytes()) + .decode_utf8() + .expect(&format!("Failed to decode key file: {}", consts::FILE_KEY)) + ) +} + fn main() -> std::io::Result<()> { let args: Vec = args().collect(); + let key = read_key(); + if args.iter().any(|arg| arg == "--help") { print_usage(); return Ok(()); } else if let Some((position_arg_encrypt, _)) = args.iter().find_position(|arg| arg == &"--encrypt") { match args.iter().nth(position_arg_encrypt + 1) { Some(mess_to_encrypt) => { - let encrypted_mess = mess_to_encrypt; - //let encrypted_mess = crypto::encrypt(key: &str, plain_text: &str); + let encrypted_mess = crypto::encrypt(&key, mess_to_encrypt); println!("Encrypted message: {}", encrypted_mess); } None => print_usage() @@ -93,8 +110,6 @@ fn main() -> std::io::Result<()> { } }; - let key = File::open(consts::FILE_KEY).expect(&format!("Failed to open key file: {}", consts::FILE_KEY)); - println!("Configuration: {:?}", config); let mut listenfd = ListenFd::from_env(); -- 2.43.0