存在しうる全ての値を計算する。
桁数が増えれば増えるほど、処理時間がかかる。6桁で約1秒(1000ms)くらいかかる。
use sha2::{Digest, Sha256};
use std::time;
fn main() {
let password: String = "13225".to_string();
let length = password.chars().count() as u32;
let crypto_hash: String = get_crypto_hash(password);
crack_password(crypto_hash, length);
}
fn get_combinations(length: u32) -> Vec<String> {
let mut combinations: Vec<String> = Vec::new();
let min_number = 0;
let max_number = 10_i32.pow(length);
for i in min_number..max_number {
let str_num: String = i.to_string();
let zeros: String = "0".repeat((length - str_num.chars().count() as u32).try_into().unwrap());
combinations.push(format!("{}{}", zeros, str_num));
}
return combinations;
}
fn get_crypto_hash(password: String) -> String {
let sha = Sha256::digest(password);
hex::encode(sha).to_string()
}
fn check_password(expected_crypto_hash: String, possible_password: String) -> bool {
let actual_crypto_hash = get_crypto_hash(possible_password);
return expected_crypto_hash == actual_crypto_hash
}
fn crack_password(crypto_hash: String, length: u32) {
println!("Processing number combinations sequentially");
let start_time = time::Instant::now();
let combinations: Vec<String> = get_combinations(length);
for combination in combinations {
if check_password(crypto_hash.clone(), combination.clone()) {
println!("PASSWORD CRACKED:{}", combination);
break;
}
}
println!("PROCESS TIME: {:?}", start_time.elapsed());
}
Compiling parallel v0.1.0 (/home/vagrant/dev/rust/parallel)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.16s
Running `target/debug/parallel`
Processing number combinations sequentially
PASSWORD CRACKED:13225
PROCESS TIME: 97.760443ms