0

Can't totally get a thing. My code was accepted and the score is 5 out of 5, however it does't work properly. After searching through the forum I inderstood that I should use strcmp() for comparing strings instead of direct comparation while (hash2 != hash). Also I had a lot of garbage in output. So my question is why that has happened. Is it a bug or so?

#include <cs50.h>
#include <stdio.h>
#include <crypt.h>
#include <string.h>

void CharIncrement(string pass, int depth);

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./crack hash");
        return 1;
    }
    string hash = argv[1];
    char salt[] = "AA";
    salt[0] = hash[0];
    salt[1] = hash[1];
//    printf("Salt: %s\n", salt);
    char pass[] = {'A', '\0', '\0', '\0', '\0'};
    while (crypt(pass, salt) != hash/* && pass[1] == '\0'*/)
    {
        CharIncrement(pass, 0);
//        printf("pass: %s, hash: %s\n", pass, crypt(pass, salt));
    }
    printf("%s\n", pass);
    return 0;
}

void CharIncrement(char pass[], int depth)
{
    if ((pass[depth] >= 'A' && pass[depth] < 'Z') || (pass[depth] >= 'a' && pass[depth] < 'z'))
    {
        // increment the character
        pass[depth]++;
        return;
    }
    else if (pass[depth] == 'Z')
    {
        pass[depth] = 'a';
        return;
    }
    else
    {
        pass[depth] = 'A';
        if (pass[depth + 1] == '\0')
        {
            pass[depth + 1] = 'A';
            return;
        }
        else
        {
            CharIncrement(pass, depth + 1);
            return;
        }
    }
}
1

There is no automated grading for crack so if you submit a solution, we expect that you will have tested it and it works per the spec. As such, you will get 5/5 upon submission.

1
  • Thanks for this answer. Now I know. Not sure it is best decision though. May be at least three tests would be better than nothing... – ArMANIAK Feb 23 '19 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .