This tutorial will guide you on how to hash a string by using OpenSSL’s MD5 hash function. This tutorial will create two C++ example files which will compile and run in Ubuntu environment.

  1. Make sure that we have both OpenSSL & libssl-dev installed in the system.
    ~$ dpkg --get-selections | grep openssl
    openssl     install
    ~$ dpkg --get-selections | grep libssl
    libssl-dev    install
    libssl-doc    install
    libssl1.0.0   install
  2. Install the modules in case there are not available in your system:
    ~$ sudo apt-get install openssl
    ~$ sudo apt-get install libssl-dev
  3. Here are the openssl md5 sample source code.

    Example #1: md5_sample1.cpp

    Example #2: md5_sample2.cpp

  4. Let’s try to compile both sample cpp files and you should observe the following output screenshot.
    ~$ gcc md5_sample1.cpp -o sample1 -lcrypto
    ~$ ./sample1
    md5 digest: 5eb53bbbe01eeed093cb22bb8f5acdc3
    ~$ gcc md5_sample2.cpp -o sample2 -lcrypto
    ~$ ./sample2
    md5 digest: 5eb53bbbe01eeed093cb22bb8f5acdc3
    Note: -lcrypto will include the crypto library from openssl