Content · Web Development · PHP

PHP hash_hmac_algos() Function: Usage and Examples

hash_hmac_algos lists only algorithms that PHP permits for keyed HMAC rather than every ordinary hash.

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

hash_hmac_algos lists only algorithms that PHP permits for keyed HMAC rather than every ordinary 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_algos lists only algorithms that PHP permits for keyed HMAC rather than every ordinary hash algorithm.

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

Syntax and Return Value


hash_hmac_algos(): array

This API accepts no required parameters, or its signature must be checked on the target runtime.

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
var_dump(in_array("sha256", hash_hmac_algos(), true));

Example 2: A Different Use Case


Inspect available algorithms rather than assuming a requested identifier exists.

<?php
// Check the alternative case.
var_dump(in_array("sha512", hash_hmac_algos(), true));

Practical Example: Checking the Result


Inspect available algorithms rather than assuming a requested identifier exists.

<?php
// Verify the expected behavior before using results.
echo count(hash_hmac_algos());

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_hmac_algoshash_hmac_algos lists only algorithms that PHP permits for keyed HMAC rather than every ordinary 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_algos lists only algorithms that PHP permits for keyed HMAC rather than every ordinary 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.