PHP - Hash hmac() 函式



定義和用法

hash_hmac() 函式用於使用 HMAC 方法生成帶金鑰的雜湊值。

HMAC 代表帶金鑰的雜湊訊息認證碼或基於雜湊的訊息認證碼。它利用諸如 md5、sha-256 等密碼雜湊函式和一個金鑰來返回給定資料的摘要雜湊。

語法

hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = FALSE ] ) : string

引數

序號 引數和描述
1

algo

雜湊演算法的名稱。hash 提供了大量的演算法列表,一些重要的演算法是 md5、sha256 等。

要獲取支援的完整演算法列表,請檢視 hash_hmac_algos()

2

data

您要雜湊的資料。

3

key

生成訊息摘要的 HMAC 變體的金鑰。

4

raw_output

預設情況下,值為 false,因此它返回小寫十六進位制值。如果值為 true,它將返回原始二進位制資料。

返回值

hash_hmac() 函式返回一個包含計算出的訊息摘要的字串,如果 raw_output 為 false,則該字串將採用小寫十六進位制的形式,否則它將返回原始二進位制資料。

PHP 版本

此函式將在 PHP 5.1.2 或更高版本中執行。

示例 1

使用 hash_hmac() -

<?php
   echo hash_hmac('md5', 'Welcome to Tutorialspoint', 'any_secretkey');
?>

輸出

這將產生以下結果 -

3e89ca31da24cb046c9d11706be688c1

示例 2

使用 ripemd128 演算法的 hash_hmac() -

<?php
   echo hash_hmac('ripemd128', 'Welcome to Tutorialspoint', 'any_secretkey');
?>

輸出

這將產生以下結果 -

c9b5c68b72808f31b4524fbd46bf87d0

示例 3

生成 hash_hmac 並將 raw_output 設定為 true -

<?php
   echo hash_hmac('ripemd128', 'Welcome to Tutorialspoint', 'any_secretkey', true);
?>

輸出

這將產生以下結果 -

ɵƋr��1�RO�F���
php_function_reference.htm
廣告