Click here to Skip to main content
14,601,061 members

ThexCS - TTH (tiger tree hash) maker in C#

Rate this:
4.40 (14 votes)
Please Sign up or sign in to vote.
4.40 (14 votes)
9 May 2007CPOL
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


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 improve 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


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" is the TTH tree.

//for ThexOptimized
ThexOptimized TTH = new ThexOptimized();

//for ThexThreaded
ThexThreaded TTH = new ThexThreaded();


  • 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).
  • the code of the project was converted to Visual Studio 2005


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Team Leader
Israel Israel
No Biography provided

Comments and Discussions

QuestionDoen't work in big files Pin
Victor Tereschenko30-Mar-12 16:11
MemberVictor Tereschenko30-Mar-12 16:11 
GeneralHash wrong when (length % 64) = 55 (TIGER Algorithm, not TTH) [modified] Pin
DvdKhl8-Sep-10 7:20
MemberDvdKhl8-Sep-10 7:20 
GeneralTthOptimized bug [modified] Pin
Erty Hackward15-Oct-09 22:52
MemberErty Hackward15-Oct-09 22:52 
Generalneed help Pin
tanmay83200331-Aug-08 20:32
Membertanmay83200331-Aug-08 20:32 
GeneralRe: need help Pin
Gil.Schmidt21-Jan-09 21:55
MemberGil.Schmidt21-Jan-09 21:55 
GeneralException Pin
Member 39274966-Apr-08 22:25
MemberMember 39274966-Apr-08 22:25 
GeneralRe: Exception Pin
Member 104517077-Dec-13 10:36
MemberMember 104517077-Dec-13 10:36 
GeneralRe: Exception Pin
Member 104517077-Dec-13 10:38
MemberMember 104517077-Dec-13 10:38 
GeneralThe C++ version Pin
chizhan27-May-07 8:08
Memberchizhan27-May-07 8:08 
GeneralRe: The C++ version Pin
Gil.Schmidt27-May-07 8:47
MemberGil.Schmidt27-May-07 8:47 
GeneralRe: The C++ version Pin
Member 104517077-Dec-13 10:39
MemberMember 104517077-Dec-13 10:39 
GeneralIndex was outside the bounds of the array. Pin
Flow846-May-07 14:20
MemberFlow846-May-07 14:20 
GeneralRe: Index was outside the bounds of the array. Pin
Gil.Schmidt6-May-07 22:04
MemberGil.Schmidt6-May-07 22:04 
GeneralRe: Index was outside the bounds of the array. Pin
Flow847-May-07 20:12
MemberFlow847-May-07 20:12 
GeneralRe: Index was outside the bounds of the array. Pin
Gil.Schmidt7-May-07 22:40
MemberGil.Schmidt7-May-07 22:40 
GeneralRe: Index was outside the bounds of the array. Pin
Member 104517077-Dec-13 10:42
MemberMember 104517077-Dec-13 10:42 
GeneralRe: Index was outside the bounds of the array. Pin
Member 104517077-Dec-13 10:54
MemberMember 104517077-Dec-13 10:54 
GeneralVery good project Pin
Flow8413-Jul-06 2:24
MemberFlow8413-Jul-06 2:24 
GeneralRe: Very good project Pin
Gil.Schmidt19-Mar-07 6:50
MemberGil.Schmidt19-Mar-07 6:50 
GeneralRe: Very good project Pin
Member 104517077-Dec-13 10:55
MemberMember 104517077-Dec-13 10:55 
GeneralRe: Very good project Pin
Member 104517077-Dec-13 10:59
MemberMember 104517077-Dec-13 10:59 
QuestionTesting? Pin
Jens Scheidtmann24-Jan-05 19:59
MemberJens Scheidtmann24-Jan-05 19:59 
AnswerRe: Testing? Pin
Gil.Schmidt24-Jan-05 21:08
MemberGil.Schmidt24-Jan-05 21:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 17 Jan 2005


34 bookmarked