Implementing the MD5 Algorithm


Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Date: 1/31/2018 @ 2 p.m. ET

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


  • question about copy rights

    Posted by Dexster on 10/09/2015 12:04pm

    Hi, The codes are great. I just wonder if the codes can be copied and used for commercial usage for free? Thanks, Dexster

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

    • how to compile

      Posted by roshni rk on 10/14/2015 08:36pm

      hi ...what is the step of compilation..when i compile using gcc md5test.cpp i got a warning message saying deprecated conversion from string constant to char *. I am trying to run in linux environment.

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

  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

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