<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/faq.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'faq.passwords.php',
    1 => 'Password Hashing',
    2 => 'Hashing passwords safely and securely',
  ),
  'up' => 
  array (
    0 => 'faq.php',
    1 => 'FAQ',
  ),
  'prev' => 
  array (
    0 => 'faq.using.php',
    1 => 'Using PHP',
  ),
  'next' => 
  array (
    0 => 'faq.html.php',
    1 => 'PHP and HTML',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'faq/passwords.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="faq.passwords" class="chapter">
  <h1 class="title">Hashing passwords safely and securely</h1>

  
  
  <p class="simpara">
   This section explains the reasons behind using hashing functions
   to secure passwords, as well as how to do so effectively.
  </p>
  
  <div class="qandaset"><ol class="qandaset_questions"><li><a href="#faq.passwords.hashing">
     
      Why should passwords supplied by users be hashed?
     
    </a></li><li><a href="#faq.passwords.fasthash">
     
      Why are common hashing functions such as md5 and
      sha1 unsuitable for passwords?
     
    </a></li><li><a href="#faq.passwords.bestpractice">
     
      How should passwords be hashed, if the common hash functions are
      not suitable?
     
    </a></li><li><a href="#faq.passwords.salt">
     
      What is a salt?
     
    </a></li><li><a href="#faq.password.storing-salts">
     
      How are salts stored?
     
    </a></li></ol></div>
   <dl class="qandaentry" id="faq.passwords.hashing">
    <dt><strong>
     <p class="simpara">
      Why should passwords supplied by users be hashed?
     </p>
    </strong></dt>
    <dd class="answer">
     <p class="simpara">
      Password hashing is one of the most basic security considerations that
      must be made when designing any application or service that accepts passwords
      from users. Without hashing, any passwords that are stored
      can be stolen if the data store is compromised, and
      then immediately used to compromise not only the application or service, but also
      the accounts of users on other services, if they do not use
      unique passwords.
     </p>
     <p class="simpara">
      By applying a hashing algorithm to the user&#039;s passwords before storing
      them, it becomes implausible for any attacker to
      determine the original password, while still being able to compare
      the resulting hash to the original password in the future.
     </p>
     <p class="simpara">
      It is important to note, however, that hashing passwords only protects
      them from being compromised in the data store, but does not necessarily
      protect them from being intercepted by malicious code injected into the
      application or service itself.
     </p>
    </dd>
   </dl>
   <dl class="qandaentry" id="faq.passwords.fasthash">
    <dt><strong>
     <p class="simpara">
      Why are common hashing functions such as <span class="function"><a href="function.md5.php" class="function">md5()</a></span> and
      <span class="function"><a href="function.sha1.php" class="function">sha1()</a></span> unsuitable for passwords?
     </p>
    </strong></dt>
    <dd class="answer">
     <p class="simpara">
      Hashing algorithms such as MD5, SHA1 and SHA256 are designed to be
      very fast and efficient. With modern techniques and computer equipment,
      it has become trivial to <q class="quote">brute force</q> the output of these algorithms,
      in order to determine the original input.
     </p>
     <p class="simpara">
      Because of how quickly a modern computer can <q class="quote">reverse</q> these hashing
      algorithms, many security professionals strongly suggest against
      their use for password hashing.
     </p>
    </dd>
   </dl>
   <dl class="qandaentry" id="faq.passwords.bestpractice">
    <dt><strong>
     <p class="simpara">
      How should passwords be hashed, if the common hash functions are
      not suitable?
     </p>
    </strong></dt>
    <dd class="answer">
     <p class="simpara">
      When hashing passwords, the two most important considerations are the
      computational expense, and the salt. The more computationally expensive
      the hashing algorithm, the longer it will take to brute force its
      output.
     </p>
     <p class="simpara">
      PHP provides
      <a href="book.password.php" class="link">a native password hashing API</a> that
      safely handles both <a href="function.password-hash.php" class="link">hashing</a>
      and <a href="function.password-verify.php" class="link">verifying passwords</a>
      in a secure manner.
     </p>
     <p class="simpara">
      The suggested algorithm to use when hashing passwords is Blowfish, which
      is also the default used by the password hashing API, as it is
      significantly more computationally expensive than MD5 or SHA1, while
      still being scalable.
     </p>
     <p class="simpara">
      The <span class="function"><a href="function.crypt.php" class="function">crypt()</a></span> function is also available for password
      hashing, but it is only recommended for interoperability with other
      systems.
      Instead, it is strongly encouraged to use the
      <a href="book.password.php" class="link">native password hashing API</a>
      whenever possible.
     </p>
    </dd>
   </dl>
   <dl class="qandaentry" id="faq.passwords.salt">
    <dt><strong>
     <p class="simpara">
      What is a salt?
     </p>
    </strong></dt>
    <dd class="answer">
     <p class="simpara">
      A cryptographic salt is data which is applied during the hashing process
      in order to eliminate the possibility of the output being looked up
      in a list of pre-calculated pairs of hashes and their input, known as
      a rainbow table.
     </p>
     <p class="simpara">
      In more simple terms, a salt is a bit of additional data which makes
      hashes significantly more difficult to crack. There are a number of
      services online which provide extensive lists of pre-computed hashes, as
      well as the original input for those hashes. The use of a salt makes it
      implausible or impossible to find the resulting hash in one of these
      lists.
     </p>
     <p class="simpara">
      <span class="function"><a href="function.password-hash.php" class="function">password_hash()</a></span> will create a random salt if one
      isn&#039;t provided, and this is generally the easiest and most secure
      approach.
     </p>
    </dd>
   </dl>
   <dl class="qandaentry" id="faq.password.storing-salts">
    <dt><strong>
     <p class="simpara">
      How are salts stored?
     </p>
    </strong></dt>
    <dd class="answer">
     <p class="simpara">
      When using <span class="function"><a href="function.password-hash.php" class="function">password_hash()</a></span> or
      <span class="function"><a href="function.crypt.php" class="function">crypt()</a></span>, the return value includes the salt as part
      of the generated hash. This value should be stored verbatim in the
      database, as it includes information about the hash function that was
      used and can then be given directly to
      <span class="function"><a href="function.password-verify.php" class="function">password_verify()</a></span> when verifying passwords.
     </p>
     <div class="warning"><strong class="warning">Warning</strong>
      <p class="simpara">
       <span class="function"><a href="function.password-verify.php" class="function">password_verify()</a></span> should always be used instead
       of re-hashing and comparing the result to a stored hash in order
       to avoid timing attacks.
      </p>
     </div>
     <p class="simpara">
      The following diagram shows the format of a return value from
      <span class="function"><a href="function.crypt.php" class="function">crypt()</a></span> or <span class="function"><a href="function.password-hash.php" class="function">password_hash()</a></span>. As can
      be seen, they are self-contained, with all the information on the
      algorithm and salt required for future password verification.
     </p>
     <p class="para">
      <div class="mediaobject">
       
       <div class="imageobject">
        <img src="images/2a34c7f2e658f6ae74f3869f2aa5886f-crypt-text-rendered.svg" alt="
        The components of the value returned by password_hash and crypt: in
        order, the chosen algorithm, the algorithm's options, the salt used,
        and the hashed password.
       " width="690" height="192" />
       </div>
      </div>
     </p>
    </dd>
   </dl>
  
  
 </div>
<?php manual_footer($setup); ?>