My code takes like 1-2 minutes to execute, and the result is always Killed
. Here is my code:
#include <cs50.h>
#include <stdio.h>
#include <crypt.h>
#include <string.h>
string crack(string hashed);
string deleteAll(string s, char target);
// Deals with input and stuff and feeds info into functions.
int main(int argc, string argv[])
{
// Returns 1 because this is an error (because there is no hashed pw).
if (argc != 2)
{
printf("Usage: ./crack hash\n");
return 1;
}
else
{
// Makes crack do all the 'hard work' of guess/checking.
string pw = crack(argv[1]);
// Prints the pw.
printf("%s\n", pw);
return 0;
}
}
// Does all the 'hard work'.
string crack(string hashed)
{
// A string is a list of chars, so this is easier than doing characters[0] = ' '; ...
string characters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
// A temporary variable that is useful for guessing.
temp = malloc(5),
// The salt for crypt() (salt will be defined in next two lines).
salt = malloc(2),
// The output.
out = malloc(5);
salt[0] = hashed[0];
salt[1] = hashed[1];
// Length will be used in the for loops in the next lines.
int length = (int) strlen(characters);
for (int a = 0; a < length; a++)
{
for (int b = 0; b < length; b++)
{
for (int c = 0; c < length; c++)
{
for (int d = 0; d < length; d++)
{
for (int e = 0; e < length; e++)
{
// Sets temp to characters[a] characters[b]...characters[e].
temp[0] = characters[a];
temp[1] = characters[b];
temp[2] = characters[c];
temp[3] = characters[d];
temp[4] = characters[e];
// Deletes spaces in temp.
temp = deleteAll(temp, ' ');
// Checks if it is the right password.
if (crypt(temp, salt) == hashed)
{
out = temp;
}
}
}
}
}
}
return out;
}
string deleteAll(string s, char target)
{
// Gets a string length.
int length = (int) strlen(s), count = 0;
// Sets the length of the output to the length of the input string (which is faster than calculating).
string out = malloc(length);
// Scans the input string and checks for target.
for (int i = 0; i < length; i++)
{
if (!(s[i] == target))
{
out[count] = s[i];
count++;
}
}
return out;
}
Is there something wrong with it?
--EDIT--
New code:
#include <cs50.h>
#include <stdio.h>
#include <crypt.h>
#include <string.h>
string crack(string hashed);
string deleteAll(string s, char target);
// Deals with input and stuff and feeds info into functions.
int main(int argc, string argv[])
{
// Returns 1 because this is an error (because there is no hashed pw).
if (argc != 2)
{
printf("Usage: ./crack hash\n");
return 1;
}
else
{
// Makes crack do all the 'hard work' of guess/checking.
string pw = crack(argv[1]);
// Prints the pw.
printf("%s\n", pw);
return 0;
}
}
// Does all the 'hard work'.
string crack(string hashed)
{
// A string is a list of chars, so this is easier than doing characters[0] = '\0'; ...
string characters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
// A temporary variable that is useful for guessing.
temp = malloc(5),
// The salt for crypt() (salt will be defined in next two lines).
salt = malloc(3),
// The output.
out = malloc(5);
salt[0] = hashed[0];
salt[1] = hashed[1];
salt[2] = '\0';
// Length will be used in the for loops in the next lines.
int length = (int) strlen(characters),
c1 = 0, // c1-4 are counters for the while loops.
c2 = 0,
c3 = 0,
c4 = 0;
// These things check if each loop is done.
bool done1 = false,
done2 = false,
done3 = false,
done4 = false;
while (!done1)
{
while (!done2)
{
while (!done3)
{
while (!done4)
{
// Sets temp to the current pw (the loops guess in the following order: a, b, c, d, e..., Z, aa, ab, ac...zz, Aa, ...).
temp[0] = characters[c1];
temp[1] = characters[c2];
temp[2] = characters[c3];
temp[3] = characters[c4];
temp[4] = '\0';
temp = deleteAll(temp, ' ');
// Checks if the pw is the correct one.
if (strcmp(crypt(temp, salt), hashed) == 0)
{
out = temp; // Sets out to temp, and exits the loops
done1 = true;
done2 = true;
done3 = true;
done4 = true;
}
if ((c1 == length - 1) & // Sees if it should stop.
(c2 == length - 1) &
(c3 == length - 1) &
(c4 == length - 1))
{
out = "Unknown hash"; // Sets out to temp, and exits the loops
done1 = true;
done2 = true;
done3 = true;
done4 = true;
}
c4++;
}
c3++;
}
c2++;
}
c1++;
}
return out;
}
string deleteAll(string s, char target)
{
// Gets a string length.
int length = (int) strlen(s), count = 0;
// Sets the length of the output to the length of the input string (which is faster than calculating).
string out = malloc(length);
// Scans the input string and checks for target.
for (int i = 0; i < length; i++)
{
if (!(s[i] == target))
{
out[count] = s[i];
count++;
}
}
return out;
}