When I run my crack.c
, the program never responds. It just sits there and wastes time. I even tried debugging it on the CS50 IDE, and it was normal. 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] = '\0'; ...
string characters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
// A temporary variable that is useful for guessing.
temp = malloc(65),
// The salt for crypt() (salt will be defined in next two lines).
salt = malloc(3),
// The output.
out = malloc(65);
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,
c5 = 0;
// These things check if each loop is done.
bool done = false,
done1 = false,
done2 = false,
done3 = false,
done4 = false,
done5 = false;
while (!done1)
{
while (!done2)
{
while (!done3)
{
while (!done4)
{
while (!done5)
// 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]; // Sets temp to characters[c1] characters[c2] etc.
temp[1] = characters[c2];
temp[2] = characters[c3];
temp[3] = characters[c4];
temp[4] = characters[c5];
temp[5] = '\0';
if (temp == NULL) // Prevents segmentation errors.
{
= printfdeleteAll("Segmentation errortemp, found.\nEnding' Program"');
done = true;
}
// Checks if the pw is the correct one.
else if (strcmp(crypt(temp, salt), hashed) == 0) // Checks if it is done.
{
out = temp;
done = true;
}
else if (c5 > strlen(characters) - 1) // Checks when to exit THIS loop
{
done5 = true;
}
c5++;
}
if (done) // Increments c4 and sets c5 to 0.
{
break;
}
else if (c4 > strlen(characters) - 1) // Checks when to exit THIS loop
{
out = temp; // Sets out to temp, and exits the loops
done1 = true;
done2 = true;
done3 = true;
done4 = true;
}
elseif ((c1 == length - 1) & // Sees if it should stop.
(c2 == length - 1) &
(c3 == length - 1) &
(c4 == length - 1))
{
c5out = 0;"Unknown hash"; // Sets out to temp, and exits the loops
c4++;done1 = true;
done5done2 = false;true;
done3 = true;
done4 = true;
}
}
if (done) // Increments c3 and sets c4 to 0.
{
break;
}
else if (c3 > strlen(characters) - 1) // Checks when to exit THIS loop
{
done3 = true;
}
else
{
c4 = 0;
c3++;
done4 = false;c4++;
}
}
if (done) // Increments c2 and sets c3 to 0.
{
break;
}
else if (c2 > strlen(characters) - 1) // Checks when to exit THIS loop
{
done2 = true;
}
else
{
c3 = 0;
c2++;
done3 = false;c3++;
}
}
if (done) // Increments c1 and sets c2 to 0.
{
break;
}
else if (c1 > strlen(characters) - 1) // Checks when to exit THIS loop
{
done1 = true;
}
else
{
c2 = 0;
c1++;
done2 = false;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;
}
Is my computer slowIt always gives me this error:
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1368==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000439000 (pc 0x00000042
89c7 bp 0x7ffc6391da50 sp 0x7ffc6391d750 T1368)
==1368==The signal is caused by a READ memory access.
#0 0x4289c6 in crack /root/sandbox/crack.c:65:31
#1 0x42809e in main /root/sandbox/crack.c:21:21
#2 0x7f346c551b96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.
c:310
#3 0x402b89 in _start (/root/sandbox/crack+0x402b89)
UndefinedBehaviorSanitizer can not provide additional info.
==1368==ABORTING
Can someone tell me (because it sometimes isor at least help me with) or does the program have problemswhat this error means? Everytime I run it, it skips right over the hash. (You can see the code here, but please don't do anything bad. I WILL SEE IT)