PHP 8.0.30
Preview: random-numbers.rst Size: 1.55 KB
/proc/self/root/opt/alt/python37/share/doc/alt-python37-cryptography/docs/random-numbers.rst

Random number generation
========================

When generating random data for use in cryptographic operations, such as an
initialization vector for encryption in
:class:`~cryptography.hazmat.primitives.ciphers.modes.CBC` mode, you do not
want to use the standard :mod:`random` module APIs. This is because they do not
provide a cryptographically secure random number generator, which can result in
major security issues depending on the algorithms in use.

Therefore, it is our recommendation to `always use your operating system's
provided random number generator`_, which is available as :func:`os.urandom`.
For example, if you need 16 bytes of random data for an initialization vector,
you can obtain them with:

.. doctest::

    >>> import os
    >>> iv = os.urandom(16)

This will use ``/dev/urandom`` on UNIX platforms, and ``CryptGenRandom`` on
Windows.

If you need your random number as an integer (for example, for
:meth:`~cryptography.x509.CertificateBuilder.serial_number`), you can use
``int.from_bytes`` to convert the result of ``os.urandom``:

.. code-block:: pycon

    >>> serial = int.from_bytes(os.urandom(20), byteorder="big")

Starting with Python 3.6 the `standard library includes`_ the ``secrets``
module, which can be used for generating cryptographically secure random
numbers, with specific helpers for text-based formats.

.. _`always use your operating system's provided random number generator`: https://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers/
.. _`standard library includes`: https://docs.python.org/3/library/secrets.html

Directory Contents

Dirs: 4 × Files: 18

Name Size Perms Modified Actions
- drwxr-xr-x 2021-10-11 07:57:09
Edit Download
hazmat DIR
- drwxr-xr-x 2021-10-11 07:57:09
Edit Download
x509 DIR
- drwxr-xr-x 2021-10-11 07:57:09
Edit Download
_static DIR
- drwxr-xr-x 2021-07-08 11:09:48
Edit Download
2.28 KB lrw-r--r-- 2018-07-18 11:21:39
Edit Download
30 B lrw-r--r-- 2018-07-18 11:21:39
Edit Download
602 B lrw-r--r-- 2018-07-18 11:21:39
Edit Download
5.67 KB lrw-r--r-- 2021-07-08 11:09:27
Edit Download
1.66 KB lrw-r--r-- 2021-07-08 11:09:27
Edit Download
2.90 KB lrw-r--r-- 2018-07-18 11:21:39
Edit Download
853 B lrw-r--r-- 2018-07-18 11:21:39
Edit Download
5.20 KB lrw-r--r-- 2018-07-18 11:21:39
Edit Download
9.77 KB lrw-r--r-- 2018-07-18 11:21:39
Edit Download
4.51 KB lrw-r--r-- 2018-07-18 11:21:39
Edit Download
2.58 KB lrw-r--r-- 2018-07-18 11:21:39
Edit Download
9.49 KB lrw-r--r-- 2018-07-18 11:21:39
Edit Download
949 B lrw-r--r-- 2018-07-18 11:21:39
Edit Download
4.99 KB lrw-r--r-- 2018-07-18 11:21:39
Edit Download
5.46 KB lrw-r--r-- 2018-07-18 11:21:39
Edit Download
1.55 KB lrw-r--r-- 2018-07-18 11:21:39
Edit Download
5.08 KB lrw-r--r-- 2018-07-18 11:21:39
Edit Download
1.00 KB lrw-r--r-- 2018-07-18 11:21:39
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).