Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

hash_hmac()

Authenticate message bytes with a secret key and a supported cryptographic hash algorithm.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Authenticate message bytes with a secret key and a supported cryptographic hash algorithm. The default result is lowercase hex, while true returns raw bytes. PHP 8 raises ValueError for unknown or noncryptographic algorithms. Verification uses the same key and byte representation.

Signature

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

Example output

[32,true,false]

Examples

<?php
$tag=hash_hmac("sha256","batch=42","example-key",true);
echo json_encode([strlen($tag),hash_equals($tag,hash_hmac("sha256","batch=42","example-key",true)),hash_equals($tag,hash_hmac("sha256","batch=43","example-key",true))]);

Result

[32,true,false]

Sources