Created
November 24, 2016 05:23
-
-
Save xnoder/a6cccf5990d168520e69cd2639735bbd to your computer and use it in GitHub Desktop.
Hash a password with bcrypt and Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bcrypt | |
# Need to supply the password as a byte | |
hashed = bcrypt.hashpw(b'somepassword', bcrypt.gensalt()) | |
print(hashed) | |
#>> b'$2b$12$rbsEiL/dJymbH3453Tbhs.kEljGWNKTmp23s5iG71adGxv5jFj72S' | |
# To check the same password against the hashed value, use checkpw() | |
# The password you're testing comes first, the hashed value second. | |
# Getting it the other way around results in an Invalid Salt error. | |
# checkpw() returns True or False on the comparison. | |
check = bcrypt.checkpw(b'somepassword', hashed) | |
if check: | |
print("The passwords match.") | |
else: | |
print("Oops. The passwords do not match.") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment