> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Creating a Custom Hash Driver

> Register a custom hash driver with Manager::extend(), which HashManager inherits, and use it transparently through the Hash facade, based on the framework source code.

## Overview

The `Hash` facade proxies to `Illuminate\Hashing\HashManager`. `HashManager` extends `Illuminate\Support\Manager`, and ships with three drivers by default: `bcrypt`, `argon`, and `argon2id`.

The official documentation only covers switching drivers with `HASH_DRIVER`, but because `HashManager` uses the same mechanism as `Manager`, you can add your own hash drivers. This guide implements a driver that mixes a server-side secret (a pepper) into the password before storing it with bcrypt.

## How a built-in driver is resolved

When you call `Hash::make()`, `Manager::driver()` first resolves the actual driver instance.

```php theme={null}
// Simplified version of Illuminate\Support\Manager::driver()
public function driver($driver = null)
{
    $driver = enum_value($driver) ?: enum_value($this->getDefaultDriver());

    return $this->drivers[$driver] ??= $this->createDriver($driver);
}
```

`createDriver()` first looks for a registered custom creator. If none exists, it looks for a `create{Name}Driver()` method.

```php theme={null}
// Simplified version of Illuminate\Support\Manager::createDriver()
protected function createDriver($driver)
{
    if (isset($this->customCreators[$driver])) {
        return $this->callCustomCreator($driver);
    }

    $method = 'create'.Str::studly($driver).'Driver';

    if (method_exists($this, $method)) {
        return $this->$method();
    }

    throw new InvalidArgumentException("Driver [$driver] not supported.");
}
```

`HashManager` has `createBcryptDriver()`, `createArgonDriver()`, and `createArgon2idDriver()`, which return the matching hasher. If you register a driver under one of these names with `extend()`, the custom creator takes precedence.

## The driver contract

A hash driver implements the `Illuminate\Contracts\Hashing\Hasher` interface. `HashManager`'s `make()`, `check()`, and related methods forward the call to the current driver returned by `driver()`.

```php theme={null}
interface Hasher
{
    public function info($hashedValue);
    public function make($value, array $options = []);
    public function check($value, $hashedValue, array $options = []);
    public function needsRehash($hashedValue, array $options = []);
}
```

The built-in `BcryptHasher` extends `AbstractHasher`, which provides the shared `info()` and `check()` logic. If your driver extends the same base class, it inherits the handling of empty hashes in `check()`.

## Implementing a pepper hasher

Instead of passing the password directly to bcrypt, this driver applies HMAC-SHA256 with a secret key first and then passes the result to bcrypt. bcrypt only uses the first 72 bytes of its input, so the HMAC step turns any password into a fixed-length value before hashing.

```php theme={null}
namespace App\Hashing;

use Illuminate\Hashing\AbstractHasher;
use Illuminate\Contracts\Hashing\Hasher;

class PepperHasher extends AbstractHasher implements Hasher
{
    public function __construct(
        protected string $pepper,
        protected int $rounds = 12,
    ) {}

    public function make($value, array $options = [])
    {
        return password_hash($this->pepperize($value), PASSWORD_BCRYPT, [
            'cost' => $this->cost($options),
        ]);
    }

    public function check($value, $hashedValue, array $options = [])
    {
        if (is_null($hashedValue) || (string) $hashedValue === '') {
            return false;
        }

        return password_verify($this->pepperize($value), $hashedValue);
    }

    public function needsRehash($hashedValue, array $options = [])
    {
        return password_needs_rehash($hashedValue, PASSWORD_BCRYPT, [
            'cost' => $this->cost($options),
        ]);
    }

    protected function pepperize(string $value): string
    {
        return hash_hmac('sha256', $value, $this->pepper);
    }

    protected function cost(array $options): int
    {
        return $options['rounds'] ?? $this->rounds;
    }
}
```

`check()` calls `password_verify()` directly because `AbstractHasher::check()` would verify the plain value without the pepper. The override applies the pepper before verification.

## Registering the driver

Register the custom driver in the `boot()` method of `AppServiceProvider` with `Hash::extend()`.

```php theme={null}
// app/Providers/AppServiceProvider.php
use App\Hashing\PepperHasher;
use Illuminate\Support\Facades\Hash;

public function boot(): void
{
    Hash::extend('pepper', function ($app) {
        return new PepperHasher(
            pepper: config('app.hash_pepper'),
            rounds: config('hashing.bcrypt.rounds', 12),
        );
    });
}
```

The closure receives the container instance. `extend()` binds the closure to `HashManager`, so `$this` inside the closure refers to the `HashManager` instance.

Store the pepper in your `.env` file. Changing the pepper makes all existing hashes unverifiable, so decide on it once and keep it stable.

```ini theme={null}
# .env
HASH_DRIVER=pepper
HASH_PEPPER=a-long-random-string-generated-once
```

```php theme={null}
// config/app.php
'hash_pepper' => env('HASH_PEPPER'),
```

> [!WARNING]
>
> `HASH_PEPPER` is a secret. Keep it in `.env` and do not commit it to version control. Changing or losing the pepper makes all existing passwords unverifiable.

## Verifying the behavior

After you register the driver, existing `Hash` facade calls work without changes.

```php theme={null}
use Illuminate\Support\Facades\Hash;

$hash = Hash::make('password123');

Hash::check('password123', $hash); // true
Hash::check('wrong', $hash);       // false

// Use a specific driver explicitly
Hash::driver('bcrypt')->make('password123');
```

`Hash::driver()` calls `HashManager::driver()` and returns the driver with the given name. `Hash::make()` uses the default driver, so setting `HASH_DRIVER=pepper` makes it use the `pepper` driver.

### Impact on existing hashes

Switching `HASH_DRIVER` or changing the pepper makes existing hashes fail `check()`. Do not change the pepper once it is set. If your application already has users, plan how to migrate their hashes before you switch drivers.

## Summary

| Goal | Approach |
| - | - |
| Register a driver | `Hash::extend('name', fn ($app) => new Hasher)` |
| Change the default driver | `HASH_DRIVER=name` |
| Use a driver explicitly | `Hash::driver('name')` |
| Driver contract | Implement `Illuminate\Contracts\Hashing\Hasher` |

## Next steps

> [card] [Custom Authentication Guard](https://kawax.biz/en/advanced/custom-auth-guard.md)
>
> Learn how authentication uses `Hash::check()` from the guard side.

> [card] [Hashing](https://kawax.biz/en/hashing.md)
>
> Review the standard bcrypt and Argon2 usage and configuration.


## Related topics

- [Hashing](https://kawax.biz/en/hashing.md)
- [Advanced Topics](https://kawax.biz/en/advanced/index.md)
- [Laravel Sentinel — route protection middleware research](https://kawax.biz/en/blog/sentinel-introduction.md)
- [Creating a Custom Provider for AI SDK](https://kawax.biz/en/advanced/ai-sdk-custom-provider.md)
- [Registering and re-resolving package Storage drivers](https://kawax.biz/en/advanced/package-filesystem-drivers.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.