How to Generate MD5 Hash with C++ and OpenSSL
Indicates only for compatibility with existing applications. In new applications, SHA-1 or RIPEMD-160 should be preferred.
We already published about generating cryptographic hash with the function SHA256 . So, to better understand this article, I recommend you read, if you haven’t already, the other article:
How to Generate SHA256 Hash with C++ and OpenSSL
You will notice many similarities with the previous implementation, the only difference in itself is that the function is different, in this case we will use MD5 .
MD5 is a cryptographically broken but still widely used hash function, producing a 128-bit hash value. While MD5 was initially designed to be used as a cryptographic hash function, it has been found to suffer from extensive vulnerabilities.
It can still be used as a checksum to verify data integrity, but only against unintentional corruption. It remains suitable for other non-cryptographic purposes, for example to determine the partition of a specific key in a partitioned database, and may be preferred due to lower computational requirements than newer secure hashing algorithms.
MD2, MD4 and MD5 are only recommended for compatibility with existing applications. In new applications, SHA-1 or RIPEMD-160 should be preferred.
Installing the OpenSSL library
Before anything you need to install the OpenSSL library, use your distro’s package manager for that, example for systems that use APT:
Note: Generally, most systems already have this library installed.
Using with C++
For this example, we are going to hash the word Terminal Root . Follow the code below:
To compile, run:
The possible and expected output will be:
To check if it is correct, use the
You can simply replace the
DIGEST_LENGTH (although the size is the same in the header there will be another macro), and the function names to use for
MD4 (use the headers
md4. h, respectively, whereas SHA256 uses
sha.h) and use to generate hash for these functions as well.