From eacd8d986c4ea1a84c197b09f31b5ff738497ac2 Mon Sep 17 00:00:00 2001 From: Jon-Eric Cook Date: Mon, 27 Jan 2020 19:53:20 -0800 Subject: [PATCH] put some logic into functions --- keygen/src/keygen.rs | 146 +++++++++++++++++++++++++++---------------- 1 file changed, 93 insertions(+), 53 deletions(-) diff --git a/keygen/src/keygen.rs b/keygen/src/keygen.rs index 13cee03559..ffe58d1e92 100644 --- a/keygen/src/keygen.rs +++ b/keygen/src/keygen.rs @@ -30,6 +30,12 @@ use std::{ const NO_PASSPHRASE: &str = ""; +struct GrindMatch { + starts: String, + ends: String, + count: AtomicU64, +} + fn check_for_overwrite(outfile: &str, matches: &ArgMatches) { let force = matches.is_present("force"); if !force && Path::new(outfile).exists() { @@ -123,6 +129,63 @@ fn grind_validator_starts_and_ends_with(v: String) -> Result<(), String> { Ok(()) } +fn grind_print_info(grind_matches: &Vec) { + println!("Searching with {} threads for:", num_cpus::get()); + for gm in grind_matches { + let mut msg = Vec::::new(); + if gm.count.load(Ordering::Relaxed) > 1 { + msg.push("pubkeys".to_string()); + msg.push("start".to_string()); + msg.push("end".to_string()); + } else { + msg.push("pubkey".to_string()); + msg.push("starts".to_string()); + msg.push("ends".to_string()); + } + println!( + "\t{} {} that {} with '{}' and {} with '{}'", + gm.count.load(Ordering::Relaxed), + msg[0], + msg[1], + gm.starts, + msg[2], + gm.ends + ); + } +} + +fn grind_parse_args( + grind_matches: &mut Vec, + starts_with_args: HashSet, + ends_with_args: HashSet, + starts_and_ends_with_args: HashSet, +) { + for sw in starts_with_args { + let args: Vec<&str> = sw.split(':').collect(); + grind_matches.push(GrindMatch { + starts: args[0].to_lowercase(), + ends: "".to_string(), + count: AtomicU64::new(args[1].parse::().unwrap()), + }); + } + for ew in ends_with_args { + let args: Vec<&str> = ew.split(':').collect(); + grind_matches.push(GrindMatch { + starts: "".to_string(), + ends: args[0].to_lowercase(), + count: AtomicU64::new(args[1].parse::().unwrap()), + }); + } + for swew in starts_and_ends_with_args { + let args: Vec<&str> = swew.split(':').collect(); + grind_matches.push(GrindMatch { + starts: args[0].to_lowercase(), + ends: args[1].to_lowercase(), + count: AtomicU64::new(args[2].parse::().unwrap()), + }); + } +} + fn main() -> Result<(), Box> { let matches = App::new(crate_name!()) .about(crate_description!()) @@ -364,12 +427,7 @@ fn main() -> Result<(), Box> { output_keypair(&keypair, &outfile, "recovered")?; } ("grind", Some(matches)) => { - struct Match { - starts: String, - ends: String, - count: AtomicU64, - } - let mut grind_matches = Vec::::new(); + let mut grind_matches = Vec::::new(); let ignore_case = matches.is_present("ignore_case"); @@ -408,53 +466,35 @@ fn main() -> Result<(), Box> { exit(1); } - for sw in &starts_with_args { - let args: Vec<&str> = sw.split(':').collect(); - grind_matches.push(Match { - starts: args[0].to_lowercase(), - ends: "".to_string(), - count: AtomicU64::new(args[1].parse::().unwrap()), - }); - } - for ew in &ends_with_args { - let args: Vec<&str> = ew.split(':').collect(); - grind_matches.push(Match { - starts: "".to_string(), - ends: args[0].to_lowercase(), - count: AtomicU64::new(args[1].parse::().unwrap()), - }); - } - for swew in &starts_and_ends_with_args { - let args: Vec<&str> = swew.split(':').collect(); - grind_matches.push(Match { - starts: args[0].to_lowercase(), - ends: args[1].to_lowercase(), - count: AtomicU64::new(args[2].parse::().unwrap()), - }); - } - - println!("Searching with {} threads for:", num_cpus::get()); - for gm in &grind_matches { - let mut msg = Vec::::new(); - if gm.count.load(Ordering::Relaxed) > 1 { - msg.push("pubkeys".to_string()); - msg.push("start".to_string()); - msg.push("end".to_string()); - } else { - msg.push("pubkey".to_string()); - msg.push("starts".to_string()); - msg.push("ends".to_string()); - } - println!( - "\t{} {} that {} with '{}' and {} with '{}'", - gm.count.load(Ordering::Relaxed), - msg[0], - msg[1], - gm.starts, - msg[2], - gm.ends - ); - } + grind_print_info(&grind_matches); + grind_parse_args( + &mut grind_matches, + starts_with_args, + ends_with_args, + starts_and_ends_with_args, + ); + // println!("Searching with {} threads for:", num_cpus::get()); + // for gm in &grind_matches { + // let mut msg = Vec::::new(); + // if gm.count.load(Ordering::Relaxed) > 1 { + // msg.push("pubkeys".to_string()); + // msg.push("start".to_string()); + // msg.push("end".to_string()); + // } else { + // msg.push("pubkey".to_string()); + // msg.push("starts".to_string()); + // msg.push("ends".to_string()); + // } + // println!( + // "\t{} {} that {} with '{}' and {} with '{}'", + // gm.count.load(Ordering::Relaxed), + // msg[0], + // msg[1], + // gm.starts, + // msg[2], + // gm.ends + // ); + // } let grind_matches_thread_safe = Arc::new(grind_matches); let attempts = Arc::new(AtomicU64::new(1));