Exemplo n.º 1
0
        /// <summary>
        /// A method for getting a ED2K hash from a specified file path
        /// </summary>
        /// <param name="filePath">The path for the file to hash</param>
        /// <returns>A string containing the ED2K hash</returns>
        public static string GetED2K(string filePath)
        {
            string hash = String.Empty;

            using (FileStream file = File.OpenRead(filePath))
            {
                byte[]      buffer;
                const int   CHUNK_SIZE      = 9728000;
                double      totalChunkCount = 0;
                long        chunkCount      = 0;
                int         bufferLength    = 0;
                MD4         md4             = new MD4();
                List <byte> md4HashedBytes  = new List <byte>();

                buffer          = new byte[CHUNK_SIZE];
                totalChunkCount = Math.Ceiling(file.Length * 1.0 / CHUNK_SIZE);
                while ((bufferLength = file.Read(buffer, 0, CHUNK_SIZE)) > 0)
                {
                    ++chunkCount;
                    byte[] chunkMd4HashedBytes = MD4.GetByteHashFromBytes(buffer.Take(bufferLength).ToArray());
                    md4HashedBytes.AddRange(chunkMd4HashedBytes);
                    buffer = new byte[CHUNK_SIZE];
                }

                hash = (chunkCount > 1
                        ? MD4.GetHexHashFromBytes(md4HashedBytes.ToArray())
                        : MD4.BytesToHex(md4HashedBytes.ToArray(), md4HashedBytes.Count)).ToLower();
            }

            return(hash);
        }
Exemplo n.º 2
0
        /// <summary>
        /// A method for getting a ED2K hash from a specified file path
        /// </summary>
        /// <param name="file">The FileStream to hash</param>
        /// <returns>A string containing the ED2K hash</returns>
        public static string GetED2K(FileStream file)
        {
            byte[]      buffer;
            const int   CHUNK_SIZE     = 9728000;
            long        chunkCount     = 0;
            List <byte> md4HashedBytes = new List <byte>();

            buffer = new byte[CHUNK_SIZE];
            int bufferLength;

            while ((bufferLength = file.Read(buffer, 0, CHUNK_SIZE)) > 0)
            {
                ++chunkCount;
                byte[] chunkMd4HashedBytes = MD4.GetByteHashFromBytes(buffer.Take(bufferLength).ToArray());
                md4HashedBytes.AddRange(chunkMd4HashedBytes);
                buffer = new byte[CHUNK_SIZE];
            }

            string hash = (chunkCount > 1
        ? MD4.GetHexHashFromBytes(md4HashedBytes.ToArray())
        : MD4.BytesToHex(md4HashedBytes.ToArray(), md4HashedBytes.Count)).ToLower();

            return(hash);
        }