The Django web framework provides a module for storing user accounts and passwords (django.contrib.auth). This module’s password hashing code supports a few simple salted digests, stored using the format id$salt$checksum (where id is an identifier assigned by Django). Passlib provides support for all the hashes used up to and including Django 1.4
See also
Django 1.4 introduced a new “hashers” framework, as well as three new modern large-salt variable-cost hash algorithms:
These classes can be used directly as follows:
>>> from passlib.hash import django_pbkdf2_sha256 as handler
>>> # encrypt password
>>> h = handler.encrypt("password")
>>> h
'pbkdf2_sha256$10000$s1w0UXDd00XB$+4ORmyvVWAQvoAEWlDgN34vlaJx1ZTZpa1pCSRey2Yk='
>>> # verify password
>>> handler.verify("password", h)
True
>>> handler.verify("eville", h)
False
See also
the generic PasswordHash usage examples
This class implements Django’s PBKDF2-HMAC-SHA256 hash, and follows the Password Hash Interface.
It supports a variable-length salt, and a variable number of rounds.
The encrypt() and genconfig() methods accept the following optional keywords:
| Parameters: |
|
|---|
This should be compatible with the hashes generated by Django 1.4’s PBKDF2PasswordHasher class.
New in version 1.6.
This class implements Django’s PBKDF2-HMAC-SHA1 hash, and follows the Password Hash Interface.
It supports a variable-length salt, and a variable number of rounds.
The encrypt() and genconfig() methods accept the following optional keywords:
| Parameters: |
|
|---|
This should be compatible with the hashes generated by Django 1.4’s PBKDF2SHA1PasswordHasher class.
New in version 1.6.
This class implements Django 1.4’s BCrypt wrapper, and follows the Password Hash Interface.
This is identical to bcrypt itself, but with the Django-specific prefix "bcrypt$" prepended. See passlib.hash.bcrypt - BCrypt for more details, the usage and behavior is identical.
This should be compatible with the hashes generated by Django 1.4’s BCryptPasswordHasher class.
New in version 1.6.
An example django_pbkdf2_sha256 hash (of password) is:
pbkdf2_sha256$10000$s1w0UXDd00XB$+4ORmyvVWAQvoAEWlDgN34vlaJx1ZTZpa1pCSRey2Yk=
Both of Django’s PBKDF2 hashes have the same basic format, ident$rounds$salt$checksum, where:
The digest porition is generated by passing the utf-8 encoded password, the ascii-encoded salt string, and the number of rounds into PBKDF2 using the HMAC-SHA256 prf; and generated a 32 byte checksum, which is then encoding using base64.
The other PBKDF2 wrapper functions similarly.
Warning
All of the following hashes are very susceptible to brute-force attacks; since they are simple single-round salted digests. They should not be used for any purpose besides manipulating existing Django password hashes.
Django 1.0 supports some basic salted digests, as well as some legacy hashes:
These classes can be used directly as follows:
>>> from passlib.hash import django_salted_sha1 as handler
>>> # encrypt password
>>> h = handler.encrypt("password")
>>> h
'sha1$c6218$161d1ac8ab38979c5a31cbaba4a67378e7e60845'
>>> # verify password
>>> handler.verify("password", h)
True
>>> handler.verify("eville", h)
False
See also
the generic PasswordHash usage examples
This class implements Django’s Salted MD5 hash, and follows the Password Hash Interface.
It supports a variable-length salt, and uses a single round of MD5.
The encrypt() and genconfig() methods accept the following optional keywords:
| Parameters: |
|
|---|
This should be compatible with the hashes generated by Django 1.4’s MD5PasswordHasher class.
This class implements Django’s Salted SHA1 hash, and follows the Password Hash Interface.
It supports a variable-length salt, and uses a single round of SHA1.
The encrypt() and genconfig() methods accept the following optional keywords:
| Parameters: |
|
|---|
This should be compatible with Django 1.4’s SHA1PasswordHasher class.
An example django_salted_sha1 hash (of password) is:
sha1$f8793$c4cd18eb02375a037885706d414d68d521ca18c7
Both of Django’s salted hashes have the same basic format, ident$salt$checksum, where:
The checksum is generated by concatenating the salt digits followed by the password, and hashing them using the specified digest (MD5 or SHA-1). The digest is then encoded to hexidecimal. If the password is unicode, it is converted to utf-8 first.
Django’s salted hashes should not be considered very secure.
This class implements Django’s des_crypt wrapper, and follows the Password Hash Interface.
It supports a fixed-length salt.
The encrypt() and genconfig() methods accept the following optional keywords:
| Parameters: | salt (str) – Optional salt string. If not specified, one will be autogenerated (this is recommended). If specified, it must be 2 characters, drawn from the regexp range [./0-9A-Za-z]. |
|---|
This should be compatible with the hashes generated by Django 1.4’s CryptPasswordHasher class. Note that Django only supports this hash on Unix systems (though django_des_crypt is available cross-platform under Passlib).
Changed in version 1.6: This class will now accept hashes with empty salt strings, since Django 1.4 generates them this way.
An example django_des_crypt hash (of password) is crypt$cd1a4$cdlRbNJGImptk; the general format is the same as the salted hashes: ident$salt$checksum, where:
It should be noted that this class essentially just shoe-horns des_crypt into a format compatible with the Django salted hashes (above). It has a few quirks, such as the fact that only the first two characters of the salt are used by des_crypt, and they are in turn duplicated as the first two characters of the checksum.
For security issues relating to django_des_crypt, see des_crypt.
This class provides disabled password behavior for Django, and follows the Password Hash Interface.
This class does not implement a hash, but instead claims the special hash string "!" which Django uses to indicate an account’s password has been disabled.
Note
Some older (pre-1.0) versions of Django encoded passwords using hex_md5, though this has been deprecated by Django, and should become increasingly rare.