Passlib: Secure Password Hashing in Python

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Passlib

Passlib is a powerful Python library specifically designed for secure password hashing. It provides a high-level solution that unifies multiple cryptographic algorithms under a single interface. Developers worldwide use this library to build robust authentication systems.

The main goal of Passlib is to give developers a simple and secure way to handle passwords, hiding the complexity of cryptographic operations behind an intuitive API. The library supports over 30 different hashing algorithms, from modern standards to legacy formats for compatibility.

Why Passlib is Essential for Modern Development

Problems with Traditional Approaches

Many developers still use outdated password hashing methods like plain SHA-256 or MD5. These approaches have critical flaws:

  • No protection against rainbow table attacks
  • Vulnerability to brute-force attacks
  • Inability to adapt to growing computing power
  • No defense against timing attacks

Benefits of a Modern Approach

Passlib solves these problems by providing:

  • Automatic salt generation for every password
  • Configurable iteration count to slow down attacks
  • Timing attack protection via constant-time execution
  • Seamless algorithm migration without data loss

Installation and Setup

Basic Installation

pip install passlib

Installation with Extra Algorithm Support

For access to modern hashing algorithms, it's recommended to install additional dependencies:

pip install passlib[bcrypt,argon2]

Verifying Your Installation

After installation, you can check which algorithms are available:

from passlib import registry
print(registry.list_crypt_handlers())

Architecture and Core Components

Library Structure

Passlib is built on a modular architecture that includes:

  • Hash Handlers — implementations of specific algorithms
  • CryptContext — centralized configuration management
  • Registry — system for registering and discovering handlers
  • Utilities — helper functions for secure operations

How It Works

The library follows a "secure by default" principle, automatically applying best cryptographic practices. Each algorithm is encapsulated in its own handler with a unified interface.

Quick Start

Simple Hashing

from passlib.hash import pbkdf2_sha256

# Create a hash
password = "my_secure_password"
hash_value = pbkdf2_sha256.hash(password)
print(f"Hash: {hash_value}")

# Verify a password
is_valid = pbkdf2_sha256.verify(password, hash_value)
print(f"Password is correct: {is_valid}")

Using CryptContext

from passlib.context import CryptContext

# Create a context with multiple algorithms
pwd_context = CryptContext(
    schemes=["bcrypt", "pbkdf2_sha256"],
    default="bcrypt",
    deprecated="auto"
)

# Hash and verify
hash_value = pwd_context.hash("user_password")
is_valid = pwd_context.verify("user_password", hash_value)

Supported Hashing Algorithms

Modern Recommended Algorithms

Argon2

Winner of the 2015 Password Hashing Competition. Provides protection against attacks with

Recommendations