Advent of Code

aoc201504-crypto.c at [2b99fb3ed7]
Login

aoc201504-crypto.c at [2b99fb3ed7]

File md5mini/aoc201504-crypto.c artifact bcbfee27e0 part of check-in 2b99fb3ed7


#include <openssl/md5.h> // gcc ... -lcrypto
#include <stdio.h>
#include <string.h>

int main(void) {
    char password[99];
    printf("Enter password: ");
    fflush(stdout);
    fgets(password, 99, stdin);
    password[strcspn(password, "\n")] = 0;
    unsigned char hash[MD5_DIGEST_LENGTH];
    union { unsigned char u[120]; char s[120]; } key;

    int n = 1;
    for (;;) {
        size_t len = sprintf(key.s, "%s%d", password, n);
        MD5(key.u, len, hash);
        if ((hash[0] == 0) && (hash[1] == 0) && (hash[2] < 16)) break;
        n++;
    }
    printf("DAY04-PART1: %d\n", n);

    for (;;) {
        size_t len = sprintf(key.s, "%s%d", password, n);
        MD5(key.u, len, hash);
        if ((hash[0] == 0) && (hash[1] == 0) && (hash[2] == 0)) break;
        n++;
    }
    printf("DAY04-PART2: %d\n", n);
    return 0;
}