Change data to be stored in mutex when encrypting

Change data to be stored in a Mutex when encrypting with a dictionary to avoid decrypting the data twice for a result.
master
trivernis 5 years ago
parent 3e9e1a1eb6
commit ed4391da13

@ -4,6 +4,7 @@ use cfb_mode::Cfb;
use des::Des; use des::Des;
use rand::Rng; use rand::Rng;
use rayon::prelude::*; use rayon::prelude::*;
use std::sync::Mutex;
type DesCfb = Cfb<Des>; type DesCfb = Cfb<Des>;
@ -35,15 +36,24 @@ pub fn decrypt_with_dictionary(
dict: Vec<PassKey>, dict: Vec<PassKey>,
checksum: &[u8], checksum: &[u8],
) -> Option<Vec<u8>> { ) -> Option<Vec<u8>> {
let decrypted = Mutex::<Option<Vec<u8>>>::new(None);
let pass = dict.par_iter().find_first(|(_pw, key)| { let pass = dict.par_iter().find_first(|(_pw, key)| {
let decrypted_data = decrypt_data(&data, key); let decrypted_data = decrypt_data(&data, key);
let decr_check = sha_checksum(&decrypted_data); let decr_check = sha_checksum(&decrypted_data);
return if decr_check == checksum {
decr_check == checksum let mut decry = decrypted.lock().unwrap();
*decry = Some(decrypted_data);
true
} else {
false
};
}); });
if let Some((pw, key)) = pass { if let Some((pw, _key)) = pass {
println!("Password found: {}", pw); println!("Password found: {}", pw);
return Some(decrypt_data(data, &key)); let decry = decrypted.lock().unwrap();
if let Some(decrypted_data) = (*decry).clone() {
return Some(decrypted_data);
}
} }
None None
} }

@ -180,7 +180,7 @@ fn create_dictionary(_opts: &Opts, args: &CreateDictionary) {
println!("Writing passwords to file..."); println!("Writing passwords to file...");
let mut fout = File::create(args.output.clone()).unwrap(); let mut fout = File::create(args.output.clone()).unwrap();
for entry in dictionary { for entry in &dictionary {
let key = base64::encode(entry.1.as_slice()); let key = base64::encode(entry.1.as_slice());
let line = format!("{},{}\n", entry.0, key); let line = format!("{},{}\n", entry.0, key);
fout.write(&line.into_bytes()).unwrap(); fout.write(&line.into_bytes()).unwrap();

Loading…
Cancel
Save