简体   繁体   English

PHP和Node.JS-加密PBKDF2

[英]PHP And Node.JS - Crypto PBKDF2

I am trying to get Node.JS Crypto PBKDF2 to match same value from PHP Crypto PBKDF2. 我试图获取Node.JS Crypto PBKDF2以匹配PHP Crypto PBKDF2的相同值。 For some reason, it is not the same. 由于某些原因,它是不一样的。

In JavaScript 在JavaScript中

const crypto = require('crypto');
crypto.pbkdf2('secret', 'salt', 100000, 20, 'sha512', (err, key) => {

  console.log(key.toString()); 
});

Output: 7E ] 9 J] i 输出: 7E ] 9 J] i

In PHP 在PHP中

$password = "secret";
$iterations = 100000;
$salt = "salt";

$hash = hash_pbkdf2("sha512", $password, $salt, $iterations, 20);
echo $hash;

Output: 3745e482c6e0ade35da1 输出: 3745e482c6e0ade35da1

Why JS output is not matching PHP? 为什么JS输出与PHP不匹配?

You can use the option raw_output of the hash_pbkdf2 method in PHP and compare their base64 您可以在PHP中使用hash_pbkdf2方法的选项raw_output并比较它们的base64

In PHP 在PHP中

<?php
$password = "secret";
$iterations = 100000;
$salt = "salt";

$hash = hash_pbkdf2("sha512", $password, $salt, $iterations, 20, true);
echo base64_encode($hash);
?>

Live example 现场例子

In NodeJS 在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. PHP字符串为十六进制格式。 Node.JS string isn't. Node.JS字符串不是。

UPDATE : key.toString('hex') in JS solve the problem. 更新: JS中的key.toString('hex')解决了这个问题。

Source : https://nodejs.org/api/crypto.html#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback 来源: https : //nodejs.org/api/crypto.html#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM