New password algorithms can be implemented by subclassing CryptAlgorithm, which provides the underlying framework used for all the password algorithms.
To create a new one, you simple subclass CryptAlgorithm, and implement the identify, encrypt, and verify methods (at the very least).
base class for holding information about password algorithm.
The following should be filled out for all crypt algorithm subclasses. Additional methods, attributes, and features may vary.
This should be a globally unique name to identify the hash algorithm with.
This is a purely informational attribute listing how many bits are in the salt your algorithm uses. (defaults to None if information is not available).
This is a purely informational attribute listing how many bits are in the cheksum part of your algorithm’s hash. (defaults to None if information is not available).
Note
Note that all the bit counts should measure the number of bits of entropy, not the number of bits a given encoding takes up.
This is a virtual attribute, calculated based on the value of the salt_bits attribute. It returns True if the algorithm contains any salt bits, else False.
Number of characters in secret which are used. If None (the default), all chars are used. BCrypt, for example, only uses the first 55 chars.
This is a purely informational attribute listing whether the algorithm can be scaled by increasing the number of rounds it contains. It is not required (defaults to False).
If this flag is true, then the algorithm’s encrypt method supports a rounds keyword which (at the very least) accepts the following strings as possible values:
- fast – number of rounds will be selected
to provide adequate security for most user accounts. This is retuned perodically to take around .25 seconds.
- medium – number of rounds will be selected
to provide adequate security for most root/administrative accounts This is retuned perodically to take around .75 seconds.
- slow – number of rounds will be selected
to require a large amount of calculation time. This is retuned perodically to take around 1.5 seconds.
Note
Last retuning of the default round sizes was done on 2009-07-06 using a 2ghz system.
identify if a hash string belongs to this algorithm.
| Parameters: | hash – the hash string to check |
|---|---|
| Returns: | True if provided hash string is handled by this class, otherwise False. |
Note
For some of the simplist algorithms (eg plaintext), there is no globally unambiguous way to identify a given hash. In this case, identify() should at the very least be able to distinguish it’s hashes from the other algorithms in use within a given context.
encrypt secret, returning resulting hash string.
| Parameters: |
|
|---|
Note
Various password algorithms may accept addition keyword arguments, usually to override default configuration parameters. For example, most has_rounds algorithms will have a rounds keyword. Such details vary on a per-algorithm basis, consult their encrypt method for details.
| Returns: | The encoded hash string, with any chrome and identifiers. All values returned by this function should pass identify(hash) -> True and verify(secret,hash) -> True. |
|---|
Usage Example:
>>> from passlib.pwhash import Md5Crypt
>>> crypt = Md5Crypt()
>>> #encrypt a secret, creating a new hash
>>> hash = crypt.encrypt("it's a secret")
>>> hash
'$1$2xYRz6ta$IWpg/auAdyc8.CyZ0K6QK/'
>>> #verify our secret
>>> crypt.verify("fluffy bunnies", hash)
False
>>> crypt.verify("it's a secret", hash)
True
>>> #encrypting again should generate a new salt,
>>> #even if we pass in the old one
>>> crypt.encrypt("it's a secret", hash)
'$1$ZS9HCWrt$dRT5Q5R9YRoc5/SLA.WkD/'
>>> _ == hash
False
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. |
See encrypt() for a usage example.
Subclass this class, and implement identify() and encrypt() so that they implement your algorithm according to it’s documentation and the specifications of the methods themselves. You must also specify :attr:name. Optionally, you may override verify() and set various informational attributes.
Note
It is recommended to use from passlib.util import srandom as your random number generator, since it should (hopefully) be the strongest rng passlib can find on your system.