Skip to content
PHP Logo

What is the difference between hashing and encryption in PHP?

Hashing and encryption are both techniques used to protect data, but they serve different purposes. Hashing is a one-way function that converts data into a fixed-length string of characters, making it ideal for storing passwords securely. Encryption, on the other hand, is a reversible process that converts data into a format that can be decrypted back to its original form. In PHP, you can use functions like password_hash() for hashing and openssl_encrypt() for encryption.

php
// Hashing example using password_hash()
$password = "secret123";
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
echo $hashedPassword;

// Encryption example using openssl_encrypt()
$data = "sensitive data";
$key = "my_secret_key";
$encryptedData = openssl_encrypt($data, 'AES-128-CBC', $key, 0, 'my_secret_iv');
echo $encryptedData;