I was testing the crypt function, and stumbled on this part which I could not understand:
- If I simply load the salt (50) and the key (rofl), and use crypt to hash the key, it works out fine.
- But then I tried using crypt 1 more time, this time passing in the hash (50fkUxYHbnXGw) and using the key (rofl) as the salt. Now the 2 results are identical. strcmp() indicated that they are the same. But they cannot be. Not only that, but both crypt functions now return a hash that is different from what the hash of "rofl" should be. See the code below. The printf function would return "roMoPGr3r5IJg roMoPGr3r5IJg" and now this is stumping me.
The initial reason for trying this was because I was trying to use crypt to hash 2 different keys using the same salt (50) and the result are identical as well. For instance, I would use 2 keys such as "abc" and "12" and crypt(key1, salt) and crypt(key2, salt) would return identical results. Does someone understand this?
#define _XOPEN_SOURCE
#include <unistd.h>
#include <string.h>
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
int main (void)
{
string salt = "50";
string key = "rofl";
string hash = "50fkUxYHbnXGw";
string code1 = crypt(key,salt);
string code2 = crypt(hash,key);
printf("%s %s \n",code1,code2);
if (strcmp(code1, code2) == 0)
printf("Hm they are equal\n");
return 0;
}