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.");


Download demo project - 127 Kb
Download source - 7 Kb


  • 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,
    More importantly, the codes have been used for some time and should be bug free.
    Source codes included.

  • 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 :)

  • thank you!

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

    Originally posted by: Dmitry Seliverstov


  • 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(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

  • Very easy to use, thanks

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

    Originally posted by: KP

    I like it

  • 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!

  • 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(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.

  • avoid Memory Leak

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

    Originally posted by: Solomon Wu

    I can avoid this error.

    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.

  • 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.

  • 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.

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

Top White Papers and Webcasts

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date