I found a working example in which the correct MD5 sum of the text 'hoi' is printed. Now I'm trying to put it into a function but I can't get it to output the right MD5 sum.
The following code is what I made:
void md5_string (const char *input) {
unsigned char digest[MD5_DIGEST_LENGTH];
MD5((unsigned char*)&input, strlen(input), (unsigned char*)&digest);
char mdString[33];
for(int i = 0; i < 16; i++)
sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
printf("md5 digest: %s\n", mdString);
}
but it gives: 7165f036e29c8043961ab1eb606302f5 as output.
The correct output is given with the code below as well as in Bash with printf "hoi" | md5sum
unsigned char digest[MD5_DIGEST_LENGTH];
char string1[] = "hoi";
MD5((unsigned char*)&string1, strlen(string1), (unsigned char*)&digest);
char mdString[33];
for(int i = 0; i < 16; i++)
sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
printf("md5 digest: %s\n", mdString);
it gives: 4216455ceebbc3038bd0550c85b6a3bf
I am sure it has something to do with my pointer wisdom or trailing \0 characters but I can't get it right. Can anybody help me with this?