passlib.hash - Implementing a Custom Crypt Algorithm

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).

class passlib.hash.CryptAlgorithm(**kwds)

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.

System Message: SEVERE/4 (/home/biscuit/dev/libs/passlib/trunk/passlib/hash.py:docstring of passlib.hash.CryptAlgorithm, line 7)

Unexpected section title.

Informational Attributes
========================
name

This should be a globally unique name to identify the hash algorithm with.

salt_bits

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).

hash_bits

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.

has_salt

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.

secret_chars

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.

has_rounds

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).

has_named_rounds

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.

System Message: SEVERE/4 (/home/biscuit/dev/libs/passlib/trunk/passlib/hash.py:docstring of passlib.hash.CryptAlgorithm, line 75)

Unexpected section title.

Common Methods
==============
identify(*args, **kwds)

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(*args, **kwds)

encrypt secret, returning resulting hash string.

Parameters:
  • secret – A string containing the secret to encode. Unicode behavior is specified on a per-hash basis, but the common case is to encode into utf-8 before processing.
  • hash – Optional hash string, containing a salt and other configuration parameters (rounds, etc). If a salt is not specified, a new salt should be generated with default configuration parameters set.
  • keep_salt (bool) –

    This option is rarely needed by end users, you can safely ignore it if you are not writing a hash algorithm.

    By default (keep_salt=False), a new salt will be generated for each call to encrypt, for added security. If a salt string is provided, only the configuration parameters (number of rounds, etc) should be preserved.

    However, it is sometimes useful to preserve the original salt bytes, instead of generating new ones (such as when verifying the hash of an existing password). In that case, set keep_salt=True. Note that most end-users will want to call self.verify(secret,hash) instead of using this flag.

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(secret, hash)

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 – A string containing the secret to check.
  • hash – A string containing the hash to check against.
Returns:

True if the secret matches, otherwise False.

See encrypt() for a usage example.

System Message: SEVERE/4 (/home/biscuit/dev/libs/passlib/trunk/passlib/hash.py:docstring of passlib.hash.CryptAlgorithm, line 83)

Unexpected section title.

Implementing a new crypt algorithm
==================================

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.

Previous topic

passlib.hash - Crypt Algorithms

Next topic

passlib.hash - Helper Functions

This Page