Skip to content

Library Overview

Passlib is a collection of routines for managing password hashes such as found in unix "shadow" files, as stored in mysql and postgres, and various other places. Passlib contents can be roughly grouped into four categories: password hashes, password contexts, two-factor authentication, and other utility functions.

Password Hashes

All hashes supported by Passlib are implemented as "hasher" classes which can be imported from the passlib.hash module. In turn, all hashers have a uniform interface, which is documented in the Hash Tutorial.

A word of warning: Some of the hashes in this library are marked as "insecure", and are provided for historical purposes only. Still others are specialized in ways that are not generally useful. If you are creating a new application and need to choose a password hash, please read the Quickstart first.

Tip

Password Contexts

Mature applications frequently have to deal with tables of existing password hashes. Over time, they have to support a number of tasks:

  • Add support for new algorithms, and deprecate old ones.
  • Raise the time-cost settings for existing algorithms as computing power increases.
  • Perform rolling upgrades of existing hashes to comply with these changes.
  • Eventually, these policies must be hardcoded in the source, or time must be spent implementing a configuration language to encode them.

In these situations, loading and handling multiple hash algorithms becomes complicated and tedious. The passlib.context module provides a single class, CryptContext, which attempts to solve all of these problems (or at least relieve developers of most of the burden).

This class handles managing multiple password hash schemes, deprecation & migration of old hashes, and supports a simple configuration language that can be serialized to an INI file.

Tip

  • Context Tutorial — walkthrough of the CryptContext class
  • passlib.context — API reference

Two-Factor Authentication

While not strictly connected to password hashing, modern applications frequently need to perform the related task of two-factor authentication. One of the most common protocols for doing this is TOTP (RFC 6238). To help get TOTP in place, the passlib.totp module provides a set of helper functions for securely configuring, persisting, and verifying TOTP tokens.

Tip

Application Helpers

Passlib also provides a number of pre-configured CryptContext instances in order to get users started quickly:

  • passlib.apps — contains pre-configured instances for managing hashes used by Postgres, Mysql, and LDAP, and others.
  • passlib.hosts — contains pre-configured instances for managing hashes as found in the /etc/shadow files on Linux and BSD systems.

Passlib also contains a couple of additional modules which provide support for certain application-specific tasks:

  • passlib.apache — classes for managing htpasswd and htdigest files.
  • passlib.ext.django — Django plugin which monkeypatches support for (almost) any hash in Passlib.