Developers.Hash.Data

From Shareaza Wiki
Jump to navigation Jump to search

Hashing data

Here's how to hash some data in Shareaza 2.0 and with camper's new code.

Shareaza 2.0

Here's a function that runs in Shareaza 2.0. It computes the MD4, MD5, and SHA1 hashes of the data you give it, expressing them as text in base 16 and base 32.

<source lang="c"> // Takes a pointer to some memory and the number of bytes of data there // Computes the MD4, MD5, and SHA1 hashes of the data void HashData(byte* p, DWORD bytes) {

// Make objects that will compute the hashes CMD4 md4; CMD5 md5; CSHA sha1;

// Add the data to the hash objects md4.Add(p, bytes); md5.Add(p, bytes); sha1.Add(p, bytes);

// Tell the hash objects that's all the data we are going to add md4.Finish(); md5.Finish(); sha1.Finish();

// Make variables that will hold the hash values MD4 md4hash; MD5 md5hash; SHA1 sha1hash;

// Get the hash values from the objects md4.GetHash(&md4hash); md5.GetHash(&md5hash); sha1.GetHash(&sha1hash);

// Get the data of the hash, and express it in base 16 and base 32 CString md4base16 = tobase16byte*)(&md4hash), sizeof(md4hash; CString md4base32 = tobase32byte*)(&md4hash), sizeof(md4hash; CString md5base16 = tobase16byte*)(&md5hash), sizeof(md5hash; CString md5base32 = tobase32byte*)(&md5hash), sizeof(md5hash; CString sha1base16 = tobase16byte*)(&sha1hash), sizeof(sha1hash; CString sha1base32 = tobase32byte*)(&sha1hash), sizeof(sha1hash; } </source>

The objects that compute the hashes are of type CMD4, CMD5, and CSHA. The Add and Finish methods take in the data. The hash values are held by MD4, MD5, and SHA1 type variables. You can call sizeof on them to find out how many bytes they hold. The object's GetHash method writes the hash into the hash value variable.

The functions tobase16 and tobase32 take a pointer to some memory and the number of bytes there. They express that data in text using base 16 or base 32, and return it as a string.

Results

Here are the results of running the code above. The first group of computed hashes are what you get when you hash no data at all. The second group is the hash of the 5 bytes that are the ASCII characters of the lowercase word hello.

<source lang="c"> HashData(NULL, 0);

md4base16 31d6cfe0d16ae931b73c59d7e0c089c0 md4base32 ghlm7ygrnlutdnz4lhl6bqejya md5base16 d41d8cd98f00b204e9800998ecf8427e md5base32 2qoyzwmpaczaj2mabgmoz6ccpy sha1base16 da39a3ee5e6b4b0d3255bfef95601890afd80709 sha1base32 3i42h3s6nnfq2msvx7xzkyayscx5qbyj

HashData[[byte*)"hello", 5);

md4base16 866437cb7a794bce2b727acc0362ee27 md4base32 qzsdps32pff44k3splgagyxoe4 md5base16 5d41402abc4b2a76b9719d911017c592 md5base32 lvauakv4jmvhnolrtwiraf6fsi sha1base16 aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d sha1base32 vl2mmho4yxukfwv63yhtwsbm3gxksq2n </source>

camper's code

Things are a little bit different with camper's code. Here's the updated function. The hashes it computes are the same.

<source lang="c"> // Takes a pointer to some memory and the number of bytes of data there // Computes the MD4, MD5, and SHA1 hashes of the data void HashData(byte* p, DWORD bytes) {

// Make objects that will compute the hashes CMD4 md4; CMD5 md5; CSHA sha1;

// Add the data to the hash objects md4.Add(p, bytes); md5.Add(p, bytes); sha1.Add(p, bytes);

// Tell the hash objects that's all the data we are going to add md4.Finish(); md5.Finish(); sha1.Finish();

// Make objects that will hold the hash values CMD4::MD4Digest md4hash; // The hash value for MD4 is different Hashes::Md5Hash md5hash; Hashes::Sha1Hash sha1hash;

// You can get the size of the MD4 hash value type DWORD size = sizeof(md4hash); // 16 bytes

// Get the hash values from the objects md4.GetHash(md4hash); md5.GetHash(md5hash); sha1.GetHash(sha1hash);

// Get the data of the hash, and express it in base 16 CString md4base16 = tobase16byte*)(&md4hash), sizeof(md4hash; CString md4base32 = tobase32byte*)(&md4hash), sizeof(md4hash; CString md5base16 = tobase16(&md5hash[0], Hashes::Md5Hash::byteCount); CString md5base32 = tobase32(&md5hash[0], Hashes::Md5Hash::byteCount); CString sha1base16 = tobase16(&sha1hash[0], Hashes::Sha1Hash::byteCount); CString sha1base32 = tobase32(&sha1hash[0], Hashes::Sha1Hash::byteCount); } </source>

MD4 is pretty much the same, while MD5 and SHA1 are different. To get to the memory of the hash value, use &sha1hash[0]. To know how many bytes are there, use Hashes::Sha1Hash::byteCount.