I need to create a password hash code PBKDF2-SHA256, but I have problems.
I downloaded the Bouncy Castle repo, but got a little stuck looking for what I was looking for in unit tests.
Found some code examples here , but this is just SHA1. The key bit of code is:
/// <summary> /// Computes the PBKDF2-SHA1 hash of a password. /// </summary> /// <param name="password">The password to hash.</param> /// <param name="salt">The salt.</param> /// <param name="iterations">The PBKDF2 iteration count.</param> /// <param name="outputBytes">The length of the hash to generate, in bytes.</param> /// <returns>A hash of the password.</returns> private static byte[] PBKDF2(string password, byte[] salt, int iterations, int outputBytes) { var pdb = new Pkcs5S2ParametersGenerator(); pdb.Init(PbeParametersGenerator.Pkcs5PasswordToBytes(password.ToCharArray()), salt, iterations); var key = (KeyParameter)pdb.GenerateDerivedMacParameters(outputBytes * 8); return key.GetKey(); }
I need to change this from SHA1 to SHA256.
From the Java documentation and this post, it looked like the following could have been done, but there is no overload in the C # library constructor.
var pdb = new Pkcs5S2ParametersGenerator(new Sha256Derived());
Finding another article on stack overflow, I thought it was possible that it was possible, but SHA hash algorithms are not on the search list, so the following will not work.
var bcparam = (KeyParameter)pdb.GenerateDerivedParameters("sha256", outputBytes * 8);
What do I need to do to get this job, please?
Note. If you read this and donβt know how to use Bouncy Castle, but you know another way, I will still be grateful for your help.