T
Tristan MSDN Keen
Hi,
I am experiencing problems with the SHA256Managed class when multiple
threads are simultaneously using it. I have reduced the problem to a small
piece of code (see below). This creates two threads, each of which repeatedly
attempts to hash a string and compare the result to a previous attempt. After
a number of tries, the result coming back is different. Running a single
thread or locking the hashAlgo object stops the problem. However, this
shouldn't be necessary as the documentation states that this class is thread
safe when using a static instance.
--- Code example:
using System;
using System.Security.Cryptography;
using System.Text;
namespace HashTester
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 2; i++)
{
System.Threading.Thread ff = new System.Threading.Thread(new
System.Threading.ParameterizedThreadStart(HashTestFn));
ff.Start("pa$$word");
}
}
private static void HashTestFn(object o)
{
HashTest test = new HashTest(o.ToString());
for (int i = 0; i < 10000; i++)
{
test.Test();
}
}
}
class HashTest
{
private static HashAlgorithm hashAlgo = new
System.Security.Cryptography.SHA256Managed();
private string input;
private string output;
public HashTest(string toHash)
{
input = toHash;
byte[] hashBytes;
hashBytes = hashAlgo.ComputeHash(Encoding.ASCII.GetBytes(input));
output = Convert.ToBase64String(hashBytes).ToUpper();
}
public void Test()
{
byte[] hashBytes =
hashAlgo.ComputeHash(Encoding.ASCII.GetBytes(input));
string newOut = Convert.ToBase64String(hashBytes).ToUpper();
if (newOut != output)
{
throw new Exception("Bad Hash!!!!");
}
}
}
}
I am experiencing problems with the SHA256Managed class when multiple
threads are simultaneously using it. I have reduced the problem to a small
piece of code (see below). This creates two threads, each of which repeatedly
attempts to hash a string and compare the result to a previous attempt. After
a number of tries, the result coming back is different. Running a single
thread or locking the hashAlgo object stops the problem. However, this
shouldn't be necessary as the documentation states that this class is thread
safe when using a static instance.
--- Code example:
using System;
using System.Security.Cryptography;
using System.Text;
namespace HashTester
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 2; i++)
{
System.Threading.Thread ff = new System.Threading.Thread(new
System.Threading.ParameterizedThreadStart(HashTestFn));
ff.Start("pa$$word");
}
}
private static void HashTestFn(object o)
{
HashTest test = new HashTest(o.ToString());
for (int i = 0; i < 10000; i++)
{
test.Test();
}
}
}
class HashTest
{
private static HashAlgorithm hashAlgo = new
System.Security.Cryptography.SHA256Managed();
private string input;
private string output;
public HashTest(string toHash)
{
input = toHash;
byte[] hashBytes;
hashBytes = hashAlgo.ComputeHash(Encoding.ASCII.GetBytes(input));
output = Convert.ToBase64String(hashBytes).ToUpper();
}
public void Test()
{
byte[] hashBytes =
hashAlgo.ComputeHash(Encoding.ASCII.GetBytes(input));
string newOut = Convert.ToBase64String(hashBytes).ToUpper();
if (newOut != output)
{
throw new Exception("Bad Hash!!!!");
}
}
}
}