extern crate crypto_hash; extern crate hex; extern crate num_cpus; use crypto_hash::{hex_digest, Algorithm}; use std::sync::mpsc::{self, TryRecvError}; use std::thread; fn main() { let target = "01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b"; let core_count = num_cpus::get() as u32; let increment: u32 = 0xffffffff / core_count; let mut threads = Vec::new(); for thread_index in 0..core_count { let (tx, rx) = mpsc::channel(); threads.push(( thread::spawn(move || { println!( "Spawned thread #{:?} with characters {:?} through {:?}\ntarget is {:?}", thread_index, increment * thread_index, increment * (thread_index + 1), target ); for x in increment * thread_index..increment * (thread_index + 1) { match rx.try_recv() { Ok(_) | Err(TryRecvError::Disconnected) => { println!("Terminating."); break; } Err(TryRecvError::Empty) => {} } let byte_vec = trim_bytes(x); let hash = hex_digest(Algorithm::SHA256, byte_vec.as_slice()); if hash == target { println!( "{:?} - {:?}", hex::encode(x.to_be_bytes()), hex_digest(Algorithm::SHA256, trim_bytes(x).as_slice()) ); break; } } }), tx, )); } loop { for thread in threads { match thread.1.try_recv() { Ok(_) => { for thread in threads { break; } } Err(_) => {} } } is_first_loop = false; } } fn trim_bytes<'a>(x: u32) -> Vec { let full_byte_array = x.to_be_bytes(); let mut v: Vec = Vec::new(); let mut has_encountered_value = false; for byte in &full_byte_array { if has_encountered_value { v.push(*byte); } else { if *byte != 0u8 { v.push(*byte); has_encountered_value = true; } } } v }