This tutorial will guide you on how to hash a string by using OpenSSL’s MD4 hash function. This tutorial will create two C++ example files which will compile and run in Ubuntu environment.
- Here are the openssl MD4 sample source code.
Example #1: md4_sample1.cpp1234567891011121314151617181920#include <stdio.h>#include <string.h>#include <openssl/md4.h>int main(){unsigned char digest[MD4_DIGEST_LENGTH];char string[] = "hello world";MD4((unsigned char*)&string, strlen(string), (unsigned char*)&digest);char mdString[33];for(int i = 0; i < MD4_DIGEST_LENGTH; i++)sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);printf("md4 digest: %s\n", mdString);return 0;}
Example #2: md4_sample2.cpp123456789101112131415161718192021#include <stdio.h>#include <string.h>#include <openssl/md4.h>int main() {unsigned char digest[MD4_DIGEST_LENGTH];const char* string = "hello world";MD4_CTX ctx;MD4_Init(&ctx);MD4_Update(&ctx, string, strlen(string));MD4_Final(digest, &ctx);char mdString[33];for (int i = 0; i < MD4_DIGEST_LENGTH; i++)sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);printf("md4 digest: %s\n", mdString);return 0;} - Let’s try to compile both sample cpp files and you should observe the following output screenshot.
Note: -lcrypto will include the crypto library from openssl~$ gcc md4_sample1.cpp -o sample1 -lcrypto
~$ ./sample1
md4 digest: aa010fbc1d14c795d86ef98c9547d17
~$ gcc md4_sample2.cpp -o sample2 -lcrypto
~$ ./sample2
md4 digest: aa010fbc1d14c795d86ef98c9547d17
OpenSSL MD4 Hashing Example in C++