PHP provides hashing functions like md5() and sha1() to create fixed-length hash values from strings. These hashes are commonly used for data integrity checks, identifiers, and legacy systems.
md5() generates a 32-character hexadecimal hashsha1() generates a 40-character hexadecimal hashstring md5(string $data)string sha1(string $data)hash(string $algo, string $data)
// Generate an MD5 hash from a password
<?php
$password = "mySecret123";
$hash = md5($password);
echo $hash;
?>
Explanation: The md5() function converts the string into a 32-character hexadecimal hash. MD5 is fast but insecure for password storage.
// Generate a SHA1 hash from a password
<?php
$password = "mySecret123";
$hash = sha1($password);
echo $hash;
?>
Explanation: sha1() produces a longer hash than MD5 but is still considered outdated for secure applications.
// Generate a stronger SHA-256 hash
<?php
$password = "mySecret123";
$hash = hash("sha256", $password);
echo $hash;
?>
Try hashing the same password using MD5, SHA1, and SHA-256. Notice how the length and complexity increase, improving resistance to brute-force attacks.
password_hash() and password_verify()password_hash()