= 5.1.2, PHP 7, PHP 8, PECL hash >= 1.1)hash_hmac — 使用 HMAC 方法生成帶有密鑰的哈希值說明hash_hmac( string $algo, string $data, string $key">

hash_hmac

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

hash_hmac使用 HMAC 方法生成帶有密鑰的哈希值

說明

hash_hmac(
    string $algo,
    string $data,
    string $key,
    bool $raw_output = false
): string

參數(shù)

algo

要使用的哈希算法名稱,例如:"md5","sha256","haval160,4" 等。 如何獲取受支持的算法清單,請參見 hash_hmac_algos() 函數(shù)。

data

要進(jìn)行哈希運(yùn)算的消息。

key

使用 HMAC 生成信息摘要時(shí)所使用的密鑰。

raw_output

設(shè)置為 true 輸出原始二進(jìn)制數(shù)據(jù), 設(shè)置為 false 輸出小寫 16 進(jìn)制字符串。

返回值

如果 raw_output 設(shè)置為 true, 則返回原始二進(jìn)制數(shù)據(jù)表示的信息摘要, 否則返回 16 進(jìn)制小寫字符串格式表示的信息摘要。 如果 algo 參數(shù)指定的不是受支持的算法,返回 false。

更新日志

版本 說明
7.2.0 不再支持非加密的哈希函數(shù)(adler32,crc32,crc32b,fnv132,fnv1a32,fnv164,fnv1a64,joaat)。

范例

示例 #1 hash_hmac() 例程

<?php
echo hash_hmac('ripemd160''The quick brown fox jumped over the lazy dog.''secret');
?>

以上例程會輸出:

b8e7ae12510bdfb1812e463a7f086122cf37e4f7

參見

  • hash() - 生成哈希值 (消息摘要)
  • hash_hmac_algos() - Return a list of registered hashing algorithms suitable for hash_hmac
  • hash_init() - 初始化增量哈希運(yùn)算上下文
  • hash_hmac_file() - 使用 HMAC 方法和給定文件的內(nèi)容生成帶密鑰的哈希值