#include <mhash.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
#include <math.h>

#define PASSLEN 5

typedef union {
    unsigned char md[MD5_DIGEST_LENGTH];
    unsigned int k[MD5_DIGEST_LENGTH/sizeof(int)];
  } hash_t;

// MD5("hello")
hash_t pass_hash = {0x5d, 0x41, 0x40, 0x2a, 0xbc, 0x4b, 0x2a, 0x76, 0xb9, 0x71, 0x9d, 0x91, 0x10, 0x17, 0xc5, 0x92};

// Possible password symbols
char symbols[] = "abcdefghijklmnopqrstuvwxyz*#";

// hash_t pass_hash = {0xa5, 0x42, 0xc2, 0x2b, 0x90, 0xf5, 0xf0, 0x11, 0x9d, 0xca, 0x3d, 0x9c, 0x72, 0x22, 0x1e, 0x27};

// char symbols[] = "ab";

char password[PASSLEN+1];

int testpass(char *password);
int xtoi(char c);

int main(int argc, char *argv[])
{
	char *hashstring;
    int i, j, k, l, m, loops, found;
	int nsymbols = strlen(symbols);
	unsigned long npasswords = pow(nsymbols, PASSLEN);

	if (argc > 1) {
		hashstring = argv[1];
		for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
			// Converts ascii characters to MD5 bytes 
			pass_hash.md[i] = (xtoi(hashstring[2*i]) << 4) + xtoi(hashstring[2*i+1]);
		}
	}
	printf("Password Hash: ");
	for (i=0; i < MD5_DIGEST_LENGTH; i++)
      printf("%02x", pass_hash.md[i]);
	printf("\n");
	
	printf("Using %d Password Characters: ", nsymbols);
	for (i=0; i < nsymbols; i++)
      printf("%c", symbols[i]);
	printf("\n");
	
	// Generate all possible passwords
	printf("\nGenerating all possible %lu passwords...\n", npasswords);
	loops = 0;
	found = 0;
	// This is the part to parallelize
	for (i = 0;  i < nsymbols && !found; i++)
		for (j = 0;  j < nsymbols && !found; j++)
			for (k = 0;  k < nsymbols && !found; k++)
				for (l = 0;  l < nsymbols && !found; l++)
					for (m = 0;  m < nsymbols && !found; m++) {
					// Generates password string from all possible characters
						password[0] =  symbols[i];
						password[1] =  symbols[j];
						password[2] =  symbols[k];
						password[3] =  symbols[l];
						password[4] =  symbols[m];
						password[5] =  0;
						loops++;
						if (testpass(password) == 0) {
							printf("Found password: %s after %d iterations\n", password, loops);		
							found = 1;
						}
					}
	exit(0);
}

int testpass(char *password)
{
    MHASH td;
	hash_t hash;
	int i;

	// Calculate password hash and compare
	// td = mhash_init(MHASH_MD5);
	// if (td == MHASH_FAILED)
		// exit(1);
	// mhash(td, password, PASSLEN);
	// mhash_deinit(td, hash.md);
	
    MD5((unsigned char *)password, PASSLEN, hash.md);

	// Compare hashes
	int diff = pass_hash.k[0] ^ hash.k[0];
	for (i = 1; i < MD5_DIGEST_LENGTH/sizeof(int); i++) {
		diff |= pass_hash.k[i] ^ hash.k[i];
	}
	if (diff == 0)
		printf("\nMatch\n");
	return diff;
}

// Converts ascii hexadecimal to integer
int xtoi(char c)
{
	int result;
	
	switch (c) {
	case 'f':
		result = 15;
		break;
	case 'e':
		result = 14;
		break;
	case 'd':
		result = 13;
		break;
	case 'c':
		result = 12;
		break;
	case 'b':
		result = 11;
		break;
	case 'a':
		result = 10;
		break;
	case '9':
		result = 9;
		break;
	case '8':
		result = 8;
		break;
	case '7':
		result = 7;
		break;
	case '6':
		result = 6;
		break;
	case '5':
		result = 5;
		break;
	case '4':
		result = 4;
		break;
	case '3':
		result = 3;
		break;
	case '2':
		result = 2;
		break;
	case '1':
		result = 1;
		break;
	case '0':
		result = 0;
	}
	return result;
}