this version of crack works for lol, and prints the following:
new hash = 50i2t3sOSAZtktr
[what is the 'tr' at the end???]
lol
for other passwords, the program never returns a decoded pw, just exits.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <crypt.h>
#define _XOPEN_Source
#include <unistd.h>
char get_hash[14]; //this is an array with 14 locations, static allocation, last location is \0; it is a string and it is pointer is get_hash
//crack a password that is up to 5 characters; using the salt, cycle through all the options from 1-5 chars
int main(int argc, char* argv[])
{
//printf ("argc1 = %d\n", argc);
if (argc != 2)
{
printf ("usage: ./crack hash\n");
return (1);
}
if (argc == 2)
{
strcpy(get_hash, argv[1]);
printf("hash being input is = %s\n", get_hash);
// {
// for (int i=0, n=strlen (argv[1]); i<n; i++)
// {
//get_hash[i]=*(argv[1][i];)
//// }
//printf ("get_hash=%str", get_hash))
}
else
{
printf ("usage: ./crack hash\n");
return (1);
}
string testit="NOPE";
string alloptions = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; //everything to be compared
char collect[6]; //string of 6 characters
char salt[3]; salt[0] = get_hash[0]; salt[1] = get_hash[1]; salt[2] = '\0'; get_hash[13]= '\0';//get the salt from the hash that is input; string
//first try all the single letter options
for (int i=0; i<26+26; i++)
{
collect[0] = alloptions[i];
collect[1] = '\0';
//printf("collect[0]= %char=, %d= \n", collect[0], collect[0]);
//printf("collect[1]= %char\n", collect[1]);
string new_hash= crypt(collect, salt);
//printf("new_hash = %str\n ", new_hash);
//printf("get_hash = %str\n", get_hash);
if (strcmp(new_hash, get_hash)==0)
{
printf("%s\n", collect);
printf("get through 1 \n");
return 0;
}
}
printf("get through 1 \n");
//////////////////////////////////////////////////////////////////////////////////
//now try 2 letter passwords
for (int i=0; i<26+26; i++)
{
collect[0] = alloptions[i];
for (int j=0; j<26+26; j++)
{
collect[1]=alloptions[j];
collect[2] = '\0';
char* new_hash= crypt(collect, salt);
if (strcmp(new_hash, get_hash) == 0)
{
printf("%s\n", collect);
return 0;
}
}
}
printf("get through 2 \n");
////////////////////////////////////////////////////////////////
//now try 3 letter passwords
for (int i=0; i<26+26; i++)
{
collect[0] = alloptions[i];
for (int j=0; j<26+26; j++)
{
collect[1] = alloptions[j];
for (int k=0; k<26+26;k++)
{
collect[2] = alloptions[k];
collect[3] = '\0';
//printf("collect[2]= %char=, %d= \n", collect[2], collect[2]);
//printf("collect[3]= %char\n", collect[3]);
char* new_hash = crypt(collect, salt);
//printf("new_hash = %str\n ", new_hash);
printf("new_hash = %str\n", new_hash);
if (strcmp(new_hash, get_hash)==0)
{
printf("%s\n", collect);
return 0;
}
}
}
}
printf("get through 3 \n");
//try 4 letters
for (int i=0; i<26+26; i++)
{
collect[0] = alloptions[i];
for (int j=0; j<26+26; j++)
{
collect[1] = alloptions[j];
for (int k=0; k<26+26;k++)
{
collect[2] = alloptions[k];
for (int l=0; l<26+26;l++)
{
collect[3]= alloptions[l];
collect[4]= '\0';
char* new_hash = crypt(collect, salt);
if (strcmp(new_hash, get_hash)==0 || strcmp(collect, testit)==0)
{
printf("%s\n", collect);
printf("%string\n", new_hash);
printf("%string\n", get_hash);
return 0;
}
}
}
}
}
printf("get through 4 \n");
//////////////////////////////////////////////////////////////////////////////
//try five characters
for (int i=0; i<26+26; i++)
{
collect[0] = alloptions[i];
for (int j=0; j<26+26; j++)
{
collect[1] = alloptions[j];
for (int k=0; k<26+26;k++)
{
collect[2] = alloptions[k];
for (int l=0; l<26+26;l++)
{
collect[3]= alloptions[l];
for (int m=0; l<26+26;l++)
{ collect[4]=alloptions[m];
collect[5]= '\0';
char* new_hash = crypt(collect, salt);
if (strcmp(new_hash, get_hash)==0)
{
printf("%s\n", collect);
return 0;
}
}
}
}
}
}
printf("no luck\n");
printf("get through 5\n");
return 1;
}