aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--day4.c53
-rw-r--r--day4.go38
2 files changed, 53 insertions, 38 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;
+}
diff --git a/day4.go b/day4.go
deleted file mode 100644
index ebf8810..0000000
--- a/day4.go
+++ /dev/null
@@ -1,38 +0,0 @@
-package main
-
-import (
- "bytes"
- "crypto/md5"
- "fmt"
- "strconv"
-)
-
-func hash(secret []byte, x int64) [16]byte {
- str := strconv.AppendInt(secret, x, 10)
- return md5.Sum(str)
-}
-
-func check(hash [16]byte, target []byte, length int) bool {
- str := []byte(fmt.Sprintf("%x", hash))
- return bytes.Equal(str[:length], target[:length])
-}
-
-func main() {
- secret := []byte("iwrupvqb")
- target := []byte("000000")
- x := int64(1)
- got5, got6 := false, false
-
- for !(got5 && got6) {
- h := hash(secret, x)
- if !got5 && check(h, target, 5) {
- fmt.Println("Lowest number for 5-zeroes hash:", x)
- got5 = true
- }
- if !got6 && check(h, target, 6) {
- fmt.Println("Lowest number for 6-zeroes hash:", x)
- got6 = true
- }
- x += 1
- }
-}