Click here to Skip to main content
14,211,795 members
Click here to Skip to main content
Posted 3 Jan 2003


86 bookmarked

The FileDigest program and the C++ implementation of some Message-Digest algorithms

Rate this:
4.93 (39 votes)
Please Sign up or sign in to vote.
4.93 (39 votes)
3 Jan 2003     Ms-PL    
Presenting the C++ implementation of some known and efficient Message-Digest algorithms (MD, RIPEMD, SHA) and a full FileDigest program that is applying the mentioned algorithms.

What is FileDigest?

FileDigest is a cryptographic product useful for generating digital signatures for files or typed text (strings) applying one of the following message-digesting methods: MD5, RIPEMD128, RIPEMD160, SHA-1 (SHA160), SHA-2 (SHA256, SHA384 and SHA512).

Short Presentation of the Algorithms

MD5 is part of the family of message-digest algorithms MD2, MD4 and MD5 developed by R. Rivest in collaboration with MIT Laboratory for Computer Science and RSA Data Security, Inc. The MD5 algorithm is an extension of the MD4 message-digest algorithm, slightly slower than MD4, but on the other side more secure. All three algorithms take a message of arbitrary length and produce a 128-bit message digest.

RIPEMD128 and RIPEMD160 are upgrades to MD4, MD5, and RIPEMD methods. RIPEMD was developed in the framework of the EU project RIPE (RACE Integrity Primitives Evaluation, 1988-1992). 128-bit hash results are considered to not offer anymore sufficient protection, and applications using 128-bit hash functions should consider upgrading to 160-bit hash functions. RIPEMD160 is a 160-bit cryptographic hash function, designed by Hans Dobbertin, Antoon Bosselaers, and Bart Preneel. It is intended to be used as a secure replacement for the 128-bit hash functions MD4, MD5, and RIPEMD.

The Secure Hash Algorithm (SHA) algorithm specified in the Secure Hash Standard (SHS), was developed by NIST and published as a federal information processing standard (FIPS PUB 180). SHA-1 was a revision to SHA that was published in 1994. The revision corrected an unpublished flaw in SHA. Its design is very similar to the MD4 family of hash functions developed by R. Rivest. The SHA-1 algorithm takes a message of any length and produces a 160-bit message digest. The algorithm is slightly slower than MD5, but the larger message digest makes it more secure against brute-force collision and inversion attacks. Motivated by the recent AES selection, NIST proposed replacements of the SHA-1 hash algorithm with the SHA-256, SHA-384, and SHA-512, together known as SHA-2, algorithms considered to provide a level of collision resistance equivalent to the security of each AES key sizes. The message digest length for SHA-1 is 160 bits (20 bytes). The message digest lengths for the new SHA-256, SHA-384, and SHA-512 are respectively 256 bits (32 bytes), 384 bits (48 bytes), 512 bits (64 bytes). The increase in size is significant, making difficult to break the new algorithms.

The table bellow is summarizing the key features of the FileDigest's implemented message-digest algorithms:

Method Digest Length (bytes) Security
MD5 16 Moderate
RIPEMD128 16 Moderate
RIPEMD160 20 High
SHA-1 20 High
SHA256 32 Very High
SHA384 48 Very High
SHA512 64 Very High

All the algorithms are implemented using a common interface, IMessageDigest, which is partially presented bellow:


class IMessageDigest
  virtual ~IMessageDigest();
  //Update context to reflect the concatenation of another buffer of bytes
  virtual void AddData(char const* pcData, int iDataLength) = 0;
  //Final wrapup - pad to BLOCKSIZE-byte boundary with the bit pattern 
  //10000...(64-bit count of bits processed, MSB-first)
  virtual void FinalDigest(char* pcDigest) = 0;
  //Reset current operation in order to prepare for a new one
  virtual void Reset() = 0;
  //Digesting a Full File
  void DigestFile(string const& rostrFileIn, char* pcDigest);

  //The core of the MessageDigest algorithm, this alters an existing MessageDigest hash to
  //reflect the addition of 64 bytes of new data
  virtual void Transform() = 0;

Notice that all the classes implementing the IMessageDigest interface have to overdide the pure virtual member functions: AddData(), FinalDigest(), Reset() and DigestFile(), while the function DigestFile() is general and already implemented.

The function Reset() is used to prepare the same object for a new message-digesting operation.

The function AddData() is adding a new buffer of data to the current message-digesting operation.

The function FinalDigest() is concluding any current message-digesting operation.

The function DigestFile() is used for digesting an entire file.

If you want to use the implemented algorithms in your own project, you need to copy the following files from the FileDigest project, and integrate them in your project: MessageDigest.h, MessageDigest.cpp, MD5.h, MD5.cpp, RIPEMD.h, RIPEMD.cpp, SHA.h, SHA.cpp, DoubleBuffering.h and DoubleBuffering.cpp. The use is easy, as is demonstrated by the following code snippet:


