@@ -5,21 +5,25 @@ The Symfony Security component comes with a collection of nice utilities
5
5
related to security. These utilities are used by Symfony, but you should
6
6
also use them if you want to solve the problem they address.
7
7
8
+ .. note ::
9
+
10
+ The functions described in this article were introduced in PHP 5.6 or 7.
11
+ For older PHP versions, a polyfill is provided by the
12
+ `Symfony Polyfill Component `_.
13
+
8
14
Comparing Strings
9
15
~~~~~~~~~~~~~~~~~
10
16
11
17
The time it takes to compare two strings depends on their differences. This
12
18
can be used by an attacker when the two strings represent a password for
13
19
instance; it is known as a `Timing attack `_.
14
20
15
- Internally, when comparing two passwords, Symfony uses a constant-time
16
- algorithm; you can use the same strategy in your own code thanks to the
17
- :class: `Symfony\\ Component\\ Security\\ Core\\ Util\\ StringUtils ` class::
18
-
19
- use Symfony\Component\Security\Core\Util\StringUtils;
21
+ When comparing two passwords, you should use the :phpfunction: `hash_equals `
22
+ function::
20
23
21
- // is some known string (e.g. password) equal to some user input?
22
- $bool = StringUtils::equals($knownString, $userInput);
24
+ if (hash_equals($knownString, $userInput)) {
25
+ // ...
26
+ }
23
27
24
28
Generating a Secure Random String
25
29
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -49,12 +53,5 @@ use the :phpfunction:`random_int` function::
49
53
50
54
$random = random_int(1, 10);
51
55
52
- .. note ::
53
-
54
- PHP 7 and up provide the ``random_bytes() `` and ``random_int() `` functions
55
- natively, for older versions of PHP a polyfill is provided by the
56
- `Symfony Polyfill Component `_ and the `paragonie/random_compat package `_.
57
-
58
56
.. _`Timing attack` : https://en.wikipedia.org/wiki/Timing_attack
59
57
.. _`Symfony Polyfill Component` : https://github.com/symfony/polyfill
60
- .. _`paragonie/random_compat package` : https://github.com/paragonie/random_compat
0 commit comments