PHP md5(): hash a string and read hexadecimal or binary output
PHP md5() calculates an MD5 (Message-Digest Algorithm 5) digest from the bytes in a string. It returns 32 lowercase hexadecimal characters by default, or the same digest as 16 raw bytes if the second argument is true. This can match a legacy checksum format. Password storage and security verification require other methods.
Compare hexadecimal and binary output
Save the first example as md5-demo.php in UTF-8, then run php md5-demo.php in a terminal with PHP 8 installed. The first argument is a string and the optional binary argument defaults to false. Its PHP named-argument name is binary. Use bin2hex() to read the raw bytes as text. PHP md5()
<?php
declare(strict_types=1);
$text = 'abc';
$hex = md5($text);
$raw = md5($text, true);
echo 'hex: ', $hex, PHP_EOL;
echo 'hex bytes: ', strlen($hex), PHP_EOL;
echo 'raw bytes: ', strlen($raw), PHP_EOL;
echo 'raw as hex: ', bin2hex($raw), PHP_EOL;
echo 'same digest: ', ($hex === bin2hex($raw) ? 'yes' : 'no'), PHP_EOL;
The hex and raw as hex lines both show 900150983cd24fb0d6963f7d28e17f72. The lengths are 32 and 16 bytes, and same digest is yes. Each hexadecimal digit encodes four bits, so 32 digits represent 128 bits. Check what your API or storage expects and use a field that supports binary data if you store raw bytes.
See why spaces and line endings change the hash
md5() processes actual input bytes. Save the next example as md5-bytes.php and run php md5-bytes.php to compare abc with variations that contain a line feed, a trailing space or uppercase letters.
<?php
declare(strict_types=1);
$samples = [
'plain' => 'abc',
'newline' => "abc\n",
'space' => 'abc ',
'uppercase' => 'ABC',
];
foreach ($samples as $label => $text) {
echo $label, ': ', md5($text), PHP_EOL;
}
The plain digest is 900150983cd24fb0d6963f7d28e17f72, and newline is 0bee89b07a248e27c83fc3d5951213c1. All four digests differ. Also check LF versus CRLF, the encoding and Unicode normalization. If your protocol includes spaces in the input, applying trim() to force a match changes what you hash.
Hash file contents instead of a file name
md5('sample.txt') hashes the file name string. Use md5_file() for the bytes in the file. The example md5-file-demo.php creates a temporary file, writes the three bytes abc and compares the string and file results. md5_file() can return false on a read failure, so check for that before using the result. PHP md5_file()
<?php
declare(strict_types=1);
$text = 'abc';
$path = tempnam(sys_get_temp_dir(), 'md5-');
if ($path === false) {
throw new RuntimeException('Could not create a temporary file.');
}
try {
if (file_put_contents($path, $text) !== strlen($text)) {
throw new RuntimeException('Could not write all bytes.');
}
$fileHash = md5_file($path);
if ($fileHash === false) {
throw new RuntimeException('Could not hash the file.');
}
echo 'same bytes: ', ($fileHash === md5($text) ? 'yes' : 'no'), PHP_EOL;
} finally {
unlink($path);
}
Run php md5-file-demo.php to see same bytes: yes. The finally block removes the temporary file. When testing your own file, compare the entire byte stream, including its line endings.
Compare hash results accurately
When comparing ordinary checksum strings, check that both use the same format and use ===. PHP’s == can convert strings that look numeric before comparing them. This comparison behavior is separate from a cryptographic collision, where different inputs produce the same digest. To compare tags from a modern keyed message authentication code, use hash_equals() to reduce timing leakage, with the known value first and the supplied value second. PHP comparison operators PHP hash_equals()
Choose suitable methods for passwords and file verification
MD5 is unsuitable where collision resistance is required. Store password hashes with password_hash() and verify them with password_verify(). MD5’s speed also makes it efficient to test many password guesses, so simply adding a salt does not make it suitable for password storage. RFC 6151 PHP password hashing
<?php
declare(strict_types=1);
echo hash('sha256', 'abc'), PHP_EOL;
Choose SHA-256 if you need a new digest for general file integrity checks. Run the last example as php sha256-demo.php. It prints ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad for abc. To verify the file’s origin as well, obtain the expected checksum through a trusted channel or verify a digital signature. PHP hash()




