protected override byte[] HashFinal() { byte[] buf = null; int nBufPos = 0; byte[] result = null; nBufPos = this.nBufPos; buf = this.buf; buf[nBufPos] = ((byte)1); nBufPos++; if (56 <= nBufPos) { Array.Clear(((Array)buf), nBufPos, ((int)(64 - nBufPos))); this.ProcessBlock(); nBufPos = 0; } Array.Clear(((Array)buf), nBufPos, ((int)((64 - nBufPos) - 8))); Tiger.LongToBytes(((long)(this.lLen << 3)), buf, 56); this.ProcessBlock(); result = new byte[24]; Tiger.LongToBytes(this.a, result, 0); Tiger.LongToBytes(this.b, result, 8); Tiger.LongToBytes(this.c, result, 16); return(result); }
private FileStream FilePtr; //dest file stream pointer. public byte[] GetTTH(string Filename) { byte[] TTH = null; try { if (!File.Exists(Filename)) { System.Diagnostics.Debug.WriteLine("file doesn't exists " + Filename); return(null); } //open the file. FilePtr = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //if the file is 0 byte long. if (FilePtr.Length == ZERO_BYTE_FILE) { Tiger TG = new Tiger(); TTH = TG.ComputeHash(new byte[] { 0 }); } //the file is smaller then a single leaf. else if (FilePtr.Length <= Leaf_Size * Block_Size) { TTH = CompressSmallBlock(); } //normal file. else { Leaf_Count = (int)(FilePtr.Length / Leaf_Size); if (FilePtr.Length % Leaf_Size > 0) { Leaf_Count++; } GetLeafHash(); //get leafs hash from file. System.Diagnostics.Debug.WriteLine("===> [ Moving to internal hash. ]"); TTH = CompressHashBlock(HashValues, Leaf_Count); //get root TTH from hash array. } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("error while trying to get TTH for file: " + Filename + ". (" + e.Message + ")"); TTH = null; } if (FilePtr != null) { FilePtr.Close(); } return(TTH); }
void ProcessLeafs() { FileStream ThreadFilePtr = new FileStream(Filename, FileMode.Open, FileAccess.Read); FileBlock ThreadFileBlock = FileParts[Convert.ToInt16(Thread.CurrentThread.Name)]; Tiger TG = new Tiger(); byte[] DataBlock; byte[] Data = new byte[LeafSize + 1]; long LeafIndex; int BlockLeafs; int i; ThreadFilePtr.Position = ThreadFileBlock.Start; while (ThreadFilePtr.Position < ThreadFileBlock.End) { LeafIndex = ThreadFilePtr.Position / 1024; if (ThreadFileBlock.End - ThreadFilePtr.Position < DataBlockSize) { DataBlock = new byte[ThreadFileBlock.End - ThreadFilePtr.Position]; } else { DataBlock = new byte[DataBlockSize]; } ThreadFilePtr.Read(DataBlock, 0, DataBlock.Length); //read block BlockLeafs = DataBlock.Length / 1024; for (i = 0; i < BlockLeafs; i++) { Buffer.BlockCopy(DataBlock, i * LeafSize, Data, 1, LeafSize); TG.Initialize(); TTH[0][LeafIndex++] = TG.ComputeHash(Data); } if (i * LeafSize < DataBlock.Length) { Data = new byte[DataBlock.Length - BlockLeafs * LeafSize + 1]; Data[0] = LeafHash; Buffer.BlockCopy(DataBlock, BlockLeafs * LeafSize, Data, 1, (Data.Length - 1)); TG.Initialize(); TTH[0][LeafIndex++] = TG.ComputeHash(Data); Data = new byte[LeafSize + 1]; Data[0] = LeafHash; } } DataBlock = null; Data = null; }
public byte[] GetTTH(string Filename) { HashHolder Result; try { FilePtr = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //if the file is 0 byte long. if (FilePtr.Length == ZERO_BYTE_FILE) { Tiger TG = new Tiger(); Result.HashValue = TG.ComputeHash(new byte[] { 0 }); } //if there's only one block in file use SmallFile(). else if (FilePtr.Length <= Block_Size) { Result.HashValue = SmallFile(); } else { //get how many leafs are in file. Leaf_Count = (int)FilePtr.Length / Block_Size; if (FilePtr.Length % Block_Size > 0) { Leaf_Count++; } //load blocks of data and get tiger hash for each one. LoadLeafHash(); //get root hash from blocks hash. Result = GetRootHash(); } FilePtr.Close(); return(Result.HashValue); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("error while trying to get TTH for file: " + Filename + ". (" + e.Message + ")"); if (FilePtr != null) { FilePtr.Close(); } return(null); } }
private byte[] SmallFile() { Tiger TG = new Tiger(); byte[] Block = new byte[Block_Size]; int BlockSize = FilePtr.Read(Block, 0, 1024); //gets hash for a single block file. return(LH(ByteExtract(Block, BlockSize))); }
private byte[] LeafHash(byte[] Raw_Data) //leaf hash. { byte[] Data = new byte[Raw_Data.Length + 1]; Data[0] = 0x00; //leaf hash mark. Raw_Data.CopyTo(Data, 1); //gets tiger hash value for leafs blocks. Tiger TG = new Tiger(); TG.Initialize(); return(TG.ComputeHash(Data)); }
void ProcessInternalLeaf(int Level, int Index, byte[] LeafA, byte[] LeafB) { Tiger TG = new Tiger(); byte[] Data = new byte[LeafA.Length + LeafB.Length + 1]; Data[0] = InternalHash; Buffer.BlockCopy(LeafA, 0, Data, 1, LeafA.Length); Buffer.BlockCopy(LeafB, 0, Data, LeafA.Length + 1, LeafA.Length); TG.Initialize(); TTH[Level][Index] = TG.ComputeHash(Data); }
private byte[] InternalHash(byte[] LeafA, byte[] LeafB) //internal hash. { byte[] Data = new byte[LeafA.Length + LeafB.Length + 1]; Data[0] = 0x01; //internal hash mark. //combines two leafs. LeafA.CopyTo(Data, 1); LeafB.CopyTo(Data, LeafA.Length + 1); //gets tiger hash value for combined leaf hash. Tiger TG = new Tiger(); TG.Initialize(); return(TG.ComputeHash(Data)); }
bool Initialize() { if (FilePtr.Length == ZERO_BYTE_FILE) { Tiger TG = new Tiger(); LevelCount = 1; TTH = new byte[1][][]; TTH[0] = new byte[1][]; TTH[0][0] = TG.ComputeHash(new byte[1] { 0 }); return(false); } else { int i = 1; LevelCount = 1; LeafCount = (int)(FilePtr.Length / LeafSize); if ((FilePtr.Length % LeafSize) > 0) { LeafCount++; } while (i < LeafCount) { i *= 2; LevelCount++; } TTH = new byte[LevelCount][][]; TTH[0] = new byte[LeafCount][]; } return(true); }
public static bool SelfTest() { ASCIIEncoding enc = null; byte[] hash = null; int nI = 0; string TEST_DATA = null; byte[] TEST_HASH = null; Tiger tg = null; int _Vb_t_i4_0 = 0; byte[] _Vb_t_array_0 = null; TEST_DATA = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; _Vb_t_array_0 = new byte[] { ((byte)15), ((byte)123), ((byte)249), ((byte)161), ((byte)155), ((byte)156), ((byte)88), ((byte)242), ((byte)183), ((byte)97), ((byte)13), ((byte)247), ((byte)232), ((byte)79), ((byte)10), ((byte)195), ((byte)167), ((byte)28), ((byte)99), ((byte)30), ((byte)123), ((byte)83), ((byte)247), ((byte)142) }; TEST_HASH = _Vb_t_array_0; tg = new Tiger(); tg.Initialize(); enc = new ASCIIEncoding(); hash = tg.ComputeHash(enc.GetBytes(TEST_DATA)); if (hash.Length != TEST_HASH.Length) { return(false); } _Vb_t_i4_0 = (TEST_HASH.Length - 1); for (nI = 0; (nI <= _Vb_t_i4_0); nI++) { if (hash[nI] != TEST_HASH[nI]) { return(false); } } return(true); }