File tree 1 file changed +9
-1
lines changed
src/Symfony/Component/Security/Core/Util
1 file changed +9
-1
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ private function __construct() {}
27
27
* Compares two strings.
28
28
*
29
29
* This method implements a constant-time algorithm to compare strings.
30
+ * Regardless of the used implementation, it will leak length information.
30
31
*
31
32
* @param string $knownString The string of known length to compare against
32
33
* @param string $userInput The string that the user can control
@@ -35,6 +36,13 @@ private function __construct() {}
35
36
*/
36
37
public static function equals ($ knownString , $ userInput )
37
38
{
39
+ $ knownString = (string ) $ knownString ;
40
+ $ userInput = (string ) $ userInput ;
41
+
42
+ if (function_exists ('hash_equals ' )) {
43
+ return hash_equals ($ knownString , $ userInput );
44
+ }
45
+
38
46
$ knownLen = strlen ($ knownString );
39
47
$ userLen = strlen ($ userInput );
40
48
@@ -45,7 +53,7 @@ public static function equals($knownString, $userInput)
45
53
$ result = $ knownLen - $ userLen ;
46
54
47
55
// Note that we ALWAYS iterate over the user-supplied length
48
- // This is to prevent leaking length information
56
+ // This is to mitigate leaking length information
49
57
for ($ i = 0 ; $ i < $ userLen ; $ i ++) {
50
58
$ result |= (ord ($ knownString [$ i ]) ^ ord ($ userInput [$ i ]));
51
59
}
You can’t perform that action at this time.
0 commit comments