I'm writing a program for a class that cracks a 5 character (aA-zZ) password by collecting a hashed password at the command prompt and matching the hash with hashes generated with crypt() function. My problem is adding the 5th loop to crack a 5 character password slows the program to a halt, even for 1-4 character passwords, and I have to cancel. Removing 5th loop cracks 1-4 character passwords in under a minute.
We have only covered the basics in class which is demonstrated in my code. So the solution must match this coding level.
#include <cs50.h>
#include <stdio.h>
#include <crypt.h>
#include <string.h>
void printpass (string fst, string hsh1, string hsh);
/* prompt user for 1 cmd line argument */
int main(int argc, string argv[])
{
/* only allow 1 cmd line argument and return cmd line error
and exit if false */
if (argc != 2)
{
printf("Invalid request!\n");
return 1;
}
string hash = argv[1];
char slt1 = hash[0];
char slt2 = hash[1];
char salt[3] = {slt1, slt2, '\0'};
char alpha[52] = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";//upper & lowercase passwords only
for (int d = 0; d < 52; d++) //search for one character pw
{
char s1 = alpha[d];
char first1[2] = {s1, '\0'};
string hash0 = crypt(first1, salt);
printpass (first1, hash0, hash);
if (strcmp(hash0, hash) != 0)
for (int f = 0; f < 52; f++) //search for two character pw
{
char s2 = alpha[f];
char first2[3] = {s1, s2, '\0'};
string hash1 = crypt(first2, salt);
printpass (first2, hash1, hash);
if (strcmp(hash1, hash) != 0 || strcmp(hash0, hash) != 0)
for (int h = 0; h < 52; h++) //search for two character pw
{
char s3 = alpha[h];
char first3[4] = {s1, s2, s3, '\0'};
string hash2 = crypt(first3, salt);
printpass (first3, hash2, hash);
if (strcmp(hash2, hash) != 0 || strcmp(hash1, hash) != 0 || strcmp(hash0, hash) != 0)
for (int j = 0; j < 52; j++) //search for two character pw
{
char s4 = alpha[j];
char first4[5] = {s1, s2, s3, s4, '\0'};
string hash3 = crypt(first4, salt);
printpass (first4, hash3, hash);
if (strcmp(hash3, hash) != 0 || strcmp(hash2, hash) != 0 || strcmp(hash1, hash) != 0 || strcmp(hash0, hash) != 0)
for (int l = 0; l < 52; l++) //search for two character pw
{
char s5 = alpha[l];
char first5[6] = {s1, s2, s3, s4, s5, '\0'};
string hash4 = crypt(first5, salt);
printpass (first5, hash4, hash);
}
}
}
}
}
}
void printpass (string fst, string hsh1, string hsh) //compare hashes and print if true
{
if (strcmp(hsh1, hsh) == 0)
{
printf("%s", fst);
}
}
1-4 character passwords should complete in under a minute with the 5th loop in the code.