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. It’s 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 passlib.hash module 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.
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.
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 myhash within the myapp.helpers module:
>>> 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 MyHash within the myapp.helpers module:
>>> 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 get_crypt_handler() when requested.
All password hashes registered with passlib can be imported by name from the passlib.hash module. 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 registry_crypt_handler_path():
>>> # 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, it's 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