From 43f04fbf4fc38dcced6e95a86f17659eb694a2e2 Mon Sep 17 00:00:00 2001 From: "Daniel P. Clark" <6ftdan@gmail.com> Date: Thu, 7 Dec 2017 01:55:57 -0500 Subject: [PATCH] wrap work parameters in struct --- src/core.rs | 33 ++++++++++++--------------------- src/main.rs | 22 +++++++++++++++++++--- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/core.rs b/src/core.rs index b1d1044..330c74c 100644 --- a/src/core.rs +++ b/src/core.rs @@ -17,19 +17,20 @@ use resume::{ResumeKey,ResumeFile}; use self::tempdir::TempDir; use std::{fs,path,env}; use std::time::{Duration, Instant}; +use ::WorkLoad; fn has_five_minutes_passed(t: Instant) -> bool { Instant::now().duration_since(t) > Duration::new(300,0) } -fn chunk_sequence(d: &mut Digits, adj: Option<&str>) -> Vec { +fn chunk_sequence(d: &mut Digits, adj: Option) -> Vec { let qty: usize = num_cpus::get() * 32; let mut counter = 0; let mut result = vec![]; loop { if counter >= qty { break; } - if let Some(a) = adj { + if let Some(a) = adj.clone() { if d.base() > 3 { result.push(d.step_non_adjacent(a.parse::().unwrap() as usize).to_s()); counter += 1; @@ -79,19 +80,14 @@ fn has_reached_end<'a>(sequencer: &Digits, max: usize) -> Result<(), Error> { Ok(()) } -pub fn aescrypt_core_loop<'a>( - characters: String, - max: usize, - mut sequencer: Digits, - target: &str, - adj: Option<&str> - ) -> Result<(), Error> { +pub fn aescrypt_core_loop<'a>(work_load: WorkLoad) -> Result<(), Error> { + let WorkLoad(characters, max, mut sequencer, target, adj) = work_load; let mut time_keeper = Instant::now(); loop { has_reached_end(&sequencer, max)?; progress_report(&sequencer); - let chunk = chunk_sequence(&mut sequencer, adj); + let chunk = chunk_sequence(&mut sequencer, adj.clone()); let code: Mutex> = Mutex::new(vec![]); chunk.par_iter().for_each(|ref value| @@ -114,7 +110,7 @@ pub fn aescrypt_core_loop<'a>( // every attempt in a temp dir or mem dir and copying that much data that many // times would be very slow and difficult to implement in a threaded way. - aes_command(code.first().unwrap(), target); + aes_command(code.first().unwrap(), &target[..]); ResumeFile::purge(); break; } @@ -123,7 +119,7 @@ pub fn aescrypt_core_loop<'a>( ResumeFile::save( ResumeKey::new( characters.clone(), - adj.map(str::to_string), + adj.clone(), sequencer.clone(), target.to_string() ) @@ -152,13 +148,8 @@ fn any_file_contents(dir: &TempDir, omit: &str) -> bool { }) } -pub fn unzip_core_loop<'a>( - characters: String, - max: usize, - mut sequencer: Digits, - target: &str, - adj: Option<&str> - ) -> Result<(), Error> { +pub fn unzip_core_loop<'a>(work_load: WorkLoad) -> Result<(), Error> { + let WorkLoad(characters, max, mut sequencer, target, adj) = work_load; let mut time_keeper = Instant::now(); if let Ok(dir) = TempDir::new("abrute") { let cwd = env::current_dir().unwrap(); @@ -171,7 +162,7 @@ pub fn unzip_core_loop<'a>( has_reached_end(&sequencer, max)?; progress_report(&sequencer); - let chunk = chunk_sequence(&mut sequencer, adj); + let chunk = chunk_sequence(&mut sequencer, adj.clone()); let code: Mutex>> = Mutex::new(vec![]); chunk.par_iter().for_each(|ref value| @@ -210,7 +201,7 @@ pub fn unzip_core_loop<'a>( ResumeFile::save( ResumeKey::new( characters.clone(), - adj.map(str::to_string), + adj.clone(), sequencer.clone(), target.to_string() ) diff --git a/src/main.rs b/src/main.rs index 22be4a3..6454570 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,14 @@ use core::*; extern crate clap; use clap::{Arg, App}; +pub struct WorkLoad( + pub String, // characters: String, + pub usize, // max: usize, + pub Digits, // mut sequencer: Digits, + pub String, // target: &str, + pub Option // adj: Option<&str> +); + fn run_app() -> Result<(), Error> { let matches = App::new("abrute - AES Brute Force File Decryptor"). version(&format!("v{}", crate_version!())[..]). @@ -120,12 +128,20 @@ USE OF THIS BINARY FALLS UNDER THE MIT LICENSE (c) 2017"). println!("Resuming from last save point: {}", sequencer.to_s()); } // End Resume Feature + + let work_load = WorkLoad( + resume_key_chars, + max, + sequencer, + target.to_string(), + adjacent.map(str::to_string) + ); if matches.is_present("zip") { - unzip_core_loop(resume_key_chars, max, sequencer, target, adjacent) - } else { - aescrypt_core_loop(resume_key_chars, max, sequencer, target, adjacent) + return unzip_core_loop(work_load); } + + aescrypt_core_loop(work_load) } fn main() {