Developers.Hash.RoundTrip: Difference between revisions

From Shareaza Wiki
Jump to navigation Jump to search
(Importing page from Tikiwiki)
 
m (1 revision)
 

Latest revision as of 20:09, 20 June 2009

Hash to text and back again

These samples demonstrate the toString, toUrn, fromString, and fromUrn methods on the hash value objects in camper's code. They perform the following steps:

  1. Compute a hash
  2. Turn it into text
  3. Turn it back into a hash
  4. Make sure that didn't change it

When run, direct, direct2, and direct3 are always all the same.

MD5

<source lang="c"> // Takes a pointer to some memory and the number of bytes of data there // Computes the MD5 hash of the information and converts it to and from text void HashMD5(byte* p, DWORD bytes) {

// Hash the data CMD5 hash; hash.Add(p, bytes); hash.Finish(); Hashes::Md5Hash value; hash.GetHash(value);

// Turn it into text CString direct, tostring, tourn; direct = tobase16(&value[0], Hashes::Md5Hash::byteCount); tostring = value.toString(); tourn = value.toUrn();

// Turn it back into a hash value Hashes::Md5Hash value2, value3; value2.fromString(tostring); value3.fromUrn(tourn);

// Access the memory directly to confirm that didn't change it CString direct2, direct3; direct2 = tobase16(&value2[0], Hashes::Md5Hash::byteCount); direct3 = tobase16(&value3[0], Hashes::Md5Hash::byteCount); } </source>

SHA1

<source lang="c"> // Takes a pointer to some memory and the number of bytes of data there // Computes the SHA1 hash of the information and converts it to and from text void HashSHA1(byte* p, DWORD bytes) {

// Hash the data CSHA hash; hash.Add(p, bytes); hash.Finish(); Hashes::Sha1Hash value; hash.GetHash(value);

// Turn it into text CString direct, tostring, tourn; direct = tobase32(&value[0], Hashes::Sha1Hash::byteCount).MakeUpper(); tostring = value.toString(); tourn = value.toUrn();

// Turn it back into a hash value Hashes::Sha1Hash value2, value3; value2.fromString(tostring); value3.fromUrn(tourn);

// Access the memory directly to confirm that didn't change it CString direct2, direct3; direct2 = tobase32(&value2[0], Hashes::Sha1Hash::byteCount).MakeUpper(); direct3 = tobase32(&value3[0], Hashes::Sha1Hash::byteCount).MakeUpper(); } </source>

TigerTree

<source lang="c"> // Takes a pointer to some memory and the number of bytes of data there // Computes the TigerTree root hash of the information and converts it to and from text void HashTiger(byte* p, DWORD bytes) {

// Hash the data CTigerTree hash; hash.BeginFile(9, bytes); // Tree height of 9 hash.AddToFile(p, bytes); hash.FinishFile(); Hashes::TigerHash value; hash.GetRoot(value);

// Turn it into text CString direct, tostring, tourn; direct = tobase32(&value[0], Hashes::TigerHash::byteCount).MakeUpper(); tostring = value.toString(); tourn = value.toUrn();

// Turn it back into a hash value Hashes::TigerHash value2, value3; value2.fromString(tostring); value3.fromUrn(tourn);

// Access the memory directly to confirm that didn't change it CString direct2, direct3; direct2 = tobase32(&value2[0], Hashes::TigerHash::byteCount).MakeUpper(); direct3 = tobase32(&value3[0], Hashes::TigerHash::byteCount).MakeUpper(); } </source>

eDonkey2000

<source lang="c"> // Takes a pointer to some memory and the number of bytes of data there // Computes the eDonkey2000 root hash of the information and converts it to and from text void HashDonkey(byte* p, DWORD bytes) {

// Hash the data CED2K hash; hash.BeginFile(bytes); hash.AddToFile(p, bytes); hash.FinishFile(); Hashes::Ed2kHash value; hash.GetRoot(value);

// Turn it into text CString direct, tostring, tourn; direct = tobase16(&value[0], Hashes::Ed2kHash::byteCount); tostring = value.toString(); tourn = value.toUrn();

// Turn it back into a hash value Hashes::Ed2kHash value2, value3; value2.fromString(tostring); value3.fromUrn(tourn);

// Access the memory directly to confirm that didn't change it CString direct2, direct3; direct2 = tobase16(&value2[0], Hashes::Ed2kHash::byteCount); direct3 = tobase16(&value3[0], Hashes::Ed2kHash::byteCount); } </source>

Questions

  • What about MD4?
  • These objects all have a toShortUrn method, but there is no corresponding fromShortUrn