1- Securely Generating Random Values
2- =================================
1+ Securely Comparing Strings and Generating Random Values
2+ =======================================================
33
44The Symfony Security component comes with a collection of nice utilities
55related to security. These utilities are used by Symfony, but you should
@@ -25,8 +25,7 @@ Generating a Secure Random String
2525~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2626
2727Whenever you need to generate a secure random string, you are highly
28- encouraged to use the
29- :phpfunction: `random_bytes ` function::
28+ encouraged to use the :phpfunction: `random_bytes ` function::
3029
3130 $random = random_bytes(10);
3231
@@ -35,26 +34,26 @@ the number bytes passed as an argument (10 in the above example).
3534
3635.. tip ::
3736
38- The ``random_bytes() `` function returns a binary string which may contain the
39- ``\0 `` character. This can cause trouble in several common scenarios, such
40- as storing this value in a database or including it as part of the URL. The
41- solution is to encode or hash the value returned by ``random_bytes() `` (to do that, you
42- can use a simple ``base64_encode() `` PHP function).
37+ The ``random_bytes() `` function returns a binary string which may contain
38+ the ``\0 `` character. This can cause trouble in several common scenarios,
39+ such as storing this value in a database or including it as part of the
40+ URL. The solution is to encode or hash the value returned by
41+ ``random_bytes() `` (to do that, you can use a simple ``base64_encode() ``
42+ PHP function).
4343
4444Generating a Secure Random Number
4545~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4646
4747If you need to generate a cryptographically secure random integer, you should
48- use the
49- :phpfunction: `random_int ` function::
48+ use the :phpfunction: `random_int ` function::
5049
5150 $random = random_int(1, 10);
5251
5352.. note ::
5453
55- PHP 7 and up provide the ``random_bytes() `` and ``random_int() `` functions natively,
56- for older versions of PHP a polyfill is provided by the ` Symfony Polyfill Component `_
57- and the `paragonie/random_compat package `_.
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 `_.
5857
5958.. _`Timing attack` : https://en.wikipedia.org/wiki/Timing_attack
6059.. _`Symfony Polyfill Component` : https://github.com/symfony/polyfill
0 commit comments