Search code examples
c#hashchecksum

Generate Running Hash (or Checksum) in C#?


Preface:

I am doing a data-import that has a verify-commit phase. The idea is that: the first phase allows taking data from various sources and then running various insert/update/validate operations on a database. The commit is rolled back but a "verification hash/checksum" is generated. The commit phase is the same, but, if the "verification hash/checksum" is the same then the operations will be committed. (The database will be running under the appropriate isolation levels.)

Restrictions:

  • Input reading and operations are forward-read-once only
  • Do not want to pre-create a stream (e.g. writing to MemoryStream not desirable) as there may be a lot of data. (It would work on our servers/load, but pretend memory is limited.)
  • Do not want to "create my own". (I am aware of available code like CRC-32 by Damien which I could use/modify but would prefer something "standard".)

And what I (think I am) looking for:

A way to generate a Hash (e.g. SHA1 or MD5?) or a Checksum (e.g. CRC32 but hopefully more) based on input + operations. (The input/operations could themselves be hashed to values more fitting to the checksum generation but it would be nice just to be able to "write to steam".)

So, the question is:

How to generate a Running Hash (or Checksum) in C#?

Also, while there are CRC32 implementations that can be modified for a Running operation, what about running SHAx or MD5 hashes?

Am I missing some sort of handy Stream approach than could be used as an adapter?

(Critiques are welcome, but please also answer the above as applicable. Also, I would prefer not to deal with threads. ;-)


Solution

  • Hashes have a build and a finalization phase. You can shove arbitrary amounts of data in during the build phase. The data can be split up as you like. Finally, you finish the hash operation and get your hash.

    You can use a writable CryptoStream to write your data. This is the easiest way.