0

I'm working on crack and have been messing around with strcmp(), but something isn't working as expected. When I print s (with argv[1] != "hello") it returns the right value, but somehow between the first printf and the second printf the value of s is changed to t, so that strcmp(s, t) is equal to zero and 'true' is printed.

// Test chars and ints
#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <cs50.h>
#include <string.h>

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Type in a password.\n");
        return 1;
    }
    else
    {
        string salt = "50";
        string input = argv[1];
        string s = crypt(input, salt);
        printf("%s\n", s);
        string t = crypt("hello", salt);
        printf("%s\n", t);

        if (strcmp(s, t) == 0)
        {
            printf("true\n");
        }
        else
        {
            printf("shit\n");
        }
    }
}
0

crypt reuses the memory. So you'd have to copy the string to another memory location, like

char s[14]; // 13 characters in hash plus null terminator
strcpy(s, crypt(input, salt));   // there it's safe
string t = crypt("hello", salt); // that one isn't, but it's last crypt call

But in crack's implementation, there's no need for two crypt results stored at once.

| improve this answer | |
  • Great explanation! – user20879 Jan 15 '18 at 16:37

You must log in to answer this question.