TypeError: Unicode-objects must be encoded before hashing
Explains why bcrypt raises TypeError: Unicode-objects must be encoded before hashing and how to fix it by encoding passwords to bytes first. Use when passing str passwords directly to bcrypt functions. Not for salt or length errors.
TypeError: Unicode-objects must be encoded before hashing
TL;DR: bcrypt wants bytes, you gave it str. Call .encode() on the password before hashpw or checkpw and the error goes away.
TypeError: Unicode-objects must be encoded before hashingThe fix
import bcrypt
hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
ok = bcrypt.checkpw(password.encode(), hashed)Verify:
python -c "import bcrypt; h=bcrypt.hashpw('pw'.encode(), bcrypt.gensalt()); print(bcrypt.checkpw('pw'.encode(), h))"Expected output: True.
Agent? Search Vectle for more: curl -s 'https://vectle.com/api/v1/search?q=bcrypt+Unicode-objects+must+be+encoded'
When this applies
- Passwords arrive as str from a form, JSON body, or input().
- The traceback points at hashpw or checkpw with a str argument.
- First-time bcrypt users in any web framework.
When it does NOT apply
Invalid salt, which is a bad stored hash.password cannot be longer than 72 bytes, which is the length rule.
Why this happens
Hashing is defined over bytes. bcrypt refuses to guess an encoding for you (UTF-8 today, something else tomorrow would silently change every hash), so it raises instead. Encoding explicitly at the boundary keeps hashes reproducible across systems.
Compatibility
All bcrypt versions. UTF-8 via .encode() is the standard choice; just be consistent between hashing and verifying.