Skip to content

Commit

Permalink
wrap work parameters in struct
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpclark committed Dec 7, 2017
1 parent 079e9a7 commit 43f04fb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
33 changes: 12 additions & 21 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
fn chunk_sequence(d: &mut Digits, adj: Option<String>) -> Vec<String> {
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::<u8>().unwrap() as usize).to_s());
counter += 1;
Expand Down Expand Up @@ -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<Vec<String>> = Mutex::new(vec![]);

chunk.par_iter().for_each(|ref value|
Expand All @@ -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;
}
Expand All @@ -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()
)
Expand Down Expand Up @@ -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();
Expand All @@ -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<Vec<Result<(), Error>>> = Mutex::new(vec![]);

chunk.par_iter().for_each(|ref value|
Expand Down Expand Up @@ -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()
)
Expand Down
22 changes: 19 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> // adj: Option<&str>
);

fn run_app() -> Result<(), Error> {
let matches = App::new("abrute - AES Brute Force File Decryptor").
version(&format!("v{}", crate_version!())[..]).
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 43f04fb

Please sign in to comment.