Skip to content

Instantly share code, notes, and snippets.

@xnoder
Created November 24, 2016 05:23
Show Gist options
  • Save xnoder/a6cccf5990d168520e69cd2639735bbd to your computer and use it in GitHub Desktop.
Save xnoder/a6cccf5990d168520e69cd2639735bbd to your computer and use it in GitHub Desktop.
Hash a password with bcrypt and Python 3
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