Skip to content

Commit 74973e0

Browse files
committed
[Security] Use hash_equals for constant-time string comparison
1 parent ac7ddaf commit 74973e0

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/Symfony/Component/Security/Core/Util/StringUtils.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* String utility functions.
1616
*
1717
* @author Fabien Potencier <fabien@symfony.com>
18+
* @author Kévin Dunglas <dunglas@gmail.com>
1819
*/
1920
class StringUtils
2021
{
@@ -35,6 +36,11 @@ private function __construct() {}
3536
*/
3637
public static function equals($knownString, $userInput)
3738
{
39+
// Use hash_equals if applicable
40+
if (function_exists('hash_equals') && strlen($knownString) === strlen($userInput)) {
41+
return hash_equals($knownString, $userInput);
42+
}
43+
3844
// Prevent issues if string length is 0
3945
$knownString .= chr(0);
4046
$userInput .= chr(0);

0 commit comments

Comments
 (0)