summaryrefslogtreecommitdiff
path: root/2016/aoc5/src/main.rs
blob: 31bef5e2444fde16e14decc18cec491c74c2ace1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
extern crate md5;

fn main() {
    let room = "reyedfim";

    let mut i = 0;
    let mut found_bytes = 0;

    let mut code: [Option<u8>; 8] = [None;8];

    println!("Cracking the passwordz");
    print_code(&code);
    
    while found_bytes < 8 {
        let room_string = format!("{}{}", room, i);
        let room_bytes = room_string.into_bytes();
        let hash = md5::compute(room_bytes.as_slice());
        if match_hash(hash) {
            let position = hash[2];
            let value = hash[3] / 16;
            if code[position as usize].is_none() {
                code[position as usize] = Some(value);
                print_code(&code);
                found_bytes += 1;
            }
        }
        
        i+=1;
    }

    println!("Password found!");
}

fn match_hash(hash: [u8; 16]) -> bool {
    hash[0] == 0 &&
        hash[1] == 0 &&
        hash[2] < 8
}

fn print_code(code: &[Option<u8>; 8]) {
    println!("");
    for &byte in code.iter() {
        match byte {
            None => {print!("-");},
            Some(x) => {print!("{:x}", x);}
        }
    }
    println!("");
}