Content · Web Development · PHP

PHP hash_hmac() Function: Usage and Examples

hash_hmac computes an HMAC authentication tag for data using a secret key and a selected cryptographic hash.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

hash_hmac computes an HMAC authentication tag for data using a secret key and a selected cryptographic hash algorithm.

The examples explain the data format, expected results and the difference between this API and nearby cryptographic operations.

What It Does


The operation is intended for: hash_hmac computes an HMAC authentication tag for data using a secret key and a selected cryptographic hash algorithm.

Part of core PHP or a normally bundled standard extension; check your deployment configuration.

Syntax and Return Value


hash_hmac(string $algo, string $data, string $key, bool $binary = ...): string
ParameterPurpose
$algo (string)The exact algorithm identifier supported by this API.
$data (string)The input data bytes. Use the exact original bytes when verifying.
$key (string)Raw key bytes in the required algorithm-specific length; protect them from disclosure.
$binary (bool)Use the PHP signature above to determine the exact expected input type; validate untrusted inputs before calling.

The declared return type is shown above. Errors can include false results or exceptions depending on this API: Authenticate with a secret key; hash_equals requires the known trusted value as its first argument and matching lengths are still observable.

Example 1: Basic Operation


Inspect the expected result: do not print secrets or reuse illustrative credentials in production.

<?php
$mac = hash_hmac("sha256", "invoice-42", "example-secret");
echo $mac;

Example 2: A Different Use Case


Inspect the expected result: do not print secrets or reuse illustrative credentials in production.

<?php
// Check the alternative case.
$mac = hash_hmac("sha256", "approved-record", "example-secret");
echo $mac;

Practical Example: Checking the Result


Inspect the expected result: do not print secrets or reuse illustrative credentials in production.

<?php
// Verify the expected behavior before using results.
$mac = hash_hmac("sha256", "new-session", "example-secret");
echo $mac;

Important Behavior and Edge Cases


Authenticate with a secret key; hash_equals requires the known trusted value as its first argument and matching lengths are still observable.

Distinguish digest encoding from secret-key authentication and keep fixed-length expected authentication values for comparisons.

Comparison with Related Functions


ApproachKey distinction
hash_hmachash_hmac computes an HMAC authentication tag for data using a secret key and a selected cryptographic hash algorithm.
Related techniqueUse HMAC to calculate a keyed digest, then hash_equals to compare the received and expected digests.

Common Beginner Mistakes


MistakeCorrect approach
Confusing this operation with a neighboring cryptographic or security mechanismUse HMAC to calculate a keyed digest, then hash_equals to compare the received and expected digests.
Ignoring a security prerequisite or failure caseAuthenticate with a secret key; hash_equals requires the known trusted value as its first argument and matching lengths are still observable.

Key Takeaways


  • hash_hmac computes an HMAC authentication tag for data using a secret key and a selected cryptographic hash algorithm.
  • Authenticate with a secret key; hash_equals requires the known trusted value as its first argument and matching lengths are still observable.
  • Use HMAC to calculate a keyed digest, then hash_equals to compare the received and expected digests.

Apply this behavior to a real application only after confirming the required extension, configuration and failure handling on the deployment runtime.

Share this resourceSend this page to someone who may find it useful.