I work with PBKDF2 on the interface (CryptoJS) and the backend (Node.js), both derived keys should be equal, so I use the same salt, the same algorithm, the same number of iterations, the same password, but the derived key is different.
This is the browser code / cryptoJS, in the comment the sample salt and key. (Hexadecimal)
<script type="text/javascript" src="pbkdf2.js"></script>
<script type="text/javascript" src="sha512.js"></script>
<script>
var salt = CryptoJS.lib.WordArray.random(128 / 8);
var key512Bits = CryptoJS.PBKDF2('anacleto', salt, { hasher:CryptoJS.algo.SHA512, keySize: 512 / 32, iterations: 1 });
console.log(salt.toString(CryptoJS.enc.Hex));
console.log(key512Bits.toString(CryptoJS.enc.Hex));
</script>
And this is nodeJS code
var crypto = require('crypto');
var salt_hex = '1427f23fd32f8f9902768e7ab7c7ffad';
var key_hex = 'de3259de39fcc55531b91b4ffb2a6c29005c674cc95b3ec5bdf18412b6087d05921f3a0e4148fc34c88a04e980481d397a4c2b143edc0cb1bb5d7434ca3b4c25';
var salt = new Buffer(salt_hex, 'hex');
crypto.pbkdf2('anacleto', salt.toString('utf8'), 1, 512/8, 'sha512', function (err, key) {
if (err)
throw err;
var x = key.toString('hex');
var y = key_hex;
console.assert(x === y, '\n' + x + '\n !== \n' + y);
});
And the console raises this assertion error:
AssertionError:
efcca398dc0eb07aec6dcf2239dae83b79c2f82212b2cc0a21270fe251c73c7ab69f0133e75bf3ca6159242826ff3f26c64075f72cf86c67ba1199af29f6d576
de3259de39fcc55531b91b4ffb2a6c29005c674cc95b3ec5bdf18412b6087d05921f3a0e4148fc34c88a04e980481d397a4c2b143edc0cb1bb5d7434ca3b4c25
As you can see, the key from node.js starts with "efcca" and CryptoJS starts with "de325".
Where is the mistake? Maybe this is another library implementation? Shouldn't be the same derived key?