blob: 12969d1c7a57bc455d03a4744f123b93a6a9e3ac [file] [log] [blame]
Alex Gaynor2a70f912014-02-06 09:47:07 -08001Random number generation
2========================
3
4When generating random data for use in cryptographic operations, such as an
5initialization vector for encryption in
6:class:`~cryptography.hazmat.primitives.ciphers.modes.CBC` mode, you do not
7want to use the standard :mod:`random` module APIs. This is because they do not
Alex Gaynorcb157162014-02-06 10:27:48 -08008provide a cryptographically secure random number generator, which can result in
9major security issues depending on the algorithms in use.
Alex Gaynor2a70f912014-02-06 09:47:07 -080010
Alex Gaynor3e4729a2014-02-25 14:12:35 -080011Therefore, it is our recommendation to `always use your operating system's
12provided random number generator`_, which is available as ``os.urandom()``. For
Alex Gaynor2a70f912014-02-06 09:47:07 -080013example, if you need 16 bytes of random data for an initialization vector, you
14can obtain them with:
15
David Reidb69aff52014-02-07 13:38:38 -080016.. code-block:: pycon
Alex Gaynor2a70f912014-02-06 09:47:07 -080017
18 >>> import os
19 >>> os.urandom(16)
20 '...'
Alex Gaynor3e4729a2014-02-25 14:12:35 -080021
22.. _`always use your operating system's provided random number generator`: http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers/