.NET Framework Bookmark and Share   
 index > Common Language Runtime > System.Security.Cryptography.CryptographicException: Hash not valid for use in specified state
 

System.Security.Cryptography.CryptographicException: Hash not valid for use in specified state

I am using the following code:

private static readonly HashAlgorithm m_annotativeStateIDHasher = new MD5CryptoServiceProvider();

//Get the hashed bytes

byte[] annotativeStateIDHashedBytes = m_annotativeStateIDHasher.ComputeHash(annotativeStateIDBytes);

Getting the following error very often:

Error: System.Security.Cryptography.CryptographicException: Hash not valid for use in specified state.

at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)

at System.Security.Cryptography.Utils._HashData(SafeHashHandle hHash, Byte[] data, Int32 ibStart, Int32 cbSize)

at System.Security.Cryptography.MD5CryptoServiceProvider.HashCore(Byte[] rgb, Int32 ibStart, Int32 cbSize)

at System.Security.Cryptography.HashAlgorithm.ComputeHash(Byte[] buffer)

Any help is really appreciated




agolovan
Alexander Golovan`

I assume the lines of code you provided aren't next to eachother, as one seems to be a class-level member, and the other a line of a function.

Are you using the static MD5 hash reference with multiple threads? If the answer is yes, either create a critical region (use Lock or a synchronization mechanism) or create a new MD5 hash instance every time you need to compute a hash value.

-Rob

Rob Teixeira

"Thread save" is a really loaded term. But in this particular case, if you're asking "can one instance of the MD5 Crypto Service Provider operate properly with multiple simultaneous threads?" Then the answer is a resounding heck no.

The MD5 Crypto Service Provider is just a thin wrapper around the Win32 Crypto API MD5 implementation. The .NET class holds onto a crypto service context handle for the MD5 Win32 "object", then makes various win32 calls to stream data to it when you call ComputeHash(). If multiple threads access the same MD5 object, it's the same handle under the covers, and you'll be sending in all sorts of amalgamated data to the same hash operation, so the error is the least of your worries - the hash codes you get at the end are probably completely wrong. Since the .NET class streams the data for you and then retrieves the result all in one shot when you call ComputeHash(), you should never get this error, and running multiple threads was the only way i could imagine you'd run into it. The error basically states that you are trying to send data to the hash algorithm after it's marked as complete (so the final results are computed).

Rob Teixeira

I assume the lines of code you provided aren't next to eachother, as one seems to be a class-level member, and the other a line of a function.

Are you using the static MD5 hash reference with multiple threads? If the answer is yes, either create a critical region (use Lock or a synchronization mechanism) or create a new MD5 hash instance every time you need to compute a hash value.

-Rob

Rob Teixeira

You think it might be that it's not thread safe?

Alexander Golovan`

"Thread save" is a really loaded term. But in this particular case, if you're asking "can one instance of the MD5 Crypto Service Provider operate properly with multiple simultaneous threads?" Then the answer is a resounding heck no.

The MD5 Crypto Service Provider is just a thin wrapper around the Win32 Crypto API MD5 implementation. The .NET class holds onto a crypto service context handle for the MD5 Win32 "object", then makes various win32 calls to stream data to it when you call ComputeHash(). If multiple threads access the same MD5 object, it's the same handle under the covers, and you'll be sending in all sorts of amalgamated data to the same hash operation, so the error is the least of your worries - the hash codes you get at the end are probably completely wrong. Since the .NET class streams the data for you and then retrieves the result all in one shot when you call ComputeHash(), you should never get this error, and running multiple threads was the only way i could imagine you'd run into it. The error basically states that you are trying to send data to the hash algorithm after it's marked as complete (so the final results are computed).

Rob Teixeira
"Are you using the static MD5 hash reference with multiple threads? If the answer is yes, either create a critical region (use Lock or a synchronization mechanism) or create a new MD5 hash instance every time you need to compute a hash value."

Which isbetter - creating a critical region or creating a new instance each time? In my case, my operations are time critical but i'm not sure if creating too many instances is a good idea either?

Thanks

rambod

You can use google to search for other answers

Custom Search

More Threads

• Profiling what code causes high CPU usage
• I'm new in Crypto and need help in TripleDES
• How to marshal structure with "two and more level of" nested structures into it?
• GC doubt
• mscoree - clr header
• CLR Profiler and memory leaks
• No of threds in DotNet
• Issue Passing string from C# to C++ dll method and manipulating it.
• .NET Framework 2.0 MSI Installation
• Need direction on storing an application password