Click here to Skip to main content
Click here to Skip to main content

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

By , 9 May 2007
 

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

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

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

License

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

About the Author

Gil.Schmidt
Team Leader EZFace
Israel Israel
Member
Web Team Leader

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: Index was outside the bounds of the array.memberFlow847 May '07 - 20:12 
I just downloaded the source and it is the old one. Frown | :(
GeneralRe: Index was outside the bounds of the array.memberGil.Schmidt7 May '07 - 22:40 
you need to wait for CodeProject to update.
i already sent the fixed version.
GeneralVery good projectmemberFlow8413 Jul '06 - 2:24 
Just wanted to thank you for this great project. Smile | :)
Im using it in http://fmdc.no-ip.org/
GeneralRe: Very good projectmemberGil_Schmidt19 Mar '07 - 6:50 
if you can please keep my name and email at the credits of the program
 
thanks, Gil
QuestionTesting?memberJens Scheidtmann24 Jan '05 - 19:59 
Dear Gil,
 
As this algorithm "is not fun": Have you tested the algorithm's validity?
 
I.e. There's test data available on the Tiger website. Have you checked, that your implementation of Tiger matches the results given there?
 
In addition you only supply an interface for the root-hash, what a about the intermediate hashes? What about those verification procedures mentioned on the THEX website?
 
I guess, if you answered these questions in your article up front or chose another name, you would have got better ratings.
 
Thanks for the links, that were very good reads. Smile | :)
 
Jens
 
P.S.: I didn't rate your article, yet.
 
--
Jens Scheidtmann

AnswerRe: Testing?memberGil_Schmidt24 Jan '05 - 21:08 
Dear Jens
 
ofcourse i tested the and it matches the values in (both the tiger hash and the thex values).
 
i supply this interface as a tth tool not a tth tree you can change the code as you wise to get you the tree you want the base is here save the values won't be so hard (and won't take much time from the cpu as well)
 
about the rating i don't care about them as long as they don't commet just putting bad / good rating without a commet to explain why they put it is not helpful.
 
thanks for the commet i appricate it
 
Gil

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 9 May 2007
Article Copyright 2005 by Gil.Schmidt
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid