From 60a4d8dc7ae91b55770102ff32a92c7b50717e49 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sun, 27 Oct 2019 11:16:45 +0100 Subject: [PATCH] [Security/Core] make encodedLength computation more generic --- .../Encoder/MessageDigestPasswordEncoder.php | 2 +- .../Core/Encoder/Pbkdf2PasswordEncoder.php | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Security/Core/Encoder/MessageDigestPasswordEncoder.php b/src/Symfony/Component/Security/Core/Encoder/MessageDigestPasswordEncoder.php index 28b5626846..324af018da 100644 --- a/src/Symfony/Component/Security/Core/Encoder/MessageDigestPasswordEncoder.php +++ b/src/Symfony/Component/Security/Core/Encoder/MessageDigestPasswordEncoder.php @@ -22,7 +22,7 @@ class MessageDigestPasswordEncoder extends BasePasswordEncoder { private $algorithm; private $encodeHashAsBase64; - private $iterations = 0; + private $iterations = 1; private $encodedLength = -1; /** diff --git a/src/Symfony/Component/Security/Core/Encoder/Pbkdf2PasswordEncoder.php b/src/Symfony/Component/Security/Core/Encoder/Pbkdf2PasswordEncoder.php index a70439a216..81b0724d27 100644 --- a/src/Symfony/Component/Security/Core/Encoder/Pbkdf2PasswordEncoder.php +++ b/src/Symfony/Component/Security/Core/Encoder/Pbkdf2PasswordEncoder.php @@ -30,9 +30,9 @@ class Pbkdf2PasswordEncoder extends BasePasswordEncoder { private $algorithm; private $encodeHashAsBase64; - private $iterations; + private $iterations = 1; private $length; - private $encodedLength; + private $encodedLength = -1; /** * @param string $algorithm The digest algorithm to use @@ -44,9 +44,15 @@ class Pbkdf2PasswordEncoder extends BasePasswordEncoder { $this->algorithm = $algorithm; $this->encodeHashAsBase64 = $encodeHashAsBase64; - $this->iterations = $iterations; $this->length = $length; - $this->encodedLength = $encodeHashAsBase64 ? intdiv($length + 2, 3) << 2 : ($length << 1); + + try { + $this->encodedLength = \strlen($this->encodePassword('', 'salt')); + } catch (\LogicException $e) { + // ignore algorithm not supported + } + + $this->iterations = $iterations; } /** @@ -74,7 +80,7 @@ class Pbkdf2PasswordEncoder extends BasePasswordEncoder */ public function isPasswordValid($encoded, $raw, $salt) { - if ((0 < $this->length && \strlen($encoded) !== $this->encodedLength) || false !== strpos($encoded, '$')) { + if (\strlen($encoded) !== $this->encodedLength || false !== strpos($encoded, '$')) { return false; }