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

  1. Here are the openssl SHA256 sample source code.

    Example #1: sha256_sample1.cpp


    Example #2: sha256_sample2.cpp

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