I need to calculate checksums of large files with a progress bar. I use this code, but the progress bar fills up before I get the hash. How to get the correct progress?
private string GetYYP_XXHash(string filename, IProgress<double> progress = null)
{
byte[] buffer;
byte[] oldBuffer;
int bytesRead;
int oldBytesRead;
long size;
long totalBytesRead = 0;
using (Stream stream = File.OpenRead(filename))
using (YYProject.XXHash.XXHash64 hashAlgorithm = YYProject.XXHash.XXHash64.Create())
{
size = stream.Length;
buffer = new byte[1048576]; // 1MB buffer
bytesRead = stream.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
do
{
oldBytesRead = bytesRead;
oldBuffer = buffer;
buffer = new byte[Convert.ToInt32(BufferHash)];
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);
}
progress?.Report((double)totalBytesRead * 100 / size);
} while (bytesRead != 0);
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < hashAlgorithm.Hash.Length; i++)
{
sBuilder.Append(hashAlgorithm.Hash[i].ToString("x2"));
}
return sBuilder.ToString();
}
}
I need to calculate checksums of large files with a progress bar. I use this code, but the progress bar fills up before I get the hash. How to get the correct progress?