// Thanks to http://www.infinitec.de/post/2007/06/09/Displaying-progress-updates-when-hashing-large-files.aspx public static string FileMD5(string path, HashProgress hander = null) { byte[] buffer; byte[] oldBuffer; int bytesRead; int oldBytesRead; long size; long totalBytesRead = 0; StringBuilder result; int prevPrecentage = -1; int percentage; bool cancel = false; using (Stream stream = File.OpenRead(path)) using (HashAlgorithm hashAlgorithm = MD5.Create()) { size = stream.Length; buffer = new byte[8192]; bytesRead = stream.Read(buffer, 0, buffer.Length); totalBytesRead += bytesRead; do { oldBytesRead = bytesRead; oldBuffer = buffer; buffer = new byte[8192]; bytesRead = stream.Read(buffer, 0, buffer.Length); totalBytesRead += bytesRead; if (bytesRead == 0) { hashAlgorithm.TransformFinalBlock(oldBuffer, 0, oldBytesRead); } else { hashAlgorithm.TransformBlock(oldBuffer, 0, oldBytesRead, oldBuffer, 0); } if (hander != null) { // Stop ourselves firing handlers all the bloody time percentage = (int)((double)totalBytesRead * 100 / size); if (percentage > prevPrecentage) { cancel = !hander(percentage); prevPrecentage = percentage; } } //BackgroundWorker.ReportProgress((int)((double)totalBytesRead * 100 / size)); } while (!cancel && bytesRead != 0); if (cancel) { return(null); } byte[] finalBytes = hashAlgorithm.Hash; result = new StringBuilder(finalBytes.Length * 2); foreach (byte b in finalBytes) { result.AppendFormat("{0:x2}", b); } } return(result.ToString()); }
public async Task<string> GetUniqueHash( IDispatcher d, HashProgress notifier) { if (m_hash == null) { await CalculateHash(d, notifier); } return m_hash; }
public async Task <string> GetUniqueHash( IDispatcher d, HashProgress notifier) { if (m_hash == null) { await CalculateHash(d, notifier); } return(m_hash); }
private void NotifyHashBegin(IDispatcher dispatcher, HashProgress notifier) { if (notifier != null) { Action action = () => notifier(Path, 0); if (dispatcher != null) { dispatcher.Execute(action); } else { action(); } } }
private void NotifyHashEnd(IDispatcher dispatcher, HashProgress notifier) { if (notifier != null) { Action action = () => notifier(Path, 100); if (dispatcher != null) { dispatcher.Execute(action); } else { action(); } } }
// Thanks to http://www.infinitec.de/post/2007/06/09/Displaying-progress-updates-when-hashing-large-files.aspx public static string FileMD5(string path, HashProgress hander = null) { byte[] buffer; byte[] oldBuffer; int bytesRead; int oldBytesRead; long size; long totalBytesRead = 0; StringBuilder result; int prevPrecentage = -1; int percentage; bool cancel = false; using (Stream stream = File.OpenRead(path)) using (HashAlgorithm hashAlgorithm = MD5.Create()) { size = stream.Length; buffer = new byte[8192]; bytesRead = stream.Read(buffer, 0, buffer.Length); totalBytesRead += bytesRead; do { oldBytesRead = bytesRead; oldBuffer = buffer; buffer = new byte[8192]; bytesRead = stream.Read(buffer, 0, buffer.Length); totalBytesRead += bytesRead; if (bytesRead == 0) { hashAlgorithm.TransformFinalBlock(oldBuffer, 0, oldBytesRead); } else { hashAlgorithm.TransformBlock(oldBuffer, 0, oldBytesRead, oldBuffer, 0); } if (hander != null) { // Stop ourselves firing handlers all the bloody time percentage = (int)((double)totalBytesRead * 100 / size); if (percentage > prevPrecentage) { cancel = !hander(percentage); prevPrecentage = percentage; } } //BackgroundWorker.ReportProgress((int)((double)totalBytesRead * 100 / size)); } while (!cancel && bytesRead != 0); if (cancel) return null; byte[] finalBytes = hashAlgorithm.Hash; result = new StringBuilder(finalBytes.Length * 2); foreach (byte b in finalBytes) result.AppendFormat("{0:x2}", b); } return result.ToString(); }
//fake async to keep to interface public async Task <string> GetUniqueHash( IDispatcher dispatcher, HashProgress notifier) { if (m_hash == null) { NotifyHashBegin(dispatcher, notifier); MD5 md5 = MD5.Create(); using (FileStream stream = System.IO.File.OpenRead(Path)) { m_hash = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower(); } NotifyHashEnd(dispatcher, notifier); } return(m_hash); }
//fake async to keep to interface public async Task<string> GetUniqueHash( IDispatcher dispatcher, HashProgress notifier) { if (m_hash == null) { NotifyHashBegin(dispatcher, notifier); MD5 md5 = MD5.Create(); using (FileStream stream = System.IO.File.OpenRead(Path)) { m_hash = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower(); } NotifyHashEnd(dispatcher, notifier); } return m_hash; }
void fileHashUtil_ProgressChanged(object sender, HashProgress e) { //int progressVal = Convert.ToInt32((double)e.BytesHashed / (double)e.FileSize); //this.Invoke(new ProgressCallback(SetProgressBarValue), progressVal); }
private async Task CalculateHash( IDispatcher d, HashProgress notifier) { if (notifier != null) { Action action = () => notifier(Path, 0); if (d == null) { action(); } else { d.Execute(action); } } //TODO - exception handling StorageFile file = await GetFromRoot(m_path, m_root); int num_chunks = (int) (GetSize()/Chunker.chunk_size) + 1; int hash_size = num_chunks*32; float current_chunk = 0; var hash_builder = new StringBuilder(hash_size); m_hash = ""; var chunker = new Chunker(GetSize()); foreach (Chunk chunk in chunker.GetChunks()) { using (IRandomAccessStream inputStream = await file.OpenAsync(FileAccessMode.Read)) { using (var dataReader = new DataReader(inputStream.GetInputStreamAt(chunk.Start))) { await dataReader.LoadAsync(chunk.Length); IBuffer buf = dataReader.ReadBuffer(chunk.Length); IBuffer hashed = m_alg.HashData(buf); hash_builder.Append(CryptographicBuffer.EncodeToHexString(hashed)); } } current_chunk++; if (notifier != null) { float percent_done = current_chunk/num_chunks; Action action = () => notifier(Path, percent_done*100); if (d == null) { action(); } else { d.Execute(action); } } } m_hash = hash_builder.ToString(); if (hash_size > 32) //hash the hash { // Convert the string to UTF8 binary data. IBuffer hashbuf = CryptographicBuffer.ConvertStringToBinary(m_hash, BinaryStringEncoding.Utf8); IBuffer hashed = m_alg.HashData(hashbuf); m_hash = CryptographicBuffer.EncodeToHexString(hashed); } if (notifier != null) { Action action = () => notifier(Path, 100); if (d == null) { action(); } else { d.Execute(action); } } }
private async Task CalculateHash( IDispatcher d, HashProgress notifier) { if (notifier != null) { Action action = () => notifier(Path, 0); if (d == null) { action(); } else { d.Execute(action); } } //TODO - exception handling StorageFile file = await GetFromRoot(m_path, m_root); int num_chunks = (int)(GetSize() / Chunker.chunk_size) + 1; int hash_size = num_chunks * 32; float current_chunk = 0; var hash_builder = new StringBuilder(hash_size); m_hash = ""; var chunker = new Chunker(GetSize()); foreach (Chunk chunk in chunker.GetChunks()) { using (IRandomAccessStream inputStream = await file.OpenAsync(FileAccessMode.Read)) { using (var dataReader = new DataReader(inputStream.GetInputStreamAt(chunk.Start))) { await dataReader.LoadAsync(chunk.Length); IBuffer buf = dataReader.ReadBuffer(chunk.Length); IBuffer hashed = m_alg.HashData(buf); hash_builder.Append(CryptographicBuffer.EncodeToHexString(hashed)); } } current_chunk++; if (notifier != null) { float percent_done = current_chunk / num_chunks; Action action = () => notifier(Path, percent_done * 100); if (d == null) { action(); } else { d.Execute(action); } } } m_hash = hash_builder.ToString(); if (hash_size > 32) //hash the hash { // Convert the string to UTF8 binary data. IBuffer hashbuf = CryptographicBuffer.ConvertStringToBinary(m_hash, BinaryStringEncoding.Utf8); IBuffer hashed = m_alg.HashData(hashbuf); m_hash = CryptographicBuffer.EncodeToHexString(hashed); } if (notifier != null) { Action action = () => notifier(Path, 100); if (d == null) { action(); } else { d.Execute(action); } } }