-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'add_argon2_support' of https://github.com/morloderex/fr…
- Loading branch information
Showing
10 changed files
with
249 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
namespace Illuminate\Hashing; | ||
|
||
use RuntimeException; | ||
use Illuminate\Contracts\Hashing\Hasher as HasherContract; | ||
|
||
class ArgonHasher implements HasherContract | ||
{ | ||
/** | ||
* Default threads factor. | ||
* | ||
* @var int | ||
*/ | ||
protected $processors = 2; | ||
|
||
/** | ||
* Default memory cost factor. | ||
* | ||
* @var int | ||
*/ | ||
protected $memory = 1024; | ||
|
||
/** | ||
* Default time cost factor. | ||
* | ||
* @var int | ||
*/ | ||
protected $time = 2; | ||
|
||
/** | ||
* Hash the given value. | ||
* | ||
* @param string $value | ||
* @param array $options | ||
* @return string | ||
*/ | ||
public function make($value, array $options = []) | ||
{ | ||
$hash = password_hash($value, PASSWORD_ARGON2I, [ | ||
'memory_cost' => $this->memory($options), | ||
'time_cost' => $this->time($options), | ||
'threads' => $this->processors($options) | ||
]); | ||
|
||
if ($hash === false) { | ||
throw new RuntimeException('Argon2 hashing not supported.'); | ||
} | ||
|
||
return $hash; | ||
} | ||
|
||
/** | ||
* Check the given plain value against a hash. | ||
* | ||
* @param string $value | ||
* @param string $hashedValue | ||
* @param array $options | ||
* @return bool | ||
*/ | ||
public function check($value, $hashedValue, array $options = []) | ||
{ | ||
if (strlen($hashedValue) === 0) { | ||
return false; | ||
} | ||
|
||
return password_verify($value, $hashedValue); | ||
} | ||
|
||
/** | ||
* Check if the given hash has been hashed using the given options. | ||
* | ||
* @param string $hashedValue | ||
* @param array $options | ||
* @return bool | ||
*/ | ||
public function needsRehash($hashedValue, array $options = []) | ||
{ | ||
return password_needs_rehash($hashedValue, PASSWORD_ARGON2I, [ | ||
'memory_cost' => $this->memory($options), | ||
'time_cost' => $this->time($options), | ||
'threads' => $this->processors($options) | ||
]); | ||
} | ||
|
||
/** | ||
* Set the default password threads factor. | ||
* | ||
* @param int $threads | ||
* | ||
* @return $this; | ||
*/ | ||
public function setProcessors(int $threads) | ||
{ | ||
$this->threads = $threads; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the default password memory factor. | ||
* | ||
* @param int $memory | ||
* | ||
* @return $this | ||
*/ | ||
public function setMemory(int $memory) | ||
{ | ||
$this->memory = $memory; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the default password timing factor. | ||
* | ||
* @param int $time | ||
* | ||
* @return $this | ||
*/ | ||
public function setTime(int $time) | ||
{ | ||
$this->time = $time; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Extract the memory cost value from the options array. | ||
* | ||
* @param $options | ||
* @return int | ||
*/ | ||
protected function memory($options) | ||
{ | ||
return $options['memory'] ?? $this->memory; | ||
} | ||
|
||
/** | ||
* Extract the time cost value from the options array. | ||
* | ||
* @param $options | ||
* @return int | ||
*/ | ||
protected function time($options) | ||
{ | ||
return $options['time'] ?? $this->time; | ||
} | ||
|
||
/** | ||
* Extract the threads value from the options array. | ||
* | ||
* @param $options | ||
* @return int | ||
*/ | ||
protected function processors($options) | ||
{ | ||
return $options['processors'] ?? $this->processors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Illuminate\Hashing; | ||
|
||
use Illuminate\Support\Manager; | ||
|
||
class HashManager extends Manager | ||
{ | ||
/** | ||
* Get the default driver name. | ||
* | ||
* @return string | ||
*/ | ||
public function getDefaultDriver() | ||
{ | ||
return $this->app['config']['hashing.driver']; | ||
} | ||
|
||
/** | ||
* Create an instance of the Brycrypt hash Driver. | ||
* | ||
* @return BcryptHasher | ||
*/ | ||
public function createBcryptDriver() | ||
{ | ||
return new BcryptHasher; | ||
} | ||
|
||
/** | ||
* Create an instance of the Argon2 hash Driver. | ||
* | ||
* @return ArgonHasher | ||
*/ | ||
public function createArgonDriver() | ||
{ | ||
return new ArgonHasher; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Hashing; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class HasherTest extends TestCase | ||
{ | ||
public function testBasicBcryptHashing() | ||
{ | ||
$hasher = new \Illuminate\Hashing\BcryptHasher; | ||
$value = $hasher->make('password'); | ||
$this->assertNotSame('password', $value); | ||
$this->assertTrue($hasher->check('password', $value)); | ||
$this->assertFalse($hasher->needsRehash($value)); | ||
$this->assertTrue($hasher->needsRehash($value, ['rounds' => 1])); | ||
} | ||
|
||
public function testBasicArgonHashing() | ||
{ | ||
if (! defined('PASSWORD_ARGON2I')) { | ||
$this->markTestSkipped('PHP not compiled with argon2 hashing support support.'); | ||
} | ||
|
||
$hasher = new \Illuminate\Hashing\ArgonHasher; | ||
$value = $hasher->make('password'); | ||
$this->assertNotSame('password', $value); | ||
$this->assertTrue($hasher->check('password', $value)); | ||
$this->assertFalse($hasher->needsRehash($value)); | ||
$this->assertTrue($hasher->needsRehash($value, ['processors' => 1])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters