Sto riscontrando un problema con un codice che mantengo. Il codice seguente ha un membro private static SHA1
(che è un IDisposable
ma poiché è static
, non dovrebbe mai essere finalizzato). Tuttavia, in condizioni di stress questo codice genera un'eccezione che suggerisce che è stato chiuso:Perché SHA1.ComputeHash non riesce con un carico elevato con molti thread?
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)
Il codice in questione è:
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));
}
La mia domanda è, ovviamente, quello che potrebbe causare questo problema. La chiamata a SHA1.Create
può fallire in silenzio (quante risorse di crittografia sono disponibili)? Ciò potrebbe essere causato dall'appunta- mento della appdomain?
Altre teorie?
Che cosa ha a che fare con lo smaltimento? Inoltre, quale classe "SHA1" è quella? –
Sei sicuro che la classe SHA1 sia protetta da thread? Sei in grado di afferrare la password che viene sottoposta a hash quando fallisce? – Rob
@John Saunders, mi spiace che tu abbia ragione. Questo non ha nulla a che fare con Dispose. Pensavo che il finalizzatore su System.Security.Cryptography.SHA1CryptoServiceProvider avrebbe potuto essere attivato in qualche modo. http://msdn.microsoft.com/en-us/library/e7hyyd4e(v=vs.110).aspx – MvdD