I recently tried to do a MD4 hashing with openssl in C: MD4 hash with openssl, save result into char array. I would like to do it again but now using EVP_Digest
. But with this code Im getting core dumped - why?
#include <string.h>
#include <stdio.h>
#include <openssl/md4.h>
int main()
{
unsigned char digest[MD4_DIGEST_LENGTH];
char string[] = "hello world";
EVP_Digest(string, strlen(string), digest, NULL, EVP_md4(), NULL);
char mdString[MD4_DIGEST_LENGTH*2+1];
int i;
for( i = 0; i < MD4_DIGEST_LENGTH; i++)
sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
printf("md4 digest: %s\n", mdString);
return 0;
}
You are passing a
NULL
pointer toEVP_Digest
as the output length variable. You need to do the following:Even if you don't use the output length (you should rather than relying on a constant), you still need to give a valid memory location for the
EVP_Digest
function to write the size value to.Also, you should
#include <openssl/evp.h>
.