Skip to content

Commit efaf1d6

Browse files
committed
feature #26175 [Security] Add configuration for Argon2i encryption (CoalaJoe)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Security] Add configuration for Argon2i encryption | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26174 | License | MIT | Doc PR | [#9300](symfony/symfony-docs#9300) Feedback? Current situation: Configuration only applies if argon2i is natively supported. Commits ------- 1300fece5f [Security] Add configuration for Argon2i encryption
2 parents 78a7e0b + 5b24220 commit efaf1d6

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

Encoder/Argon2iPasswordEncoder.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,30 @@
1717
* Argon2iPasswordEncoder uses the Argon2i hashing algorithm.
1818
*
1919
* @author Zan Baldwin <hello@zanbaldwin.com>
20+
* @author Dominik Müller <dominik.mueller@jkweb.ch>
2021
*/
2122
class Argon2iPasswordEncoder extends BasePasswordEncoder implements SelfSaltingEncoderInterface
2223
{
24+
private $config = array();
25+
26+
/**
27+
* Argon2iPasswordEncoder constructor.
28+
*
29+
* @param int|null $memoryCost memory usage of the algorithm
30+
* @param int|null $timeCost number of iterations
31+
* @param int|null $threads number of parallel threads
32+
*/
33+
public function __construct(int $memoryCost = null, int $timeCost = null, int $threads = null)
34+
{
35+
if (\defined('PASSWORD_ARGON2I')) {
36+
$this->config = array(
37+
'memory_cost' => $memoryCost ?? \PASSWORD_ARGON2_DEFAULT_MEMORY_COST,
38+
'time_cost' => $timeCost ?? \PASSWORD_ARGON2_DEFAULT_TIME_COST,
39+
'threads' => $threads ?? \PASSWORD_ARGON2_DEFAULT_THREADS,
40+
);
41+
}
42+
}
43+
2344
public static function isSupported()
2445
{
2546
if (\defined('PASSWORD_ARGON2I')) {
@@ -81,7 +102,7 @@ public function isPasswordValid($encoded, $raw, $salt)
81102

82103
private function encodePasswordNative($raw)
83104
{
84-
return password_hash($raw, \PASSWORD_ARGON2I);
105+
return password_hash($raw, \PASSWORD_ARGON2I, $this->config);
85106
}
86107

87108
private function encodePasswordSodiumFunction($raw)

Encoder/EncoderFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ private function getEncoderConfigFromAlgorithm($config)
111111
case 'argon2i':
112112
return array(
113113
'class' => Argon2iPasswordEncoder::class,
114-
'arguments' => array(),
114+
'arguments' => array(
115+
$config['memory_cost'],
116+
$config['time_cost'],
117+
$config['threads'],
118+
),
115119
);
116120
}
117121

Tests/Encoder/Argon2iPasswordEncoderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ protected function setUp()
2828
}
2929
}
3030

31+
public function testValidationWithConfig()
32+
{
33+
$encoder = new Argon2iPasswordEncoder(4, 4, 1);
34+
$result = $encoder->encodePassword(self::PASSWORD, null);
35+
$this->assertTrue($encoder->isPasswordValid($result, self::PASSWORD, null));
36+
$this->assertFalse($encoder->isPasswordValid($result, 'anotherPassword', null));
37+
}
38+
3139
public function testValidation()
3240
{
3341
$encoder = new Argon2iPasswordEncoder();

0 commit comments

Comments
 (0)