javascript - 如何在 Node.js 中复制这个 PHP 哈希实现?
问题描述
我正在尝试在最初用 PHP (7.2) 编写的 node.js (LTS latest--14.x) 中重新创建密码哈希实现。我相信我编写的 node.js 实现应该做同样的事情;但是,在循环中第一次通过哈希之后,node.js 的实现会有所不同。我在这里想念什么?
PHP 实现(我无法更改它,因为它是 Web 框架的一部分,并且现有的身份验证依赖于保持不变的散列机制):
$algo = "sha512";
$salt = "someSalt";
$password = 'somePassword';
$count = 32768;
$hash = hash($algo, $salt . $password, TRUE);
// $hash is the same as in the corresponding line in the node.js implementation
do {
$hash = hash($algo, $hash . $password, TRUE);
// $hash differs from the node.js implementation after the first pass here... why?
} while (--$count);
Node.js 实现:
const crypto = require('crypto');
const algorithm = 'sha512';
const salt = 'someSalt';
const password = 'somePassword';
let count = 32768;
let hash = crypto
.createHash(algorithm)
.update(salt + password)
.digest('binary');
// hash is the same as in the PHP implementation here
do {
hash = crypto.createHash(algorithm).update(hash + password).digest('binary');
// hash differs between the two implementations after the first pass here... why?
} while (--count);
编辑:更新以显示原始 Node.js 实现,其中我没有对传递给的数据进行字符串化update()
。
解决方案
扩展我的评论是我认为以下可能有效。
节点:
const crypto = require('crypto');
const algorithm = 'sha512';
const salt = 'someSalt';
const password = 'somePassword';
let count = 32768;
let hash = crypto
.createHash(algorithm)
.update(String(salt) + String(password))
.digest('hex');
// hash is the same as in the PHP implementation here
do {
hash = crypto.createHash(algorithm).update(String(hash) + String(password)).digest('hex');
// hash differs between the two implementations after the first pass here... why?
} while (--count);
console.log(hash);
PHP代码:
$algo = "sha512";
$salt = "someSalt";
$password = 'somePassword';
$count = 32768;
$hash = bin2hex(hash($algo, $salt . $password, TRUE));
// $hash is the same as in the corresponding line in the node.js implementation
do {
$hash = bin2hex(hash($algo, $hash . $password, TRUE));
// $hash differs from the node.js implementation after the first pass here... why?
} while (--$count);
var_dump($hash);
用 NODE 的输出证实了我的发现:
node nodeTest.js
df8202221e5cbff38c16a33945efa8dcb44d0e7267cdf1514cefffb3df321f69ad1d9b01cfb6360391f1de4791e26a179fd165248b4b75699cb2d3395c971351
PHP 输出:
php test.php
string(128) "df8202221e5cbff38c16a33945efa8dcb44d0e7267cdf1514cefffb3df321f69ad1d9b01cfb6360391f1de4791e26a179fd165248b4b75699cb2d3395c971351"
推荐阅读
- reactjs - ServiceWorker 没有更新,在 React 应用程序中跳过等待
- django - 我无法将模型类导入序列化程序文件
- php - Laravel - 雄辩的关系在使用 JsonArray 的 Mongo 中不起作用
- excel - 如何在 COUNTIF 函数中使用 2 个条件(与 AND 连接)?
- python - 如何使用 matplotlib 渲染乳胶矩阵
- android - Android 通知不适用于广播。它只在调试模式下工作
- r - 评估积分的代码:从 Matlab 转换为 R
- java - 有没有办法使用传递的类对象的方法?
- sql - 如何将两个 oracle sql 对象类型与通用基超类进行比较
- python - 在 Python 中测试 Flash 消息的内容