aboutsummaryrefslogtreecommitdiff
path: root/day4.c
blob: ae3fb943eaeeb483e08a52649459e690ae796ff2 (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
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>

unsigned char* secret = "iwrupvqb";
unsigned char* target = "000000";
unsigned char result[MD5_DIGEST_LENGTH];

void hash(int i) {
  char num[12];
  sprintf(num, "%d", i);
  char str[22] = "";
  strcat(str, secret);
  strcat(str, num);

  MD5(str, strlen(str), result);
}

int tohex() {
  int i, dg = 0;
  for(i = 0; i < 4; i++) {
    dg <<= 8;
    dg += result[i];
  }
  return dg;
}

int check(int len) {
  unsigned char str[8];
  int dg = tohex();
  sprintf(str, "%08x", dg);
  return strncmp(str, target, len) == 0;
}

int main() {
  int i = 1, got5 = 0, got6 = 0;

  while(!(got5 && got6)) {
    hash(i);
    if(!got5 && check(5)) {
      got5 = 1;
      printf("Lowest number for 5-zeroes hash: %d\n", i);
    }
    if(!got6 && check(6)) {
      got6 = 1;
      printf("Lowest number for 6-zeroes hash: %d\n", i);
    }
    i++;
  }

  return 0;
}