Click here to Skip to main content
15,446,613 members
Articles / Programming Languages / C++
Posted 9 Jan 2001


88 bookmarked

CMD5: A C++ Message Digest 5 Class

Rate me:
Please Sign up or sign in to vote.
4.74/5 (30 votes)
9 Jan 2001
This C++ class serves as a wrapper for the Message Digest 5 code described in the Internet RFC 1321.


There has been some recent discussion here concerning how to save password information. In general, it is a Very Bad Idea(tm) to just store a password in an application, data file, INI file, registry, or anywhere but in volatile memory.

A better idea than trying to save a password is to save a "cryptological hash" of the password. Cryptographers call this type of hash a "digest". A digest function in this context is just a function that takes a block of text (called "the plaintext") and computes a checksum type number for that block. Cryptological digests have the properties that,

  • It is highly unlikely that different input text will produce the same digest.
  • It is very, very difficult if not totally impossible to get from the digest back to the input plaintext.

Programmers wishing to secure their password data can immediately compute the digest of the password, and then save that. When the user is prompted for a password, the digest of the input password candidate is computed, and this digest is compared against the stored digest. If they are equal, then the user has entered the correct password. Note that the programmer can post the stored hash value on a public web page or a billboard on the highway, and the security of his product is not compromised. He or she has no worries about someone with a hex editor digging out passwords.

Digests may also be used to verify that a message has been transmitted correctly, in a manner similar to cyclic redundancy checks.

A proven digest algorithm is "Message Digest Five" by Ron Rivest (Ron is the ‘R’ in RSA). It is described in Internet RFC 1321, "The MD5 Message-Digest Algorithm". A google search will provide hundreds of links to this RFC, I used the one I found here. This RFC includes a K&R style ‘C’ implementation of the MD5 algorithm. This implementation is a bit awkward to use, so I’ve wrapped it in a simple generic C++ class.  The class is called CMD5. It has only four interesting members:

CMD5();    //default ctor

CMD5(const char* plainText);  //set plaintext in ctor

void setPlainText(const char* plainText);
    // set plaintext with a mutator, it's ok to 
    // to call this multiple times, the digest is 
    // recalculated after each call.
const char* getMD5Digest();
    // access message digest (aka hash), return 0 if
    // plaintext has not been set

To use the class, just set the plain text either in the constructor or with the setPlainText member. The class calls a very slightly modified version of the RFC 1321 code to compute the digest, which is immediately available by calling getMD5Digest.

Here is some pseudo code showing how to use this class:

cout >> "Enter password:"; 
cin >> strUserEnteredPassword;
CMD5 md5((const char*)strUserEnteredPassword));
  (const char*)m_PreviouslyStoredDigestOfTheActualPassword)==0)
    //password is valid
    //user entered incorrect password

To use the class, you need these files:

  • md5class.h and md5class.cpp - The CMD5 C++ code.
  • global.h, MD5.h, md5.c - Slightly modified versions of the RFC 1321 code.

These files are generic C++. The zip file includes a Win32 console app project that includes a main.cpp file. This little program runs the verification examples provided in the RFC. I use STL in this demo, but not in the class itself.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Web Developer
United States United States
just another c++ developer, except that im really
old, almost 51!

Comments and Discussions

QuestionPotential flaw Pin
-dZb-13-Sep-11 11:08
Member-dZb-13-Sep-11 11:08 
QuestionRestore the password how I can do? Pin
JasonShen11122-Mar-06 20:05
MemberJasonShen11122-Mar-06 20:05 
AnswerRe: Restore the password how I can do? Pin
Jim Howard23-Mar-06 4:29
MemberJim Howard23-Mar-06 4:29 
General[Small Tip] To reuse CMD5 in your MFC/eVC++4 project Pin
Jae Hyoung Kim18-Sep-05 11:59
MemberJae Hyoung Kim18-Sep-05 11:59 
GeneralRe: [Small Tip] To reuse CMD5 in your MFC/eVC++4 project Pin
freaky_flow4-Oct-07 2:56
Memberfreaky_flow4-Oct-07 2:56 
General[solved]Re: [Small Tip] To reuse CMD5 in your MFC/eVC++4 project Pin
freaky_flow4-Oct-07 3:56
Memberfreaky_flow4-Oct-07 3:56 
Generalsmaller tip Pin
DrJohnAir12-Mar-08 18:40
MemberDrJohnAir12-Mar-08 18:40 
GeneralMD5 broken... Pin
Anonymous16-Feb-05 7:47
MemberAnonymous16-Feb-05 7:47 
QuestionUnicode? Pin
blongtq1-Mar-04 23:11
Memberblongtq1-Mar-04 23:11 
AnswerRe: Unicode? Pin
jeffreyz8-Feb-05 14:49
Memberjeffreyz8-Feb-05 14:49 
GeneralRe: Unicode? Pin
Tonde7-Sep-05 8:50
MemberTonde7-Sep-05 8:50 
Generalclarification - very urgent Pin
Nareshsiva26-Jan-04 1:19
MemberNareshsiva26-Jan-04 1:19 
GeneralRe: I'm Using C++ Builder 6.0 Pin
bobsuk20-Nov-06 1:30
Memberbobsuk20-Nov-06 1:30 
GeneralBug! Pin
the coder2-Mar-03 11:22
Memberthe coder2-Mar-03 11:22 
GeneralRe: Bug! Pin
Jim Howard2-Mar-03 16:26
MemberJim Howard2-Mar-03 16:26 
GeneralJust a little bit more secure Pin
dayWalker21-Oct-02 23:26
MemberdayWalker21-Oct-02 23:26 
GeneralCompatibility Pin
Hugo Hallman9-Oct-02 22:43
MemberHugo Hallman9-Oct-02 22:43 
GeneralRe: Compatibility Pin
Anonymous10-Oct-02 2:43
MemberAnonymous10-Oct-02 2:43 
GeneralToo many critics ! Pin
Stanislav Panasik2-Oct-02 2:05
MemberStanislav Panasik2-Oct-02 2:05 
GeneralRe: Too many critics ! Pin
minetti30-Nov-02 13:27
Memberminetti30-Nov-02 13:27 
GeneralWeird !! Pin
Anonymous21-Aug-02 9:57
MemberAnonymous21-Aug-02 9:57 
GeneralRe: Weird !! Pin
Jim Howard23-Sep-02 4:01
MemberJim Howard23-Sep-02 4:01 
Generalpassword storage Pin
barmak19-Jul-02 18:29
Memberbarmak19-Jul-02 18:29 
GeneralRe: password storage Pin
Jim Howard20-Jul-02 5:41
MemberJim Howard20-Jul-02 5:41 
GeneralRe: password storage Pin
Jason Hooper20-Jul-02 9:34
MemberJason Hooper20-Jul-02 9:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.