PHP Reference ยท String Functions
PHP md5() and sha1() functions
Reference for PHP md5(), sha1(), md5_file() and sha1_file(): compute legacy hashes for checksums and cache keys, and why never to use them for passwords.
md5() and sha1() compute the MD5 and SHA-1 message digests of a string. MD5 produces a 128-bit hash (32 hex characters) and SHA-1 a 160-bit hash (40 hex characters). The same input always gives the same output, and a tiny change in the input changes the whole hash.
Both algorithms are broken for security purposes: attackers can create two different inputs with the same hash (a collision), and modern hardware tests billions of guesses per second. Use them only for non-security jobs such as cache keys, quick change detection or compatibility with an old system. For passwords, use password_hash(); for integrity or signatures use hash() with SHA-256 or better.
<?php echo md5("hello"); // 5d41402abc4b2a76b9719d911017c592 echo sha1("hello"); // aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
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
md5(string $string, bool $binary = false): string sha1(string $string, bool $binary = false): string md5_file(string $filename, bool $binary = false): string|false sha1_file(string $filename, bool $binary = false): string|false
| Name | Description |
|---|---|
$string |
The data to hash. It is treated as bytes. |
$filename |
Path of the file to hash. The file is read in chunks, so large files do not fill memory. |
$binary |
false (default) returns lowercase hex. true returns the raw bytes: 16 for MD5, 20 for SHA-1. |
Return value: the digest as a string. The _file variants return false and emit a warning if the file cannot be read.
Usage notes
- Never hash passwords with them. They are far too fast, unsalted and have precomputed lookup tables. Use
password_hash()andpassword_verify(). - Not for tokens or signatures. Secrets need random bytes (
random_bytes()) and, for message authentication,hash_hmac(). - Compare digests safely. When one side is secret, use
hash_equals()instead of===to avoid timing leaks. - Compare hex digests with
===. With==, two different strings such as0e123and0e456are both read as the number zero and compare equal (the "magic hash" bug). This still applies in PHP 8. - Fine for non-security work: cache keys, ETags, deduplicating files you control. For speed on large data,
hash("xxh3", ...)orcrc32bare faster. - Equivalent to
hash("md5", $string)andhash("sha1", $string).
Related
hash(),hash_hmac(),crc32(),password_hash(),bin2hex()- Lessons: Password hashing, Secure coding