use crate::symbols::combination_count;
use crate::{CrackParameter, CrackTarget};
#[derive(Debug)]
pub(crate) struct InternalCrackParameter<T: CrackTarget> {
crack_param: CrackParameter<T>,
thread_count: usize,
combinations_total: usize,
combinations_p_t: usize,
}
impl<T: CrackTarget> InternalCrackParameter<T> {
pub fn crack_param(&self) -> &CrackParameter<T> {
&self.crack_param
}
pub fn thread_count(&self) -> usize {
self.thread_count
}
pub fn combinations_total(&self) -> usize {
self.combinations_total
}
pub fn combinations_p_t(&self) -> usize {
self.combinations_p_t
}
pub fn hash_matches(&self, input: &str) -> bool {
self.crack_param()
.target_hash_and_hash_fnc
.hash_matches(input)
}
}
impl<T: CrackTarget> From<CrackParameter<T>> for InternalCrackParameter<T> {
fn from(cp: CrackParameter<T>) -> Self {
let combinations_total = combination_count(
cp.basic.alphabet(),
cp.basic.max_length(),
cp.basic.min_length(),
);
let mut thread_count = get_thread_count(cp.basic.fair_mode());
if thread_count > combinations_total {
log::trace!("there are so few combinations to check, that only one thread is used");
thread_count = 1;
}
let combinations_p_t = combinations_total / thread_count;
Self {
crack_param: cp,
thread_count,
combinations_total,
combinations_p_t,
}
}
}
fn get_thread_count(fair_mode: bool) -> usize {
let cpus = num_cpus::get();
if cpus > 1 && fair_mode {
cpus - 1
} else {
cpus
}
}