Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How exactly do I use this to calculate CRC value for a file? #7

Open
cryodream opened this issue May 14, 2018 · 6 comments
Open

How exactly do I use this to calculate CRC value for a file? #7

cryodream opened this issue May 14, 2018 · 6 comments

Comments

@cryodream
Copy link

Hi,
Like the title says, how do I use this library to calculate hashes for files?

Thanks for any help.

@force-net
Copy link
Owner

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);

@kgamecarter
Copy link

using (var stream = new FileStream(path, FileMode.Open))
using (var crc = new Crc32Algorithm())
{
    var crc32bytes = crc.Compute(stream);
    var crc32 = BitConverter.ToUInt32(crc32bytes, 0);
}

@Agagamand
Copy link

Agagamand commented Aug 17, 2019

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

@PeterCodar
Copy link

This better:

Could you please let us know why this [is] better?

@force-net
Copy link
Owner

@PeterCodar
I do not know...
Differences:

  • Larger buffer
  • Return value is string
  • Support for 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 time

@EnduringBeta
Copy link

Adding 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants