This tutorial will guide you on how to hash a string by using OpenSSL’s HMAC hash function. HMAC hashing allow user to hash with a secret key. HMAC can take most of the hash engine in order to hash your data with the secret key. This tutorial will create two C++ example files which will compile and run in Ubuntu environment.

  1. Here are the openssl HMAC sample source code.

    Example #1: hmac_sample1.cpp

    Example #2: hmac_sample2.cpp

  2. Let’s try to compile both sample cpp files and you should observe the following output screenshot.
    ~$ gcc hmac_sample1.cpp -o sample1 -lcrypto
    ~$ ./sample1
    HMAC digest: e19e220122b37b708bfb95aca2577905acabf0c0
    ~$ gcc hmac_sample2.cpp -o sample2 -lcrypto
    ~$ ./sample2
    HMAC digest: e19e220122b37b708bfb95aca2577905acabf0c0
    Note: -lcrypto will include the crypto library from openssl