Skip to content

Commit a1c6c0b

Browse files
authored
Merge pull request #11096 from nanaya/hasher-cleanup
Simplify custom hasher driver
2 parents 813cccb + cae2493 commit a1c6c0b

File tree

6 files changed

+18
-39
lines changed

6 files changed

+18
-39
lines changed

app/Hashing/OsuHasher.php app/Hashing/OsuBcryptHasher.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use Illuminate\Contracts\Hashing\Hasher;
99

10-
class OsuHasher implements Hasher
10+
class OsuBcryptHasher implements Hasher
1111
{
1212
/**
1313
* The number of rounds to hash, as 2^n.
@@ -37,12 +37,10 @@ public function info($hashedValue)
3737
*/
3838
public function make($value, array $options = [])
3939
{
40-
$cost = array_get($options, 'cost', $this->rounds);
41-
4240
// When we originally moved to bcrypt (quite a few years ago),
4341
// we had to migrate everything without waiting for every user to
4442
// change their passwords, hence the md5 still being there.
45-
$hash = password_hash(md5($value), PASSWORD_BCRYPT, ['cost' => $cost]);
43+
$hash = password_hash(md5($value), PASSWORD_BCRYPT, ['cost' => $this->cost($options)]);
4644

4745
// see static::check()
4846
return str_replace('$2y$', '$2a$', $hash);
@@ -83,9 +81,13 @@ public function check($value, $hashedValue, array $options = [])
8381
*/
8482
public function needsRehash($hashedValue, array $options = [])
8583
{
86-
$cost = array_get($options, 'cost', $this->rounds);
8784
$hashedValue = str_replace('$2a$', '$2y$', $hashedValue);
8885

89-
return password_needs_rehash($hashedValue, PASSWORD_BCRYPT, ['cost' => $cost]);
86+
return password_needs_rehash($hashedValue, PASSWORD_BCRYPT, ['cost' => $this->cost($options)]);
87+
}
88+
89+
protected function cost(array $options): int
90+
{
91+
return $options['rounds'] ?? $this->rounds;
9092
}
9193
}

app/Hashing/OsuHashManager.php

-16
This file was deleted.

app/Providers/AppServiceProvider.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace App\Providers;
77

8-
use App\Hashing\OsuHashManager;
8+
use App\Hashing\OsuBcryptHasher;
99
use App\Libraries\MorphMap;
1010
use App\Libraries\OsuCookieJar;
1111
use App\Libraries\OsuMessageSelector;
@@ -79,6 +79,8 @@ public function boot()
7979
// newest scribe tries to rename {modelName} parameters to {id}
8080
// but it kind of doesn't work with our route handlers.
8181
Scribe::normalizeEndpointUrlUsing(fn ($url) => $url);
82+
83+
\Hash::extend('osubcrypt', fn () => new OsuBcryptHasher());
8284
}
8385

8486
/**
@@ -99,14 +101,6 @@ public function register()
99101
$localCacheManager->registerSingleton(app($name));
100102
}
101103

102-
$this->app->singleton('hash', function ($app) {
103-
return new OsuHashManager($app);
104-
});
105-
106-
$this->app->singleton('hash.driver', function ($app) {
107-
return $app['hash']->driver();
108-
});
109-
110104
$this->app->singleton('cookie', function ($app) {
111105
$config = $GLOBALS['cfg']['session'];
112106

config/app.php

-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@
178178

179179
'providers' => ServiceProvider::defaultProviders()->except([
180180
Illuminate\Cookie\CookieServiceProvider::class,
181-
Illuminate\Hashing\HashServiceProvider::class,
182181
Illuminate\Session\SessionServiceProvider::class,
183182
])->merge([
184183
App\Providers\AppServiceProvider::class,

config/hashing.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
| passwords for your application. By default, the bcrypt algorithm is
1212
| used; however, you remain free to modify this option if you wish.
1313
|
14-
| Supported: "bcrypt", "argon", "argon2id"
14+
| Supported: "bcrypt", "argon", "argon2id", "osubcrypt"
1515
|
1616
*/
1717

18-
'driver' => 'bcrypt',
18+
'driver' => 'osubcrypt',
1919

2020
/*
2121
|--------------------------------------------------------------------------

tests/Hashing/OsuHasherTest.php tests/Hashing/OsuBcryptHasherTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@
55

66
namespace Tests\Hashing;
77

8-
use App\Hashing\OsuHasher;
8+
use App\Hashing\OsuBcryptHasher;
99
use Illuminate\Contracts\Hashing\Hasher;
1010
use PHPUnit\Framework\TestCase;
1111

12-
class OsuHasherTest extends TestCase
12+
class OsuBcryptHasherTest extends TestCase
1313
{
1414
public function testBasicHashing()
1515
{
16-
$hasher = new OsuHasher();
16+
$hasher = new OsuBcryptHasher();
1717
$value = $hasher->make('password');
1818
$this->assertNotSame('password', $value);
1919
$this->assertNotSame(md5('password'), $value);
2020

2121
$this->assertTrue($hasher->check('password', $value));
2222
$this->assertFalse($hasher->needsRehash($value));
23-
$this->assertTrue($hasher->needsRehash($value, ['cost' => 4]));
23+
$this->assertTrue($hasher->needsRehash($value, ['rounds' => 4]));
2424
}
2525

2626
public function testImplementsHasher()
2727
{
28-
$hasher = new OsuHasher();
28+
$hasher = new OsuBcryptHasher();
2929
$this->assertInstanceOf(Hasher::class, $hasher);
3030
}
3131
}

0 commit comments

Comments
 (0)