passwords.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. =============================
  2. Password management in Django
  3. =============================
  4. Password management is something that should generally not be reinvented
  5. unnecessarily, and Django endeavors to provide a secure and flexible set of
  6. tools for managing user passwords. This document describes how Django stores
  7. passwords, how the storage hashing can be configured, and some utilities to
  8. work with hashed passwords.
  9. .. _auth_password_storage:
  10. How Django stores passwords
  11. ===========================
  12. Django provides a flexible password storage system and uses PBKDF2 by default.
  13. The :attr:`~django.contrib.auth.models.User.password` attribute of a
  14. :class:`~django.contrib.auth.models.User` object is a string in this format::
  15. <algorithm>$<iterations>$<salt>$<hash>
  16. Those are the components used for storing a User's password, separated by the
  17. dollar-sign character and consist of: the hashing algorithm, the number of
  18. algorithm iterations (work factor), the random salt, and the resulting password
  19. hash. The algorithm is one of a number of one-way hashing or password storage
  20. algorithms Django can use; see below. Iterations describe the number of times
  21. the algorithm is run over the hash. Salt is the random seed used and the hash
  22. is the result of the one-way function.
  23. By default, Django uses the PBKDF2_ algorithm with a SHA256 hash, a
  24. password stretching mechanism recommended by NIST_. This should be
  25. sufficient for most users: it's quite secure, requiring massive
  26. amounts of computing time to break.
  27. However, depending on your requirements, you may choose a different
  28. algorithm, or even use a custom algorithm to match your specific
  29. security situation. Again, most users shouldn't need to do this -- if
  30. you're not sure, you probably don't. If you do, please read on:
  31. Django chooses the algorithm to use by consulting the
  32. :setting:`PASSWORD_HASHERS` setting. This is a list of hashing algorithm
  33. classes that this Django installation supports. The first entry in this list
  34. (that is, ``settings.PASSWORD_HASHERS[0]``) will be used to store passwords,
  35. and all the other entries are valid hashers that can be used to check existing
  36. passwords. This means that if you want to use a different algorithm, you'll
  37. need to modify :setting:`PASSWORD_HASHERS` to list your preferred algorithm
  38. first in the list.
  39. The default for :setting:`PASSWORD_HASHERS` is::
  40. PASSWORD_HASHERS = (
  41. 'django.contrib.auth.hashers.PBKDF2PasswordHasher',
  42. 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
  43. 'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
  44. 'django.contrib.auth.hashers.BCryptPasswordHasher',
  45. 'django.contrib.auth.hashers.SHA1PasswordHasher',
  46. 'django.contrib.auth.hashers.MD5PasswordHasher',
  47. 'django.contrib.auth.hashers.CryptPasswordHasher',
  48. )
  49. This means that Django will use PBKDF2_ to store all passwords, but will support
  50. checking passwords stored with PBKDF2SHA1, bcrypt_, SHA1_, etc. The next few
  51. sections describe a couple of common ways advanced users may want to modify this
  52. setting.
  53. .. _bcrypt_usage:
  54. Using bcrypt with Django
  55. ------------------------
  56. Bcrypt_ is a popular password storage algorithm that's specifically designed
  57. for long-term password storage. It's not the default used by Django since it
  58. requires the use of third-party libraries, but since many people may want to
  59. use it Django supports bcrypt with minimal effort.
  60. To use Bcrypt as your default storage algorithm, do the following:
  61. 1. Install the `bcrypt library`_ (probably by running ``sudo pip install
  62. bcrypt``, or downloading the library and installing it with ``python
  63. setup.py install``).
  64. 2. Modify :setting:`PASSWORD_HASHERS` to list ``BCryptSHA256PasswordHasher``
  65. first. That is, in your settings file, you'd put::
  66. PASSWORD_HASHERS = (
  67. 'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
  68. 'django.contrib.auth.hashers.BCryptPasswordHasher',
  69. 'django.contrib.auth.hashers.PBKDF2PasswordHasher',
  70. 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
  71. 'django.contrib.auth.hashers.SHA1PasswordHasher',
  72. 'django.contrib.auth.hashers.MD5PasswordHasher',
  73. 'django.contrib.auth.hashers.CryptPasswordHasher',
  74. )
  75. (You need to keep the other entries in this list, or else Django won't
  76. be able to upgrade passwords; see below).
  77. That's it -- now your Django install will use Bcrypt as the default storage
  78. algorithm.
  79. .. admonition:: Password truncation with BCryptPasswordHasher
  80. The designers of bcrypt truncate all passwords at 72 characters which means
  81. that ``bcrypt(password_with_100_chars) == bcrypt(password_with_100_chars[:72])``.
  82. The original ``BCryptPasswordHasher`` does not have any special handling and
  83. thus is also subject to this hidden password length limit.
  84. ``BCryptSHA256PasswordHasher`` fixes this by first first hashing the
  85. password using sha256. This prevents the password truncation and so should
  86. be preferred over the ``BCryptPasswordHasher``. The practical ramification
  87. of this truncation is pretty marginal as the average user does not have a
  88. password greater than 72 characters in length and even being truncated at 72
  89. the compute powered required to brute force bcrypt in any useful amount of
  90. time is still astronomical. Nonetheless, we recommend you use
  91. ``BCryptSHA256PasswordHasher`` anyway on the principle of "better safe than
  92. sorry".
  93. .. admonition:: Other bcrypt implementations
  94. There are several other implementations that allow bcrypt to be
  95. used with Django. Django's bcrypt support is NOT directly
  96. compatible with these. To upgrade, you will need to modify the
  97. hashes in your database to be in the form ``bcrypt$(raw bcrypt
  98. output)``. For example:
  99. ``bcrypt$$2a$12$NT0I31Sa7ihGEWpka9ASYrEFkhuTNeBQ2xfZskIiiJeyFXhRgS.Sy``.
  100. .. _increasing-password-algorithm-work-factor:
  101. Increasing the work factor
  102. --------------------------
  103. The PBKDF2 and bcrypt algorithms use a number of iterations or rounds of
  104. hashing. This deliberately slows down attackers, making attacks against hashed
  105. passwords harder. However, as computing power increases, the number of
  106. iterations needs to be increased. We've chosen a reasonable default (and will
  107. increase it with each release of Django), but you may wish to tune it up or
  108. down, depending on your security needs and available processing power. To do so,
  109. you'll subclass the appropriate algorithm and override the ``iterations``
  110. parameters. For example, to increase the number of iterations used by the
  111. default PBKDF2 algorithm:
  112. 1. Create a subclass of ``django.contrib.auth.hashers.PBKDF2PasswordHasher``::
  113. from django.contrib.auth.hashers import PBKDF2PasswordHasher
  114. class MyPBKDF2PasswordHasher(PBKDF2PasswordHasher):
  115. """
  116. A subclass of PBKDF2PasswordHasher that uses 100 times more iterations.
  117. """
  118. iterations = PBKDF2PasswordHasher.iterations * 100
  119. Save this somewhere in your project. For example, you might put this in
  120. a file like ``myproject/hashers.py``.
  121. 2. Add your new hasher as the first entry in :setting:`PASSWORD_HASHERS`::
  122. PASSWORD_HASHERS = (
  123. 'myproject.hashers.MyPBKDF2PasswordHasher',
  124. 'django.contrib.auth.hashers.PBKDF2PasswordHasher',
  125. 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
  126. 'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
  127. 'django.contrib.auth.hashers.BCryptPasswordHasher',
  128. 'django.contrib.auth.hashers.SHA1PasswordHasher',
  129. 'django.contrib.auth.hashers.MD5PasswordHasher',
  130. 'django.contrib.auth.hashers.CryptPasswordHasher',
  131. )
  132. That's it -- now your Django install will use more iterations when it
  133. stores passwords using PBKDF2.
  134. .. _password-upgrades:
  135. Password upgrading
  136. ------------------
  137. When users log in, if their passwords are stored with anything other than
  138. the preferred algorithm, Django will automatically upgrade the algorithm
  139. to the preferred one. This means that old installs of Django will get
  140. automatically more secure as users log in, and it also means that you
  141. can switch to new (and better) storage algorithms as they get invented.
  142. However, Django can only upgrade passwords that use algorithms mentioned in
  143. :setting:`PASSWORD_HASHERS`, so as you upgrade to new systems you should make
  144. sure never to *remove* entries from this list. If you do, users using
  145. unmentioned algorithms won't be able to upgrade.
  146. .. versionadded:: 1.6
  147. Passwords will be upgraded when changing the PBKDF2 iteration count.
  148. .. _sha1: http://en.wikipedia.org/wiki/SHA1
  149. .. _pbkdf2: http://en.wikipedia.org/wiki/PBKDF2
  150. .. _nist: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf
  151. .. _bcrypt: http://en.wikipedia.org/wiki/Bcrypt
  152. .. _`bcrypt library`: https://pypi.python.org/pypi/bcrypt/
  153. Manually managing a user's password
  154. ===================================
  155. .. module:: django.contrib.auth.hashers
  156. The :mod:`django.contrib.auth.hashers` module provides a set of functions
  157. to create and validate hashed password. You can use them independently
  158. from the ``User`` model.
  159. .. function:: check_password(password, encoded)
  160. If you'd like to manually authenticate a user by comparing a plain-text
  161. password to the hashed password in the database, use the convenience
  162. function :func:`check_password`. It takes two arguments: the plain-text
  163. password to check, and the full value of a user's ``password`` field in the
  164. database to check against, and returns ``True`` if they match, ``False``
  165. otherwise.
  166. .. versionchanged:: 1.6
  167. In Django 1.4 and 1.5, a blank string was unintentionally considered
  168. to be an unusable password, resulting in this method returning
  169. ``False`` for such a password.
  170. .. function:: make_password(password[, salt, hashers])
  171. Creates a hashed password in the format used by this application. It takes
  172. one mandatory argument: the password in plain-text. Optionally, you can
  173. provide a salt and a hashing algorithm to use, if you don't want to use the
  174. defaults (first entry of ``PASSWORD_HASHERS`` setting).
  175. Currently supported algorithms are: ``'pbkdf2_sha256'``, ``'pbkdf2_sha1'``,
  176. ``'bcrypt_sha256'`` (see :ref:`bcrypt_usage`), ``'bcrypt'``, ``'sha1'``,
  177. ``'md5'``, ``'unsalted_md5'`` (only for backward compatibility) and ``'crypt'``
  178. if you have the ``crypt`` library installed. If the password argument is
  179. ``None``, an unusable password is returned (a one that will be never
  180. accepted by :func:`check_password`).
  181. .. function:: is_password_usable(encoded_password)
  182. Checks if the given string is a hashed password that has a chance
  183. of being verified against :func:`check_password`.