I want my program to extract the first two characters of the given hash hash
. These first two characters represent a nonce/salt that the password was encrypted with (DES-based, crypt()
function). The first two characters of hash
are stored in the array nonceAsArray[]
, which is being passed down to the function concatenateCharacters()
, whose job is to turn these characters into a nonce of type string
and save it in the variable nonce
so that it can be used later on in order to encrypt a password.
The function seems to concatenate the two characters perfectly fine. However, when nonce
is given to the crypt()
function as an argument, it returns null but only, if I calculate both, generatedHash1
and generatedHash2
:
Output:
generatedHash1: 14dJperBYV6zU
generatedHash2: (null)
However, when I exclude the calculation of the first hash string generatedHash1 = crypt("myPassword", "14");
, my program outputs the following:
generatedHash2: dJperBYV6zU
The crypt()
function now seems to have accepted the value that is being stored in nonce
. Another odd thing is that crypt()
returns a hash without the nonce being represented in the first two characters of generatedHash2
. The encrypted password however should be 13 characters long in total.
Fired up the debugger and checked the values that are being stored in nonce
. I stumbled upon this:
nonce: 0x7fffffffdd40 "14"
and
*nonce: 49 '1'
I assume that the first part that starts with 0x7f...
is the memmory address and next to it the value that stored at this address.
Can anyone help me understand as to why the crypt()
function doesn't seem to accept the value in nonce
? I would greatly appreciate if anyone could give me a hint where to look or an explenation as to why it fails.
(...)
#include <cs50.h>
#include <string.h>
(...)
// extract the first two characters of 'hash' (== nonce/salt)
string hash = "14dJperBYV6zU";
char nonceAsArray[2];
for (int i = 0; i < 2; i++)
{
nonceAsArray[i] = hash[i];
}
string nonce = concatenateCharacters(nonceAsArray, 2);
printf("first hash: %s\n", crypt("myPassword", "14"));
printf("second hash: %s\n", crypt("myPassword", nonce));
// connects characters to strings
string concatenateCharacters(char characters[], int arraySize)
{
char terminator[1] = {'\0'};
// create array that can store the password and to which the terminator can be appended (hence +1)
char bigEnoughArray[arraySize + 1];
for (int i = 0; i < arraySize; i++)
{
bigEnoughArray[i] = characters[i];
}
return strcat(bigEnoughArray, terminator);
}