In our system we are hashing passwords with the following method.
public static string HashPassword(string password, byte[] salt)
{
if (salt == null)
{
throw new ArgumentNullException($"{nameof(salt)}");
}
var pbkdf2 = new Rfc2898DeriveBytes(
password,
salt,
Iterations,
HashingAlgorithm
);
byte[] hash = pbkdf2.GetBytes(KeySize);
return $"{Convert.ToBase64String(salt)}{SaltEndMarker}{Convert.ToBase64String(hash)}";
}
For maintenance reasons, we want to set initial hash in the database. As this is a complex algorithm, I thought to use a SQL CLR function, instead of implementing the algorithm directly in SQL. The SQL CLR function approach works, but in comparison to the pure .NET App, is slower in the order of 2-3 magnitudes.
I did some experiments by changing the number of iterations, 1000 takes less than 1 sec, 10K takes 7 sec and 100K 70 sec.
Does anyone have an explanation why the SQL CLR function vs pure .NER App difference? How can it be sped up?
Edited:
- SQL server version: Microsoft SQL Server 2019
- I know that the number of iterations should be greater than 10K. I tried with different values to see how the execution time changes.