LaraflareDocsFrameworkPackagesReleases13.x

Docs / Security / Hashing

Hashing

Introduction

The Hash facade provides secure hashing for storing user passwords. It offers the bcrypt, argon and argon2id drivers, and bcrypt is the default.

Note

The bcrypt driver writes the same $2y$ hashes PHP does, so the passwords of an application moved to laraflare keep working. On Workers, Bcrypt runs as JavaScript and uses about a quarter of a second of CPU time at 12 rounds, which needs a paid Workers plan; on a server it is no different. Workers have no Argon2, so the argon and argon2id drivers are backed by scrypt, with memory, time and threads mapped onto its cost parameters.

Slow is good when hashing passwords, and the work factor is adjustable, which means that the time it takes to generate a hash can be increased as hardware power increases. The longer an algorithm takes to hash a password, the longer it takes malicious users to generate "rainbow tables" of all possible string hash values that may be used in brute force attacks against applications.

Configuration

By default, laraflare uses the bcrypt hashing driver when hashing data. However, the argon and argon2id drivers are also supported.

You may customize your application's hashing options in its config/hashing.ts configuration file. If the file is not in your application yet, publish it using the config:publish Artisan command:

npx artisan config:publish hashing

Basic Usage

Hashing Passwords

You may hash a password by calling the make method on the Hash facade. Hashing is asynchronous, so the result is awaited:

import { redirect, type Request } from '@laraflare/framework';
import { Hash } from '@laraflare/framework/hashing';

Route.put('/password', async (request: Request) => {
  // Validate the new password length...

  const user = request.user();

  user.fill({
    password: await Hash.make(request.input('newPassword')),
  });

  await user.save();

  return redirect('/profile');
});

Adjusting The Bcrypt Work Factor

If you are using the Bcrypt algorithm, the make method allows you to manage the work factor of the algorithm using the rounds option; however, the default work factor managed by laraflare (12) is acceptable for most applications:

const hashed = await Hash.make('password', {
  rounds: 12,
});

Adjusting The Argon2 Work Factor

If you are using the Argon2 algorithm, the make method allows you to manage the work factor of the algorithm using the memory, time, and threads options; however, the default values managed by laraflare are acceptable for most applications:

const hashed = await Hash.make('password', {
  memory: 1024,
  time: 2,
  threads: 2,
});

Verifying That a Password Matches a Hash

The check method provided by the Hash facade allows you to verify that a given plain-text string corresponds to a given hash:

if (await Hash.check('plain-text', hashedPassword)) {
  // The passwords match...
}

Determining if a Password Needs to be Rehashed

The needsRehash method provided by the Hash facade allows you to determine if the work factor used by the hasher has changed since the password was hashed. Some applications choose to perform this check during the application's authentication process:

if (await Hash.needsRehash(hashed)) {
  hashed = await Hash.make('plain-text');
}

Hash Algorithm Verification

To prevent hash algorithm manipulation, the Hash.check method will first verify the given hash was generated using the application's selected hashing algorithm. If the algorithms are different, a RuntimeException exception will be thrown.

This is the expected behavior for most applications, where the hashing algorithm is not expected to change and different algorithms can be an indication of a malicious attack. However, if you need to support multiple hashing algorithms within your application, such as when migrating from one algorithm to another, you can disable hash algorithm verification by setting the HASH_VERIFY environment variable to false:

HASH_VERIFY=false
Adapted from Laravel's documentation, Copyright (c) Taylor Otwell, used under the MIT License.