public byte[] ComputeHash(IReaderCloser r, HashAlgorithm hashAlgorithm) { bool cancelFlag = false; int bytesRead = 0, read = 0; long length = r.Length; byte[] buffer = new byte[BLOCK_SIZE]; while ((read = r.Read(buffer, 0, BLOCK_SIZE)) > 0) { hashAlgorithm.TransformBlock(buffer, 0, read, null, 0); bytesRead += read; cancelFlag = false; if (OnProgress != null) { OnProgress(bytesRead, length, ref cancelFlag); if (cancelFlag == true) { hashAlgorithm.TransformFinalBlock(buffer, 0, 0); return(null); } } } hashAlgorithm.TransformFinalBlock(buffer, 0, 0); return(hashAlgorithm.Hash); }
static byte[] Compute(IReaderCloser r, HashAlgorithm hashAlgorithm) { byte[] buffer = new byte[BLOCK_SIZE]; int read = 0; while ((read = r.Read(buffer, 0, BLOCK_SIZE)) > 0) { hashAlgorithm.TransformBlock(buffer, 0, read, null, 0); } hashAlgorithm.TransformFinalBlock(buffer, 0, 0); return(hashAlgorithm.Hash); }
public async Task <byte[]> ComputeHashAsync(IReaderCloser r, HashAlgorithm hashAlgorithm, IProgress <int> progress) { using (_cancellationTokenSource = new CancellationTokenSource()) { try { var ret = await Task <byte[]> .Run(() => { byte[] buffer = new byte[BLOCK_SIZE]; int bytesRead = 0, read = 0; float length = r.Length; while ((read = r.Read(buffer, 0, BLOCK_SIZE)) > 0) { _cancellationTokenSource.Token.ThrowIfCancellationRequested(); hashAlgorithm.TransformBlock(buffer, 0, read, null, 0); bytesRead += read; int value = (int)((bytesRead / length) * 100); progress.Report(value); } hashAlgorithm.TransformFinalBlock(buffer, 0, 0); return(hashAlgorithm.Hash); }, _cancellationTokenSource.Token); return(ret); } catch (OperationCanceledException) { progress.Report(-1); return(null); } } }