Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/hashing.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'bcrypt' => [
'rounds' => env('BCRYPT_ROUNDS', 12),
'verify' => env('HASH_VERIFY', true),
'limit_length' => env('BCRYPT_LIMIT_LENGTH', true),
],

/*
Expand Down
19 changes: 19 additions & 0 deletions src/Illuminate/Hashing/BcryptHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

class BcryptHasher extends AbstractHasher implements HasherContract
{
/**
* The maximum byte length of the value to hash.
*
* @var int
*/
const MAX_BYTE_LENGTH = 72;

/**
* The default cost factor.
*
Expand All @@ -22,6 +29,13 @@ class BcryptHasher extends AbstractHasher implements HasherContract
*/
protected $verifyAlgorithm = false;

/**
* Impose bcrypt byte limit.
*
* @var bool
*/
protected $limitLength = false;

/**
* Create a new hasher instance.
*
Expand All @@ -32,6 +46,7 @@ public function __construct(array $options = [])
{
$this->rounds = $options['rounds'] ?? $this->rounds;
$this->verifyAlgorithm = $options['verify'] ?? $this->verifyAlgorithm;
$this->limitLength = $options['limit_length'] ?? $this->limitLength;
}

/**
Expand All @@ -46,6 +61,10 @@ public function __construct(array $options = [])
public function make(#[\SensitiveParameter] $value, array $options = [])
{
try {
if ($this->limitLength && strlen($value) > self::MAX_BYTE_LENGTH) {
throw new BcryptValueTooLongException;
}

$hash = password_hash($value, PASSWORD_BCRYPT, [
'cost' => $this->cost($options),
]);
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Hashing/BcryptValueTooLongException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Illuminate\Hashing;

use RuntimeException;

class BcryptValueTooLongException extends RuntimeException
{
public function __construct(int $maxLength = BcryptHasher::MAX_BYTE_LENGTH)
{
parent::__construct("Value to hash is too long. Bcrypt only allows for a maximum length of {$maxLength} bytes. Shorten your value or use a different hashing algorithm.");
}
}
8 changes: 8 additions & 0 deletions tests/Hashing/HasherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Hashing\Argon2IdHasher;
use Illuminate\Hashing\ArgonHasher;
use Illuminate\Hashing\BcryptHasher;
use Illuminate\Hashing\BcryptValueTooLongException;
use Illuminate\Hashing\HashManager;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -59,6 +60,13 @@ public function testBasicBcryptHashing()
$this->assertTrue($this->hashManager->isHashed($value));
}

public function testBcryptValueTooLong()
{
$this->expectException(BcryptValueTooLongException::class);
$hasher = new BcryptHasher(['limit_length' => true]);
$hasher->make(str_repeat('a', 73));
}

public function testBasicArgon2iHashing()
{
$hasher = new ArgonHasher;
Expand Down