Click here to Skip to main content
Click here to Skip to main content

CHash 1.5 - An MFC hashing class

, 9 Aug 2005
Rate this:
Please Sign up or sign in to vote.
An MFC implementation of hashing files and strings with CRC32, GOST-Hash, MD2, MD4, MD5, SHA-1 and SHA-2 (256/384/512).


A few times I've seen implementations of individual hashing algorithms, and thought it may be a good idea to group them together in an easy to use class. Well, here it is.

What are hashes anyway?

Hashes are a string of letters/numbers. They are used as a representation of an amount of data, but they are one way, you cannot go from a hash back to the original data; as hashes are fixed length, you also cannot determine the length or amount of the data represented. This lends hashes to practical security uses, as well as integrity uses.

Why use hashes?

There are multiple uses for hashes, the main one being data integrity. For example, a P2P client would use hashes to validate a file on completion, to check it's not corrupt or "fake". In this way, by generating a hash of a file, you can compare it against another hash, and check whether the files are the same.

Using the code

Putting CHash into use is relatively simple.

The main functions that will be used are:

  • DoHash
  • SetHashAlgorithm
  • SetHashFile
  • SetHashOperation
  • SetHashString

An example of hashing a string with MD5:

// Define a CHash object
CHash hashObj;

// Set the algorithm

// Set the operation

// Set the string
hashObj.SetHashString("String to hash");

// Hash the string
CString outHash = hashObj.DoHash();

An example of hashing a file with SHA-1:

// Define a CHash object
CHash hashObj;

// Set the algorithm

// Set the operation

// Set the file

// Hash the file
CString outHash = hashObj.DoHash();

The code is the same throughout, except for SHA-2, which has an extra function, SetSHA2Strength, which takes one parameter, the strength of the hash, which can be 256, 384 or 512.

An example usage of this is:

// Define a CHash object
CHash hashObj;

// Set the operation

// Set the algorithm

// Set the SHA-2 strength

// Set the file

// Hash the file
CString outHash = hashObj.DoHash();

Choosing which algorithms to include

