Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit 178ee8b

Browse files
committed
feature #19843 [Security] Allow run-time configuration of hash algo (nicolas-grekas)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Security] Allow run-time configuration of hash algo | Q | A | ------------- | --- | Branch? | master | New feature? | yes | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Required if we want run-time config with env vars. See #19681 Commits ------- 7903a46 [Security] Allow run-time configuration of hash algo
2 parents 52847d3 + eba3802 commit 178ee8b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Core/Encoder/EncoderFactory.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public function getEncoder($user)
6969
*/
7070
private function createEncoder(array $config)
7171
{
72+
if (isset($config['algorithm'])) {
73+
$config = $this->getEncoderConfigFromAlgorithm($config);
74+
}
7275
if (!isset($config['class'])) {
7376
throw new \InvalidArgumentException(sprintf('"class" must be set in %s.', json_encode($config)));
7477
}
@@ -80,4 +83,41 @@ private function createEncoder(array $config)
8083

8184
return $reflection->newInstanceArgs($config['arguments']);
8285
}
86+
87+
private function getEncoderConfigFromAlgorithm($config)
88+
{
89+
switch ($config['algorithm']) {
90+
case 'plaintext':
91+
return array(
92+
'class' => PlaintextPasswordEncoder::class,
93+
'arguments' => array($config['ignore_case']),
94+
);
95+
96+
case 'pbkdf2':
97+
return array(
98+
'class' => Pbkdf2PasswordEncoder::class,
99+
'arguments' => array(
100+
$config['hash_algorithm'],
101+
$config['encode_as_base64'],
102+
$config['iterations'],
103+
$config['key_length'],
104+
),
105+
);
106+
107+
case 'bcrypt':
108+
return array(
109+
'class' => BCryptPasswordEncoder::class,
110+
'arguments' => array($config['cost']),
111+
);
112+
}
113+
114+
return array(
115+
'class' => MessageDigestPasswordEncoder::class,
116+
'arguments' => array(
117+
$config['algorithm'],
118+
$config['encode_as_base64'],
119+
$config['iterations'],
120+
),
121+
);
122+
}
83123
}

0 commit comments

Comments
 (0)