【发布时间】:2021-10-06 13:25:52
【问题描述】:
我正在使用带有 Docker 的 RabbitMQ。我想直接在definitions.json 文件中更新配置。用户应该使用rabbit_password_hashing_sha256 散列算法将密码存储在那里。我找到了一个有用的 Python 脚本来散列密码,但我无法在 NodeJS 中使用 Crypto 库重现它的逻辑。
Python 脚本:
#!/usr/bin/env python3
# RabbitMQ password hashing algorith as laid out in:
# http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/2011-May/012765.html
from __future__ import print_function
import base64
import os
import hashlib
import struct
import sys
# The plain password to encode
password = sys.argv[1]
# Generate a random 32 bit salt
salt = os.urandom(4)
# Concatenate with the UTF-8 representation of plaintext password
tmp0 = salt + password.encode('utf-8')
# Take the SHA256 hash and get the bytes back
tmp1 = hashlib.sha256(tmp0).digest()
# Concatenate the salt again
salted_hash = salt + tmp1
# Convert to base64 encoding
pass_hash = base64.b64encode(salted_hash)
# Print to the console (stdout)
print(pass_hash.decode("utf-8"))
输出: python hash-password.py test >> t7+JG/ovWbTd9lfrYrPXdFhNZLcO+y56x4z0d8S2OutE6XTE
第一次实施失败:
const crypto = require('crypto');
this.password = process.argv[2];
this.salt = crypto.randomBytes(16).toString('hex');
this.password_hash = crypto.pbkdf2Sync(this.password.trim(), this.salt, 1000, 24, `sha256`).toString(`hex`);
console.log(this.password_hash);
输出: node password.js 测试 >> 7611058fb147f5e7a0faab8a806f56f047c1a091d8355544
在NodeJS中无法复现,所以收集了stdout子进程执行结果,不太优雅。
第二次实施失败:
const crypto = require('crypto');
const utf8 = require('utf8');
this.password = process.argv[2];
this.salt = crypto.randomBytes(4);
this.tmp0 = this.salt + utf8.encode(this.password);
this.tmp1 = crypto.createHash(`sha256`).digest();
this.salted_hash = this.salt + this.tmp1;
this.pass_hash = Buffer.from(this.salted_hash).toString('base64');
console.log(utf8.decode(this.pass_hash));
输出: node password.js 测试 >> Mu+/ve+/vWnvv73vv71C77+977+9HBTvv73vv73vv73ImW/vv70kJ++/vUHvv71k77+977+9TO+/ve+/ve+/vRt4Uu+/vVU=
任何人都可以帮助正确实施吗?
【问题讨论】:
-
请发布您最近的 NodeJS 代码并描述问题。
-
请同时添加示例密码、您从 python 代码中获得的哈希值以及您的 node.js 代码中的结果。
-
用代码和输出扩展了问题。
标签: python node.js hash rabbitmq cryptojs