From 05901e23db671e76824aa68de011920f57ffb38e Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Fri, 4 Dec 2015 18:20:40 +0000 Subject: Made day4 C. Yay! --- day4.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ day4.go | 38 -------------------------------------- 2 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 day4.c delete mode 100644 day4.go 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 +#include +#include +#include + +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 - } -} -- cgit v1.2.1