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
Encrypt secret using a password hash algorithm.
| Parameters: |
|
|---|
All other keywords are passed on to the specific password algorithm being used to encrypt the secret.
| Parameters: |
|
|---|---|
| Returns: | The secret as encoded by the specified algorithm and options. |
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: |
|
|---|---|
| Returns: | True if the secret matches, otherwise False. |
Identify algorithm which generated a password hash.
| Parameters: |
|
|---|
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).