![]() |
General Programming »
Cryptography & Security »
Cryptography
Intermediate
CHash 1.5 - An MFC hashing classBy .rich.wAn MFC implementation of hashing files and strings with CRC32, GOST-Hash, MD2, MD4, MD5, SHA-1 and SHA-2 (256/384/512). |
VC6, Windows, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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.
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.
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 hashObj.SetHashAlgorithm(MD5); // Set the operation hashObj.SetHashOperation(STRING_HASH); // 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 hashObj.SetHashAlgorithm(SHA1); // Set the operation hashObj.SetHashOperation(FILE_HASH); // Set the file hashObj.SetHashFile("C:\\Windows\\Explorer.exe"); // 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 hashObj.SetHashOperation(FILE_HASH); // Set the algorithm hashObj.SetHashAlgorithm(SHA2); // Set the SHA-2 strength hashObj.SetSHA2Strength(256); // Set the file hashObj.SetHashFile("C:\\Windows\\Explorer.exe"); // Hash the file CString outHash = hashObj.DoHash();
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.
In version 1.2, I added hashing styles. This allows the programmer to customize the output hashes. There are four styles:
b4df98798c02b7c7a500d18632bf5b7d
b4 df 98 79 8c 02 b7 c7 a5 00 d1 86 32 bf 5b 7d
B4DF98798C02B7C7A500D18632BF5B7Dd
B4 DF 98 79 8C 02 B7 C7 A5 00 D1 86 32 BF 5B 7DThese can be set with SetHashFormat().
SetHashAlgorithm() and DoHash() as recommended. GetHashAlgorithm().
GetHashFormat().
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 9 Aug 2005 Editor: Smitha Vijayan |
Copyright 2005 by .rich.w Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |