Sophie

Sophie

distrib > Mageia > 7 > armv7hl > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 2382

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Safe Password Hashing</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="faq.using.html">Using PHP</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="faq.html.html">PHP and HTML</a></div>
 <div class="up"><a href="faq.html">FAQ</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="faq.passwords" class="chapter">
  <h1>Safe Password Hashing</h1>

  
  
  <p class="para">
   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 I hash passwords supplied by users of my application?
     
    </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 I hash my passwords, 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 do I store my salts?
     
    </a></li></ol></div>
   <dl class="qandaentry" id="faq.passwords.hashing">
    <dt><strong>
     
      Why should I hash passwords supplied by users of my application?
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      Password hashing is one of the most basic security considerations that
      must be made when designing any application that accepts passwords
      from users. Without hashing, any passwords that are stored in your
      application&#039;s database can be stolen if the database is compromised, and
      then immediately used to compromise not only your application, but also
      the accounts of your users on other services, if they do not use
      unique passwords.
     </p>
     <p class="para">
      By applying a hashing algorithm to your user&#039;s passwords before storing
      them in your database, you make it 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="para">
      It is important to note, however, that hashing passwords only protects
      them from being compromised in your data store, but does not necessarily
      protect them from being intercepted by malicious code injected into your
      application itself.
     </p>
    </dd>
   </dl>
   <dl class="qandaentry" id="faq.passwords.fasthash">
    <dt><strong>
     
      Why are common hashing functions such as <span class="function"><a href="function.md5.html" class="function">md5()</a></span> and
      <span class="function"><a href="function.sha1.html" class="function">sha1()</a></span> unsuitable for passwords?
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      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 &quot;brute force&quot; the output of these algorithms,
      in order to determine the original input.
     </p>
     <p class="para">
      Because of how quickly a modern computer can &quot;reverse&quot; 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>
     
      How should I hash my passwords, if the common hash functions are
      not suitable?
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      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="para">
      PHP 5.5 provides
      <a href="book.password.html" class="link">a native password hashing API</a> that
      safely handles both <a href="function.password-hash.html" class="link">hashing</a>
      and <a href="function.password-verify.html" class="link">verifying passwords</a>
      in a secure manner. There is also
      <a href="https://github.com/ircmaxell/password_compat" class="link external">&raquo;&nbsp;a pure PHP compatibility library</a>
      available for PHP 5.3.7 and later.
     </p>
     <p class="para">
      Another option is the <span class="function"><a href="function.crypt.html" class="function">crypt()</a></span> function, which
      supports several hashing algorithms in PHP 5.3 and later. When using
      this function, you are guaranteed that the algorithm you select is
      available, as PHP contains native implementations of each supported
      algorithm, in case one or more are not supported by your system.
     </p>
     <p class="para">
      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="para">
      Note that if you are using <span class="function"><a href="function.crypt.html" class="function">crypt()</a></span> to verify a
      password, you will need to take care to prevent timing attacks by using
      a constant time string comparison. Neither PHP&#039;s
      <a href="language.operators.comparison.html" class="link">== and === operators</a>
      nor <span class="function"><a href="function.strcmp.html" class="function">strcmp()</a></span> perform constant time string
      comparisons. As <span class="function"><a href="function.password-verify.html" class="function">password_verify()</a></span> will do this for
      you, you are strongly encouraged to use the
      <a href="book.password.html" class="link">native password hashing API</a>
      whenever possible.
     </p>
    </dd>
   </dl>
   <dl class="qandaentry" id="faq.passwords.salt">
    <dt><strong>
     
      What is a salt?
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      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="para">
      In more simple terms, a salt is a bit of additional data which makes
      your 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="para">
      <span class="function"><a href="function.password-hash.html" 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>
     
      How do I store my salts?
     
    </strong></dt>
    <dd class="answer">
     <p class="para">
      When using <span class="function"><a href="function.password-hash.html" class="function">password_hash()</a></span> or
      <span class="function"><a href="function.crypt.html" class="function">crypt()</a></span>, the return value includes the salt as part
      of the generated hash. This value should be stored verbatim in your
      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.html" class="function">password_verify()</a></span> or <span class="function"><a href="function.crypt.html" class="function">crypt()</a></span> when
      verifying passwords.
     </p>
     <p class="para">
      The following diagram shows the format of a return value from
      <span class="function"><a href="function.crypt.html" class="function">crypt()</a></span> or <span class="function"><a href="function.password-hash.html" class="function">password_hash()</a></span>. As you
      can see, 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>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="faq.using.html">Using PHP</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="faq.html.html">PHP and HTML</a></div>
 <div class="up"><a href="faq.html">FAQ</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>