Free hash [A reusable CRC-32 HashAlgorithm implementation for .NET]


This blog has moved to a new location and comments have been disabled.

All old posts, new posts, and comments can be found on The blog of dlaa.me.

See you there!

Comments (8)
  1. Delay's Blog says:

    Last week, I released the ComputeFileHashes tool for calculating file checksums . (To read more about

  2. Delay's Blog says:

    While working on code for an upcoming blog post, I found myself dealing with the HashAlgorithm.HashSize

  3. Delay's Blog says:

    I was very happy with last week’s release of ComputeFileHashes supporting the command-line, WPF, Silverlight,

  4. Delay's Blog says:

    Kind reader Gregor Zurowski contacted me over the weekend to let me know that he was using my free CRC-32

  5. Pete Davis says:

    Found it odd that the first byte that I put through this caused a Arithmetic Overflow Exception. However the fix was to change this:

    byte index = (byte)(_crc32Value ^ array[i]);

    into:

    var index = (_crc32Value ^ array[i]) & 0xFF;

    On the plus side, surely the bitwise and is faster than a type cast…

  6. Delay says:

    Pete Davis,

    Could you please describe your scenario and/or tell me how to reproduce it?

    I just wrote/ran the following program to send every byte value through the algorithm (verifying with the debugger that it runs the line you reference above) and encountered no exceptions when compiling for .NET 4 or .NET 2:

    var crc32 = new Delay.CRC32();

    var data = new byte[1];

    for (int i = byte.MinValue; i < byte.MaxValue; i++)

    {

       data[0] = (byte)i;

       crc32.ComputeHash(data);

    }

    I also manipulated the values in the debugger a bit and was unable to reproduce your experience. Could you please double-check you're using the latest version of CRC32.cs without modification (you should be able to copy the code above)?

    Thanks for your help!

  7. Alex Smith says:

    Hi!

    Try to use your code as managed in PowerShell: http://pastebin.com/rEdKjtiy

    $hasher = [Delay.CRC32]::Create()

    $inputStream = New-Object System.IO.StreamReader ($file)

    $hashBytes = $hasher.ComputeHash($inputStream.BaseStream)

    $inputStream.Close()

    $builder = New-Object System.Text.StringBuilder

    $hashBytes | Foreach-Object { [void] $builder.Append($_.ToString("X2")) }

    $builder.ToString()

    …but it returns SHA1 hash.

    Maybe I call it wrong way?

  8. Delay says:

    Alex Smith,

    You're calling HashAlgorithm.Create which is documented to return a SHA1 implementation: msdn.microsoft.com/…/b0ky3sbb.aspx

    You should new up an instance of CRC32 directly.

    Hope this helps!

Comments are closed.