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

Commit 97fb123

Browse files
ircmaxellfabpot
authored andcommitted
Change behavior to mirror hash_equals() returning early if there is a length mismatch
1 parent 25343ea commit 97fb123

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

Core/Util/StringUtils.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ private function __construct()
3838
*/
3939
public static function equals($knownString, $userInput)
4040
{
41-
if (function_exists('hash_equals')) {
42-
return hash_equals($knownString, $userInput);
43-
}
44-
4541
// Avoid making unnecessary duplications of secret data
4642
if (!is_string($knownString)) {
4743
$knownString = (string) $knownString;
@@ -51,16 +47,20 @@ public static function equals($knownString, $userInput)
5147
$userInput = (string) $userInput;
5248
}
5349

50+
if (function_exists('hash_equals')) {
51+
return hash_equals($knownString, $userInput);
52+
}
53+
5454
$knownLen = self::safeStrlen($knownString);
5555
$userLen = self::safeStrlen($userInput);
5656

57-
// Set the result to the difference between the lengths
58-
$result = $knownLen - $userLen;
57+
if ($userLen != $knownLen) {
58+
return false;
59+
}
5960

60-
// Always iterate over the minimum length possible.
61-
$iterationLen = min($knownLen, $userLen);
61+
$result = 0;
6262

63-
for ($i = 0; $i < $iterationLen; $i++) {
63+
for ($i = 0; $i < $knownLen; $i++) {
6464
$result |= (ord($knownString[$i]) ^ ord($userInput[$i]));
6565
}
6666

0 commit comments

Comments
 (0)