public CalculationResult Calc(FileInfo file) { CalculationResult result = null; using (var stream = file.OpenRead()) { var totalChunkCount = Math.Ceiling(file.Length * 1.0 / ED2K_CHUNK_SIZE); var chunkCount = 0; var bufferLength = 0; var buffer = new byte[ED2K_CHUNK_SIZE]; var md4HashedBytes = new List <byte>(); string hash = null; while ((bufferLength = stream.Read(buffer, 0, ED2K_CHUNK_SIZE)) > 0) { ++chunkCount; var chunkMd4HashedBytes = _md4.GetByteHashFromBytes(buffer.Take(bufferLength).ToArray()); md4HashedBytes.AddRange(chunkMd4HashedBytes); Program.ClearLine(); Console.Write($"{chunkCount}/{totalChunkCount}: {file.Name} "); buffer = new byte[ED2K_CHUNK_SIZE]; } Program.ClearLine(); hash = chunkCount > 1 ? _md4.GetHexHashFromBytes(md4HashedBytes.ToArray()) : MD4.BytesToHex(md4HashedBytes.ToArray(), md4HashedBytes.Count); result = new CalculationResult(file, hash); } return(result); }
/// <summary> /// This constructor is here to implement the clonability of this class /// </summary> /// <param name = "md"> </param> private MD4(MD4 md) : this() { //this(); context = (uint[])md.context.Clone(); buffer = (byte[])md.buffer.Clone(); count = md.count; }
/// <summary> /// Returns a binary hash from an input byte array /// </summary> /// <param name = "b">byte-array to hash</param> /// <returns>binary hash of input</returns> public byte[] GetByteHashFromBytes(byte[] b) { var md4 = new MD4(); md4.EngineUpdate(b, 0, b.Length); return(md4.EngineDigest()); }
/// <summary> /// Returns a byte hash from the input byte /// </summary> /// <param name = "b">byte to hash</param> /// <returns>binary hash of the input byte</returns> public byte[] GetByteHashFromByte(byte b) { var md4 = new MD4(); md4.EngineUpdate(b); return(md4.EngineDigest()); }
/// <summary> /// Returns a byte hash from a string /// </summary> /// <param name = "s">string to hash</param> /// <returns>byte-array that contains the hash</returns> public byte[] GetByteHashFromString(string s) { byte[] b = Encoding.UTF8.GetBytes(s); var md4 = new MD4(); md4.EngineUpdate(b, 0, b.Length); return(md4.EngineDigest()); }