In version 1.5, I made the class modular, so you can exclude/include specific algorithms (cutting down on unnecessary code if you don't want certain ones). To choose which you want to use, go to CHash.h, and find:

// Choose which algorithms you want
// Put 1s to support algorithms, else 0 to not support

Under here you will find defines such as:

#define        SUPPORT_CRC32          1

Change the 1s to 0s if you wish to exclude an algorithm.

Hashing styles

In version 1.2, I added hashing styles. This allows the programmer to customize the output hashes. There are four styles:

  • Lowercase, no spaces:
  • Lowercase, spaces:
    b4 df 98 79 8c 02 b7 c7 a5 00 d1 86 32 bf 5b 7d
  • Uppercase, no spaces:
  • Uppercase, spaces:
    B4 DF 98 79 8C 02 B7 C7 A5 00 D1 86 32 BF 5B 7D

These can be set with SetHashFormat().


  • Thanks to Dominik Reichl for his excellent ReHash program.
  • Thanks to Markku-Juhani O. Saarinen for his GOST-Hash implementation.
  • Thanks to Dr. Brian Gladman for the SHA-1/SHA-2 implementations.


  • 1.0
    • 1st May, 2005: First public release.
  • 1.1
    • 2nd May, 2005: Added SetHashAlgorithm() and DoHash() as recommended.
  • 1.2
    • 3rd May, 2005:
      • Added hashing styles.
      • Added the GOSTHash algorithm.
      • Added GetHashAlgorithm().
      • Rewrote the hashing functions to be more efficient.
  • 1.3
    • 4th May, 2005: Updated the demo project.
  • 1.5
    • 8th May, 2005:
      • Added CRC32.
      • Made the class "modular" so that you can exclude algorithms from compile.
      • Added GetHashFormat().
      • Updated the code as suggested.
  • 1.6
    • 3rd August, 2005:
      • Fixed a memory leak.
      • Made each hash more efficient, tidied up code in general.
  • License

    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


    About the Author

    United States United States
    No Biography provided

    Comments and Discussions

    GeneralMy vote of 5 Pinmembertoann29-Jul-11 8:41 
    Generalsome function suggestions Pinmemberxtrmblop20-May-10 5:54 
    Generalsmall bug in CHash.cpp Pinmemberxtrmblop20-May-10 1:15 
    GeneralRe: small bug in CHash.cpp PinmemberOmar.Pessoa26-Jan-12 1:13 
    Generalsmall bug [modified] Pinmemberatom6667-Nov-08 3:04 
    Generalerror when choosing algorithms to support Pinmemberbreakpoint4-Apr-06 2:00 
    GeneralDecrypt back to plaintext Pinmembershinyhui26-Sep-05 15:27 
    i just wonder if i use this hash function to hash my text it return some cipertext, did here provide us to decrypt back to plaintext?D'Oh! | :doh:
    GeneralRe: Decrypt back to plaintext 5:07 
    GeneralRe: Decrypt back to plaintext Pinmembershinyhui27-Sep-05 15:17 
    GeneralRe: Decrypt back to plaintext 5:00 
    GeneralRe: Decrypt back to plaintext Pinmembershinyhui28-Sep-05 16:10 
    GeneralRe: Decrypt back to plaintext Pinmemberbanjaxx6-Mar-06 21:37 
    GeneralI vote Pinmemberpeder88819-Aug-05 5:40 
    Generalreturn values not defined... Pinmemberbevpet9-Aug-05 7:48 
    QuestionWhy all these seperate classes? PinmemberGeert van Horrik9-Aug-05 2:07 
    AnswerRe: Why all these seperate classes? Pinmemberbevpet9-Aug-05 7:59 
    GeneralRe: Why all these seperate classes? 20:59 
    GeneralUsing hash on files PinmemberGeert van Horrik9-Aug-05 2:04 
    GeneralRe: Using hash on files 20:58 
    JokeC-Hash :-) PinstaffNishant Sivakumar9-Aug-05 0:34 
    GeneralRe: C-Hash :-) Pinmemberbevpet9-Aug-05 7:56 
    Generalcheck if file exists before attempting hash Pinmemberbevpet27-May-05 7:02 
    GeneralRe: check if file exists before attempting hash 1:37 
    Generalnice work.. Pinmemberbevpet26-May-05 19:00 
    GeneralSpeed PinmemberdennisV24-May-05 20:08 
    GeneralRe: Speed 5:01 
    GeneralNewbie on hashes PinmemberJohann Gerell14-May-05 23:46 
    GeneralRe: Newbie on hashes 9:24 
    GeneralRe: Newbie on hashes PinsussAnonymous16-Jun-05 13:16 
    GeneralRe: Newbie on hashes PinmemberStrayJay12-Aug-05 10:54 
    GeneralGood work and a lot of updates PinmemberMike Angel Martin7-May-05 9:21 
    GeneralRe: Good work and a lot of updates 23:53 
    GeneralMake it more complete. PinmemberWREY3-May-05 6:31 
    GeneralRe: Make it more complete. 6:48 
    GeneralDone 7:35 
    GeneralVery Nice. PinmemberWREY8-May-05 13:13 
    GeneralHash PinmemberAnon E. Mouse2-May-05 10:42 
    GeneralRe: Hash 11:11 
    GeneralRe: Hash PinmemberAnon E. Mouse2-May-05 11:46 
    GeneralGood Work PinmemberJaxyboi2-May-05 0:45 
    GeneralGood Idea - Bad implementation PinmemberMike Eriksson1-May-05 23:43 
    GeneralRe: Good Idea - Bad implementation 0:39 
    GeneralRe: Good Idea - Bad implementation PinmemberMike Eriksson2-May-05 0:53 
    GeneralNew implementation, article, source and demo updated 1:00 
    GeneralRe: Good Idea - Bad implementation PinmemberBlake Miller2-May-05 10:23 
    GeneralRe: Good Idea - Bad implementation PinmemberStrayJay3-May-05 13:11 
    GeneralNew version, 1.5 1:45 
    GeneralGood suggestions. PinmemberWREY8-May-05 12:57 

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

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

    | Advertise | Privacy | Terms of Use | Mobile
    Web03 | 2.8.150305.1 | Last Updated 9 Aug 2005
    Article Copyright 2005 by .rich.w
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid