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

  • Question

  •  

    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
    Thursday, March 27, 2008 8:56 PM

Answers

  • "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).

    Saturday, March 29, 2008 12:31 AM
  • 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

     

    Friday, March 28, 2008 8:58 PM

All replies

  • 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

     

    Friday, March 28, 2008 8:58 PM
  • You think it might be that it's not thread safe?

    Friday, March 28, 2008 11:23 PM
  • "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).

    Saturday, March 29, 2008 12:31 AM
  • "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 is better - 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

    Tuesday, September 15, 2009 3:22 PM