@@ -5,21 +5,24 @@ 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
+ All functions described in this article were introduced in PHP 7. For older
11
+ PHP versions, a polyfill is provided by the `Symfony Polyfill Component `_.
12
+
8
13
Comparing Strings
9
14
~~~~~~~~~~~~~~~~~
10
15
11
16
The time it takes to compare two strings depends on their differences. This
12
17
can be used by an attacker when the two strings represent a password for
13
18
instance; it is known as a `Timing attack `_.
14
19
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;
20
+ When comparing two passwords, you should use the :phpfunction: `hash_equals `
21
+ function::
20
22
21
- // is some known string (e.g. password) equal to some user input?
22
- $bool = StringUtils::equals($knownString, $userInput);
23
+ if (hash_equals($knownString, $userInput)) {
24
+ // ...
25
+ }
23
26
24
27
Generating a Secure Random String
25
28
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -49,12 +52,5 @@ use the :phpfunction:`random_int` function::
49
52
50
53
$random = random_int(1, 10);
51
54
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
55
.. _`Timing attack` : https://en.wikipedia.org/wiki/Timing_attack
59
56
.. _`Symfony Polyfill Component` : https://github.com/symfony/polyfill
60
- .. _`paragonie/random_compat package` : https://github.com/paragonie/random_compat
0 commit comments