//The array that will receive the final result (32bytes for SHA256)
char acDigest[33];
//Declare an object
//Add data repeatedly
oSHA.AddData("string1", 7);
oSHA.AddData("string2", 7);
oSHA.AddData("string3", 7);
//Conclude the operation, after that the result is in acDigest

Notice that the result is a binary array of characters, so it cannot be manipulated as usual 0 terminated strings of characters.

Using the FileDigest program

A Help menu of the FileDigest program is not implemented right now. The information given in the following paragraphs can be considered as a small User Manual.

There are two modes of operations of FileDigest selectable from the Mode Group: String Mode and File Mode.

How to use FileDigest in String Mode


The message-digest method can be selected from the Method Group Combo Box one of the values: MD5, RIPEMD128, RIPEMD160, SHA-1 (SHA160), SHA-2 (SHA256, SHA384, SHA512).

The text (string) can to be introduced in one of the Edit Boxes in the String Group, depending on the selected mode: Alpha or Hex. In the Hex Edit Box only hexadecimal characters (0-9,A-F) can be introduced, if you try to type any other characters an error signaling beep will be generated. Also if you try to copy a text from the Clipboard it will be first verified if is in hexa format (restricted to the set of hexa characters and even in length). These restrictions apply to all the other Hex Edit Boxes used in FileCrypt.

After pressing the String Digest Button the result will be displayed in the Digest Result (string) Edit Box as a string of characters and in the Digest Result (hexadecimal) Edit Box as a hexadecimal format string.

How to use FileDigest in File mode

The message-digest method is selected similarly as in the String Mode.

The File can be typed in the File Edit Box, or browsed from the browsing button located near the File Edit Box. By pressing the File Digest button the result will be displayed in Digest Result (string) Edit Box as a string of characters and in Digest Result (hexadecimal) Edit Box as a string in hexadecimal format. The result can be saved in a file (default extension .fdg) from the Save As... Button.

When you need to check a digital signature against a file, you can load the digital signature from a digest file using the Load... Button, and then check against the file selected in the File Edit Box using the Check Button.


The project attached to this article is including the source code of the presented message-digest algorithms and FileDigest program. I am interested in any opinions and new ideas about this implementation.

Typical Disclaimer: This Article and attached code are Copyright (C) 2003 by George Anescu. You have the right to use and distribute the article's content and code in any way you see fit as long as this paragraph is included with the distribution. No warranties or claims are made as to the validity of the information and code contained herein, so use it at your own risk.


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

George Anescu
Web Developer
Romania Romania
No Biography provided

Comments and Discussions

Questionunicode? Pin
jacksp6-Sep-11 19:33
memberjacksp6-Sep-11 19:33 
Questionerror message error C4867 Pin
suriyani8-Dec-09 17:13
membersuriyani8-Dec-09 17:13 
QuestionMD5 safe to file check integrity of files ? Pin
Rui Frazao14-Jan-08 0:08
memberRui Frazao14-Jan-08 0:08 
Questiona request from kale.priya Pin
Sean Ewington11-Jun-07 8:24
staffSean Ewington11-Jun-07 8:24 
QuestionDouble buffering? Pin
Graham Reeds7-Dec-06 5:58
memberGraham Reeds7-Dec-06 5:58 
Generalwarnigs Pin
kishore j hp3-Nov-05 19:26
memberkishore j hp3-Nov-05 19:26 
Generalimplementing md5 checksum Pin
nisha b1-Jan-05 21:39
sussnisha b1-Jan-05 21:39 
Generalimplementation problem Pin
_kane_26-Oct-04 21:48
member_kane_26-Oct-04 21:48 
QuestionWhat about Decoding? Pin
Balkrishna Talele27-Aug-04 0:30
memberBalkrishna Talele27-Aug-04 0:30 
AnswerRe: What about Decoding? Pin
Gautam Jain2-Sep-04 0:39
memberGautam Jain2-Sep-04 0:39 
GeneralRe: What about Decoding? Pin
Balkrishna Talele2-Sep-04 0:56
memberBalkrishna Talele2-Sep-04 0:56 
QuestionHow about evC 3.0? Pin
Mad_C19-Apr-04 2:28
memberMad_C19-Apr-04 2:28 
GeneralPleasee help about DSA Pin
jayamohan16-Sep-03 6:58
memberjayamohan16-Sep-03 6:58 
GeneralI have a problem Pin
drax22-Feb-03 3:39
memberdrax22-Feb-03 3:39 
GeneralRe: I have a problem Pin
Dominik Reichl5-Sep-03 22:44
memberDominik Reichl5-Sep-03 22:44 
QuestionDoes it work? Pin
Andreas Saurwein Franci Gonçalves5-Jan-03 9:59
memberAndreas Saurwein Franci Gonçalves5-Jan-03 9:59 
AnswerRe: Does it work? Pin
George Anescu5-Jan-03 12:38
memberGeorge Anescu5-Jan-03 12:38 

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.

Layout: fixed | fluid

Article Copyright 2003 by George Anescu
Everything else Copyright © CodeProject, 1999-2019

Server Web01
Version 2.8.190619.2