So I was wondering if there are any major differences between the various implementations of the hash algorithms, take the SHA series of algorithms for example. All of them
I did a quick and dirty comparison between CNG
and managed
on SHA512 which is the slowest of all SHA algorithms AFAIK using the code below.
static void Main(string[] args)
{
int loops = 10000000;
var data = Encoding.ASCII.GetBytes("123");
var hashLoop = new Action((HashAlgorithm ha) =>
{
for (int i = 0; i < loops; i++)
ha.ComputeHash(data);
});
var t1 = Task.Factory.StartNew(() =>
{
Time(hashLoop, new SHA512Managed());
});
var t2 = Task.Factory.StartNew(() =>
{
Time(hashLoop, new SHA512Cng());
});
Task.WaitAll(t1, t2);
Console.WriteLine("Benchmark done!");
Console.ReadKey();
}
static void Time(Action action, HashAlgorithm ha)
{
var sw = new Stopwatch();
sw.Start();
action(ha);
sw.Stop();
Console.WriteLine("{1} done in {0}ms", sw.ElapsedMilliseconds, ha.ToString());
}
After several runs I found that the difference was that CNG was considerably faster than managed version of the algorithm with about 21.7% to 49.5%