PHP hash_algos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

Definition

In PHP, hash_algos() returns an array with the names of all hashing algorithms your server supports.

Use it to check that an algorithm exists before you pass its name to hash().

Syntax

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

PHP
$algos = hash_algos();
CallWhat it does
hash_algos()Returns an array of algorithm names.
in_array('sha256', hash_algos(), true)Checks that sha256 is supported.

The exact list depends on your PHP version. sha256, sha512 and md5 are always there.

Library declarations
PHP
hash_algos(): array

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
arrayA list of algorithm names, such as "md5", "sha256", "sha512" and "crc32b".

PHP Examples

Example 1 — Check If an Algorithm Exists

Test names before you use them.

PHP
<?php
$algos = hash_algos();

echo "sha256: ", in_array('sha256', $algos, true) ? "yes" : "no", PHP_EOL;
echo "md5: ", in_array('md5', $algos, true) ? "yes" : "no", PHP_EOL;
echo "fake256: ", in_array('fake256', $algos, true) ? "yes" : "no", PHP_EOL;

How It Works

  1. Get the list. It is a plain array of strings.
  2. Search with in_array(). Strict mode compares exact names.
  3. Real names return yes. sha256 and md5 are built in.
  4. Made-up names return no. Calling hash('fake256', ...) would fail.

Example 2 — Digest Length per Algorithm

Hash a short string with each supported algorithm.

PHP
<?php
foreach (['md5', 'sha1', 'sha256'] as $algo) {
    if (in_array($algo, hash_algos(), true)) {
        echo $algo, " => ", strlen(hash($algo, 'x')), PHP_EOL;
    }
}

How It Works

  1. List your candidates. These three are always supported.
  2. Guard each call. Skip names that are missing.
  3. Hash a tiny string. Length only depends on the algorithm.
  4. Print the length. Hex digits double the byte count.

Example 3 — Allow Only Safe Algorithms

Reject weak or unknown names from user input.

PHP
<?php
function checkAlgo(string $algo): string
{
    if (!in_array($algo, hash_algos(), true)) {
        return "not available";
    }
    $weak = ['md5', 'sha1', 'crc32', 'crc32b', 'crc32c', 'adler32'];
    return in_array($algo, $weak, true) ? "avoid" : "ok";
}

foreach (['sha256', 'md5', 'nope'] as $algo) {
    echo $algo, ": ", checkAlgo($algo), PHP_EOL;
}

How It Works

  1. Check support first. Unknown names stop early.
  2. Keep a weak list. These are fast or broken for security.
  3. Return a status. Each name gets a clear answer.
  4. Reuse the check. Run it on any algorithm name you accept.

Frequently Asked Questions

It returns an array with the names of every hashing algorithm available in your PHP, ready to pass to hash().
hash_algos(): array. It takes no parameters.
No. It depends on the PHP version. New algorithms such as sha3 and xxh were added in later versions, so check before you use one.
Use in_array('sha256', hash_algos(), true). It returns true if the name is supported.
hash_hmac_algos() lists only algorithms that work with keys. Checksum algorithms like crc32b appear in hash_algos() but not in hash_hmac_algos().

Next: hash_copy()

hash_copy() clones a hashing context so you can branch it.

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