PHP hash_hmac_algos() Function

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.
$algos = hash_hmac_algos();
| Call | What 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
Parameters
| Parameter | Description |
|---|
| — | This function takes no parameters. |
Return Value
| Return Value | Description |
|---|
| array | A 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
$algos = hash_hmac_algos();
foreach (['sha256', 'md5', 'crc32b'] as $name) {
echo $name, ": ", in_array($name, $algos, true) ? "yes" : "no", PHP_EOL;
}
sha256: yes
md5: yes
crc32b: no
How It Works
- Get the HMAC list. It skips checksums.
- Loop over three names. One is a checksum.
- Search each name.
in_array() answers yes or no. - Skip checksums. They are not made for keys.
Example 2 — A Shorter List Than hash_algos()
Some algorithms are left out.
<?php
echo count(hash_hmac_algos()) < count(hash_algos()) ? "Fewer HMAC algorithms" : "Same size", PHP_EOL;
How It Works
- Count both lists.
count() gives the sizes. - Compare them. The HMAC list is smaller.
- Know why. Checksums and some fast hashes are excluded.
- Use the right list. Check HMAC names here.
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.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
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