Skip to content

Instantly share code, notes, and snippets.

@skradel
Created March 29, 2012 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skradel/2242252 to your computer and use it in GitHub Desktop.
Save skradel/2242252 to your computer and use it in GitHub Desktop.
Simple hash alg timings
Timings apropos to article: http://zetetic.net/blog/2012/3/29/strong-password-hashing-for-aspnet.html
500 iterations on a 4-core i7 920;
PBKDF2 work factor 5000, bcrypt default logfactor 10
Update 30-Mar-2012: Added two cycles to shake out oddly slow MD5 performance.
0 System.Security.Cryptography.MD5CryptoServiceProvider -> 220272 ticks
0 System.Security.Cryptography.SHA1CryptoServiceProvider -> 10620 ticks
0 System.Security.Cryptography.SHA256CryptoServiceProvider -> 14470 ticks
0 System.Security.Cryptography.SHA512CryptoServiceProvider -> 14493 ticks
0 System.Security.Cryptography.HMACSHA1 -> 59847 ticks
0 Zetetic.Security.BCryptHash -> 145235436 ticks
0 Zetetic.Security.Pbkdf2Hash -> 147956907 ticks
1 System.Security.Cryptography.MD5CryptoServiceProvider -> 10037 ticks
1 System.Security.Cryptography.SHA1CryptoServiceProvider -> 8758 ticks
1 System.Security.Cryptography.SHA256CryptoServiceProvider -> 16351 ticks
1 System.Security.Cryptography.SHA512CryptoServiceProvider -> 17091 ticks
1 System.Security.Cryptography.HMACSHA1 -> 18638 ticks
1 Zetetic.Security.BCryptHash -> 142438229 ticks
1 Zetetic.Security.Pbkdf2Hash -> 147777115 ticks
Program code
----
static void Main(string[] args)
{
var salt = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
Type[] types = new Type[]
{
typeof(System.Security.Cryptography.MD5CryptoServiceProvider),
typeof(System.Security.Cryptography.SHA1CryptoServiceProvider),
typeof(System.Security.Cryptography.SHA256CryptoServiceProvider),
typeof(System.Security.Cryptography.SHA512CryptoServiceProvider),
typeof(System.Security.Cryptography.HMACSHA1),
typeof(Zetetic.Security.BCryptHash),
typeof(Zetetic.Security.Pbkdf2Hash)
};
for (int warmup = 0; warmup < 2; warmup++ )
foreach (var algtype in types)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
Parallel.ForEach<int, HashAlgorithm>(Enumerable.Range(1, 500),
() =>
{
var t = (HashAlgorithm)Activator.CreateInstance(algtype);
if (t is KeyedHashAlgorithm)
{
((KeyedHashAlgorithm)t).Key = salt;
}
return t;
},
(i, pls, alg) =>
{
alg.ComputeHash(new byte[] { 1, 2, 3, 4 });
return alg;
},
(alg) => { });
sw.Stop();
Console.WriteLine("{0} {1,-60} -> {2,11} ticks", warmup, algtype, sw.Elapsed.Ticks);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment