23aK9s2c8@1M7q4)9K6b7g2)9J5c8W2)9J5c8Y4N6%4N6#2)9J5k6h3y4G2k6r3g2H3M7X3!0B7k6h3y4@1i4K6u0W2j5$3!0E0i4K6u0r3N6i4y4W2M7X3W2@1k6h3#2K6i4K6u0r3j5$3S2S2M7$3S2Q4x3X3g2S2M7%4l9`.
Introduction
A few times i've seen implementations of individual hashing algorithms, and thought how 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 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.
// 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, this takes 1 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();
Hashing styles
In the latest version I have added hashing styles. This allows the programmer to customize the outputted hashes. There are 4 styles:
Lowercase, no spaces: b4df98798c02b7c7a500d18632bf5b7d