Python Bcrypt Tutorial: hashpw, checkpw and gensalt with Examples
Hash and check passwords with bcrypt in Python, with tested bcrypt 5.0, Flask, Django and FastAPI examples and fixes for "Invalid salt" errors.
To hash a password with bcrypt in Python, install the bcrypt package and use two functions: hashpw() to create a hash and checkpw() to verify a password against it.
pip install bcrypt
import bcrypt
password = "correct horse battery staple"
hashed = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())
print(hashed) # b'$2b$12$...'
if bcrypt.checkpw(password.encode("utf-8"), hashed):
print("Password is correct")
That's the core of it. This guide covers the details that trip people up: bytes versus strings, the "Invalid salt" error, choosing rounds, storing hashes in a database, and how to do the same in Flask, Django and FastAPI. Every example was run with Python 3.14, bcrypt 5.0.0, Flask-Bcrypt 1.0.1, Django 6.1.2 and pwdlib 0.3.1.
Hashing with hashpw() and gensalt()
bcrypt.gensalt() creates a new random salt and records the cost (the number of "rounds") inside it. bcrypt.hashpw() then hashes the password with that salt. You never store the salt separately, because it's part of the hash.
salt = bcrypt.gensalt(rounds=12) # cost 12 (the default)
legacy_salt = bcrypt.gensalt(rounds=12, prefix=b"2a") # $2a$ for very old systems
In bcrypt 5.0 the signature is gensalt(rounds=12, prefix=b"2b"), so calling gensalt() with no arguments gives cost 12 and the modern $2b$ format. Each extra round doubles the time a hash takes. See how many bcrypt rounds to use for choosing a value.
Hashing the same password twice gives two different results because of the random salt, and that's expected. Never check a password by hashing it again and comparing the strings; always use checkpw().
Checking a password with checkpw()
bcrypt.checkpw(password.encode("utf-8"), hashed) # True or False
What does bcrypt.checkpw() return? True if the password matches the hash, False if it doesn't. In our tests it also returned False, rather than raising, for a hash cut short by a too-small database column. It raises an exception only when the arguments are the wrong type or the hash is unreadable (see the errors below).
checkpw() reads the cost and salt from the stored hash itself, so it works for hashes created with any cost. It also verified a $2y$ hash created by PHP.
Store the hash as a string
hashpw() returns bytes, but most databases and ORMs store text. Decode the hash when you save it and encode it again when you check it. These two helpers do that:
def hash_password(password: str) -> str:
return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
def check_password(password: str, stored_hash: str) -> bool:
return bcrypt.checkpw(password.encode("utf-8"), stored_hash.encode("utf-8"))
The stored string is always 60 characters: $2b$12$ followed by 53 more. Give the column room to grow, such as VARCHAR(255) or TEXT, in case you switch algorithms later. See how to store bcrypt hashes in MySQL and PostgreSQL.
Fixing common Python bcrypt errors
TypeError: 'str' object cannot be converted to 'PyBytes'
You passed a str where bcrypt expects bytes. In bcrypt 5.0 the full message is TypeError: argument 'password': 'str' object cannot be converted to 'PyBytes'. Add .encode("utf-8") to the password, and to the hash if you stored it as text.
ValueError: Invalid salt
This means the second argument to checkpw() isn't a valid bcrypt hash. In our tests the usual causes were:
- You saved
str(hashed)instead ofhashed.decode(). That stores the literal textb'$2b$12$...', including theb'and quotes. Decode instead, and fix any rows already stored this way. - The arguments are swapped. It's
checkpw(password, hashed), password first. - The hash came from Django. Django stores its own format, for example
bcrypt_sha256$$2b$12$..., which the plainbcryptlibrary can't read. Use Django'scheck_password()for those hashes.
ValueError: password cannot be longer than 72 bytes
Since bcrypt 5.0, passwords over 72 bytes raise this instead of being silently cut off. If you see it from passlib or FastAPI code, even for short passwords, that's a passlib incompatibility. Both are explained, with fixes, in our guide to bcrypt's maximum password length.
If checkpw() returns False for a password you know is right, see why bcrypt password checks fail for the full list of causes.
Upgrading old hashes when you raise the cost
The plain bcrypt library has no helper to read the cost, but it's the number between the second and third $. Check it at login, the one moment you have the plain password, and re-hash if it's below your target:
def get_cost(stored_hash: str) -> int:
return int(stored_hash.split("$")[2])
TARGET_COST = 12
def login(password: str, stored_hash: str) -> tuple[bool, str]:
if not check_password(password, stored_hash):
return False, stored_hash
if get_cost(stored_hash) < TARGET_COST:
stored_hash = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt(TARGET_COST)).decode("utf-8")
# save stored_hash to the database here
return True, stored_hash
In our test, a cost-10 hash was upgraded to cost 12 on the next successful login, and a wrong password left it untouched.
Async code: don't block the event loop
A bcrypt hash takes a few hundred milliseconds at cost 12. In an async app (FastAPI, aiohttp and similar), run it in a worker thread so other requests keep moving:
import asyncio
async def hash_password_async(password: str) -> str:
return await asyncio.to_thread(hash_password, password)
Flask: using Flask-Bcrypt
Flask-Bcrypt wraps the same bcrypt package in a Flask extension:
pip install flask-bcrypt
from flask import Flask
from flask_bcrypt import Bcrypt
app = Flask(__name__)
app.config["BCRYPT_LOG_ROUNDS"] = 12
flask_bcrypt = Bcrypt(app)
pw_hash = flask_bcrypt.generate_password_hash("correct horse battery staple").decode("utf-8")
flask_bcrypt.check_password_hash(pw_hash, "correct horse battery staple") # True
Three things to know:
generate_password_hash()returnsbytes, so decode it before saving, as above.check_password_hash()accepts the decoded string.- Flask-Bcrypt uses the
bcryptpackage underneath, so withbcrypt5.0 it also raisesValueErrorfor passwords over 72 bytes. Validate the length first. - Its hashes are standard bcrypt. A hash from Flask-Bcrypt verified with the plain
check_password()helper above, so you can move away from the extension later without resetting anyone's password.
Flask-Bcrypt or Werkzeug? Flask already ships with Werkzeug's generate_password_hash(), which uses scrypt by default rather than bcrypt. Its hashes are much longer (162 characters in our test). Both are reasonable choices, but their hashes aren't interchangeable.
Django: enabling bcrypt
Django uses PBKDF2 by default (Django password docs). To use bcrypt, install bcrypt and put Django's bcrypt hasher first in settings.py:
PASSWORD_HASHERS = [
"django.contrib.auth.hashers.BCryptSHA256PasswordHasher",
"django.contrib.auth.hashers.PBKDF2PasswordHasher",
]
Keep PBKDF2PasswordHasher in the list so existing users can still log in. Django verifies their old hashes and upgrades them to the first hasher when they next sign in. Then use Django's normal make_password(), check_password() and User.set_password(); you don't call bcrypt directly.
BCryptSHA256PasswordHasher hashes the password with SHA-256 before bcrypt, which avoids the 72-byte limit. In our test, a 116-byte password verified correctly, and the same password with one character removed was rejected. The stored value looks like bcrypt_sha256$$2b$12$... and was 74 characters in our test, well within Django's default 128-character password field.
FastAPI: use pwdlib
FastAPI's security tutorial now uses pwdlib instead of passlib, which breaks with bcrypt 5.0 (details here). pwdlib can also verify your existing bcrypt hashes and upgrade them to Argon2id as users log in:
pip install "pwdlib[argon2,bcrypt]"
from pwdlib import PasswordHash
from pwdlib.hashers.argon2 import Argon2Hasher
from pwdlib.hashers.bcrypt import BcryptHasher
password_hash = PasswordHash((Argon2Hasher(), BcryptHasher()))
def verify_and_upgrade(password: str, stored_hash: str) -> tuple[bool, str | None]:
# Returns (valid, new_hash): new_hash is set when the stored hash should be replaced
return password_hash.verify_and_update(password, stored_hash)
New passwords are hashed with the first hasher, Argon2id. In our test, an existing bcrypt hash verified and came back with an Argon2id replacement to save. An Argon2id hash came back with nothing to update, and a wrong password returned (False, None). If you'd rather stay on bcrypt, put BcryptHasher() first.
Encrypt and decrypt passwords in Python?
People often search for "how to encrypt and decrypt a password in Python". For user passwords you store, don't encrypt them: hash them with bcrypt (or Argon2id) as shown above. A hash can't be turned back into the password, and that's the point: if the database leaks, there's nothing to decrypt. You only ever check whether a password matches, with checkpw().
Encryption, which can be reversed with a key, is for secrets your program must read back later, such as an API key it sends to another service. Use a library like cryptography for that, never bcrypt. More on why in can you decrypt a bcrypt hash?
Checklist
-
hashpw()to hash,checkpw()to verify, never compare two hashes - Encode passwords with
.encode("utf-8"); store hashes with.decode("utf-8") - Cost 12 or more; re-hash at login when you raise it
- Passwords limited to 72 bytes, or a framework that pre-hashes (Django's
BCryptSHA256PasswordHasher) - Hashing runs in
asyncio.to_thread()in async apps - FastAPI projects use pwdlib, not passlib, with
bcrypt5.0
Try it in your browser: the bcrypt generator creates hashes in the same $2b$ format, and the bcrypt checker verifies any hash your code produces. For other languages, see the Node.js and PHP guides.
Drafted with AI assistance; reviewed and tested by Shahzeb Zafar.
Ready to try it?
Open Bcrypt Generator →Related Articles
bcrypt vs bcryptjs: Which Should You Use in Node.js?
We benchmarked bcrypt 6 and bcryptjs 3 on Node 24: one login takes about the same time, eight at once differ 4x. Results, install facts and which to use.
Bcrypt Hash Length: How to Store It in MySQL and PostgreSQL
A bcrypt hash is always 60 characters. Learn the right column type for MySQL and PostgreSQL, why VARCHAR(50) breaks logins, and how to fix a truncated column.
Can You Decrypt a Bcrypt Hash? No, and Here Is What Works Instead
Bcrypt hashes can't be decrypted, online or with the salt. See what "bcrypt decrypt" sites really do, and how to check, reset or migrate passwords instead.