Installing and using OpenSSL SHA-256 in Visual C++


SHA-256 is a cryptographic hash function developed by the US. National Security Agency (NSA) as a U.S. Federal Information Processing Standard (FIPS). The SHA acronym stands for Secure Hash Algorithm. A hash function operates on an arbitrary amount of data and returns a fixed-size bit string, the cryptographic hash value.

So why would such an algorithm be useful? Its utility lies in the fact that attempts to change the original data will almost certainly change the hash value returned by the algorithm. An obvious application would be to apply such an algorithm to a file (text or binary) as a means of determining whether or not it has been tampered with.

This post shows you how to use SHA-256 as implemented by the OpenSSL open source project, and use it within Windows / Visual C++ environments to produce digital signatures of strings or files.

Step 1: Download and install OpenSSL

See this web page for the list of all Win32-related implementations of OpenSSL. Specifically they recommend that you download and run the following executable as recommended for software developers by the creators of OpenSSL:

Installing OpenSSL is straightfoward, but here are the screenshots showing the process of installing OpenSSL for Windows:








Step 2: Set up your Visual Studio environment

Here I use Visual Studio 2010, but there is no reason these instructions cannot be applied to earlier or later versions.

In Visual Studio create an empty project. Select File > New > Project. In the Visual C++ section select ‘Empty Project’ and give your project a name eg ‘SHA256’:


Right-click your project folder and select Properties > C/C++ > General.

Update the ‘Additional Include Directories’ section, so that your C++ project implementing the SHA-256 algorithm knows where to find the necessary header files:


Right-click your project folder and select Properties > Linker > Input.

Set the ‘Additional Dependencies’ section to include the ‘libeay32.lib’ library file:


Right-click your project folder and select Properties > Linker > General.

Set the ‘Additional Library Directories’ section to specify the location of the OpenSSL libraries:


Step 3: Try it!

All that remains is to try out the OpenSSL SHA-256 functionality with some sample code.

For this purpose I use some example code found on the StackOverflow site. Some very useful links here:

Sample code as follows:

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include <openssl/sha.h>


std::string sha256( const std::string str )
    unsigned char hash[SHA256_DIGEST_LENGTH];
    SHA256_CTX sha256;
    SHA256_Update(&sha256, str.c_str(), str.size());
	SHA256_Final(hash, &sha256);
	std::stringstream ss;
    for(int i = 0; i 

Giving the following output:


Sample 'Hamlet.txt' text file can be downloaded here.

Leave a Reply