Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท String Functions

PHP hash() function

Reference for PHP hash(): compute SHA-256, SHA-3, xxHash, CRC32 and other digests, list algorithms with hash_algos(), hash files and use incremental hashing.

hash() computes a message digest of a string with the algorithm you name. It is the general-purpose replacement for md5() and sha1(), with dozens of algorithms behind one function. The result is a lowercase hex string by default.

For integrity checks and fingerprints, SHA-256 ("sha256") is a solid default. Fast non-cryptographic hashes such as xxh3 suit hash tables and deduplication. None of these general hashes is suitable for storing passwords: those need a slow, salted algorithm from password_hash().

PHP
<?php
echo hash("sha256", "hello");
// 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

Try it

PHP Choosing an algorithm

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Files, incremental hashing and safe comparison

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
hash(string $algo, string $data, bool $binary = false, array $options = []): string
hash_file(string $algo, string $filename, bool $binary = false, array $options = []): string|false
hash_algos(): array
Name Description
$algo Algorithm name in lowercase, such as "sha256", "sha3-512", "md5", "crc32b", "xxh3". Get the full list from hash_algos().
$data The bytes to hash.
$binary true returns raw bytes instead of hex.
$options Algorithm options (for example a seed for the MurmurHash and xxHash families, secret for xxh3).

Return value: the digest. hash_file() returns false with a warning if the file cannot be read.

Errors: an unknown algorithm throws a ValueError (PHP 8.0 and later).

Usage notes

  • Pick the algorithm for the job. sha256 or sha3-256 for integrity and fingerprints; xxh3 or xxh128 for speed when security does not matter; crc32b for simple checksums.
  • Avoid broken algorithms for security. MD5 and SHA-1 are collision-prone. hash() still supports them because old systems need them.
  • Not for passwords. Plain hashes are fast, so guesses are cheap. Use password_hash(), or hash_pbkdf2() if you need a key derived from a password.
  • Message authentication: use hash_hmac() with a secret key, not hash($key . $data), which is vulnerable to length-extension attacks with some algorithms.
  • Use hash_equals() to compare digests when one is secret.
  • Incremental API: hash_init(), hash_update() and hash_final() hash streams without holding everything in memory.
  • Name differences: crc32 in hash() is the bzip2 variant and differs from crc32(); crc32b matches the function.
  • Availability: the hash extension is always compiled in since PHP 7.4. Some algorithms are newer (xxh*, murmur* from 8.1).