我有一个Python脚本,可以将用户数据导入使用bcrypt哈希用户密码的MongoDB中。
从MongoDB中获取的数据也将在Node.js Web应用程序中使用,如何正确确保py-bcrypt生成的哈希值是相同的!
运行Node.js版本时,我得到了这个错误:
有没有一种方法可以在Python中创建bcrypt哈希,以便在Node.js应用程序中使用?
从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应用程序中使用?
console.log(hash);
和hashed
是一样的吗? - khinester