首页
社区
课程
招聘
[推荐]CHash - An MFC hashing class
发表于: 2005-5-5 14:26 5221

[推荐]CHash - An MFC hashing class

2005-5-5 14:26
5221
b58K9s2c8@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.

The main functions that will be used are:

- DoHash
- SetHashAlgorithm
- SetHashFile
- SetHashOperation
- SetHashString

An example of these is 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, 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

Lowercase, spaces: b4 df 98 79 8c 02 b7 c7 a5 00 d1 86 32 bf 5b 7d

Uppercase, no spaces: B4DF98798C02B7C7A500D18632BF5B7Dd

Uppercase, spaces: B4 DF 98 79 8C 02 B7 C7 A5 00 D1 86 32 BF 5B 7D

These can be set with SetHashFormat().

History
4th May 2005: (wow, an update 4 days in a row) Updated demo project

3rd May 2005:
- Added hashing styles
- Added the GOSTHash algorithm
- Added GetHashAlgorithm()
- Rewrote the hashing functions to be more efficient

2nd May 2005: Added SetHashAlgorithm and DoHash as recommended

1st May 2005: First public release

附件:chash_src.zip 附件:chash_demo.zip

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (1)
雪    币: 106860
活跃值: (202484)
能力值: (RANK:10 )
在线值:
发帖
回帖
粉丝
2
谢谢
2005-5-5 14:30
0
游客
登录 | 注册 方可回帖
返回