put some logic into functions

This commit is contained in:
Jon-Eric Cook
2020-01-27 19:53:20 -08:00
committed by Michael Vines
parent 1d32603b49
commit eacd8d986c

View File

@ -30,6 +30,12 @@ use std::{
const NO_PASSPHRASE: &str = ""; const NO_PASSPHRASE: &str = "";
struct GrindMatch {
starts: String,
ends: String,
count: AtomicU64,
}
fn check_for_overwrite(outfile: &str, matches: &ArgMatches) { fn check_for_overwrite(outfile: &str, matches: &ArgMatches) {
let force = matches.is_present("force"); let force = matches.is_present("force");
if !force && Path::new(outfile).exists() { if !force && Path::new(outfile).exists() {
@ -123,6 +129,63 @@ fn grind_validator_starts_and_ends_with(v: String) -> Result<(), String> {
Ok(()) Ok(())
} }
fn grind_print_info(grind_matches: &Vec<GrindMatch>) {
println!("Searching with {} threads for:", num_cpus::get());
for gm in grind_matches {
let mut msg = Vec::<String>::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<GrindMatch>,
starts_with_args: HashSet<String>,
ends_with_args: HashSet<String>,
starts_and_ends_with_args: HashSet<String>,
) {
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::<u64>().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::<u64>().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::<u64>().unwrap()),
});
}
}
fn main() -> Result<(), Box<dyn error::Error>> { fn main() -> Result<(), Box<dyn error::Error>> {
let matches = App::new(crate_name!()) let matches = App::new(crate_name!())
.about(crate_description!()) .about(crate_description!())
@ -364,12 +427,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
output_keypair(&keypair, &outfile, "recovered")?; output_keypair(&keypair, &outfile, "recovered")?;
} }
("grind", Some(matches)) => { ("grind", Some(matches)) => {
struct Match { let mut grind_matches = Vec::<GrindMatch>::new();
starts: String,
ends: String,
count: AtomicU64,
}
let mut grind_matches = Vec::<Match>::new();
let ignore_case = matches.is_present("ignore_case"); let ignore_case = matches.is_present("ignore_case");
@ -408,53 +466,35 @@ fn main() -> Result<(), Box<dyn error::Error>> {
exit(1); exit(1);
} }
for sw in &starts_with_args { grind_print_info(&grind_matches);
let args: Vec<&str> = sw.split(':').collect(); grind_parse_args(
grind_matches.push(Match { &mut grind_matches,
starts: args[0].to_lowercase(), starts_with_args,
ends: "".to_string(), ends_with_args,
count: AtomicU64::new(args[1].parse::<u64>().unwrap()), starts_and_ends_with_args,
}); );
} // println!("Searching with {} threads for:", num_cpus::get());
for ew in &ends_with_args { // for gm in &grind_matches {
let args: Vec<&str> = ew.split(':').collect(); // let mut msg = Vec::<String>::new();
grind_matches.push(Match { // if gm.count.load(Ordering::Relaxed) > 1 {
starts: "".to_string(), // msg.push("pubkeys".to_string());
ends: args[0].to_lowercase(), // msg.push("start".to_string());
count: AtomicU64::new(args[1].parse::<u64>().unwrap()), // msg.push("end".to_string());
}); // } else {
} // msg.push("pubkey".to_string());
for swew in &starts_and_ends_with_args { // msg.push("starts".to_string());
let args: Vec<&str> = swew.split(':').collect(); // msg.push("ends".to_string());
grind_matches.push(Match { // }
starts: args[0].to_lowercase(), // println!(
ends: args[1].to_lowercase(), // "\t{} {} that {} with '{}' and {} with '{}'",
count: AtomicU64::new(args[2].parse::<u64>().unwrap()), // gm.count.load(Ordering::Relaxed),
}); // msg[0],
} // msg[1],
// gm.starts,
println!("Searching with {} threads for:", num_cpus::get()); // msg[2],
for gm in &grind_matches { // gm.ends
let mut msg = Vec::<String>::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 grind_matches_thread_safe = Arc::new(grind_matches);
let attempts = Arc::new(AtomicU64::new(1)); let attempts = Arc::new(AtomicU64::new(1));