Flask-Bcrypt

1.0.1 · active · verified Fri Apr 10

Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application. It uses the bcrypt password-hashing function, which is intentionally slow and resistant to brute-force attacks, making it suitable for securing sensitive data like passwords. The current version is 1.0.1, and it maintains an active development status with periodic updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Flask-Bcrypt with your Flask application and use its primary methods, `generate_password_hash` and `check_password_hash`, to secure user passwords. Note the `.decode('utf-8')` call for Python 3 compatibility when storing the hash as a string.

from flask import Flask
from flask_bcrypt import Bcrypt

app = Flask(__name__)
# Configure secret key for session management, if applicable
app.config['SECRET_KEY'] = 'a_very_secret_key_for_demo'

bcrypt = Bcrypt(app)

# Example usage in a Flask context (e.g., a route or application setup)
password_plaintext = "mysecretpassword123"

# Generate a password hash (output is bytes, must decode for storage/comparison as string in Py3)
pw_hash = bcrypt.generate_password_hash(password_plaintext).decode('utf-8')

print(f"Plaintext Password: {password_plaintext}")
print(f"Hashed Password: {pw_hash}")

# Check a password against the hash
is_correct = bcrypt.check_password_hash(pw_hash, password_plaintext)
print(f"Password check against correct password: {is_correct}") # Should be True

is_wrong = bcrypt.check_password_hash(pw_hash, "wrongpassword")
print(f"Password check against wrong password: {is_wrong}") # Should be False

if __name__ == '__main__':
    # In a real app, you would store pw_hash in a database
    # and then retrieve it for check_password_hash
    # For demonstration, we just print the results.
    print("Quickstart demonstrated hashing and checking.")

view raw JSON →