passlib.registry - Password Handler Registry¶
This module contains the code Passlib uses to track all password hash handlers
that it knows about. While custom handlers can be used directly within an application,
or even handed to a
CryptContext; it is frequently useful to register
them globally within a process and then refer to them by name.
This module provides facilities for that, as well as programmatically
querying Passlib to detect what algorithms are available.
This module is primarily used as an internal support module. Its interface has not been finalized yet, and may be changed somewhat between major releases of Passlib, as the internal code is cleaned up and simplified.
Applications should access hashes through the
where possible (new ones may also be registered by writing to that module).
return handler for specified password hash scheme.
this method looks up a handler for the specified scheme. if the handler is not already loaded, it checks if the location is known, and loads it first.
- name – name of handler to return
- default – optional default value to return if no handler with specified name is found.
if no handler matching that name is found, and no default specified, a KeyError will be raised.
handler attached to name, or default value (if specified).
return sorted list of all known crypt handler names.
Parameters: loaded_only – if
True, only returns names of handlers which have actually been loaded.
Returns: list of names of all known handlers
register location to lazy-load handler when requested.
custom hashes may be registered via
register_crypt_handler(), or they may be registered by this function, which will delay actually importing and loading the handler until a call to
get_crypt_handler()is made for the specified name.
- name – name of handler
- path – module import path
the specified module path should contain a password hash handler called
name, or the path may contain a colon, specifying the module and module attribute to use. for example, the following would cause
get_handler("myhash")to look for a class named
>>> from passlib.registry import registry_crypt_handler_path >>> registry_crypt_handler_path("myhash", "myapp.helpers")
...while this form would cause
get_handler("myhash")to look for a class name
>>> from passlib.registry import registry_crypt_handler_path >>> registry_crypt_handler_path("myhash", "myapp.helpers:MyHash")
register password hash handler.
this method immediately registers a handler with the internal passlib registry, so that it will be returned by
- handler – the password hash handler to register
- force – force override of existing handler (defaults to False)
- _attr – [internal kwd] if specified, ensures
handler.namematches this value, or raises
- TypeError – if the specified object does not appear to be a valid handler.
- ValueError – if the specified object’s name (or other required attributes) contain invalid values.
- KeyError – if a (different) handler was already registered with
the same name, and
force=Truewas not specified.
All password hashes registered with passlib
can be imported by name from the
This is true not just of the built-in hashes,
but for any hash registered with the registration functions
in this module.
Example showing how to use
>>> # register the location of a handler without loading it >>> from passlib.registry import register_crypt_handler_path >>> register_crypt_handler_path("myhash", "myapp.support.hashes") >>> # even before being loaded, its name will show up as available >>> from passlib.registry import list_crypt_handlers >>> 'myhash' in list_crypt_handlers() True >>> 'myhash' in list_crypt_handlers(loaded_only=True) False >>> # when the name "myhash" is next referenced, >>> # the class "myhash" will be imported from the module "myapp.support.hashes" >>> from passlib.context import CryptContext >>> cc = CryptContext(schemes=["myhash"]) #<-- this will cause autoimport
Example showing how to load a hash by name:
>>> from passlib.registry import get_crypt_handler >>> get_crypt_handler("sha512_crypt") <class 'passlib.handlers.sha2_crypt.sha512_crypt'> >>> get_crypt_handler("missing_hash") KeyError: "no crypt handler found for algorithm: 'missing_hash'" >>> get_crypt_handler("missing_hash", None) None