passlib.hash - Quick Start

Usage Example

In order to get off the ground quickly, here’s an example of how to quickly encrypt and verify passwords without having to delve too deeply into this module:

>>> from bps.security import pwhash

>>> #encrypt password using strongest algorithm defined by this module
>>> hash = pwhash.encrypt("too many secrets")
>>> hash
$6$rounds=39000$DNnCxm85LEP1WXUh$IVkALQeSuhr2hcUV90Tv8forzli3K.XwX.1JzPjgwltgvCAgllN3x1jNpG9E1C8IQPm0gEIesqATDyKh/nEnh0'

>>> #verify password against hash
>>> pwhash.verify("mypass", hash)
False
>>> pwhash.verify("too many secrets", hash)
True

>>> #identify the algorithm used in a hash
>>> pwhash.identify(hash)
'sha512-crypt'

>>> #choose a specific algorithm to use (instead of the default)
>>> hash2 = pwhash.encrypt("too many secrets", alg="bcrypt")
'$2a$11$unZuTsMEjeo5mqFX6rmRduQPBDx9t3djd2voi9W.oFhUDQu1NNMcW'

>>> #check if we used right algorithm
>>> pwhash.identify(hash2)
'bcrypt'

>>> #the hash type is autodetected by verify
>>> pwhash.verify("too many secrets", hash2)
True

Frontend Functions

passlib.hash.encrypt(secret, hash=None, alg=None, **kwds)

Encrypt secret using a password hash algorithm.

Parameters:
  • secret (str) – String containing the secret to encrypt
  • hash (str|None) – Optional previously existing hash string which will be used to provide default value for the salt, rounds, or other algorithm-specific options. If not specified, algorithm-chosen defaults will be used.
  • alg (str|None) – Optionally specify the name of the algorithm to use. If no algorithm is specified, an attempt is made to guess from the hash string. If no hash string is specified, sha512-crypt will be used. See identify() for a list of algorithm names.

All other keywords are passed on to the specific password algorithm being used to encrypt the secret.

Parameters:
  • keep_salt (bool) –

    This option is accepted by all of the builtin algorithms.

    By default, a new salt value generated each time a secret is encrypted. However, if this keyword is set to True, and a previous hash string is provided, the salt from that string will be used instead.

    Note

    This is generally only useful when verifying an existing hash (see verify()). Other than that, this option should be avoided, as re-using a salt will needlessly decrease security.

  • rounds (int) –

    For the sha256-crypt and sha512-crypt algorithms, this option lets you specify the number of rounds of encryption to use. For the bcrypt algorithm, this option lets you specify the log-base-2 of the number of rounds of encryption to use.

    For all three of these algorithms, you can either specify a positive integer, or one of the strings “fast”, “medium”, “slow” to choose a preset number of rounds corresponding to an appropriate level of encryption.

Returns:

The secret as encoded by the specified algorithm and options.

passlib.hash.verify(secret, hash, alg=None)

verify a secret against an existing hash.

This checks if a secret matches against the one stored inside the specified hash. By default this uses encrypt() to re-crypt the secret, and compares it to the provided hash; though some algorithms may implement this in a more efficient manner.

Parameters:
  • secret (str) – A string containing the secret to check.
  • hash (str) – A string containing the hash to check against.
  • alg (str|None) – Optionally specify the name of the algorithm to use. If no algorithm is specified, an attempt is made to guess from the hash string. If it can’t be identified, a ValueError will be raised. See identify() for a list of algorithm names.
Returns:

True if the secret matches, otherwise False.

passlib.hash.identify(hash, resolve=False)

Identify algorithm which generated a password hash.

Parameters:
  • hash – The hash string to identify.
  • resolve – If True, this function will return a CryptAlgorithm instance which can handle the hash. If False (the default), then only the name of the hash algorithm will be returned.

The following algorithms are currently recognized:

Name Description
"unix-crypt" the historic unix-crypt algorithm
"md5-crypt" the md5-crypt algorithm, usually identified by the prefix $1$ in unix shadow files.
"bcrypt" the openbsd blowfish-crypt algorithm, usually identified by the prefixes $2$ or $2a$ in unix shadow files.
"sha256-crypt" the 256-bit version of the sha-crypt algorithm, usually identified by the prefix $5$ in unix shadow files.
"sha512-crypt" the 512-bit version of the sha-crypt algorithm, usually identified by the prefix $6$ in unix shadow files.
Returns:The name of the hash, or None if the hash could not be identified. (The return may be altered by the resolve keyword).

Note

This is a convience wrapper for pwhash.default_context.identify(hash).

Table Of Contents

Previous topic

passlib.hash - Password Hashing

Next topic

passlib.hash - Crypt Contexts

This Page