Implementing the MD5 Algorithm

Environment: Win32 & VC++ or Linux and gcc/egcs

According to the "Executive Summary" of RFC 1321,

"The MD5 algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input. It is conjectured that it is computationally infeasible to produce two messages having the same message digest, or to produce any message having a given prespecified target message digest. The MD5 algorithm is intended for digital signature applications, where a large file must be "compressed" in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA."

This algorithm was developed by Professor Ronald L. Rivest of MIT and can be found presented in several languages. What I provide to you here is a C++ derivative of the original C implementation of Professor Rivets.

The library code itself is platform-independant and has been tested in Redhat Linux. I've included the sample code and makefile that I used for the Linux test. The demo, however, was written with Visual C++ 6 on a Windows 2000 platform.

The screenshot here is of the MFC demo projects that I've included to show how to make use of the library calls. With this, you can genereate MD5 fingerprints of text phrases or files.

The library is well insulated and provides wrapper functions to make it as easy to use as this:

CString csString = MD5String("Some string you want to generate an
    MD5 key for.");

Downloads

Download demo project - 127 Kb
Download source - 7 Kb


Comments

  • Other algorithm

    Posted by sedi on 02/15/2009 11:39pm

    For those who are interested in md5 (and other crypto/hash algorithms), go here, http://www.cryptopp.com/
    
    More importantly, the codes have been used for some time and should be bug free.
    
    Source codes included.

    Reply
  • Demo error

    Posted by phoenixatlantios on 06/24/2006 05:54am

    You'll need to change the: #include "md5.h" to #include "md5.cpp" For the test file to compile properly, other than that it's awesome - exactly what I was looking for :)

    Reply
  • thank you!

    Posted by Legacy on 12/16/2003 12:00am

    Originally posted by: Dmitry Seliverstov

    *

    Reply
  • other solution method about memory leak

    Posted by Legacy on 11/26/2003 12:00am

    Originally posted by: chenxinyi

    every invoke MD5String function that it will return a pointer, you can do:

    delete pointer;

    • m-leaks

      Posted by MAxZ on 07/15/2005 07:56am

      char* PrintMD5(uchar md5Digest[16])
      {
      	char chBuffer[256];
      	char chEach[10];
      	int nCount;
      
      	memset(chBuffer,0,256);
      	memset(chEach, 0, 10);
      
      	for (nCount = 0; nCount < 16; nCount++)
      	{
      		sprintf(chEach, "%02x", md5Digest[nCount]);
      		strncat(chBuffer, chEach, sizeof(chEach));
      	}
      
      
      	return strdup(chBuffer);// this memory need to be returned
      }

      Reply
    Reply
  • Very easy to use, thanks

    Posted by Legacy on 05/15/2003 12:00am

    Originally posted by: KP

    I like it

    Reply
  • Not be thread securite!

    Posted by Legacy on 05/03/2003 12:00am

    Originally posted by: Zhiwei Li


    Don't use static in function, because it is not thread security.

    So, you should use TLS instead!


    Reply
  • More correct memory leak solution

    Posted by Legacy on 09/10/2002 12:00am

    Originally posted by: M A V

    The code of PrintMD5 should be rewritten:
    
    

    static char chBuffer[256]; //changed this
    char chEach[10];
    int nCount;

    memset(chBuffer,0,256);
    memset(chEach, 0, 10);

    for (nCount = 0; nCount < 16; nCount++)
    {
    sprintf(chEach, "%02x", md5Digest[nCount]);
    strncat(chBuffer, chEach, sizeof(chEach));
    }

    return chBuffer; //changed this


    Thus you will use pointer to buffer nor from heap(causes memory leaks) nor from stack (unpredictable data) but from static variable.


    Reply
  • avoid Memory Leak

    Posted by Legacy on 09/09/2002 12:00am

    Originally posted by: Solomon Wu

    Hi:
    
    I can avoid this error.

    method:
    1. open file md5.cpp
    2. find function "PrintMD5(uchar md5Digest[])
    3. replace statement "return strdup(chBuffer);" to "return chBuffer;"
    OK, won't occur memory leak.(w2k server + vc6.0)

    Why Occur this Error?
    Because,The strdup function calls [malloc] to allocate storage space for a copy of strSource and then copies.But when return from PrintMD5, we whitout use any funtion release this memory. Because this, we call n times PrintMD5 will occur n times memory leak.

    Sorry, using English expression is very difficult as me,Hope you can understand what i say.

    Reply
  • Memory Leak?

    Posted by Legacy on 09/01/2002 12:00am

    Originally posted by: HenryVIII

    Running your demo program in debug and memory leaks were detected.

    Reply
  • What about Endian?

    Posted by Legacy on 08/15/2002 12:00am

    Originally posted by: Anthony Mai

    You claim your code is platform independent. But your code does not take into consideration of whether the system is big endian or little endian. It can't work on both systems since it does not take care of endianness.

    Also within your source code you need to provide a known string and known output so one can verify that the algorithm produces expected output, instead of trash.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

  • Flash technology is becoming more prominent in the storage industry. Offering superior speed and reliability when compared to traditional hard disk drives – flash storage is a flexible and increasingly cost-effective technology that can be used to optimize enterprise storage environments. This ebook explores the many uses and benefits of flash storage technology in the enterprise. Check it out to discover and learn all you need to: Optimize storage performance Leverage server flash as storage cache …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds