passwords.txt 9.0 KB

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