<?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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.hash-init.php',
    1 => 'hash_init',
    2 => '初始化增量散列运算上下文',
  ),
  'up' => 
  array (
    0 => 'ref.hash.php',
    1 => 'Hash 函数',
  ),
  'prev' => 
  array (
    0 => 'function.hash-hmac-file.php',
    1 => 'hash_hmac_file',
  ),
  'next' => 
  array (
    0 => 'function.hash-pbkdf2.php',
    1 => 'hash_pbkdf2',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/hash/functions/hash-init.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.hash-init" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">hash_init</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.2, PHP 7, PHP 8, PECL hash &gt;= 1.1)</p><p class="refpurpose"><span class="refname">hash_init</span> &mdash; <span class="dc-title">初始化增量散列运算上下文</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.hash-init-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>hash_init</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="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></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 class="initializer"> = &quot;&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$options</code><span class="initializer"> = []</span></span><br>): <span class="type"><a href="class.hashcontext.php" class="type HashContext">HashContext</a></span></div>


 </div>

 <div class="refsect1 parameters" id="refsect1-function.hash-init-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">algo</code></dt>
     <dd>
      <p class="para">
       要使用的散列算法的名称（例如：<code class="literal">“sha256”</code>）。
       可以在 <span class="function"><a href="function.hash-algos.php" class="function">hash_algos()</a></span> 中查看当前支持的算法。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        如果指定 <strong><code><a href="hash.constants.php#constant.hash-hmac">HASH_HMAC</a></code></strong> 标志，则不允许使用非加密哈希函数。
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       进行散列运算的可选设置，目前仅支持一个选项：<strong><code><a href="hash.constants.php#constant.hash-hmac">HASH_HMAC</a></code></strong>。当指定此选项的时候，<em>必须</em>指定
       <code class="parameter">key</code> 参数。
      </p>
     </dd>
    
    
     <dt><code class="parameter">key</code></dt>
     <dd>
      <p class="para">
       当 <code class="parameter">flags</code> 参数为 
       <strong><code><a href="hash.constants.php#constant.hash-hmac">HASH_HMAC</a></code></strong> 时，使用此参数传入进行 HMAC 散列运算时的共享密钥。
      </p>
     </dd>
    
    
     <dt><code class="parameter">options</code></dt>
     <dd>
      <p class="para">
       各种散列算法的一系列选项数组。
       目前 MurmurHash 算法仅支持 <code class="literal">“seed”</code> 参数。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.hash-init-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回散列运算上下文对象，以供
   <span class="function"><a href="function.hash-update.php" class="function">hash_update()</a></span>、<span class="function"><a href="function.hash-update-stream.php" class="function">hash_update_stream()</a></span>、<span class="function"><a href="function.hash-update-file.php" class="function">hash_update_file()</a></span>
   和 <span class="function"><a href="function.hash-final.php" class="function">hash_final()</a></span> 函数使用。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.hash-init-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     如果 <code class="parameter">algo</code> 未知或非加密散列函数，或者 <code class="parameter">key</code>
     为空时，抛出 <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> 异常。
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     在 <code class="parameter">options</code> 中传递错误类型的配置选项时，现在会发出 <strong><code><a href="errorfunc.constants.php#constant.e-deprecated">E_DEPRECATED</a></code></strong>
     错误，因为会解读错误。在未来，这将成为 <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span>。
    </span>
   </li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.hash-init-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.4.0</td>
       <td>现在已弃用传递错误类型的选项。</td>
      </tr>

      <tr>
       <td>8.1.0</td>
       <td>新增 <code class="parameter">options</code> 参数。</td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        如果 <code class="parameter">algo</code> 未知或非加密散列函数，或者 <code class="parameter">key</code>
        为空时，现在抛出 <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> 异常。之前返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>
        并发出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> 消息。
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>当使用 <strong><code><a href="hash.constants.php#constant.hash-hmac">HASH_HMAC</a></code></strong> 选项的时候，不再支持非加密的散列函数（adler32，crc32，crc32b，fnv132，fnv1a32，fnv164，fnv1a64，joaat）。</td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        返回 <span class="classname"><a href="class.hashcontext.php" class="classname">HashContext</a></span> 对象，不再返回资源类型。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.hash-init-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 增量散列运算示例</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$hash </span><span style="color: #007700">= </span><span style="color: #0000BB">hash</span><span style="color: #007700">(</span><span style="color: #DD0000">'sha256'</span><span style="color: #007700">, </span><span style="color: #DD0000">'The quick brown fox jumped over the lazy dog.'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$ctx </span><span style="color: #007700">= </span><span style="color: #0000BB">hash_init</span><span style="color: #007700">(</span><span style="color: #DD0000">'sha256'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">hash_update</span><span style="color: #007700">(</span><span style="color: #0000BB">$ctx</span><span style="color: #007700">, </span><span style="color: #DD0000">'The quick brown fox '</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">hash_update</span><span style="color: #007700">(</span><span style="color: #0000BB">$ctx</span><span style="color: #007700">, </span><span style="color: #DD0000">'jumped over the lazy dog.'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$incremental_hash </span><span style="color: #007700">= </span><span style="color: #0000BB">hash_final</span><span style="color: #007700">(</span><span style="color: #0000BB">$ctx</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$incremental_hash</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$hash </span><span style="color: #007700">=== </span><span style="color: #0000BB">$incremental_hash</span><span style="color: #007700">);<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">68b1282b91de2c054c36629cb8dd447f12f096d3e3c587978dc2248444633483
bool(true)</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.hash-init-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.hash-algos.php" class="function" rel="rdfs-seeAlso">hash_algos()</a> - 返回已注册的散列算法列表</span></li>
    <li><span class="function"><a href="function.hash-update.php" class="function" rel="rdfs-seeAlso">hash_update()</a> - 向活跃的哈希运算上下文中填充数据</span></li>
    <li><span class="function"><a href="function.hash-update-file.php" class="function" rel="rdfs-seeAlso">hash_update_file()</a> - 从文件向活跃的散列运算上下文中填充数据</span></li>
    <li><span class="function"><a href="function.hash-update-stream.php" class="function" rel="rdfs-seeAlso">hash_update_stream()</a> - 从打开的流向活跃的散列运算上下文中填充数据</span></li>
    <li><span class="function"><a href="function.hash-final.php" class="function" rel="rdfs-seeAlso">hash_final()</a> - 结束增量散列且返回摘要结果</span></li>
   </ul>
  </p>
 </div>


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