MD5 is a widely used hash function that produces a fixed-size 128-bit hash value from arbitrary input data. In PHP, creating MD5 hashes is a common task, often used for password hashing, data integrity verification, or generating unique identifiers. In this article, we’ll explore different approaches to create MD5 hashes in PHP, these are:
Approach 1: Using md5 Function
The basic method for creating MD5 hashes in PHP is by using the built-in md5 function.
PHP
<?php // Input string $str = "Hello, MD5!" ;
// Create MD5 hash using md5 function $hashStr = md5( $str );
// Display the result echo "MD5 Hash: $hashStr" ;
?> |
Output
MD5 Hash: 383e139e64e5f46de9d03ba3695da2d8
Approach 2: Using hash Function with “md5” Algorithm
PHP provides the hash function that supports various hashing algorithms, including MD5.
PHP
<?php $str = "Hello, MD5!" ;
$hashStr = hash( "md5" , $str );
echo "MD5 Hash: $hashStr" ;
?> |
Output
MD5 Hash: 383e139e64e5f46de9d03ba3695da2d8