From 240cd8dff12ec6f76bb4d73c1ae0f9b4b7184f9d Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Fri, 9 Apr 2021 19:30:45 -0500 Subject: [PATCH] :recycle: refactor: extract gen_key pure function --- crates/ptth_server/src/bin/ptth_server.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/ptth_server/src/bin/ptth_server.rs b/crates/ptth_server/src/bin/ptth_server.rs index f1efafb..93b5fea 100644 --- a/crates/ptth_server/src/bin/ptth_server.rs +++ b/crates/ptth_server/src/bin/ptth_server.rs @@ -48,12 +48,18 @@ pub struct ConfigFile { pub file_server_root: Option , } -fn gen_key (path: &Path) -> anyhow::Result <()> { +/// Generates 64 bytes of entropy and returns it as Base64 + +fn gen_key () -> String { use rand::RngCore; let mut buffer = vec! [0_u8; 64]; rand::thread_rng ().fill_bytes (&mut buffer); - let api_key = base64::encode (&buffer); + base64::encode (&buffer) +} + +fn gen_and_save_key (path: &Path) -> anyhow::Result <()> { + let api_key = gen_key (); { let mut f = File::create (path)?; @@ -89,7 +95,7 @@ async fn main () -> Result <(), anyhow::Error> { let config_file: ConfigFile = match load_toml::load (&path) { Err (ptth_server::errors::LoadTomlError::Io (_)) => if opt.auto_gen_key { - gen_key (&path)?; + gen_and_save_key (&path)?; load_toml::load (&path)? }