相关疑难解决方法(0)

为什么SHA1.ComputeHash在很多线程的高负载下失败?

我看到我维护的一些代码存在问题.下面的代码有一个private static SHA1成员(这是一个IDisposable但是因为它static,它应该永远不会最终确定).但是,在压力下,此代码会抛出一个异常,表明它已被关闭:

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)
Run Code Online (Sandbox Code Playgroud)

有问题的代码是:

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));
    }
Run Code Online (Sandbox Code Playgroud)

我的问题显然是可能导致这个问题的原因.呼叫是否可以SHA1.Create静默失败(有多少加密资源可用)?这可能是因为appdomain失败了吗?

还有其他理论吗?

c# stress-testing finalizer sha

9
推荐指数
1
解决办法
4321
查看次数

标签 统计

c# ×1

finalizer ×1

sha ×1

stress-testing ×1