Skip to content

Instantly share code, notes, and snippets.

@PaulStovell
Created November 2, 2011 18:33
Show Gist options
  • Save PaulStovell/1334451 to your computer and use it in GitHub Desktop.
Save PaulStovell/1334451 to your computer and use it in GitHub Desktop.
SHA512Managed is not thread safe
static void Main(string[] args)
{
var hashAlgorithm = SHA512.Create();
var text = "Hello world";
var bytes = Encoding.Default.GetBytes(text);
var hashes = new List<string>();
Parallel.For(0, 10000, ignored =>
{
var hashed = hashAlgorithm.ComputeHash(bytes);
var base64 = Convert.ToBase64String(hashed);
hashes.Add(base64);
});
foreach (var hash in hashes)
{
Console.WriteLine(hash);
}
Console.ReadKey();
}
@pbackhuvud
Copy link

No, List is not threadsafe. Use a ConcurrentBag instead....

@tsvx
Copy link

tsvx commented Sep 19, 2018

@pbackhuvud Yes, List is not thread-safe, but ComputeHash is not thread-safe too, that's the problem, I suppose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment