I'm trying to get crack.c
to print out the key used in the crypt()
function if the output from crypt()
is the same as the user-inputted hash. To test this out, I've pre-generated a hash to use in testing.
crypt("a", "AA")
returns the hash AAa6CzJlsalyo
.
My code is:
#define _XOPEN_SOURCE
#include <stdio.h>
#include <cs50.h>
#include <unistd.h>
#include <string.h>
int main(int argc, string argv[])
{
if(argc != 2)
{
printf("Usage: ./crack hash\n");
return 1;
}
string cracked = " ",
keyrange = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char salt[] = " ";
salt[0] = argv[1][0];
salt[1] = argv[1][1];
char onelen[] = "A";
for(int i = 0, n = strlen(keyrange); i < n; i++)
{
onelen[0] = keyrange[i];
cracked = crypt(onelen, salt);
printf("key: %s | salt: %s | hash: %s\n", onelen, salt, cracked); //used for debugging purposes
if(cracked == argv[1])
{
break;
}
}
printf("%s\n", onelen);
}
It works when I run it, it checks for every possible combination with a specific salt. My problem is that the for
loop does not terminate when the condition cracked == argv[1]
is true.
What am I doing wrong?