PHP hash_hmac_algos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

Definition

In PHP, hash_hmac_algos() returns the names of the hashing algorithms that you can use with hash_hmac() and hash_init() in HMAC mode.

It leaves out checksum algorithms such as crc32, which are not suited for keyed hashing.

Syntax

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

PHP
$algos = hash_hmac_algos();
CallWhat it does
hash_hmac_algos()Returns an array of algorithm names.
in_array('sha256', hash_hmac_algos(), true)Checks that sha256 works with HMAC.

Added in PHP 7.2. sha256, sha512 and md5 are in the list. crc32b is not.

Library declarations
PHP
hash_hmac_algos(): array

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
arrayA list of algorithm names that are allowed for HMAC.

PHP Examples

Example 1 — Check HMAC Support

sha256 and md5 work with HMAC. crc32b does not.

PHP
<?php
$algos = hash_hmac_algos();

foreach (['sha256', 'md5', 'crc32b'] as $name) {
    echo $name, ": ", in_array($name, $algos, true) ? "yes" : "no", PHP_EOL;
}

How It Works

  1. Get the HMAC list. It skips checksums.
  2. Loop over three names. One is a checksum.
  3. Search each name. in_array() answers yes or no.
  4. Skip checksums. They are not made for keys.

Example 2 — A Shorter List Than hash_algos()

Some algorithms are left out.

PHP
<?php
echo count(hash_hmac_algos()) < count(hash_algos()) ? "Fewer HMAC algorithms" : "Same size", PHP_EOL;

How It Works

  1. Count both lists. count() gives the sizes.
  2. Compare them. The HMAC list is smaller.
  3. Know why. Checksums and some fast hashes are excluded.
  4. Use the right list. Check HMAC names here.

Example 3 — Guard a Signing Function

Refuse algorithms that do not support HMAC. Only the first 16 characters are printed.

PHP
<?php
function sign(string $algo, string $data, string $key): string
{
    if (!in_array($algo, hash_hmac_algos(), true)) {
        return "unsupported algorithm";
    }
    return substr(hash_hmac($algo, $data, $key), 0, 16);
}

echo sign('sha256', 'hello', 'key'), PHP_EOL;
echo sign('crc32b', 'hello', 'key'), PHP_EOL;

How It Works

  1. Check the name first. Reject anything not in the list.
  2. Sign with hash_hmac(). Only for allowed names.
  3. Shorten the output. substr() keeps 16 characters.
  4. Return a clear message. No fatal error for bad input.

Frequently Asked Questions

It returns the names of the algorithms that can be used with hash_hmac(). Checksum algorithms are left out.
hash_hmac_algos(): array. It takes no parameters.
PHP 7.2. On older versions use hash_algos() and avoid checksum names yourself.
hash_algos() lists every algorithm. hash_hmac_algos() lists only those allowed for HMAC, so it is a shorter list.
hash_hmac() throws a ValueError for non-cryptographic algorithms such as crc32b.

Next: hash_hmac_file()

hash_hmac_file() creates a keyed hash of a file.

hash_hmac_file() 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