One Way Hash functions — OpenSSL

Hash or Fingerprint generation functions are always an interesting Chapter in Cryptography as they are the basics of the most cryptographic protocols.
Definition for a Cryptographic Hash Functions
Let us consider that M is the message and h is it’s hash value after applying the hash function H. Or, it can be stated mathematically as: H (M) :=h. Where H will also satisfy the following characteristics.
  1. Given M, it is easy to compute h
  2. Given h, it is hard to compute M such that H(M) = h
  3. Given M, it is hard to find another messag, M1 , such that H(M) = H(M1)

SHA-*, MD* and RIPEMD-* are the most popular Hash functions. OpenSSL provides both generic APIs to these Hash functions also provides direct APIs. Accessing through generic API (the EVP) is preferred.

Now let us first use the Hash APIs from OpenSSL and generate fingerprint or calculate hash value. The alorithm, that we will use, is MD5. MD5 (MD stands for Message Digest ) is One way hash algorithm from Ron Rivest. There are test vectors in MD5 RFC, which we can use to calculate (or validate) the hash values. The following figure shows the process of calculation MD5 hash using OpenSSL :-OpenSSL - Using Message Digest APIs

Below is an example to calculate message digest using MD5 algorithm.

Code Snippet :



#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>

int main(int argc, char *argv[  ]) {

	int i,j;
	const int	totalTestVectors = 7;
	The ouput length for the claculated
	digest. This will be fixed for a parti-
	cular Hash algorithm and will very algo-
	rithm to algorithm.
	unsigned int	outputLength;
	The Message Digest Context object, which
	will hold the intermediate state.
	EVP_MD_CTX    messageDigestContext;

	The buffer to store message digest after
	computing. 64 bytes is enough for any hash
	function. For MD5 128/8 would be fine as
	MD5 has 128 bit output length.
	Someone can directly use the 128/8 as
	the size but if you change the Hash
	algorithm, for example SHA1,has 160 bit
	output, the length need to be changed to
	160/8 bytes.

	unsigned char messageDigest[EVP_MAX_MD_SIZE];

	Hashes will be calculated for the following strings.
	These strings are from the MD5 RFC.
	const char *strMessages[] = 
	{	"", // Input String : 1
		"a", // Input String : 2
		"abc",// Input String : 3
		"message digest",// Input String : 4
		"abcdefghijklmnopqrstuvwxyz",// Input String : 5
		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",// Input String : 6
		"12345678901234567890123456789012345678901234567890123456789012345678901234567890"// Input String : 7
	For each Input string the Expected Output are (from RFC,
	this is to make sure that, this implementation is not bogus):
	1: d41d8cd98f00b204e9800998ecf8427e
	2: 0cc175b9c0f1b6a831c399e269772661
	3: 900150983cd24fb0d6963f7d28e17f72
	4: f96b697d7cb7938d525a2f31aaf161d0
	5: f96b697d7cb7938d525a2f31aaf161d0
	6: d174ab98d277d9f5a5611c2c9f419d9f
	7: 57edf4a22be3c955ac49da2e2107b67a
	Initialize the Message Digest Context
	Calculate Message Digest
	//EVP_DigestInit(&amp;amp;amp;messageDigestContext, EVP_md5());
	for (j=0; j < totalTestVectors; j++)
		EVP_DigestInit(&messageDigestContext, EVP_md5()); // for SHA1 use EVP_sha1()
		EVP_DigestUpdate(&messageDigestContext, strMessages[j], strlen(strMessages[j]));
		EVP_DigestFinal(&messageDigestContext, messageDigest, &outputLength);
		printf("Test Vector : \"%s\" \n, Digest : = \"",strMessages[j]);
		for (i = 0;  i < outputLength;  i++) printf("%02x", messageDigest[i]);
	return 0;

The above code work fine on windows or Linux but you should have already openssl libs with you or you can download it.


Private Inheritance in C++

“Why Private Inheritance in C++ at all?” Concrete Classes in C++ are pretty easy to understand but what is not easy is Interface (Java style Interface), though C++ claims similar functionality using the Pure Abstract Class. Pure Abstract Class consists only Pure Virtual functions not any data (except the static one). In this way Interface (or Pure Abstract Class) refers to type of an object (instance of a class) not the Class of the object.

class Interface1{
virtual void pureVitualMethod() const=0;

class ConcreteClass1 :private Interface1{
void pureVitualMethod() const;

Now inheriting privately the Interface (as done in above code snippet) makes no use at this point of time as they will be inaccessible through the Interface’s pointer.

But class inheritance will aid you to define new objects with respect to the old class(Base class). How about inheriting it privately?

  • You cannot access derived class’s object through base class’s pointer.
  • You cannot access public or protected member function of the base class though the derived class’s object

Then the next question would be what is the use? The reasons are as follows;-

  • Pure Class inheritance (not subtyping) can be achieved via private inheritance in C++.
  • private inheritance will result in Composition (or, it exhibits the “has a” relationship rather than “is a” relationship)
  • can be necessary when you need to override a virtual function

Let see the following example, which utilizes a private inheritance,-

class HashEnvelop {
unsigned char* _mhashValue;
unsigned char* getHashValue(){
return _mhashValue;

/* Some other functions related to Hash calculations

virtual void calculateHashValue(const unsigned char* messageBytes){
// Calclates hash value for the
// specified input bytes (messageBytes)
// and stores the claculated value(in _mhashValue)

class MD5HashEnvelop: private HashEnvelop{
using HashEnvelop::getHashValue;
void calculateHashValue(const unsigned char * messageBytes){
// Calculate the hash value using MD5 algorithm
// and stores the value (in messageBytes)

Reference Readings :

  2. Design Patterns Elements of Reusable Object-Oriented Software, by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides