I need to compute hashes of multiple blocks of data independently. Something like this:
using( HashAlgorithm hasher = new ActualHashAlgorithm() ) {
for( int i = 0; i = numberOfBlocks; i++ ) {
byte[] block = getBlock( i );
byte[] hash = hasher.ComputeHash( block );
// use hash
}
}
Can I reuse the same HashAlgorithm
object between blocks? Will HashAlgorithm
reset state between calls to ComputeHash()
or do I need to dispose the HashAlgorithm
object and create new one for each new block of data?
Actually, when you need hash under .NET framework, I strongly recommended coding this function manually but not to use .NET framework.
Some months age, I immigrated a 32bit .NET program into 64bit windows. The program is crashed. At least I found hash value is different under different 32/64bit system, although same .NET program. I used Djb algorithm instead of .NET hash algorithm, and the program run okay.
This document is about Djb hash algorithm, you can rewrite by C#. It is not a hard work.
Using
ComputeHash
is usually stateless although it depends on the actual implementation... you can check that at runtime by accessingState
after the call toComputeHash
...see