All of the crypt algorithms must inherit from CryptAlgorithm, which defines a common interface all algorithms must support. You may use the algorithms directly, by creating an instance and calling it as described in Implementing a Crypt Algorithm. However, you will normally will not need to deal with the internals of the algorithms directly, but rather take advantage of one of the predefined algorithms, through the frontend functions or a custom crypt context.
The following algorithms are all standard password hashing algorithms used by various Posix operating systems over the years.
Note
BPS tries to use external accelaration for these classes when possible, but provides a pure-python fallback so that these algorithms will ALWAYS be available for use.
Old Unix-Crypt Algorithm, as originally used on unix before md5-crypt arrived. This implementation uses the builtin crypt module when available, but contains a pure-python fallback so that this algorithm can always be used.
This provides the MD5-crypt algorithm, used in many 1990’s era unix systems. It should be byte compatible with unix shadow hashes beginning with $1$.
This class implements the SHA-256 Crypt Algorithm, according to the specification at http://people.redhat.com/drepper/SHA-crypt.txt. It should be byte-compatible with unix shadow hashes beginning with $5$.
See Sha512Crypt for usage examples and details.
This class implements the SHA-512 Crypt Algorithm, according to the specification at http://people.redhat.com/drepper/SHA-crypt.txt. It should be byte-compatible with unix shadow hashes beginning with $6$.
This implementation is based on a pure-python translation of the original specification.
Note
This is not just the raw SHA-512 hash of the password, which is sometimes incorrectly referred to as sha512-crypt. This is a variable-round descendant of md5-crypt, and is comparable in strength to bcrypt.
Usage Example:
>>> from passlib.hash import Sha512Crypt
>>> crypt = Sha512Crypt()
>>> #to encrypt a new secret with this algorithm
>>> hash = crypt.encrypt("forget me not")
>>> hash
'$6$rounds=11949$KkBupsnnII6YXqgT$O8qAEcEgDyJlMC4UB3buST8vE1PsPPABA.0lQIUARTNnlLPZyBRVXAvqqynVByGRLTRMIorkcR0bsVQS5i3Xw1'
>>> #to verify an existing secret
>>> crypt.verify("forget me not", hash)
True
>>> crypt.verify("i forgot it", hash)
False
encrypt using sha256/512-crypt.
In addition to the normal options that CryptAlgorithm.encrypt() takes, this function also accepts the following:
| Parameters: | rounds – Optionally specify the number of rounds to use. This can be one of “fast”, “medium”, “slow”, or an integer in the range 1000...999999999. See CryptAlgorithm.has_named_rounds for details on the meaning of “fast”, “medium” and “slow”. |
|---|
Implementation of OpenBSD’s BCrypt algorithm.
BPS will use the py-bcrypt package if it is available, otherwise it will fall back to a slower pure-python implementation that is builtin.
encrypt using bcrypt.
In addition to the normal options that CryptAlgorithm.encrypt() takes, this function also accepts the following:
| Parameters: | rounds – Optionally specify the number of rounds to use (technically, bcrypt will actually use 2**rounds). This can be one of “fast”, “medium”, “slow”, or an integer in the range 4..31. See CryptAlgorithm.has_named_rounds for details on the meaning of “fast”, “medium” and “slow”. |
|---|
BPS also provides implementations of the hash algorithms used by MySql and PostgreSQL.
This implements Mysql’s OLD_PASSWORD algorithm, used prior to version 4.1.
See Mysql41Crypt for the new algorithm was put in place in version 4.1
This function is known to be very insecure, and should only be used to verify existing password hashes.
This implements Mysql new PASSWORD algorithm, introduced in version 4.1.
This function is unsalted, and therefore not very secure against rainbow attacks. It should only be used when dealing with mysql passwords, for all other purposes, you should use a salted hash function.
Description taken from http://dev.mysql.com/doc/refman/6.0/en/password-hashing.html
This implements the md5-based hash algorithm used by Postgres to store passwords in the pg_shadow table.
This algorithm shouldn’t be used for any purpose besides Postgres interaction, it’s a weak unsalted algorithm which could easily be attacked with a rainbow table.
Warning
This algorithm is slightly different from most of the others, in that both encrypt() and verify() require you pass in the name of the user account via the required ‘user’ keyword, since postgres uses this in place of a salt :(
Usage Example:
>>> from passlib import hash
>>> crypt = hash.PostgresMd5Crypt()
>>> crypt.encrypt("mypass", user="postgres")
'md55fba2ea04fd36069d2574ea71c8efe9d'
>>> crypt.verify("mypass", 'md55fba2ea04fd36069d2574ea71c8efe9d', user="postgres")
True
This context object contains the algorithms used by MySql 4.1 and newer for storing user passwords.
This context object should be able to read/write/verify the values found in the password field of the pg_shadow table in Postgres.