<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Password management in Django — Django 1.5.9 documentation</title> <link rel="stylesheet" href="../../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../', VERSION: '1.5.9', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../../_static/jquery.js"></script> <script type="text/javascript" src="../../_static/underscore.js"></script> <script type="text/javascript" src="../../_static/doctools.js"></script> <link rel="top" title="Django 1.5.9 documentation" href="../../index.html" /> <link rel="up" title="User authentication in Django" href="index.html" /> <link rel="next" title="Customizing authentication in Django" href="customizing.html" /> <link rel="prev" title="Using the Django authentication system" href="default.html" /> <script type="text/javascript" src="../../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../../ref/templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../index.html">Django 1.5.9 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../../index.html">Home</a> | <a title="Table of contents" href="../../contents.html">Table of contents</a> | <a title="Global index" href="../../genindex.html">Index</a> | <a title="Module index" href="../../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="default.html" title="Using the Django authentication system">previous</a> | <a href="../index.html" title="Using Django" accesskey="U">up</a> | <a href="customizing.html" title="Customizing authentication in Django">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="topics-auth-passwords"> <div class="section" id="s-password-management-in-django"> <span id="password-management-in-django"></span><h1>Password management in Django<a class="headerlink" href="#password-management-in-django" title="Permalink to this headline">¶</a></h1> <p>Password management is something that should generally not be reinvented unnecessarily, and Django endeavors to provide a secure and flexible set of tools for managing user passwords. This document describes how Django stores passwords, how the storage hashing can be configured, and some utilities to work with hashed passwords.</p> <div class="section" id="s-how-django-stores-passwords"> <span id="s-auth-password-storage"></span><span id="how-django-stores-passwords"></span><span id="auth-password-storage"></span><h2>How Django stores passwords<a class="headerlink" href="#how-django-stores-passwords" title="Permalink to this headline">¶</a></h2> <div class="versionadded"> <span class="title">New in Django 1.4:</span> Django 1.4 introduces a new flexible password storage system and uses PBKDF2 by default. Previous versions of Django used SHA1, and other algorithms couldn’t be chosen.</div> <p>The <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User.password" title="django.contrib.auth.models.User.password"><tt class="xref py py-attr docutils literal"><span class="pre">password</span></tt></a> attribute of a <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">User</span></tt></a> object is a string in this format:</p> <div class="highlight-python"><pre><algorithm>$<iterations>$<salt>$<hash></pre> </div> <p>Those are the components used for storing a User’s password, separated by the dollar-sign character and consist of: the hashing algorithm, the number of algorithm iterations (work factor), the random salt, and the resulting password hash. The algorithm is one of a number of one-way hashing or password storage algorithms Django can use; see below. Iterations describe the number of times the algorithm is run over the hash. Salt is the random seed used and the hash is the result of the one-way function.</p> <p>By default, Django uses the <a class="reference external" href="http://en.wikipedia.org/wiki/PBKDF2">PBKDF2</a> algorithm with a SHA256 hash, a password stretching mechanism recommended by <a class="reference external" href="http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf">NIST</a>. This should be sufficient for most users: it’s quite secure, requiring massive amounts of computing time to break.</p> <p>However, depending on your requirements, you may choose a different algorithm, or even use a custom algorithm to match your specific security situation. Again, most users shouldn’t need to do this – if you’re not sure, you probably don’t. If you do, please read on:</p> <p>Django chooses the algorithm to use by consulting the <a class="reference internal" href="../../ref/settings.html#std:setting-PASSWORD_HASHERS"><tt class="xref std std-setting docutils literal"><span class="pre">PASSWORD_HASHERS</span></tt></a> setting. This is a list of hashing algorithm classes that this Django installation supports. The first entry in this list (that is, <tt class="docutils literal"><span class="pre">settings.PASSWORD_HASHERS[0]</span></tt>) will be used to store passwords, and all the other entries are valid hashers that can be used to check existing passwords. This means that if you want to use a different algorithm, you’ll need to modify <a class="reference internal" href="../../ref/settings.html#std:setting-PASSWORD_HASHERS"><tt class="xref std std-setting docutils literal"><span class="pre">PASSWORD_HASHERS</span></tt></a> to list your preferred algorithm first in the list.</p> <p>The default for <a class="reference internal" href="../../ref/settings.html#std:setting-PASSWORD_HASHERS"><tt class="xref std std-setting docutils literal"><span class="pre">PASSWORD_HASHERS</span></tt></a> is:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">PASSWORD_HASHERS</span> <span class="o">=</span> <span class="p">(</span> <span class="s">'django.contrib.auth.hashers.PBKDF2PasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.BCryptPasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.SHA1PasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.MD5PasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.CryptPasswordHasher'</span><span class="p">,</span> <span class="p">)</span> </pre></div> </div> <p>This means that Django will use <a class="reference external" href="http://en.wikipedia.org/wiki/PBKDF2">PBKDF2</a> to store all passwords, but will support checking passwords stored with PBKDF2SHA1, <a class="reference external" href="http://en.wikipedia.org/wiki/Bcrypt">bcrypt</a>, <a class="reference external" href="http://en.wikipedia.org/wiki/SHA1">SHA1</a>, etc. The next few sections describe a couple of common ways advanced users may want to modify this setting.</p> <div class="section" id="s-using-bcrypt-with-django"> <span id="s-bcrypt-usage"></span><span id="using-bcrypt-with-django"></span><span id="bcrypt-usage"></span><h3>Using bcrypt with Django<a class="headerlink" href="#using-bcrypt-with-django" title="Permalink to this headline">¶</a></h3> <p><a class="reference external" href="http://en.wikipedia.org/wiki/Bcrypt">Bcrypt</a> is a popular password storage algorithm that’s specifically designed for long-term password storage. It’s not the default used by Django since it requires the use of third-party libraries, but since many people may want to use it Django supports bcrypt with minimal effort.</p> <p>To use Bcrypt as your default storage algorithm, do the following:</p> <ol class="arabic"> <li><p class="first">Install the <a class="reference external" href="http://pypi.python.org/pypi/py-bcrypt/">py-bcrypt</a> library (probably by running <tt class="docutils literal"><span class="pre">sudo</span> <span class="pre">pip</span> <span class="pre">install</span> <span class="pre">py-bcrypt</span></tt>, or downloading the library and installing it with <tt class="docutils literal"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">install</span></tt>).</p> </li> <li><p class="first">Modify <a class="reference internal" href="../../ref/settings.html#std:setting-PASSWORD_HASHERS"><tt class="xref std std-setting docutils literal"><span class="pre">PASSWORD_HASHERS</span></tt></a> to list <tt class="docutils literal"><span class="pre">BCryptPasswordHasher</span></tt> first. That is, in your settings file, you’d put:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">PASSWORD_HASHERS</span> <span class="o">=</span> <span class="p">(</span> <span class="s">'django.contrib.auth.hashers.BCryptPasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.PBKDF2PasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.SHA1PasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.MD5PasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.CryptPasswordHasher'</span><span class="p">,</span> <span class="p">)</span> </pre></div> </div> <p>(You need to keep the other entries in this list, or else Django won’t be able to upgrade passwords; see below).</p> </li> </ol> <p>That’s it – now your Django install will use Bcrypt as the default storage algorithm.</p> <div class="admonition-password-truncation-with-bcryptpasswordhasher admonition"> <p class="first admonition-title">Password truncation with BCryptPasswordHasher</p> <p class="last">The designers of bcrypt truncate all passwords at 72 characters which means that <tt class="docutils literal"><span class="pre">bcrypt(password_with_100_chars)</span> <span class="pre">==</span> <span class="pre">bcrypt(password_with_100_chars[:72])</span></tt>. <tt class="docutils literal"><span class="pre">BCryptPasswordHasher</span></tt> does not have any special handling and thus is also subject to this hidden password length limit. The practical ramification of this truncation is pretty marginal as the average user does not have a password greater than 72 characters in length and even being truncated at 72 the compute powered required to brute force bcrypt in any useful amount of time is still astronomical.</p> </div> <div class="admonition-other-bcrypt-implementations admonition"> <p class="first admonition-title">Other bcrypt implementations</p> <p class="last">There are several other implementations that allow bcrypt to be used with Django. Django’s bcrypt support is NOT directly compatible with these. To upgrade, you will need to modify the hashes in your database to be in the form <tt class="docutils literal"><span class="pre">bcrypt$(raw</span> <span class="pre">bcrypt</span> <span class="pre">output)</span></tt>. For example: <tt class="docutils literal"><span class="pre">bcrypt$$2a$12$NT0I31Sa7ihGEWpka9ASYrEFkhuTNeBQ2xfZskIiiJeyFXhRgS.Sy</span></tt>.</p> </div> </div> <div class="section" id="s-increasing-the-work-factor"> <span id="increasing-the-work-factor"></span><h3>Increasing the work factor<a class="headerlink" href="#increasing-the-work-factor" title="Permalink to this headline">¶</a></h3> <p>The PBKDF2 and bcrypt algorithms use a number of iterations or rounds of hashing. This deliberately slows down attackers, making attacks against hashed passwords harder. However, as computing power increases, the number of iterations needs to be increased. We’ve chosen a reasonable default (and will increase it with each release of Django), but you may wish to tune it up or down, depending on your security needs and available processing power. To do so, you’ll subclass the appropriate algorithm and override the <tt class="docutils literal"><span class="pre">iterations</span></tt> parameters. For example, to increase the number of iterations used by the default PBKDF2 algorithm:</p> <ol class="arabic"> <li><p class="first">Create a subclass of <tt class="docutils literal"><span class="pre">django.contrib.auth.hashers.PBKDF2PasswordHasher</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib.auth.hashers</span> <span class="kn">import</span> <span class="n">PBKDF2PasswordHasher</span> <span class="k">class</span> <span class="nc">MyPBKDF2PasswordHasher</span><span class="p">(</span><span class="n">PBKDF2PasswordHasher</span><span class="p">):</span> <span class="sd">"""</span> <span class="sd"> A subclass of PBKDF2PasswordHasher that uses 100 times more iterations.</span> <span class="sd"> """</span> <span class="n">iterations</span> <span class="o">=</span> <span class="n">PBKDF2PasswordHasher</span><span class="o">.</span><span class="n">iterations</span> <span class="o">*</span> <span class="mi">100</span> </pre></div> </div> <p>Save this somewhere in your project. For example, you might put this in a file like <tt class="docutils literal"><span class="pre">myproject/hashers.py</span></tt>.</p> </li> <li><p class="first">Add your new hasher as the first entry in <a class="reference internal" href="../../ref/settings.html#std:setting-PASSWORD_HASHERS"><tt class="xref std std-setting docutils literal"><span class="pre">PASSWORD_HASHERS</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">PASSWORD_HASHERS</span> <span class="o">=</span> <span class="p">(</span> <span class="s">'myproject.hashers.MyPBKDF2PasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.PBKDF2PasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.BCryptPasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.SHA1PasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.MD5PasswordHasher'</span><span class="p">,</span> <span class="s">'django.contrib.auth.hashers.CryptPasswordHasher'</span><span class="p">,</span> <span class="p">)</span> </pre></div> </div> </li> </ol> <p>That’s it – now your Django install will use more iterations when it stores passwords using PBKDF2.</p> </div> <div class="section" id="s-password-upgrading"> <span id="password-upgrading"></span><h3>Password upgrading<a class="headerlink" href="#password-upgrading" title="Permalink to this headline">¶</a></h3> <p>When users log in, if their passwords are stored with anything other than the preferred algorithm, Django will automatically upgrade the algorithm to the preferred one. This means that old installs of Django will get automatically more secure as users log in, and it also means that you can switch to new (and better) storage algorithms as they get invented.</p> <p>However, Django can only upgrade passwords that use algorithms mentioned in <a class="reference internal" href="../../ref/settings.html#std:setting-PASSWORD_HASHERS"><tt class="xref std std-setting docutils literal"><span class="pre">PASSWORD_HASHERS</span></tt></a>, so as you upgrade to new systems you should make sure never to <em>remove</em> entries from this list. If you do, users using un- mentioned algorithms won’t be able to upgrade.</p> </div> </div> <div class="section" id="s-module-django.contrib.auth.hashers"> <span id="s-manually-managing-a-user-s-password"></span><span id="module-django.contrib.auth.hashers"></span><span id="manually-managing-a-user-s-password"></span><h2>Manually managing a user’s password<a class="headerlink" href="#module-django.contrib.auth.hashers" title="Permalink to this headline">¶</a></h2> <div class="versionadded"> <span class="title">New in Django 1.4:</span> The <a class="reference internal" href="#module-django.contrib.auth.hashers" title="django.contrib.auth.hashers"><tt class="xref py py-mod docutils literal"><span class="pre">django.contrib.auth.hashers</span></tt></a> module provides a set of functions to create and validate hashed password. You can use them independently from the <tt class="docutils literal"><span class="pre">User</span></tt> model.</div> <dl class="function"> <dt id="django.contrib.auth.hashers.check_password"> <tt class="descname">check_password</tt>(<em>password</em>, <em>encoded</em>)<a class="headerlink" href="#django.contrib.auth.hashers.check_password" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.4.</span> </div> <p>If you’d like to manually authenticate a user by comparing a plain-text password to the hashed password in the database, use the convenience function <a class="reference internal" href="#django.contrib.auth.hashers.check_password" title="django.contrib.auth.hashers.check_password"><tt class="xref py py-func docutils literal"><span class="pre">check_password()</span></tt></a>. It takes two arguments: the plain-text password to check, and the full value of a user’s <tt class="docutils literal"><span class="pre">password</span></tt> field in the database to check against, and returns <tt class="docutils literal"><span class="pre">True</span></tt> if they match, <tt class="docutils literal"><span class="pre">False</span></tt> otherwise.</p> </dd></dl> <dl class="function"> <dt id="django.contrib.auth.hashers.make_password"> <tt class="descname">make_password</tt>(<em>password</em><span class="optional">[</span>, <em>salt</em>, <em>hashers</em><span class="optional">]</span>)<a class="headerlink" href="#django.contrib.auth.hashers.make_password" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.4.</span> </div> <p>Creates a hashed password in the format used by this application. It takes one mandatory argument: the password in plain-text. Optionally, you can provide a salt and a hashing algorithm to use, if you don’t want to use the defaults (first entry of <tt class="docutils literal"><span class="pre">PASSWORD_HASHERS</span></tt> setting). Currently supported algorithms are: <tt class="docutils literal"><span class="pre">'pbkdf2_sha256'</span></tt>, <tt class="docutils literal"><span class="pre">'pbkdf2_sha1'</span></tt>, <tt class="docutils literal"><span class="pre">'bcrypt'</span></tt> (see <a class="reference internal" href="#bcrypt-usage"><em>Using bcrypt with Django</em></a>), <tt class="docutils literal"><span class="pre">'sha1'</span></tt>, <tt class="docutils literal"><span class="pre">'md5'</span></tt>, <tt class="docutils literal"><span class="pre">'unsalted_md5'</span></tt> (only for backward compatibility) and <tt class="docutils literal"><span class="pre">'crypt'</span></tt> if you have the <tt class="docutils literal"><span class="pre">crypt</span></tt> library installed. If the password argument is <tt class="docutils literal"><span class="pre">None</span></tt>, an unusable password is returned (a one that will be never accepted by <a class="reference internal" href="#django.contrib.auth.hashers.check_password" title="django.contrib.auth.hashers.check_password"><tt class="xref py py-func docutils literal"><span class="pre">check_password()</span></tt></a>).</p> </dd></dl> <dl class="function"> <dt id="django.contrib.auth.hashers.is_password_usable"> <tt class="descname">is_password_usable</tt>(<em>encoded_password</em>)<a class="headerlink" href="#django.contrib.auth.hashers.is_password_usable" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.4.</span> </div> <p>Checks if the given string is a hashed password that has a chance of being verified against <a class="reference internal" href="#django.contrib.auth.hashers.check_password" title="django.contrib.auth.hashers.check_password"><tt class="xref py py-func docutils literal"><span class="pre">check_password()</span></tt></a>.</p> </dd></dl> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Password management in Django</a><ul> <li><a class="reference internal" href="#how-django-stores-passwords">How Django stores passwords</a><ul> <li><a class="reference internal" href="#using-bcrypt-with-django">Using bcrypt with Django</a></li> <li><a class="reference internal" href="#increasing-the-work-factor">Increasing the work factor</a></li> <li><a class="reference internal" href="#password-upgrading">Password upgrading</a></li> </ul> </li> <li><a class="reference internal" href="#module-django.contrib.auth.hashers">Manually managing a user’s password</a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="default.html">Using the Django authentication system</a></li> <li>Next: <a href="customizing.html">Customizing authentication in Django</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../index.html">Django 1.5.9 documentation</a> <ul><li><a href="../index.html">Using Django</a> <ul><li><a href="index.html">User authentication in Django</a> <ul><li>Password management in Django</li></ul> </li></ul></li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/topics/auth/passwords.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Aug 21, 2014</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="default.html" title="Using the Django authentication system">previous</a> | <a href="../index.html" title="Using Django" accesskey="U">up</a> | <a href="customizing.html" title="Customizing authentication in Django">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>