RSA MD5 Message Digest


This article was contributed by Nick Stone of Langfine Ltd.

Overview

This is a C++ implementation of the RSA MD5 message digest algorithm. The algorithm calculates a 16 byte checksum for any data sequence (e.g., array of bytes, a string or a file). Full details of the MD5 algorithm are provided within the code.

Two projects are provided:

  • MD5.dsw—builds a library that provides the MD5 calculation routines.
  • MD5ChecksumTest.dsw—builds a modeless dialog test environment that demonstrates use of the MD5 library and provides a simple verification tool.

Important Notes

Wherever this implementation of the RSA MD5 algorithm is used, the RSA copyright notices etc. must be adhered to, as described within the code and the test application's "About" box.

All use of this algorithm is at the users risk—no liability of any kind whatsoever is accepted by Nick Stone nor Langfine Ltd.

Further Details

The MD5 message digest algorithm is wrapped in a C++ class named CMD5Checksum.

Three public functions are exported:

CString GetMD5(BYTE* pBuf, UINT nLength);
CString GetMD5(CFile& File);
CString GetMD5(const CString& strFilePath);

All are implemented as static functions. The checksum calculated is returned as a 32 character hexadecimal number held in a CString. GetMD5 is overloaded to take data to be checksummed as either an array of bytes or a file.

The class CMD5Checksum is held within the MD5.dsw project. Building this project creates MD5.lib.

A test environment is provided in the project MD5ChecksumTest.dsw. This allows the user to type a string and see its checksum calculated in real time and also to select a file for checksum. Data files are provided containing simple test data; get the checksum for these and then try typing the file's contents into the first edit box and compare the two checksums thus obtained (they should be the same!) The test environment also includes a "Self Test" button. This checksums each of the supplied test data files in turn and checks the calculated checksum with the known correct value.

Downloads

Download source - 18 Kb
Download demo project - 46 Kb



Comments

  • Thanks a lot

    Posted by Huleinpylo on 09/01/2012 10:38am

    Thanks a lot. Very usefull.

    Reply
  • Is there a builder 6.0 implementation?

    Posted by Legacy on 08/27/2003 12:00am

    Originally posted by: Abderrahmane Sider

    Hello
    
    I m trying to use the ICoderMd5 Component from Indy Palette
    but the CodeString memeber doesn't work .I will try to use the md5.lib alltogother with bcb 6.0 but I would like to have the source in buinder.
    Thanks very much!

    Reply
  • How can I make it faster?

    Posted by Legacy on 01/21/2003 12:00am

    Originally posted by: Lee Jang won

    It works so slowly. When it made md5 code of 50MB file, it took 40 seconds. If there is a way to enhance its processing speed, plz tell me.

    • re:How can I make it faster?

      Posted by Kikiriki on 07/27/2005 07:37am

      the problem comes from these inline functions: inline DWORD RotateLeft(DWORD x, int n); inline void FF( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T); inline void GG( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T); inline void HH( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T); inline void II( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T); the main problem is that the compiller not always makes them inline functions. As it is said in the MS C++ reference "The compiler treats the inline expansion options and keywords as suggestions. There is no guarantee that functions will be inlined. You cannot force the compiler to inline a particular function, even with the __forceinline keyword". So the better thing to do it is by using macroses ;) which is usable for these functions. The things u need to do is : 1. remove them from the class prototype in the MD5Checksum.h 2. remove them from the class body in MD5Checksum.cpp and add these macroses instead: #define RotateLeft(x,n) (x << n) | (x >> (32-n)) #define FF( A, B, C, D, X, S,T) {DWORD F = (B & C) | (~B & D); A += F + X + T; A = RotateLeft(A, S);A += B;} #define GG( A, B, C, D, X, S,T) {DWORD G = (B & D) | (C & ~D); A += G + X + T; A = RotateLeft(A, S);A += B;} #define HH( A, B, C, D, X, S,T) {DWORD H = (B ^ C ^ D);A += H + X + T;A = RotateLeft(A, S); A += B;} #define II( A, B, C, D, X, S,T) {DWORD I = (C ^ (B | ~D));A += I + X + T;A = RotateLeft(A, S);A += B;} I tried with about 51 MB large files and saw it is really slow ... now the check takes me no more than 4.5 secs per 51 MB file which I think is a pretty good enhance ;).

      Reply
    Reply
  • modify of CString CMD5Checksum::Final()

    Posted by Legacy on 11/04/2002 12:00am

    Originally posted by: amowen

    CString CMD5Checksum::Final()
    {
    .....
    CString strMD5;
    for ( int i=0; i < nMD5Size; i++)
    {
    CString Str;
    if (lpszMD5[i] == 0) {
    Str = CString("00");
    ^^^^^
    }
    else if (lpszMD5[i] <= 15) {
    Str.Format("0%x",lpszMD5[i]);
    ^^^^^
    }
    else {
    Str.Format("%x",lpszMD5[i]);
    ^^^^^
    }

    ASSERT( Str.GetLength() == 2 );
    strMD5 += Str;
    }
    .....
    }

    Str.Format("%02x",lpszMD5[i]);
    get rid of three if/else

    Reply
  • how do i use the MD5.lib?

    Posted by Legacy on 11/02/2002 12:00am

    Originally posted by: Patrick

    always get error message after compilation
    "error C2065: 'GetMD5' : undeclared identifier"

    any help?

    thanks!

    Reply
  • Minor enhancements

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

    Originally posted by: Victor Boctor

    Nice work, thanks a lot. Following are some comments:

    1. Use constant when approriate:
    replace "static CString GetMD5(BYTE* pBuf, UINT nLength);" with "static CString GetMD5(const BYTE * const pBuf, UINT nLength);"
    Note: this will require changing some other methods to accept constant parameters.

    2. Accept strings as LPCTSTR rather than const CString &:
    Replace "static CString GetMD5(const CString& strFilePath);" with "static CString GetMD5(LPCTSTR pszFilePath);"

    Regards,
    Victor

    Reply
  • Very Good!

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

    Originally posted by: zhou

    Thanks very much!It is so good!

    Nice work!

    Reply
  • Thanks!

    Posted by Legacy on 06/10/2001 12:00am

    Originally posted by: Michael G. Spohn

    Just what I needed for a project I am working on. This is so usefull I added it to my static library of favorite utilities. Very much appreciate your efforts.

    MGS

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds