I am trying to get Node.JS Crypto PBKDF2 to match same value from PHP Crypto PBKDF2. For some reason, it is not the same.
In JavaScript
const crypto = require('crypto');
crypto.pbkdf2('secret', 'salt', 100000, 20, 'sha512', (err, key) => {
console.log(key.toString());
});
Output: 7E ] 9 J] i
In PHP
$password = "secret";
$iterations = 100000;
$salt = "salt";
$hash = hash_pbkdf2("sha512", $password, $salt, $iterations, 20);
echo $hash;
Output: 3745e482c6e0ade35da1
Why JS output is not matching PHP?
You can use the option raw_output of the hash_pbkdf2 method in PHP and compare their base64
In PHP
<?php
$password = "secret";
$iterations = 100000;
$salt = "salt";
$hash = hash_pbkdf2("sha512", $password, $salt, $iterations, 20, true);
echo base64_encode($hash);
?>
In NodeJS
const crypto = require('crypto');
crypto.pbkdf2('secret', 'salt', 100000, 20, 'sha512', (err, key) => {
console.log(new Buffer(key).toString('base64'));
});
PHP string is in hexadecimal format. Node.JS string isn't.
UPDATE : key.toString('hex')
in JS solve the problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.