Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ThexCS - TTH (Tiger Tree Hash) Maker in C#

0.00/5 (No votes)
9 May 2007 1  
Tiger Tree Hash is a common way for hashing files. It is mostly known in DirectConnect P2P network which uses it as the hash algorithm.

Screenshot - ThexCS.jpg

Introduction

This code was originally written for a project called SharpConnect - a C# client of the DirectConnect protocol.

Several updates have been posted, this is the fourth update. This newest version now includes:

  • optional getting the whole TTH tree instead of just the TTH root value
  • use of threads for better CPU usage
  • tree compression for better memory usage

If you are looking for the best time processing performance, use ThexOptimized, if you like to keep your CPU usage low, try ThexThread. The oldest version (Thex.cs) is also included into the zip file but that is just to keep track on the updates.

Problems on the Way

Threads colliding in the FileStream.Read stage which throw an exception that was caused from two or more threads reading the file at the same time.
Solution: I tried fixing it up with Monitor which made it thread-safe but slower and overloaded the CPU. Finally, a FileStream was added for each thread which required a rewrite of the threads code. I also decide to update the way data is pulled into memory - the new method is to read data in big blocks instead of reading it leaf (1024K) by leaf which improves the file I/O reads number dramatically).

Usage Tips

  • You should have a look at the Block_Size value to adjust performance.
  • If you use ThexThreaded, you should try and test ThreadCount value for your own needs. I recommend 4-6 threads for stable CPU usage, 1-3 threads for faster results.

Background

Tiger Tree Hash is constructed from two parts, the Tiger Hash Algorithm and Merkle Tree.

The original Tiger Hash code was taken from Tiger.Net which is written in Visual Basic.NET.

Using the Code

  • GetTTH_Value returns the root TTH value in byte[] array.
  • GetTTH_Tree returns a byte[][][] array of the full tree.

Tip: You can have a look at LevelCount for checking how "deep" the TTH tree is.

//for ThexOptimized
ThexOptimized TTH = new ThexOptimized();
Console.WriteLine(Base32.ToBase32String(TTH.GetTTH(@"c:\joy.txt"));

//for ThexThreaded
ThexThreaded TTH = new ThexThreaded();
Console.WriteLine(Base32.ToBase32String(TTH.GetTTH_Value(@"c:\joy.txt"));

History

  • First version only calculated an algorithm for getting TTH values very slowly
  • Second version was based on byte arrays instead of ArrayList and compressed the block to make the byte arrays smaller which got the values faster and was called ThexOptimized
  • Third version is based on threads that were supposed to make the code much faster but then turned out to be almost the same. However, CPU usage is much better.
  • Fixed 0 byte file calculation (thanks Flow84)
  • Improved GUI (added option to chose ThexCS method and time calculation)
  • Code of the project was converted to Visual Studio 2005

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here