From 7ac00d911b1393e4b5c82b74769996733c3bec1a Mon Sep 17 00:00:00 2001 From: Trivernis Date: Tue, 10 Mar 2020 12:41:52 +0100 Subject: [PATCH 1/2] Improve memory efficientcy of decryption --- src/main.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index a428904..ffd4a42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -129,19 +129,21 @@ fn decrypt(_opts: &Opts, args: &Decrypt) { if let Some(dict) = dictionary { let sp = spinner("Reading dictionary..."); - let dictionary = fs::read_to_string(dict).expect("Failed to read dictionary file!"); - let lines = dictionary.par_lines(); - - let pw_table: Vec = lines - .map(|line| { - let parts: Vec<&str> = line.split("\t").collect::>(); - let pw = parts[0].parse().unwrap(); - let key_str: String = parts[1].parse().unwrap(); - let key = base64::decode(&key_str).unwrap(); - - (pw, key) - }) - .collect(); + let pw_table: Vec; + { + let dictionary = fs::read_to_string(dict).expect("Failed to read dictionary file!"); + let lines = dictionary.par_lines(); + pw_table = lines + .map(|line| { + let parts: Vec<&str> = line.split("\t").collect::>(); + let pw = parts[0].to_string(); + let key_str: String = parts[1].to_string(); + let key = base64::decode(&key_str).unwrap(); + + (pw, key) + }) + .collect(); + } sp.message("Dictionary decrypting file multithreaded".into()); if let Some(dec_data) = decrypt_with_dictionary(&data, pw_table, &data_checksum) { sp.stop(); From dd781209ffc72cebdf89c8443b27ee1943c1f36e Mon Sep 17 00:00:00 2001 From: Trivernis Date: Tue, 10 Mar 2020 13:00:37 +0100 Subject: [PATCH 2/2] Add duration output after encryption --- src/lib/crypt.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/crypt.rs b/src/lib/crypt.rs index 39605ec..129da04 100644 --- a/src/lib/crypt.rs +++ b/src/lib/crypt.rs @@ -5,6 +5,7 @@ use des::Des; use rand::Rng; use rayon::prelude::*; use std::sync::Mutex; +use std::time::Instant; type DesCfb = Cfb; @@ -37,6 +38,7 @@ pub fn decrypt_with_dictionary( checksum: &[u8], ) -> Option> { let decrypted = Mutex::>>::new(None); + let start = Instant::now(); let pass = dict.par_iter().find_first(|(_pw, key)| { let decrypted_data = decrypt_data(&data, key); let decr_check = sha_checksum(&decrypted_data); @@ -49,7 +51,11 @@ pub fn decrypt_with_dictionary( }; }); if let Some((pw, _key)) = pass { - println!("\nPassword found: {}", pw); + println!( + "\nPassword found in {:.2}s: {}", + start.elapsed().as_secs_f32(), + pw + ); let decry = decrypted.lock().unwrap(); if let Some(decrypted_data) = (*decry).clone() { return Some(decrypted_data);