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) { if (UseDll()) { byte[] hash = new byte[56]; Hashes rhash = new Hashes(); if (CalculateHashes_dll(strPath, ref hash, onHashProgress)) { rhash.ed2k = HashToString(hash, 0, 16); rhash.crc32 = HashToString(hash, 16, 4); rhash.md5 = HashToString(hash, 20, 16); rhash.sha1 = HashToString(hash, 36, 20); } else { rhash.ed2k = string.Empty; rhash.crc32 = string.Empty; rhash.md5 = string.Empty; rhash.sha1 = string.Empty; } return rhash; } return CalculateHashes_here(strPath, onHashProgress); }
// Calculates hash immediately (with progress) protected static bool CalculateHashes_dll(string strFileName, ref byte[] hash, OnHashProgress HashProgress) { OnHashProgress pHashProgress = new OnHashProgress(HashProgress); GCHandle gcHashProgress = GCHandle.Alloc(pHashProgress); //to make sure the GC doesn't dispose the delegate int nResult = CalculateHashes_callback_dll(strFileName, hash, pHashProgress); return (nResult == 0); }
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; }
// Calculates hash immediately (with progress) protected static bool CalculateHashes_dll(string strFileName, ref byte[] hash, OnHashProgress HashProgress, bool getCRC32, bool getMD5, bool getSHA1) { logger.Trace("Using DLL to hash file: {0}", strFileName); OnHashProgress pHashProgress = new OnHashProgress(HashProgress); GCHandle gcHashProgress = GCHandle.Alloc(pHashProgress); //to make sure the GC doesn't dispose the delegate int nResult = CalculateHashes_callback_dll(strFileName, hash, pHashProgress, getCRC32, getMD5, getSHA1); return (nResult == 0); }
// Calculates hash immediately (with progress) protected static bool CalculateHashes_dll(string strFileName, ref byte[] hash, OnHashProgress HashProgress, bool getCRC32, bool getMD5, bool getSHA1) { logger.Trace("Using DLL to hash file: {0}", strFileName); OnHashProgress pHashProgress = new OnHashProgress(HashProgress); GCHandle gcHashProgress = GCHandle.Alloc(pHashProgress); //to make sure the GC doesn't dispose the delegate int nResult = CalculateHashes_callback_dll(strFileName, hash, pHashProgress, getCRC32, getMD5, getSHA1); return(nResult == 0); }
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); }
public static Hashes CalculateHashes(string strPath, OnHashProgress onHashProgress) { return(CalculateHashes(strPath, onHashProgress, true, true, true)); }
protected static Hashes CalculateHashes_here(string strPath, OnHashProgress onHashProgress) { FileStream fs; Hashes rhash=new Hashes(); FileInfo fi = new FileInfo(strPath); fs = fi.OpenRead(); int lChunkSize = 9728000; //string sHash = ""; 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++; Console.WriteLine("Hashing Chunk: " + iChunkCount.ToString()); int nBytesRead = fs.Read(ByteArray, 0, nBytesToRead); byte[] baHash = md4.ComputeHash(ByteArray, 0, nBytesRead); md5.TransformBlock(ByteArray, 0, nBytesRead, ByteArray, 0); sha1.TransformBlock(ByteArray, 0, nBytesRead, ByteArray, 0); crc32.TransformBlock(ByteArray, 0, nBytesRead, ByteArray, 0); int percentComplete = (int)((float)iChunkCount / (float)nBlocks * 100); if (onHashProgress != null) onHashProgress(strPath, percentComplete); int j = (int)((iChunkCount - 1) * 16); for (int i = 0; i < 16; i++) baED2KHash[j + i] = baHash[i]; iOffSet += lChunkSize; nBytesRemaining = nBytes - iOffSet; if (nBytesRemaining < lChunkSize) nBytesToRead = (int)nBytesRemaining; } md5.TransformFinalBlock(ByteArray, 0, 0); sha1.TransformFinalBlock(ByteArray, 0, 0); crc32.TransformFinalBlock(ByteArray, 0, 0); fs.Close(); if (onHashProgress != null) onHashProgress(strPath, 100); byte[] baHashFinal = md4.ComputeHash(baED2KHash); rhash.ed2k = BitConverter.ToString(baHashFinal).Replace("-", "").ToUpper(); rhash.crc32 = BitConverter.ToString(crc32.Hash).Replace("-", "").ToUpper(); rhash.md5 = BitConverter.ToString(md5.Hash).Replace("-", "").ToUpper(); rhash.sha1 = BitConverter.ToString(sha1.Hash).Replace("-", "").ToUpper(); return rhash; }
internal void RaiseOnHashProgress(int percentageDone, string message) { OnHashProgress?.Invoke(percentageDone, message); }
// Calculates hash immediately (with progress) protected static bool CalculateHashes_dll(string strFileName, ref byte[] hash, OnHashProgress HashProgress) { OnHashProgress pHashProgress = new OnHashProgress(HashProgress); GCHandle gcHashProgress = GCHandle.Alloc(pHashProgress); //to make sure the GC doesn't dispose the delegate int nResult = CalculateHashes_callback_dll(strFileName, hash, pHashProgress); return(nResult == 0); }
private static extern int CalculateHashes_callback_dll( [MarshalAs(UnmanagedType.LPWStr)] string szFileName, [MarshalAs(UnmanagedType.LPArray)] byte[] hash, [MarshalAs(UnmanagedType.FunctionPtr)] OnHashProgress lpHashProgressFunc );
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; int rval = -1; try { string filename = strPath; if (Utils.IsLinux) { (string e2Dk, string crc32, string md5, string sha1) = NativeHasher.GetHash(filename); rhash.ED2K = e2Dk; if (!string.IsNullOrEmpty(rhash.ED2K)) { gotHash = true; } if (getCRC32) { rhash.CRC32 = crc32; } if (getMD5) { rhash.MD5 = md5; } if (getSHA1) { rhash.SHA1 = sha1; } } else { filename = strPath.StartsWith(@"\\") ? strPath : @"\\?\" + strPath; //only prepend non-UNC paths (or paths that have this already) rval = CalculateHashes_dll(filename, ref hash, onHashProgress, getCRC32, getMD5, getSHA1); if (rval == 0) { 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) { return(rhash); } logger.Error("Error using DLL to get hash (Functon returned {0}), trying C# code instead: {1}", rval, strPath); } return(CalculateHashes_here(strPath, onHashProgress, getCRC32, getMD5, getSHA1)); }
public static Hashes CalculateHashes(string strPath, OnHashProgress onHashProgress) { return CalculateHashes(strPath, onHashProgress, true, true, true); }
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); }