Last active
May 16, 2017 21:59
-
-
Save kennethlove/d75cce3bfea15c60fc67bef691739daa to your computer and use it in GitHub Desktop.
This file contains 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
use std::io; | |
fn main() { | |
let mut password = String::new(); | |
let chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 \'\".?!,-_$#@!%^&*()}{=+/\\`~<>"; | |
let mut guess = String::new(); | |
let mut iterations = 0; | |
let mut failed = 0; | |
let mut keep_running = true; | |
println!("What's your password?"); | |
io::stdin() | |
.read_line(&mut password) | |
.expect("Failed to read line"); | |
password = String::from(password.trim_matches('\n')); | |
while keep_running { | |
for char in chars.repeat(2).chars() { | |
let mut tmp_guess = guess.clone(); | |
if failed <= chars.len() { | |
tmp_guess.push(char); | |
} else if failed <= chars.len() * 2 { | |
tmp_guess.insert(0, char); | |
} else { | |
keep_running = false; | |
} | |
match password.find(&tmp_guess) { | |
Some(_) => { | |
guess = tmp_guess; | |
failed = 0 | |
}, | |
None => failed += 1 | |
} | |
if password == guess { | |
keep_running = false; | |
} | |
iterations += 1; | |
} | |
} | |
if password == guess { | |
println!("Your password is {}", guess); | |
println!("It took {} iterations", iterations); | |
} else { | |
println!("Couldn't break your password"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment