private FileStream FilePtr; //dest file stream pointer. /// <summary> /// Gets a TTH hash for the file /// </summary> /// <param name="filename">The path of the file</param> /// <returns>The hash of the file in byte array form</returns> public byte[] GetTTH(string filename) { byte[] TTH; 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); }
private static 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)); }
private static 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)); }