Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is HashAlgorithm.ComputeHash() stateful?

Tags:

c#

.net

hash

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?

like image 457
sharptooth Avatar asked Oct 20 '11 07:10

sharptooth


1 Answers

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
like image 65
Yahia Avatar answered Sep 21 '22 21:09

Yahia