<?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-hkdf.php',
    1 => 'hash_hkdf',
    2 => 'Generate a HKDF key derivation of a supplied key input',
  ),
  'up' => 
  array (
    0 => 'ref.hash.php',
    1 => 'Функції Hash',
  ),
  'prev' => 
  array (
    0 => 'function.hash-final.php',
    1 => 'hash_final',
  ),
  'next' => 
  array (
    0 => 'function.hash-hmac.php',
    1 => 'hash_hmac',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/hash/functions/hash-hkdf.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.hash-hkdf" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">hash_hkdf</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.1.2, PHP 8)</p><p class="refpurpose"><span class="refname">hash_hkdf</span> &mdash; <span class="dc-title">Generate a HKDF key derivation of a supplied key input</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.hash-hkdf-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>hash_hkdf</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$algo</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<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">$key</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$length</code><span class="initializer"> = 0</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$info</code><span class="initializer"> = &quot;&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$salt</code><span class="initializer"> = &quot;&quot;</span></span><br>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>


 </div>

 <div class="refsect1 parameters" id="refsect1-function.hash-hkdf-parameters">
  <h3 class="title">Параметри</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">algo</code></dt>
     <dd>
      <p class="para">
       Name of selected hashing algorithm (e.g. <code class="literal">&quot;sha256&quot;</code>).
       For a list of supported algorithms see <span class="function"><a href="function.hash-hmac-algos.php" class="function">hash_hmac_algos()</a></span>.
       <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
        <p class="para">
         Non-cryptographic hash functions are not allowed.
        </p>
       </p></blockquote>
      </p>
     </dd>
    
    
     <dt><code class="parameter">key</code></dt>
     <dd>
      <p class="para">
       Input keying material (raw binary). Cannot be empty.
      </p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       Desired output length in bytes.
       Cannot be greater than 255 times the chosen hash function size.
      </p>
      <p class="para">
       If <code class="parameter">length</code> is <code class="literal">0</code>, the output length
       will default to the chosen hash function size.
      </p>
     </dd>
    
    
     <dt><code class="parameter">info</code></dt>
     <dd>
      <p class="para">
       Application/context-specific info string.
      </p>
     </dd>
    
    
     <dt><code class="parameter">salt</code></dt>
     <dd>
      <p class="para">
       Salt to use during derivation.
      </p>
      <p class="para">
       While optional, adding random salt significantly improves the strength of HKDF.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.hash-hkdf-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
   Returns a string containing a raw binary representation of the derived key
   (also known as output keying material - OKM).
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.hash-hkdf-errors">
  <h3 class="title">Помилки/виключення</h3>
  <p class="para">
   Throws a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> exception if <code class="parameter">key</code>
   is empty, <code class="parameter">algo</code> is unknown/non-cryptographic,
   <code class="parameter">length</code> is less than <code class="literal">0</code> or too large
   (greater than 255 times the size of the hash function).
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.hash-hkdf-changelog">
  <h3 class="title">Журнал змін</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Версія</th>
       <th>Опис</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        Now throws a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> exception on error.
        Previously, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> was returned and an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>
        message was emitted.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.hash-hkdf-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   The example below produces a pair of separate keys, suitable for creation
   of an encrypt-then-HMAC construct, using AES-256 and SHA-256 for encryption
   and authentication respectively.
  </p>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #1 <span class="function"><strong>hash_hkdf()</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 /></span><span style="color: #FF8000">// Generate a random key, and salt to strengthen it during derivation.<br /></span><span style="color: #0000BB">$inputKey </span><span style="color: #007700">= </span><span style="color: #0000BB">random_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">32</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$salt </span><span style="color: #007700">= </span><span style="color: #0000BB">random_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">16</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Derive a pair of separate keys, using the same input created above.<br /></span><span style="color: #0000BB">$encryptionKey </span><span style="color: #007700">= </span><span style="color: #0000BB">hash_hkdf</span><span style="color: #007700">(</span><span style="color: #DD0000">'sha256'</span><span style="color: #007700">, </span><span style="color: #0000BB">$inputKey</span><span style="color: #007700">, </span><span style="color: #0000BB">32</span><span style="color: #007700">, </span><span style="color: #DD0000">'aes-256-encryption'</span><span style="color: #007700">, </span><span style="color: #0000BB">$salt</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$authenticationKey </span><span style="color: #007700">= </span><span style="color: #0000BB">hash_hkdf</span><span style="color: #007700">(</span><span style="color: #DD0000">'sha256'</span><span style="color: #007700">, </span><span style="color: #0000BB">$inputKey</span><span style="color: #007700">, </span><span style="color: #0000BB">32</span><span style="color: #007700">, </span><span style="color: #DD0000">'sha-256-authentication'</span><span style="color: #007700">, </span><span style="color: #0000BB">$salt</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$encryptionKey </span><span style="color: #007700">!== </span><span style="color: #0000BB">$authenticationKey</span><span style="color: #007700">); </span><span style="color: #FF8000">// bool(true)<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.hash-hkdf-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.hash-pbkdf2.php" class="function" rel="rdfs-seeAlso">hash_pbkdf2()</a> - Generate a PBKDF2 key derivation of a supplied password</span></li>
    <li><a href="https://datatracker.ietf.org/doc/html/rfc5869" class="link external">&raquo;&nbsp;RFC 5869</a></li>
    <li><a href="https://github.com/narfbg/hash_hkdf_compat" class="link external">&raquo;&nbsp;userland implementation</a></li>
   </ul>
  </p>
 </div>


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