<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.hash.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.hash-equals.php',
    1 => 'hash_equals',
    2 => 'Timing attack safe string comparison',
  ),
  'up' => 
  array (
    0 => 'ref.hash.php',
    1 => 'Функції Hash',
  ),
  'prev' => 
  array (
    0 => 'function.hash-copy.php',
    1 => 'hash_copy',
  ),
  'next' => 
  array (
    0 => 'function.hash-file.php',
    1 => 'hash_file',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/hash/functions/hash-equals.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.hash-equals" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">hash_equals</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.6.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">hash_equals</span> &mdash; <span class="dc-title">Timing attack safe string comparison</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.hash-equals-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>hash_equals</strong></span>(<span class="methodparam"><span class="attribute"><a href="class.sensitiveparameter.php">#[\SensitiveParameter]</a> </span><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$known_string</code></span>, <span class="methodparam"><span class="attribute"><a href="class.sensitiveparameter.php">#[\SensitiveParameter]</a> </span><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$user_string</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   Checks whether two strings are equal without leaking information about the
   contents of <code class="parameter">known_string</code> via the execution time.
  </p>
  <p class="para">
   This function can be used to mitigate timing attacks. Performing a regular
   comparison with <code class="code">===</code> will take more or less time to execute
   depending on whether the two values are different or not and at which
   position the first difference can be found, thus leaking information about
   the contents of the secret <code class="parameter">known_string</code>.
  </p>
  <div class="caution"><strong class="caution">Застереження</strong>
   <p class="para">
    It is important to provide the user-supplied string as the second
    parameter, rather than the first.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.hash-equals-parameters">
  <h3 class="title">Параметри</h3>
  <dl>
   
    <dt><code class="parameter">known_string</code></dt>
    <dd>
     <p class="para">
       The known <span class="type"><a href="language.types.string.php" class="type string">string</a></span> that must be kept secret.
     </p>
    </dd>
   
   
    <dt><code class="parameter">user_string</code></dt>
    <dd>
     <p class="para">
      The user-supplied <span class="type"><a href="language.types.string.php" class="type string">string</a></span> to compare against.
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.hash-equals-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
    Returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> when the two strings are equal, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> otherwise.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.hash-equals-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   <div class="example" id="foobar.examples.basic">
    <p><strong>Приклад #1 <span class="function"><strong>hash_equals()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$secretKey </span><span style="color: #007700">= </span><span style="color: #DD0000">'8uRhAeH89naXfFXKGOEj'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Value and signature are provided by the user, e.g. within the URL<br />// and retrieved using $_GET.<br /></span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #DD0000">'username=rasmuslerdorf'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$signature </span><span style="color: #007700">= </span><span style="color: #DD0000">'8c35009d3b50caf7f5d2c1e031842e6b7823a1bb781d33c5237cd27b57b5f327'</span><span style="color: #007700">;<br /><br />if (</span><span style="color: #0000BB">hash_equals</span><span style="color: #007700">(</span><span style="color: #0000BB">hash_hmac</span><span style="color: #007700">(</span><span style="color: #DD0000">'sha256'</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">, </span><span style="color: #0000BB">$secretKey</span><span style="color: #007700">), </span><span style="color: #0000BB">$signature</span><span style="color: #007700">)) {<br />    echo </span><span style="color: #DD0000">"The value is correctly signed."</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />} else {<br />    echo </span><span style="color: #DD0000">"The value was tampered with."</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">The value is correctly signed.</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.hash-equals-notes">
  <h3 class="title">Примітки</h3>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    Both arguments must be of the same length to be compared successfully.
    When arguments of differing length are supplied, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> is returned immediately and
    the length of the known string may be leaked in case of a timing attack.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.hash-equals-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.hash-hmac.php" class="function" rel="rdfs-seeAlso">hash_hmac()</a> - Generate a keyed hash value using the HMAC method</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>