I see a problem with some code that I support. The code below has a private static SHA1 member (which is IDisposable , but since it is static , it should never terminate). However, under stress, this code raises an exception that assumes that it was closed:
Caught exception. Safe handle has been closed" Stack trace: Call stack where exception was thrown at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success) at System.Security.Cryptography.Utils.HashData(SafeHashHandle hHash, Byte[] data, Int32 cbData, Int32 ibStart, Int32 cbSize) at System.Security.Cryptography.Utils.HashData(SafeHashHandle hHash, Byte[] data, Int32 ibStart, Int32 cbSize) at System.Security.Cryptography.HashAlgorithm.ComputeHash(Byte[] buffer)
This code:
internal class TokenCache { private static SHA1 _sha1 = SHA1.Create(); private string ComputeHash(string password) { byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(password); return UTF8Encoding.UTF8.GetString(_sha1.ComputeHash(passwordBytes)); }
My question can obviously cause this problem. Can the SHA1.Create call fail (how many cryptographic resources are available)? Could this be caused by a decrease in the approximation?
Any other theories?
c # sha finalizer stress-testing
Mvdd
source share