PHP hash_hmac() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

Definition

In PHP, hash_hmac() creates a keyed hash of your data. Only someone with the secret key can make the same value.

Use it to sign cookies, API requests and webhooks, and to check that a message was not changed.

Syntax

Call hash_hmac() with the values you need. Start with this line.

PHP
$mac = hash_hmac('sha256', $data, $secretKey);
CallWhat it does
hash_hmac('sha256', 'msg', 'key')Returns a 64-character hex signature.
hash_hmac('sha256', 'msg', 'key', true)Returns 32 raw bytes.

Compare signatures with hash_equals(), never with ==.

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

Parameters

ParameterDescription
algoRequired. A cryptographic algorithm such as "sha256". See hash_hmac_algos().
dataRequired. The message to sign.
keyRequired. The secret key.
binaryOptional. false (default) returns hex. true returns raw bytes.

Return Value

Return ValueDescription
stringThe HMAC as hex, or raw bytes when binary is true.

PHP Examples

Example 1 — Sign a Message

The same message and key always give the same signature.

PHP
<?php
echo hash_hmac('sha256', 'The quick brown fox', 'secret-key'), PHP_EOL;

How It Works

  1. Pass the algorithm. sha256 is a good default.
  2. Pass the message. This is the data to sign.
  3. Pass the secret key. Keep it out of public code.
  4. Get a 64-character signature. It is repeatable.

Example 2 — A Different Key Changes the Result

Only the right key reproduces the signature.

PHP
<?php
$a = hash_hmac('sha256', 'data', 'key-one');
$b = hash_hmac('sha256', 'data', 'key-one');
$c = hash_hmac('sha256', 'data', 'key-two');

echo "Same key: ", $a === $b ? "yes" : "no", PHP_EOL;
echo "Other key: ", $a === $c ? "yes" : "no", PHP_EOL;

How It Works

  1. Sign twice with one key. The results match.
  2. Sign once with another key. The result differs.
  3. Know the point. A signature needs the key.
  4. In real checks, use `hash_equals()` to compare.

Example 3 — Base64 Signature for Headers

Request signatures are often sent as base64.

PHP
<?php
$raw = hash_hmac('sha256', 'order=1042', 'secret-key', true);

echo base64_encode($raw), PHP_EOL;

How It Works

  1. Ask for raw bytes. Pass true as the fourth argument.
  2. Encode with `base64_encode()`. Bytes become safe text.
  3. Send it in a header. The receiver recomputes and compares.
  4. Pick one format. Both sides must use the same.

Frequently Asked Questions

It creates a keyed hash of a message. The result proves the message came from someone who knows the key.
hash_hmac(string $algo, string $data, string $key, bool $binary = false): string.
hash() has no key, so anyone can recompute it. hash_hmac() needs the secret key, so only you can create or check the value.
Recompute it from the received data and compare with hash_equals(). This avoids timing leaks.
sha256 is a solid choice. Use sha512 if you need a longer value. Avoid md5 and sha1 in new work.

Next: hash_hmac_algos()

hash_hmac_algos() lists algorithms that work with HMAC.

hash_hmac_algos() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful