Is HashAlgorithm.ComputeHash() stateful?

2019-04-25 14:48发布

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?

标签: c# .net hash
2条回答
倾城 Initia
2楼-- · 2019-04-25 15:34

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.

查看更多
登录 后发表回答