PHP 8.3.4 Released!

hash_hmac_file

(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL hash >= 1.1)

hash_hmac_file使用 HMAC 方法和给定文件的内容生成带密钥的散列值

说明

hash_hmac_file(
    string $algo,
    string $filename,
    string $key,
    bool $binary = false
): string|false

参数

algo

要使用的散列算法名称,例如:"md5","sha256","haval160,4" 等。 如何获取受支持的算法清单,请参见 hash_hmac_algos() 函数。

filename

要进行散列运算的文件路径,支持 fopen 封装器。

key

使用 HMAC 生成信息摘要时所使用的密钥。

binary

设置为 true 输出原始二进制数据, 设置为 false 输出小写 16 进制字符串。

返回值

如果 binary 设置为 true, 则返回原始二进制数据表示的信息摘要,否则返回 16 进制小写字符串格式表示的信息摘要。如果无法读取文件 filename,则返回 false

错误/异常

如果 algo 未知或非加密散列函数,则抛出 ValueError 异常。

更新日志

版本 说明
8.0.0 如果 algo 未知或非加密散列函数,现在抛出 ValueError 异常,之前返回 false
7.2.0 禁用非加密散列函数(adler32、crc32、crc32b、fnv132、fnv1a32、fnv164、fnv1a64、joaat)。

示例

示例 #1 hash_hmac_file() 示例

<?php
/* 创建一个要计算散列值的文件 */
file_put_contents('example.txt', 'The quick brown fox jumped over the lazy dog.');

echo
hash_hmac_file('sha256', 'example.txt', 'secret');
?>

以上示例会输出:

9c5c42422b03f0ee32949920649445e417b2c634050833c5165704b825c2a53b

参见

  • hash_hmac_algos() - 返回适用于 hash_hmac 的已注册散列算法列表
  • hash_hmac() - 使用 HMAC 方法生成带有密钥的散列值
  • hash_file() - 给指定文件的内容生成散列值

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top