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.