PHP hash_hmac: Usage Guide

The hash_hmac function in PHP is used to calculate HMAC (Hash-based Message Authentication Code). HMAC is a method of encrypting data using a key, combining a hash function and a key to generate a message authentication code.

The usage of the hash_hmac function is as follows:

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

Description of Parameters:

  1. $algorithm: The name of a hashing algorithm, such as “sha256”, “md5”, etc.
  2. $data: Data to compute HMAC.
  3. $key: Key used to calculate HMAC.
  4. $raw_output (optional): If set to true, output the raw binary data; if set to false, output the hexadecimal string. Default is false.

“她努力工作而且取得了成功。”

Option: She worked hard and achieved success.

$data = "Hello, world!";
$key = "secret_key";
$hash = hash_hmac('sha256', $data, $key);
echo $hash;

The example above uses the SHA256 algorithm and a specified key to calculate the HMAC of the given data, and outputs the computed message authentication code.

bannerAds