<?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-hmac.php',
    1 => 'hash_hmac',
    2 => '使用 HMAC 方法生成带有密钥的散列值',
  ),
  'up' => 
  array (
    0 => 'ref.hash.php',
    1 => 'Hash 函数',
  ),
  'prev' => 
  array (
    0 => 'function.hash-hkdf.php',
    1 => 'hash_hkdf',
  ),
  'next' => 
  array (
    0 => 'function.hash-hmac-algos.php',
    1 => 'hash_hmac_algos',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/hash/functions/hash-hmac.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.hash-hmac" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">hash_hmac</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_hmac</span> &mdash; <span class="dc-title">使用 HMAC 方法生成带有密钥的散列值</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.hash-hmac-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>hash_hmac</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.string.php" class="type string">string</a></span> <code class="parameter">$data</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.boolean.php" class="type bool">bool</a></span> <code class="parameter">$binary</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></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-hmac-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-hmac-algos.php" class="function">hash_hmac_algos()</a></span> 中查看当前支持的算法。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        不允许使用非加密哈希函数。
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">data</code></dt>
     <dd>
      <p class="para">
       要进行散列运算的消息。
      </p>
     </dd>
    
    
     <dt><code class="parameter">key</code></dt>
     <dd>
      <p class="para">
       使用 HMAC 生成信息摘要时所使用的密钥。
      </p>
     </dd>
    
    
     <dt><code class="parameter">binary</code></dt>
     <dd>
      <p class="para">
       设置为 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> 输出原始二进制数据，设置为 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> 输出小写 16 进制字符串。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.hash-hmac-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果 <code class="parameter">binary</code> 设置为 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>，则返回原始二进制数据表示的信息摘要，否则返回
   16 进制小写字符串格式表示的信息摘要。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.hash-hmac-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   如果 <code class="parameter">algo</code> 未知或非加密散列函数，则抛出
   <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> 异常。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.hash-hmac-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>
        如果 <code class="parameter">algo</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>。
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>不再支持非加密的散列函数（adler32、crc32、crc32b、fnv132、fnv1a32、fnv164、fnv1a64、joaat）。</td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.hash-hmac-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>hash_hmac()</strong></span> 示例</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: #007700">echo </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: #DD0000">'The quick brown fox jumped over the lazy dog.'</span><span style="color: #007700">, </span><span style="color: #DD0000">'secret'</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">9c5c42422b03f0ee32949920649445e417b2c634050833c5165704b825c2a53b</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.hash-hmac-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.hash-hmac-algos.php" class="function" rel="rdfs-seeAlso">hash_hmac_algos()</a> - 返回适用于 hash_hmac 的已注册散列算法列表</span></li>
    <li><span class="function"><a href="function.hash-hmac-file.php" class="function" rel="rdfs-seeAlso">hash_hmac_file()</a> - 使用 HMAC 方法和给定文件的内容生成带密钥的散列值</span></li>
    <li><span class="function"><a href="function.hash-equals.php" class="function" rel="rdfs-seeAlso">hash_equals()</a> - 可防止时序攻击的字符串比较</span></li>
   </ul>
  </p>
 </div>


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