internal void RaiseOnHashProgress(int percentageDone, string message) { OnHashProgress?.Invoke(percentageDone, message); }
public static Hashes CalculateHashes_here(string strPath, OnHashProgress onHashProgress, bool getCRC32, bool getMD5, bool getSHA1) { bool getED2k = true; logger.Trace("Using C# code to has file: {0}", strPath); FileStream fs; Hashes rhash = new Hashes(); FileInfo fi = new FileInfo(strPath); fs = fi.OpenRead(); int lChunkSize = 9728000; long nBytes = fs.Length; long nBytesRemaining = fs.Length; int nBytesToRead = 0; long nBlocks = nBytes / lChunkSize; long nRemainder = nBytes % lChunkSize; //mod if (nRemainder > 0) { nBlocks++; } byte[] baED2KHash = new byte[16 * nBlocks]; if (nBytes > lChunkSize) { nBytesToRead = lChunkSize; } else { nBytesToRead = (int)nBytesRemaining; } onHashProgress?.Invoke(strPath, 0); MD4 md4 = MD4.Create(); MD5 md5 = MD5.Create(); SHA1 sha1 = SHA1.Create(); Crc32 crc32 = new Crc32(); byte[] ByteArray = new byte[nBytesToRead]; long iOffSet = 0; long iChunkCount = 0; while (nBytesRemaining > 0) { iChunkCount++; //logger.Trace("Hashing Chunk: " + iChunkCount.ToString()); int nBytesRead = fs.Read(ByteArray, 0, nBytesToRead); if (getED2k) { byte[] baHash = md4.ComputeHash(ByteArray, 0, nBytesRead); int j = (int)((iChunkCount - 1) * 16); for (int i = 0; i < 16; i++) { baED2KHash[j + i] = baHash[i]; } } if (getMD5) { md5.TransformBlock(ByteArray, 0, nBytesRead, ByteArray, 0); } if (getSHA1) { sha1.TransformBlock(ByteArray, 0, nBytesRead, ByteArray, 0); } if (getCRC32) { crc32.TransformBlock(ByteArray, 0, nBytesRead, ByteArray, 0); } int percentComplete = (int)(iChunkCount / (float)nBlocks * 100); onHashProgress?.Invoke(strPath, percentComplete); iOffSet += lChunkSize; nBytesRemaining = nBytes - iOffSet; if (nBytesRemaining < lChunkSize) { nBytesToRead = (int)nBytesRemaining; } } if (getMD5) { md5.TransformFinalBlock(ByteArray, 0, 0); } if (getSHA1) { sha1.TransformFinalBlock(ByteArray, 0, 0); } if (getCRC32) { crc32.TransformFinalBlock(ByteArray, 0, 0); } fs.Close(); onHashProgress?.Invoke(strPath, 100); if (getED2k) { //byte[] baHashFinal = md4.ComputeHash(baED2KHash); //rhash.ed2k = BitConverter.ToString(baHashFinal).Replace("-", string.Empty).ToUpper(); rhash.ED2K = nBlocks > 1 ? BitConverter.ToString(md4.ComputeHash(baED2KHash)).Replace("-", string.Empty).ToUpper() : BitConverter.ToString(baED2KHash).Replace("-", string.Empty).ToUpper(); } if (getCRC32) { rhash.CRC32 = BitConverter.ToString(crc32.Hash).Replace("-", string.Empty).ToUpper(); } if (getMD5) { rhash.MD5 = BitConverter.ToString(md5.Hash).Replace("-", string.Empty).ToUpper(); } if (getSHA1) { rhash.SHA1 = BitConverter.ToString(sha1.Hash).Replace("-", string.Empty).ToUpper(); } return(rhash); }