From 4c360e4aa9b52a5d3bf2a1b95d405acdc8377750 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 8 Aug 2015 18:18:09 -0400 Subject: Document how to get a random number as an integer, fixes #2190 --- docs/random-numbers.rst | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'docs/random-numbers.rst') diff --git a/docs/random-numbers.rst b/docs/random-numbers.rst index 8b119a3e..5f94bf13 100644 --- a/docs/random-numbers.rst +++ b/docs/random-numbers.rst @@ -21,4 +21,12 @@ you can obtain them with: 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") + .. _`always use your operating system's provided random number generator`: http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers/ -- cgit v1.2.3