예제 #1
0
        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);
        }
예제 #2
0
        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));
        }
예제 #3
0
        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));
        }
예제 #4
0
        /// <summary>
        /// Generates the final hash
        /// </summary>
        /// <returns>The hashed byte array</returns>
        protected override byte[] HashFinal()
        {
            int nBufPos = this.nBufPos;

            byte[] 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();
            byte[] result = new byte[24];
            Tiger.LongToBytes(this.a, result, 0);
            Tiger.LongToBytes(this.b, result, 8);
            Tiger.LongToBytes(this.c, result, 16);
            return(result);
        }