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.
// 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;
Keywords
Related Questions
- What are the consequences of using deprecated mysql_* functions in PHP 7?
- What are some considerations when deciding to explore new technologies like Node.js or Redux/React as a PHP developer?
- Is it advisable to validate HTML output during deployment or through continuous integration for PHP projects?