Python bcrypt和Node.js bcrypt

3
我有一个Python脚本,可以将用户数据导入使用bcrypt哈希用户密码的MongoDB中。
从MongoDB中获取的数据也将在Node.js Web应用程序中使用,如何正确确保py-bcrypt生成的哈希值是相同的!
运行Node.js版本时,我得到了这个错误:
> bcrypt.genSalt(10, function(err, salt) {
... bcrypt.hash("a", salt, function(err, hash) {
..... console.log(hash);
..... });
... });
undefined
> $2a$10$tOT8MN1.3gsb6jWVL2hMRe0PHnJnXCxJX9xBewNl.2iRDnZCV/NeC

并且在Python中

>>> import bcrypt
>>> password =b"a"
>>> hashed = bcrypt.hashpw(password, bcrypt.gensalt(10))
>>> hashed
'$2a$10$RzKqQppa3Y7ZZV8f7Ay5COFB5GMEGu7aLH7Fe2HchCyYF1gWVMZ/m'

使用由Node生成的哈希值进行Python中的哈希比较,返回:

>>> node_hash = b"$2a$10$tOT8MN1.3gsb6jWVL2hMRe0PHnJnXCxJX9xBewNl.2iRDnZCV/NeC"
>>> if bcrypt.hashpw(password, node_hash) == node_hash:
...     print("It Matches!")
... else:
...     print("Does not match")
... 
It Matches!

有没有一种方法可以在Python中创建bcrypt哈希,以便在Node.js应用程序中使用?
1个回答

0

是的,与Node中一样,将哈希作为第二个参数传递以进行检查。使用不同的随机盐创建哈希应该不匹配。


我在Python和Node中使用相同的盐,所以console.log(hash);hashed是一样的吗? - khinester

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接