I'm using the following function in c# .net core 5 to generate a pbkdf2 key hash value:
HashPassword = KeyDerivation.Pbkdf2(password, SaltPassword, KeyDerivationPrf.HMACSHA256, 10000, 16);
the salt is a byte array, the password is a text string.
I need to be able to generate the same value in JavaScript. I've made it work with asmCrypto but would like to switch to the faster & standard Web Crypto API.
I believe that I need to execute this code in JavaScript (which I lifted from another example):
window.crypto.subtle.deriveBits(
{
name: "PBKDF2",
hash: "SHA-256",
salt: window.crypto.getRandomValues(new Uint8Array(16)),
iterations: 10000
},
key,
10000)
.then(function (bits) {
//returns the derived bits as an ArrayBuffer
console.log(new Uint8Array(bits));
})
.catch(function (err) {
console.error(err);
});
But I have been unsuccessful in generating a proper 'key'. I've tried with generateKey() - unsure if maybe it's importKey() - but I am unable to make it work either way. I believe generateKey needs HMAC-SHA1 to be compatible with c# pbkdf2.
Any help to make it run would be greatly appreciated. :-) Just a pointer to maybe how to generate the key and I can post the response once I validate they generate identical results.
Thank you.
-- Post answer I'm posting my final code here just in case it's useful for anyone needing a JS function as close to the C# version as possible:
/**
* @param {string} strPassword The clear text password
* @param {Uint8Array} salt The salt
* @param {string} hash The Hash model, e.g. ["SHA-256" | "SHA-512"]
* @param {int} iterations Number of iterations
* @param {int} len The output length in bytes, e.g. 16
*/
async function pbkdf2(strPassword, salt, hash, iterations, len) {
var password = new TextEncoder().encode(strPassword);
var ik = await window.crypto.subtle.importKey("raw", password, { name: "PBKDF2" }, false, ["deriveBits"]);
var dk = await window.crypto.subtle.deriveBits(
{
name: "PBKDF2",
hash: hash,
salt: salt,
iterations: iterations
},
ik,
len * 8); // Bytes to bits
return new Uint8Array(dk);
}