Open cryodream opened 6 years ago
Example code:
var crc = 0u;
using (var f = File.OpenRead("somefile"))
{
var buffer = new byte[65536];
while (true)
{
var count = f.Read(buffer, 0, buffer.Length);
if (count == 0)
break;
crc = Crc32Algorithm.Append(crc, buffer, 0, count);
}
}
Console.WriteLine(crc);
using (var stream = new FileStream(path, FileMode.Open))
using (var crc = new Crc32Algorithm())
{
var crc32bytes = crc.Compute(stream);
var crc32 = BitConverter.ToUInt32(crc32bytes, 0);
}
This better:
private string GetCRC32C(string filename, IProgress<double> progress = null)
{
uint hash = 0;
byte[] buffer = new byte[1048576]; // 1MB buffer
using (var entryStream = File.OpenRead(filename))
{
int currentBlockSize = 0;
while ((currentBlockSize = entryStream.Read(buffer, 0, buffer.Length)) > 0)
{
hash = Crc32CAlgorithm.Append(hash, buffer, 0, currentBlockSize);
progress?.Report((double)currentBlockSize / entryStream.Length * 100);
}
}
return hash.ToString("X8");
}
Maybe you should add this example to the Wiki? https://github.com/force-net/Crc32.NET/wiki
This better:
Could you please let us know why this [is] better?
@PeterCodar I do not know... Differences:
IProgress
interface. But with this interface - last progress does not notify,
and length of file is calculated every time
May be it uses larger buffer, so it can be faster someway. Also, code uses IProgress
interface. It can be helpful? but usage of this interface is bad - no 100% progress and length of file is calculated every time (I do not know complexity of this operation). Also, progress is not cumulative, it sends the same value every timeAdding one or more working examples to the readme would dramatically improve a person's experience using the package if new to them, like me. Thanks to those who asked and answered this question over here.
Hi, Like the title says, how do I use this library to calculate hashes for files?
Thanks for any help.