1

So, this isn't the full solution to the problem, I'm currently just messing around with crypt, I've got the following code:

#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("Usage: ./crack <password>\n");
        return 1;
    }

    string password = "getting!";
    string password_hashed = crypt(password, "ge"); //assume that we only know of this

    printf("Testing with: getting! : %s\n", password_hashed);

    string dict[7] = {"bobb", "test1", "test3", "something", "somethingelse", "whatever", "getting!"};

    for(int i = 0; i < 7; i++) {

        char salt[3] = {dict[i][0], dict[i][1], '\0'};

        string currentHash = crypt(dict[i], salt);
        printf("Current iteration: %s : %s : %s ---------- ", dict[i], salt, currentHash);
        if(strcmp(password_hashed, currentHash) == 0) {
            printf("MATCH\n");
        }
        else {
            printf("NO MATCH\n");
        }
    }
}

I have a user-defined password, that I calculate the hash of, using the first 2 characters as the salt, and I store the hash as a string inside of password_hased.

Then I have a user-defined array of strings (dict) that I want to test the hash against.

However, my problem is that strcmp() (inside of the for loop) always return 0 (declaring the hashes to be the same).

So, the output ends up being:

Testing with: getting! : geB7qvbbFTmjU
Current iteration: bobb : bo : bo7p/Imwi7yBw ---------- MATCH
Current iteration: test1 : te : tecdIjhx8TVq. ---------- MATCH
Current iteration: test3 : te : teWtwbeIm3BdA ---------- MATCH
Current iteration: something : so : sonH/h2hpGtHk ---------- MATCH
Current iteration: somethingelse : so : sonH/h2hpGtHk ---------- MATCH
Current iteration: whatever : wh : whL2U/6/5wh4Y ---------- MATCH
Current iteration: getting! : ge : geB7qvbbFTmjU ---------- MATCH

While only the last line should've been a MATCH. I tried hardcoding the hash values of "bobb" and "getting!" at the beginning of main and comparing them with strcmp and the result isn't 0, so the problem is certainly inside of my for loop.

Any help'd be appreciated. Thanks.

1

When you read the man page for crypt, you maybe missed this:

The return value points to static data whose content is overwritten by each call.

Each time you call crypt, the static string that password_hash points to is being changed to the new return value, and both strings are pointing to the same static value.

You can see this if you change your print line to include it:

        printf("Current iteration: %s: %s : %s : %s --- ", password_hashed, dict[i], salt, currentHash);
2
  • Thanks! How am I then able to compare two different hashes in one program?
    – Bogdan M.
    Nov 13 '17 at 0:00
  • You shouldn't need to compare 2 hashes that were both from crypt because per the spec, the first hash is given to you by the user on the command line. Alternatively, you can use strcpy to copy the value to a new string.
    – curiouskiwi
    Nov 13 '17 at 0:23

You must log in to answer this question.

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