Created
May 8, 2018 15:47
-
-
Save amcsi/a36df6586cf95cb6f91be14aa71e5e08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate rayon; | |
use rayon::prelude::*; | |
use std::str; | |
use std::ops::Range; | |
use rayon::range::Iter; | |
fn main() { | |
crack("zzzzz"); | |
} | |
struct PasswordToCrack<'a> { | |
password: &'a str, | |
i: u64, | |
} | |
/// Generates a string to crack based on an index seed | |
fn generate_string(mut i: u64) -> Vec<u8> { | |
let mut result = vec![]; | |
if i == 0 { | |
return result; | |
} | |
let radix = 26; | |
const A_DEC: u8 = 97; | |
while i > 0 { | |
let remainder = i % radix; | |
let remainder_zero_shifted = if remainder == 0 { radix } else { remainder }; | |
let m = remainder_zero_shifted - 1; | |
i = (i - remainder_zero_shifted) / radix; | |
result.push(A_DEC + m as u8); | |
} | |
result.into_iter().rev().collect() | |
} | |
struct PasswordIterator { | |
i: u64, | |
} | |
impl PasswordIterator { | |
fn new() -> Self { | |
PasswordIterator { i: 0 } | |
} | |
} | |
impl Iterator for PasswordIterator { | |
type Item = Vec<u8>; | |
fn next(&mut self) -> Option<Vec<u8>> { | |
let i = self.i; | |
self.i += 1; | |
return Some(generate_string(i)); | |
} | |
} | |
//struct PasswordCracker<'a> { | |
// password: &'a str, | |
// password_iterator: PasswordIterator, | |
//} | |
// | |
//impl PasswordCracker { | |
// fn new(password: &str) -> Self { | |
// Self { | |
// password, | |
// password_iterator: PasswordIterator::new(), | |
// } | |
// } | |
//} | |
// | |
//impl Iterator for PasswordCracker { | |
// type Item = Option<Vec<u8>>; | |
// | |
// fn next(&self) -> Option<Option<Vec<u8>>> { | |
// let current_string = self.password_iterator.next(); | |
// if (current_string == self.password) { | |
// return Some(current_string); | |
// } | |
// let i = self.i; | |
// self.i += 1; | |
// return Some(generate_string(i)); | |
// } | |
//} | |
fn crack(password: &str) -> bool { | |
let password_bytes = Vec::from(password); | |
let found_string_index = ((0u64)..).into_par_iter().find_first(|i: u64| -> bool { | |
return &password_bytes == &generate_string(i); | |
}); | |
println!("Found: {}", String::from_utf8(generate_string(found_string_index.unwrap())).unwrap()); | |
true | |
} | |
#[test] | |
fn test_calculate_str_len() { | |
assert_eq!(0, generate_string(0).len()); | |
assert_eq!(1, generate_string(1).len()); | |
assert_eq!(1, generate_string(26).len()); | |
assert_eq!(2, generate_string(27).len()); | |
assert_eq!(2, generate_string(702).len()); | |
assert_eq!(3, generate_string(703).len()); | |
} | |
#[test] | |
fn test_generate_string() { | |
assert_eq!("", str::from_utf8(&generate_string(0)[..]).unwrap()); | |
assert_eq!("a", str::from_utf8(&generate_string(1)[..]).unwrap()); | |
assert_eq!("z", str::from_utf8(&generate_string(26)[..]).unwrap()); | |
assert_eq!("aa", str::from_utf8(&generate_string(27)[..]).unwrap()); | |
assert_eq!("zz", str::from_utf8(&generate_string(702)[..]).unwrap()); | |
assert_eq!("aaa", str::from_utf8(&generate_string(703)[..]).unwrap()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment