This is my first attempt to do any hashing. I wanted to make a class that handled everything I needed to implement PBKDF2. The only question I have is am I ok to store the iterations in the database record or is this a bad practice? I was planning on creating a column to store salt, another for the hashed password, and possibly one for the amount of iterations. My thought with the iterations was that I read that it should increase every so often so when creating new users or they update their passwords it would pull the value to pass in from the database.
public static byte[] GenerateSalt()
{
using (var randomNumberGenerator = new RNGCryptoServiceProvider())
{
var salt = new byte[32];
randomNumberGenerator.GetBytes(salt);
return salt;
}
}
public static byte[] HashPassword(byte[] password, byte[] salt, int iterations)
{
using (var rfc2898 = new Rfc2898DeriveBytes(password, salt, iterations))
{
return rfc2898.GetBytes(32);
}
}
public static bool CompareByteArrays(byte[] array1, byte[] array2)
{
if (array1.Length != array2.Length)
{
return false;
}
for (int i = 0; i < array1.Length; i++)
{
if (array1[i] != array2[i])
{
return false;
}
}
return true;
}
SequenceEqual
rather than yourCompareByteArrays
. Simple to use, has the same "short circuit". \$\endgroup\$