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 echo hash("sha256", "hello"); // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
Try it
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.
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
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.
sha256orsha3-256for integrity and fingerprints;xxh3orxxh128for speed when security does not matter;crc32bfor 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(), orhash_pbkdf2()if you need a key derived from a password. - Message authentication: use
hash_hmac()with a secret key, nothash($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()andhash_final()hash streams without holding everything in memory. - Name differences:
crc32inhash()is the bzip2 variant and differs fromcrc32();crc32bmatches the function. - Availability: the
hashextension is always compiled in since PHP 7.4. Some algorithms are newer (xxh*,murmur*from 8.1).