Using Password Hashing for Security

Tutorial 3 of 5

Tutorial: Using Password Hashing for Security in PHP

1. Introduction

Goal

The goal of this tutorial is to help you understand how to securely hash passwords in PHP. This is an essential part of web development, as it helps protect sensitive user data.

Learning Outcomes

By the end of this tutorial, you will be able to:
- Understand the concept of password hashing.
- Use PHP's built-in functions to create and verify hashed passwords.

Prerequisites

  • A basic understanding of PHP.
  • A working PHP development environment.

2. Step-by-Step Guide

Password hashing is a cryptographic process where a password is transformed into a fixed-length sequence of characters, which is nearly impossible to reverse.

PHP provides built-in functions to hash and verify passwords: password_hash() and password_verify().

password_hash()

This function takes two parameters: the password to be hashed and the algorithm to use. The recommended algorithm to use is PASSWORD_DEFAULT.

Example:

$hashedPassword = password_hash('my_password', PASSWORD_DEFAULT);
echo $hashedPassword;

password_verify()

This function checks whether a given password matches a hashed password. It takes two parameters: the password to check, and the hashed password.

Example:

if (password_verify('my_password', $hashedPassword)) {
    echo 'Password is valid!';
} else {
    echo 'Invalid password.';
}

3. Code Examples

Example 1

Creating a hashed password:

// The user's password
$password = 'my_password';

// Use PHP's password_hash() function to create a hashed password
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);

// The hashed password is now ready to be stored in a database
echo $hashedPassword;

Example 2

Verifying a password:

// The password entered by the user
$userPassword = 'my_password';

// The hashed password retrieved from the database
$hashedPassword = '$2y$10$QdPvHO/G2ZzOzn7lBuq8xO/';

// Use PHP's password_verify() function to check the password
if (password_verify($userPassword, $hashedPassword)) {
    echo 'Password is valid!';
} else {
    echo 'Invalid password.';
}

4. Summary

In this tutorial, you learned about the importance of password hashing, how to use PHP's password_hash() function to hash a password, and the password_verify() function to check a password against a hashed value.

For further learning, you could explore more about different password hashing algorithms and how to handle password salts.

5. Practice Exercises

Exercise 1

Write a PHP script that hashes a password and then verifies it.

Exercise 2

Modify the script from Exercise 1 so that it retrieves a hashed password from a simulated database (array).

Exercise 3

Create a registration and login form that uses password hashing for security.

Solutions and Explanations

  1. You can use password_hash() to hash a password and password_verify() to verify it. Review the examples in this tutorial.
  2. You can simulate a database by storing hashed passwords in an array. When checking a password, retrieve the hashed password from the array.
  3. Create a form that takes a username and password. When the user registers, hash the password and store it in the database. When the user logs in, use password_verify() to check the password.

Remember, never store passwords in plain text in a database. Always hash passwords using a secure method like password_hash().