Developers.Hash.Object
How to hash data
The Shareaza 2.0 code defines objects that can hash data. Here's one in use:
<source lang="c"> // Takes a pointer to some memory and the number of bytes of data there // Computes SHA1 hashes of the information and converts it to and from text void HashSHA1(byte* p, DWORD bytes) {
// Hash the data CSHA hash; // Make the object that will compute the hash hash.Add(p, bytes); // Give the object the data to hash hash.Finish(); // Tell the hash object that is all the data we are going to add
// Get the hash value SHA1 value; // Make a local instance of a structure to hold the hash value DWORD size = sizeof(value); // The value is 20 bytes, 160 bits, 40 hex characters hash.GetHash(&value); // Get the hash value
// Access the memory of the hash value directly to express it as text CString s = tobase16byte*)(&value), sizeof(value; } </source>
First, the code makes a new local instance of the CSHA class, and names it hash. It calls the Add method on hash, giving it a pointer to some data and the number of bytes it should read there. We can call Add multiple times to keep feeding the object data, even though this example only shows a single call to Add. Finish tells the object that we won't call Add anymore.
Then, it's time to get the hash object to tell us the value it computed. The code above defines a new local SHA1 variable named value. The GetHash method takes a pointer to this variable, and fills it with the hash value.
The classes CMD4, CMD5, and CSHA all work this way. Make an object and call its Add, Finish, and GetHash methods to hash data. These classes also implement Reset, which lets you start over without having to make a new object.
Classes: CMD4, CMD5, CSHA Methods: Reset, Add, Finish, GetHash
camper's code
The same function updated to interact with camper's code looks like this:
<source lang="c"> // Takes a pointer to some memory and the number of bytes of data there // Computes SHA1 hashes of the information and converts it to and from text void HashSHA1(byte* p, DWORD bytes) {
// Hash the data CSHA hash; // Make the object that will compute the hash hash.Add(p, bytes); // Give the object the data to hash hash.Finish(); // Tell the hash object that is all the data we are going to add
// Get the hash value Hashes::Sha1Hash value; // This will hold the hash value DWORD size = Hashes::Sha1Hash::byteCount; // Can't use sizeof, read byteCount which is 20 hash.GetHash(value); // Don't use the & operator here
// Access the memory of the hash value directly to express it as text CString s = tobase16(&value[0], Hashes::Sha1Hash::byteCount); } </source>
The hash value variable is different. You can't call sizeof on it, and should read byteCount instead. To access it's memory, index it like an array with &value[0].
The classes and methods that compute these 3 types of hashes are the same.
Classes: CMD4, CMD5, CSHA Methods: Reset, Add, Finish, GetHash