Skip to content

Instantly share code, notes, and snippets.

@jamesottaway
Created September 19, 2012 05:46
Show Gist options
  • Save jamesottaway/3747884 to your computer and use it in GitHub Desktop.
Save jamesottaway/3747884 to your computer and use it in GitHub Desktop.
.NET CryptSharp PBKDF2 with HMACSHA512
var password = "password";
var salt = "saltsalt";
var iterations = 50000;
var passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
var saltBytes = System.Text.Encoding.UTF8.GetBytes(salt);
var hashSize = new System.Security.Cryptography.HMACSHA512().HashSize / 8;
var hashedPassword = new byte[hashSize];
var sha512Callback = CryptSharp.Utility.Pbkdf2.CallbackFromHmac<System.Security.Cryptography.HMACSHA512>();
var sha512pbkdf2 = new CryptSharp.Utility.Pbkdf2(passwordBytes, saltBytes, iterations, sha512Callback, hashSize);
sha512pbkdf2.Read(hashedPassword);
Console.WriteLine(Convert.ToBase64String(hashedPassword));
@ststeiger
Copy link

With version 2, it's now:
var algo = new System.Security.Cryptography.HMACSHA512();
byte[] hashedPassword = CryptSharp.Utility.Pbkdf2.ComputeDerivedKey(algo, saltBytes, iterations, hashSize);
Console.WriteLine(System.Convert.ToBase64String(hashedPassword));

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