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

Commit a4ff3be

Browse files
[Security/Core] make encodedLength computation more generic
1 parent 4d4c029 commit a4ff3be

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

Core/Encoder/MessageDigestPasswordEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class MessageDigestPasswordEncoder extends BasePasswordEncoder
2222
{
2323
private $algorithm;
2424
private $encodeHashAsBase64;
25-
private $iterations = 0;
25+
private $iterations = 1;
2626
private $encodedLength = -1;
2727

2828
/**

Core/Encoder/Pbkdf2PasswordEncoder.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class Pbkdf2PasswordEncoder extends BasePasswordEncoder
3030
{
3131
private $algorithm;
3232
private $encodeHashAsBase64;
33-
private $iterations;
33+
private $iterations = 1;
3434
private $length;
35-
private $encodedLength;
35+
private $encodedLength = -1;
3636

3737
/**
3838
* @param string $algorithm The digest algorithm to use
@@ -44,9 +44,15 @@ public function __construct(string $algorithm = 'sha512', bool $encodeHashAsBase
4444
{
4545
$this->algorithm = $algorithm;
4646
$this->encodeHashAsBase64 = $encodeHashAsBase64;
47-
$this->iterations = $iterations;
4847
$this->length = $length;
49-
$this->encodedLength = $encodeHashAsBase64 ? intdiv($length + 2, 3) << 2 : ($length << 1);
48+
49+
try {
50+
$this->encodedLength = \strlen($this->encodePassword('', 'salt'));
51+
} catch (\LogicException $e) {
52+
// ignore algorithm not supported
53+
}
54+
55+
$this->iterations = $iterations;
5056
}
5157

5258
/**
@@ -74,7 +80,7 @@ public function encodePassword($raw, $salt)
7480
*/
7581
public function isPasswordValid($encoded, $raw, $salt)
7682
{
77-
if ((0 < $this->length && \strlen($encoded) !== $this->encodedLength) || false !== strpos($encoded, '$')) {
83+
if (\strlen($encoded) !== $this->encodedLength || false !== strpos($encoded, '$')) {
7884
return false;
7985
}
8086

0 commit comments

Comments
 (0)