[Security/Core] add fast path when encoded password cannot match anything

This commit is contained in:
Nicolas Grekas 2019-10-27 10:41:22 +01:00
parent bfd308ff4a
commit c57f8f7f93
2 changed files with 19 additions and 1 deletions

View File

@ -22,7 +22,8 @@ class MessageDigestPasswordEncoder extends BasePasswordEncoder
{
private $algorithm;
private $encodeHashAsBase64;
private $iterations;
private $iterations = 0;
private $encodedLength = -1;
/**
* @param string $algorithm The digest algorithm to use
@ -33,6 +34,13 @@ class MessageDigestPasswordEncoder extends BasePasswordEncoder
{
$this->algorithm = $algorithm;
$this->encodeHashAsBase64 = $encodeHashAsBase64;
try {
$this->encodedLength = \strlen($this->encodePassword('', 'salt'));
} catch (\LogicException $e) {
// ignore algorithm not supported
}
$this->iterations = $iterations;
}
@ -65,6 +73,10 @@ class MessageDigestPasswordEncoder extends BasePasswordEncoder
*/
public function isPasswordValid($encoded, $raw, $salt)
{
if (\strlen($encoded) !== $this->encodedLength || false !== strpos($encoded, '$')) {
return false;
}
return !$this->isPasswordTooLong($raw) && $this->comparePasswords($encoded, $this->encodePassword($raw, $salt));
}
}

View File

@ -32,6 +32,7 @@ class Pbkdf2PasswordEncoder extends BasePasswordEncoder
private $encodeHashAsBase64;
private $iterations;
private $length;
private $encodedLength;
/**
* @param string $algorithm The digest algorithm to use
@ -45,6 +46,7 @@ class Pbkdf2PasswordEncoder extends BasePasswordEncoder
$this->encodeHashAsBase64 = $encodeHashAsBase64;
$this->iterations = $iterations;
$this->length = $length;
$this->encodedLength = $encodeHashAsBase64 ? intdiv($length + 2, 3) << 2 : ($length << 1);
}
/**
@ -72,6 +74,10 @@ class Pbkdf2PasswordEncoder extends BasePasswordEncoder
*/
public function isPasswordValid($encoded, $raw, $salt)
{
if ((0 < $this->length && \strlen($encoded) !== $this->encodedLength) || false !== strpos($encoded, '$')) {
return false;
}
return !$this->isPasswordTooLong($raw) && $this->comparePasswords($encoded, $this->encodePassword($raw, $salt));
}
}