MD4 openssl core dumped

192 Views Asked by At

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;
}
1

There are 1 best solutions below

0
On

You are passing a NULL pointer to EVP_Digest as the output length variable. You need to do the following:

unsigned int digestLen;
EVP_Digest(string, strlen(string), digest, &digestLen, EVP_md4(), NULL);

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>.