简体   繁体   中英

python bcrypt and node.js bcrypt

i have a python script that imports user data into a mongodb which uses bcrypt to hash the user's password.

the data will from the mongodb will also be used within a node.js web application, what is the correct way to ensure that the hash generated by the py-bcrypt is the same!

when running the node.js version, i get this:

> bcrypt.genSalt(10, function(err, salt) {
... bcrypt.hash("a", salt, function(err, hash) {
..... console.log(hash);
..... });
... });
undefined
> $2a$10$tOT8MN1.3gsb6jWVL2hMRe0PHnJnXCxJX9xBewNl.2iRDnZCV/NeC

and in python

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

comparing the hash in python using the hash generated by node, returns:

>>> 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!

is there a way to create a bcrypt hash in python to be used in a node.js application?

Yes, same way as in Node, pass the hash in as the 2nd parameter to check it. Creating one with a different random salt shouldn't match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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