Skip to content

Commit

Permalink
Do not rehash already hashed value (laravel#47029)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdebrauwer authored and milwad-dev committed May 12, 2023
1 parent ad8e03a commit 7e589c1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ public static function encryptUsing($encrypter)
*/
protected function castAttributeAsHashedString($key, $value)
{
return Hash::needsRehash($value) ? Hash::make($value) : $value;
return $value !== null && password_get_info($value)['algo'] === null ? Hash::make($value) : $value;
}

/**
Expand Down
14 changes: 3 additions & 11 deletions tests/Integration/Database/EloquentModelHashedCastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed()

public function testHashed()
{
$this->hasher->expects('needsRehash')
->with('this is a password')
->andReturnTrue();

$this->hasher->expects('make')
->with('this is a password')
->andReturn('hashed-password');
Expand All @@ -51,18 +47,14 @@ public function testHashed()

public function testNotHashedIfAlreadyHashed()
{
$this->hasher->expects('needsRehash')
->with('already-hashed-password')
->andReturnFalse();

$subject = HashedCast::create([
'password' => 'already-hashed-password',
'password' => $hashedPassword = '$argon2i$v=19$m=65536,t=4,p=1$RHFPR1Zjc1p5cUVXTVJEcg$ooJoZb7NOa3r35WeeDRvnFwBTfaqlbbo1WcdJP5nPp8',
]);

$this->assertSame('already-hashed-password', $subject->password);
$this->assertSame($hashedPassword, $subject->password);
$this->assertDatabaseHas('hashed_casts', [
'id' => $subject->id,
'password' => 'already-hashed-password',
'password' => $hashedPassword,
]);
}

Expand Down

0 comments on commit 7e589c1

Please sign in to comment.