summaryrefslogtreecommitdiffstats
path: root/2015/rs/code/four/src/main.rs
blob: a7d284282b77a8d69edd6c7c15ac0367eaf78ecb (plain) (blame)
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
use md5::{Digest, Md5};
use std::fmt::Write;
use std::io::Write as WriteI;

static INPUT: &[u8] = b"yzbqklnj";

fn main() {
  let mut buf = Vec::with_capacity(INPUT.len() + 10);
  buf.extend_from_slice(INPUT);

  for i in 0.. {
    if i % 1000 == 0 { print!("{i}\r"); let _ = std::io::stdout().flush(); }

    let mut hasher = Md5::new();
    let _ = write!(&mut buf, "{i}");
    hasher.update(buf.as_slice());
    let hash = hasher.finalize();
    if hash.starts_with(&[0, 0]) && hash[2] < 16 {
      let mut hexhash = String::with_capacity(32);
      for digit in hash {
        let _ = write!(&mut hexhash, "{digit:02x}");
      }
      println!("Got 5 digit hash: {hexhash}, at {i}");
      break;
    }
    buf.truncate(INPUT.len());
  }

  for i in 0.. {
    if i % 1000 == 0 { print!("{i}\r"); let _ = std::io::stdout().flush(); }

    let mut hasher = Md5::new();
    let _ = write!(&mut buf, "{i}");
    hasher.update(buf.as_slice());
    let hash = hasher.finalize();
    if hash.starts_with(&[0, 0, 0]) {
      let mut hexhash = String::with_capacity(32);
      for digit in hash {
        let _ = write!(&mut hexhash, "{digit:02x}");
      }
      println!("Got 6 digit hash: {hexhash}, at {i}");
      break;
    }
    buf.truncate(INPUT.len());
  }
}