How can I improve this hash matching program using either abstraction or search/sort algorithms?
We briefly were introduced to searching methods such as bubble sort and merge sort, but I'm not sure how they can be implemented to improve my code.
The program takes a command-line hash input, compares it against generated hashes, and if it finds a match, prints out the key used to generate the matched hash.
(This is part of Harvard's CS50 on eDx Week 2 Problem Set)
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crypt.h>
#define _XOPEN_SOURCE
#include <unistd.h>
/*
* Goal: Program that takes cmd ln arg of password hash
* and compares it to program generated hashes until a
* match is found.
* Assumptions: password is max 5 chars && all chars alpha
*/
bool crack(string hash);
int main(int argc, string argv[])
{
// Check for 2 command line arguments
if (argc != 2)
{
printf("[ERROR 1] Usage: ./crack hash\n");
return 1;
}
string hash = argv[1];
if (!crack(hash))
{
printf("[ERROR 2] Could not crack! \n");
return 1;
}
}
bool crack(string hash)
{
string alphas = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char key[6];
char salt[3];
salt[0] = hash[0];
salt[1] = hash[1];
salt[2] = '\0';
// 1 char pw test
for (int i = 0; i < strlen(alphas); i++)
{
// Iterate over alpha chars to generate test key
key[0] = alphas[i];
key[1] = '\0';
// Create new test hash
string crackhash = crypt(key, salt);
// Compare hashes
if(strcmp(crackhash, hash) == 0)
{
printf("%s \n", key); // Array of chars is a string
return true;
}
}
// 2 char pw test
for (int i = 0; i < strlen(alphas); i++)
{
key[0] = alphas[i];
for (int j = 0; j < strlen(alphas); j++)
{
key[1] = alphas[j];
key[2] = '\0';
char* crackhash = crypt(key, salt);
if(strcmp(crackhash, hash) == 0)
{
printf("%s \n", key);
return true;
}
}
}
// 3 char pw test
for (int i = 0; i < strlen(alphas); i++)
{
key[0] = alphas[i];
for (int j = 0; j < strlen(alphas); j++)
{
key [1] = alphas[j];
for (int k = 0; k < strlen(alphas); k++)
{
key[2] = alphas[k];
key[3] = '\0';
char* crackhash = crypt(key, salt);
if(strcmp(crackhash, hash) == 0)
{
printf("%s \n", key);
return true;
}
}
}
}
// 4 char pw test
for (int i = 0; i < strlen(alphas); i++)
{
key[0] = alphas[i];
for (int j = 0; j < strlen(alphas); j++)
{
key [1] = alphas[j];
for (int k = 0; k < strlen(alphas); k++)
{
key [2] = alphas[k];
for (int l = 0; l < strlen(alphas); l++)
{
key[3] = alphas[l];
key[4] = '\0';
char* crackhash = crypt(key, salt);
if(strcmp(crackhash, hash) == 0)
{
printf("%s \n", key);
return true;
}
}
}
}
}
// 5 char pw test
for (int i = 0; i < strlen(alphas); i++)
{
key[0] = alphas[i];
for (int j = 0; j < strlen(alphas); j++)
{
key [1] = alphas[j];
for (int k = 0; k < strlen(alphas); k++)
{
key [2] = alphas[k];
for (int l = 0; l < strlen(alphas); l++)
{
key[3] = alphas[l];
for (int m = 0; m < strlen(alphas); m++)
{
key[4] = alphas[m];
key[5] = '\0';
char* crackhash = crypt(key, salt);
if(strcmp(crackhash, hash) == 0)
{
printf("%s \n", key);
return true;
}
}
}
}
}
}
return false;
}