aboutsummaryrefslogtreecommitdiff
path: root/day4.c
diff options
context:
space:
mode:
authorNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-12-04 18:20:40 +0000
committerNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-12-04 18:20:40 +0000
commit05901e23db671e76824aa68de011920f57ffb38e (patch)
tree2e4b13408fcf4a9b8edbc35862471c5ab0b090b6 /day4.c
parenta6702d7d72a293694d6d7180c446596cda08808c (diff)
Made day4 C. Yay!
Diffstat (limited to 'day4.c')
-rw-r--r--day4.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/day4.c b/day4.c
new file mode 100644
index 0000000..ae3fb94
--- /dev/null
+++ b/day4.c
@@ -0,0 +1,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;
+}