public static Hashes CalculateHashes(string strPath, OnHashProgress onHashProgress, bool getCRC32, bool getMD5, bool getSHA1) { Hashes rhash = new Hashes(); if (Finalise.ModuleHandle != IntPtr.Zero) { byte[] hash = new byte[56]; bool gotHash = false; try { if (CalculateHashes_dll(strPath, ref hash, onHashProgress, getCRC32, getMD5, getSHA1)) { rhash.ed2k = HashToString(hash, 0, 16); if (!string.IsNullOrEmpty(rhash.ed2k)) gotHash = true; if (getCRC32) rhash.crc32 = HashToString(hash, 16, 4); if (getMD5) rhash.md5 = HashToString(hash, 20, 16); if (getSHA1) rhash.sha1 = HashToString(hash, 36, 20); } } catch (Exception ex) { logger.Error( ex,ex.ToString()); } if (!gotHash) { logger.Error("Error using DLL to get hash (Functon returned FALSE), trying C# code instead: {0}", strPath); return CalculateHashes_here(strPath, onHashProgress, getCRC32, getMD5, getSHA1); } return rhash; } return CalculateHashes_here(strPath, onHashProgress, getCRC32, getMD5, getSHA1); }
public static Hashes CalculateHashes(string strPath, OnHashProgress onHashProgress, bool getED2k, bool getCRC32, bool getMD5, bool getSHA1) { // the DLL is returning the wrong results for CRC's so don't use it bool gotED2k = false; bool gotMD5 = false; bool gotSHA1 = false; bool stillNeedInfo = false; Hashes rhash = new Hashes(); if (UseDll()) { byte[] hash = new byte[56]; if (CalculateHashes_dll(strPath, ref hash, onHashProgress, getCRC32, getMD5, getSHA1)) { rhash.ed2k = HashToString(hash, 0, 16); //if (getCRC32) rhash.crc32 = HashToString(hash, 16, 4); if (getMD5) rhash.md5 = HashToString(hash, 20, 16); if (getSHA1) rhash.sha1 = HashToString(hash, 36, 20); gotED2k = getED2k; gotMD5 = getMD5; gotSHA1 = getSHA1; } else { rhash.ed2k = string.Empty; rhash.crc32 = string.Empty; rhash.md5 = string.Empty; rhash.sha1 = string.Empty; } } else stillNeedInfo = true; if (gotED2k) { if (getCRC32) stillNeedInfo = true; } bool getED2kTemp = getED2k && !gotED2k; bool getMD5Temp = getMD5 && !gotMD5; bool getSHA1Temp = getSHA1 && !gotSHA1; if (stillNeedInfo) { Hashes rhashTemp = CalculateHashes_here(strPath, onHashProgress, getED2kTemp, getCRC32, getMD5Temp, getSHA1Temp); rhash.crc32 = rhashTemp.crc32; if (string.IsNullOrEmpty(rhash.ed2k)) rhash.ed2k = rhashTemp.ed2k; if (string.IsNullOrEmpty(rhash.md5)) rhash.md5 = rhashTemp.md5; if (string.IsNullOrEmpty(rhash.sha1)) rhash.sha1 = rhashTemp.sha1; } return rhash; }
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 = (long) fs.Length; long nBytesRemaining = (long) 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; if (onHashProgress != null) onHashProgress(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) ((float) iChunkCount/(float) nBlocks*100); if (onHashProgress != null) onHashProgress(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(); if (onHashProgress != null) onHashProgress(strPath, 100); if (getED2k) { //byte[] baHashFinal = md4.ComputeHash(baED2KHash); //rhash.ed2k = BitConverter.ToString(baHashFinal).Replace("-", "").ToUpper(); rhash.ed2k = nBlocks > 1 ? BitConverter.ToString(md4.ComputeHash(baED2KHash)).Replace("-", "").ToUpper() : BitConverter.ToString(baED2KHash).Replace("-", "").ToUpper(); } if (getCRC32) rhash.crc32 = BitConverter.ToString(crc32.Hash).Replace("-", "").ToUpper(); if (getMD5) rhash.md5 = BitConverter.ToString(md5.Hash).Replace("-", "").ToUpper(); if (getSHA1) rhash.sha1 = BitConverter.ToString(sha1.Hash).Replace("-", "").ToUpper(); return rhash; }
/// <summary> /// Get all the hash info and video/audio info for a video file /// </summary> /// <param name="fileName"></param> /// <param name="hashInfo"></param> /// <param name="vidInfo"></param> public static void GetVideoInfo(string fileName, ref Hashes hashInfo, ref MediaInfoResult vidInfo, bool forceRefresh) { hashInfo = Hasher.CalculateHashes(fileName, null); if (vidInfo == null) vidInfo = new MediaInfoResult(); MediaInfoReader.ReadMediaInfo(fileName, forceRefresh, ref vidInfo); }