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?
Using ComputeHash
is usually stateless although it depends on the actual implementation... you can check that at runtime by accessing State
after the call to ComputeHash
...
see
- http://msdn.microsoft.com/en-us/library/s02tk69a.aspx
- http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.transformblock.aspx
- http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.transformfinalblock.aspx
- http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.state.aspx
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.