passwords.txt 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. .. versionadded:: 1.4
  13. Django 1.4 introduces a new flexible password storage system and uses
  14. PBKDF2 by default. Previous versions of Django used SHA1, and other
  15. algorithms couldn't be chosen.
  16. The :attr:`~django.contrib.auth.models.User.password` attribute of a
  17. :class:`~django.contrib.auth.models.User` object is a string in this format::
  18. algorithm$hash
  19. That's a storage algorithm, and hash, separated by the dollar-sign
  20. character. The algorithm is one of a number of one way hashing or password
  21. storage algorithms Django can use; see below. The hash is the result of the one-
  22. 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 an algorithm by consulting the :setting:`PASSWORD_HASHERS`
  32. setting. This is a list of hashing algorithm classes that this Django
  33. installation supports. The first entry in this list (that is,
  34. ``settings.PASSWORD_HASHERS[0]``) will be used to store passwords, and all the
  35. other entries are valid hashers that can be used to check existing passwords.
  36. This means that if you want to use a different algorithm, you'll need to modify
  37. :setting:`PASSWORD_HASHERS` to list your preferred algorithm first in the list.
  38. The default for :setting:`PASSWORD_HASHERS` is::
  39. PASSWORD_HASHERS = (
  40. 'django.contrib.auth.hashers.PBKDF2PasswordHasher',
  41. 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
  42. 'django.contrib.auth.hashers.BCryptPasswordHasher',
  43. 'django.contrib.auth.hashers.SHA1PasswordHasher',
  44. 'django.contrib.auth.hashers.MD5PasswordHasher',
  45. 'django.contrib.auth.hashers.CryptPasswordHasher',
  46. )
  47. This means that Django will use PBKDF2_ to store all passwords, but will support
  48. checking passwords stored with PBKDF2SHA1, bcrypt_, SHA1_, etc. The next few
  49. sections describe a couple of common ways advanced users may want to modify this
  50. setting.
  51. .. _bcrypt_usage:
  52. Using bcrypt with Django
  53. ------------------------
  54. Bcrypt_ is a popular password storage algorithm that's specifically designed
  55. for long-term password storage. It's not the default used by Django since it
  56. requires the use of third-party libraries, but since many people may want to
  57. use it Django supports bcrypt with minimal effort.
  58. To use Bcrypt as your default storage algorithm, do the following:
  59. 1. Install the `py-bcrypt`_ library (probably by running ``sudo pip install
  60. py-bcrypt``, or downloading the library and installing it with ``python
  61. setup.py install``).
  62. 2. Modify :setting:`PASSWORD_HASHERS` to list ``BCryptPasswordHasher``
  63. first. That is, in your settings file, you'd put::
  64. PASSWORD_HASHERS = (
  65. 'django.contrib.auth.hashers.BCryptPasswordHasher',
  66. 'django.contrib.auth.hashers.PBKDF2PasswordHasher',
  67. 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
  68. 'django.contrib.auth.hashers.SHA1PasswordHasher',
  69. 'django.contrib.auth.hashers.MD5PasswordHasher',
  70. 'django.contrib.auth.hashers.CryptPasswordHasher',
  71. )
  72. (You need to keep the other entries in this list, or else Django won't
  73. be able to upgrade passwords; see below).
  74. That's it -- now your Django install will use Bcrypt as the default storage
  75. algorithm.
  76. .. admonition:: Other bcrypt implementations
  77. There are several other implementations that allow bcrypt to be
  78. used with Django. Django's bcrypt support is NOT directly
  79. compatible with these. To upgrade, you will need to modify the
  80. hashes in your database to be in the form `bcrypt$(raw bcrypt
  81. output)`. For example:
  82. `bcrypt$$2a$12$NT0I31Sa7ihGEWpka9ASYrEFkhuTNeBQ2xfZskIiiJeyFXhRgS.Sy`.
  83. Increasing the work factor
  84. --------------------------
  85. The PBKDF2 and bcrypt algorithms use a number of iterations or rounds of
  86. hashing. This deliberately slows down attackers, making attacks against hashed
  87. passwords harder. However, as computing power increases, the number of
  88. iterations needs to be increased. We've chosen a reasonable default (and will
  89. increase it with each release of Django), but you may wish to tune it up or
  90. down, depending on your security needs and available processing power. To do so,
  91. you'll subclass the appropriate algorithm and override the ``iterations``
  92. parameters. For example, to increase the number of iterations used by the
  93. default PBKDF2 algorithm:
  94. 1. Create a subclass of ``django.contrib.auth.hashers.PBKDF2PasswordHasher``::
  95. from django.contrib.auth.hashers import PBKDF2PasswordHasher
  96. class MyPBKDF2PasswordHasher(PBKDF2PasswordHasher):
  97. """
  98. A subclass of PBKDF2PasswordHasher that uses 100 times more iterations.
  99. """
  100. iterations = PBKDF2PasswordHasher.iterations * 100
  101. Save this somewhere in your project. For example, you might put this in
  102. a file like ``myproject/hashers.py``.
  103. 2. Add your new hasher as the first entry in :setting:`PASSWORD_HASHERS`::
  104. PASSWORD_HASHERS = (
  105. 'myproject.hashers.MyPBKDF2PasswordHasher',
  106. 'django.contrib.auth.hashers.PBKDF2PasswordHasher',
  107. 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
  108. 'django.contrib.auth.hashers.BCryptPasswordHasher',
  109. 'django.contrib.auth.hashers.SHA1PasswordHasher',
  110. 'django.contrib.auth.hashers.MD5PasswordHasher',
  111. 'django.contrib.auth.hashers.CryptPasswordHasher',
  112. )
  113. That's it -- now your Django install will use more iterations when it
  114. stores passwords using PBKDF2.
  115. Password upgrading
  116. ------------------
  117. When users log in, if their passwords are stored with anything other than
  118. the preferred algorithm, Django will automatically upgrade the algorithm
  119. to the preferred one. This means that old installs of Django will get
  120. automatically more secure as users log in, and it also means that you
  121. can switch to new (and better) storage algorithms as they get invented.
  122. However, Django can only upgrade passwords that use algorithms mentioned in
  123. :setting:`PASSWORD_HASHERS`, so as you upgrade to new systems you should make
  124. sure never to *remove* entries from this list. If you do, users using un-
  125. mentioned algorithms won't be able to upgrade.
  126. .. _sha1: http://en.wikipedia.org/wiki/SHA1
  127. .. _pbkdf2: http://en.wikipedia.org/wiki/PBKDF2
  128. .. _nist: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf
  129. .. _bcrypt: http://en.wikipedia.org/wiki/Bcrypt
  130. .. _py-bcrypt: http://pypi.python.org/pypi/py-bcrypt/
  131. Manually managing a user's password
  132. ===================================
  133. .. module:: django.contrib.auth.hashers
  134. .. versionadded:: 1.4
  135. The :mod:`django.contrib.auth.hashers` module provides a set of functions
  136. to create and validate hashed password. You can use them independently
  137. from the ``User`` model.
  138. .. function:: check_password(password, encoded)
  139. .. versionadded:: 1.4
  140. If you'd like to manually authenticate a user by comparing a plain-text
  141. password to the hashed password in the database, use the convenience
  142. function :func:`django.contrib.auth.hashers.check_password`. It takes two
  143. arguments: the plain-text password to check, and the full value of a
  144. user's ``password`` field in the database to check against, and returns
  145. ``True`` if they match, ``False`` otherwise.
  146. .. function:: make_password(password[, salt, hashers])
  147. .. versionadded:: 1.4
  148. Creates a hashed password in the format used by this application. It takes
  149. one mandatory argument: the password in plain-text. Optionally, you can
  150. provide a salt and a hashing algorithm to use, if you don't want to use the
  151. defaults (first entry of ``PASSWORD_HASHERS`` setting).
  152. Currently supported algorithms are: ``'pbkdf2_sha256'``, ``'pbkdf2_sha1'``,
  153. ``'bcrypt'`` (see :ref:`bcrypt_usage`), ``'sha1'``, ``'md5'``,
  154. ``'unsalted_md5'`` (only for backward compatibility) and ``'crypt'``
  155. if you have the ``crypt`` library installed. If the password argument is
  156. ``None``, an unusable password is returned (a one that will be never
  157. accepted by :func:`django.contrib.auth.hashers.check_password`).
  158. .. function:: is_password_usable(encoded_password)
  159. .. versionadded:: 1.4
  160. Checks if the given string is a hashed password that has a chance
  161. of being verified against :func:`django.contrib.auth.hashers.check_password`.