This repository was archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Add Argon2i for password hashing #58
Open
MatthiasKuehneEllerhold
wants to merge
2
commits into
zendframework:develop
Choose a base branch
from
MatthiasKuehneEllerhold:add-argon2i
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,212 @@ | ||
| <?php | ||
| /** | ||
| * @see https://github.com/zendframework/zend-crypt for the canonical source repository | ||
| * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com) | ||
| * @license https://github.com/zendframework/zend-crypt/blob/master/LICENSE.md New BSD License | ||
| */ | ||
|
|
||
| namespace Zend\Crypt\Password; | ||
|
|
||
| use Traversable; | ||
| use Zend\Stdlib\ArrayUtils; | ||
|
|
||
| use const PASSWORD_ARGON2I; | ||
| use const PHP_VERSION_ID; | ||
|
|
||
| use function is_array; | ||
| use function is_numeric; | ||
| use function is_scalar; | ||
| use function password_hash; | ||
| use function password_verify; | ||
| use function strtolower; | ||
|
|
||
| /** | ||
| * Argon2i algorithm using password_hash() function of PHP | ||
| */ | ||
| class Argon2i implements PasswordInterface | ||
| { | ||
| /** | ||
| * Maximum memory (in kibibytes) that may be used to compute the Argon2i hash | ||
| * | ||
| * @var int|null | ||
| */ | ||
| protected $memoryCost; | ||
|
|
||
| /** | ||
| * Maximum amount of time it may take to compute the Argon2i hash | ||
| * | ||
| * @var int|null | ||
| */ | ||
| protected $timeCost; | ||
|
|
||
| /** | ||
| * Number of threads to use for computing the Argon2i hash | ||
| * | ||
| * @var int|null | ||
| */ | ||
| protected $threads; | ||
|
|
||
| /** | ||
| * Constructor | ||
| * | ||
| * @param array|Traversable $options | ||
| * | ||
| * @throws Exception\InvalidArgumentException | ||
| */ | ||
| public function __construct($options = []) | ||
| { | ||
| if (PHP_VERSION_ID < 70200) { | ||
| throw new Exception\InvalidArgumentException( | ||
| 'The Argon2i password hash is only nativly available on PHP7.2+' | ||
| ); | ||
| } | ||
|
|
||
| if (empty($options)) { | ||
| return; | ||
| } | ||
|
|
||
| if ($options instanceof Traversable) { | ||
| $options = ArrayUtils::iteratorToArray($options); | ||
| } | ||
|
|
||
| if (! is_array($options)) { | ||
| throw new Exception\InvalidArgumentException( | ||
| 'The options parameter must be an array or a Traversable' | ||
| ); | ||
| } | ||
|
|
||
| foreach ($options as $key => $value) { | ||
| switch (strtolower($key)) { | ||
| case 'memory_cost': | ||
| $this->setMemoryCost($value); | ||
| break; | ||
| case 'time_cost': | ||
| $this->setTimeCost($value); | ||
| break; | ||
| case 'threads': | ||
| $this->setThreads($value); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the maximum memory (in kibibytes) that may be used to compute the Argon2i hash | ||
| * | ||
| * @return int | ||
| */ | ||
| public function getMemoryCost() | ||
| { | ||
| return $this->memoryCost; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the maximum memory (in kibibytes) that may be used to compute the Argon2i hash | ||
| * | ||
| * @param int $memoryCost | ||
| * | ||
| * @return self | ||
| */ | ||
| public function setMemoryCost($memoryCost) | ||
| { | ||
| if (!is_scalar($memoryCost) || !is_numeric($memoryCost) || ($memoryCost < 0)) { | ||
| throw new Exception\InvalidArgumentException( | ||
| 'The memory cost parameter of argon2i must be an integer greater 0' | ||
| ); | ||
| } | ||
| $this->memoryCost = (int) $memoryCost; | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the maximum amount of time it may take to compute the Argon2i hash | ||
| * | ||
| * @return int | ||
| */ | ||
| public function getTimeCost() | ||
| { | ||
| return $this->timeCost; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the maximum amount of time it may take to compute the Argon2i hash | ||
| * | ||
| * @param int $timeCost | ||
| * | ||
| * @return self | ||
| */ | ||
| public function setTimeCost($timeCost) | ||
| { | ||
| if (!is_scalar($timeCost) || !is_numeric($timeCost) || ($timeCost < 0)) { | ||
| throw new Exception\InvalidArgumentException( | ||
| 'The time cost parameter of argon2i must be an integer greater 0' | ||
| ); | ||
| } | ||
| $this->timeCost = (int) $timeCost; | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the number of threads to use for computing the Argon2i hash | ||
| * | ||
| * @return int | ||
| */ | ||
| public function getThreads() | ||
| { | ||
| return $this->threads; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the number of threads to use for computing the Argon2i hash | ||
| * | ||
| * @param int $threads | ||
| * | ||
| * @return self | ||
| */ | ||
| public function setThreads($threads) | ||
| { | ||
| if (!is_scalar($threads) || !is_numeric($threads) || ($threads < 0)) { | ||
| throw new Exception\InvalidArgumentException( | ||
| 'The thread-count parameter of argon2i must be an integer greater 0' | ||
| ); | ||
| } | ||
| $this->threads = (int) $threads; | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $password | ||
| * @throws Exception\RuntimeException | ||
| * @return string | ||
| */ | ||
| public function create($password) | ||
| { | ||
| $options = []; | ||
|
|
||
| if ($this->memoryCost !== null) { | ||
| $options['memory_cost'] = $this->memoryCost; | ||
| } | ||
|
|
||
| if ($this->timeCost !== null) { | ||
| $options['time_cost'] = $this->timeCost; | ||
| } | ||
|
|
||
| if ($this->threads !== null) { | ||
| $options['threads'] = $this->threads; | ||
| } | ||
|
|
||
| return password_hash($password, PASSWORD_ARGON2I, $options); | ||
| } | ||
|
|
||
| /** | ||
| * Verify if a password is correct against a hash value | ||
| * | ||
| * @param string $password | ||
| * @param string $hash | ||
| * @return bool | ||
| */ | ||
| public function verify($password, $hash) | ||
| { | ||
| return password_verify($password, $hash); | ||
| } | ||
| } | ||
This file contains hidden or 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,137 @@ | ||
| <?php | ||
| /** | ||
| * Zend Framework (http://framework.zend.com/) | ||
| * | ||
| * @link http://github.com/zendframework/zf2 for the canonical source repository | ||
| * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com) | ||
| * @license http://framework.zend.com/license/new-bsd New BSD License | ||
| */ | ||
|
|
||
| namespace ZendTest\Crypt\Password; | ||
|
|
||
| use ArrayObject; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Zend\Crypt\Password\Argon2i; | ||
| use Zend\Crypt\Password\Exception; | ||
|
|
||
| /** | ||
| * @group Zend_Crypt | ||
| */ | ||
| class Argon2iTest extends TestCase | ||
| { | ||
| public function testConstruct() | ||
| { | ||
| if (PHP_VERSION_ID < 70200) { | ||
| $this->expectException(Exception\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('The Argon2i password hash is only nativly available on PHP7.2+'); | ||
| } | ||
|
|
||
| $argon2i = new Argon2i(); | ||
|
|
||
| // This will only be executed on PHP7.2+ | ||
| $this->assertEquals(PASSWORD_ARGON2_DEFAULT_MEMORY_COST, $argon2i->getMemoryCost()); | ||
| $this->assertEquals(PASSWORD_ARGON2_DEFAULT_TIME_COST, $argon2i->getTimeCost()); | ||
| $this->assertEquals(PASSWORD_ARGON2_DEFAULT_THREADS, $argon2i->getThreads()); | ||
| } | ||
|
|
||
| /** | ||
| * @requires PHP 7.2 | ||
| */ | ||
| public function testConstructByOptions() | ||
| { | ||
| $options = [ | ||
| 'memory_cost' => 512, | ||
| 'time_cost' => 5, | ||
| 'threads' => 1, | ||
| ]; | ||
| $argon2i = new Argon2i($options); | ||
| $this->assertEquals(512, $argon2i->getMemoryCost()); | ||
| $this->assertEquals(5, $argon2i->getTimeCost()); | ||
| $this->assertEquals(1, $argon2i->getThreads()); | ||
| } | ||
|
|
||
| /** | ||
| * This test uses ArrayObject to simulate a Zend\Config\Config instance; | ||
| * the class itself only tests for Traversable. | ||
| * | ||
| * @requires PHP 7.2 | ||
| */ | ||
| public function testConstructByConfig() | ||
| { | ||
| $options = [ | ||
| 'memory_cost' => 512, | ||
| 'time_cost' => 5, | ||
| 'threads' => 1, | ||
| ]; | ||
| $config = new ArrayObject($options); | ||
| $argon2i = new Argon2i($config); | ||
| $this->assertEquals(512, $argon2i->getMemoryCost()); | ||
| $this->assertEquals(5, $argon2i->getTimeCost()); | ||
| $this->assertEquals(1, $argon2i->getThreads()); | ||
| } | ||
|
|
||
| /** | ||
| * @requires PHP 7.2 | ||
| */ | ||
| public function testWrongConstruct() | ||
| { | ||
| $this->expectException(Exception\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('The options parameter must be an array or a Traversable'); | ||
| new Argon2i('test'); | ||
| } | ||
|
|
||
| /** | ||
| * @requires PHP 7.2 | ||
| */ | ||
| public function testSetMemoryCost() | ||
| { | ||
| $argon2i = new Argon2i(); | ||
| $argon2i->setMemoryCost(512); | ||
| $this->assertEquals(512, $argon2i->getMemoryCost()); | ||
| } | ||
|
|
||
| /** | ||
| * @requires PHP 7.2 | ||
| */ | ||
| public function testSetTimeCost() | ||
| { | ||
| $argon2i = new Argon2i(); | ||
| $argon2i->setTimeCost(10); | ||
| $this->assertEquals(10, $argon2i->getTimeCost()); | ||
| } | ||
|
|
||
| /** | ||
| * @requires PHP 7.2 | ||
| */ | ||
| public function testCreate() | ||
| { | ||
| $argon2i = new Argon2i(); | ||
| $password = $argon2i->create('test'); | ||
| $this->assertNotEmpty($password); | ||
| $this->assertGreaterThanOrEqual(95, strlen($password)); | ||
| } | ||
|
|
||
| /** | ||
| * @requires PHP 7.2 | ||
| */ | ||
| public function testVerify() | ||
| { | ||
| $argon2i = new Argon2i(); | ||
| $hash = '$argon2i$v=19$m=1024,t=2,p=2$eFlUWVhOMWRjY2swVW1neQ$QASXjIL0nEMfep30FGefpWPdh0wISQpljW3FYPy5m0U'; | ||
| $this->assertTrue($argon2i->verify('test', $hash)); | ||
| $this->assertFalse($argon2i->verify('other', $hash)); | ||
| } | ||
|
|
||
| /** | ||
| * The class should also positivly verify bcrypt hashes | ||
| * | ||
| * @requires PHP 7.2 | ||
| */ | ||
| public function testVerifyBcryptHashes() | ||
| { | ||
| $argon2i = new Argon2i(); | ||
| $hash = '$2y$10$123456789012345678901uIcehzOq0s9RvVtyXJFIsuuxuE2XZRMq'; | ||
| $this->assertTrue($argon2i->verify('test', $hash)); | ||
| $this->assertFalse($argon2i->verify('other', $hash)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use the default PHP values, we can omit this check with null values and always pass the
$optionstopassword_hash.