blob: 18789b8e1d298e7f2cce72d0114279b9dd23529a [file] [log] [blame]
Jonathan Ballet6381da32011-07-20 16:43:38 +09001.. _openssl-rand:
2
3:py:mod:`rand` --- An interface to the OpenSSL pseudo random number generator
4=============================================================================
5
Jonathan Balletc9e066c2011-07-17 22:56:05 +09006.. py:module:: OpenSSL.rand
Jonathan Ballet6381da32011-07-20 16:43:38 +09007 :synopsis: An interface to the OpenSSL pseudo random number generator
Jonathan Balletc9e066c2011-07-17 22:56:05 +09008
Jonathan Balletc9e066c2011-07-17 22:56:05 +09009
10This module handles the OpenSSL pseudo random number generator (PRNG) and
11declares the following:
12
13.. py:function:: add(string, entropy)
14
15 Mix bytes from *string* into the PRNG state. The *entropy* argument is
16 (the lower bound of) an estimate of how much randomness is contained in
17 *string*, measured in bytes. For more information, see e.g. :rfc:`1750`.
Jonathan Ballet648875f2011-07-16 14:14:58 +090018
19
Jonathan Balletc9e066c2011-07-17 22:56:05 +090020.. py:function:: bytes(num_bytes)
21
22 Get some random bytes from the PRNG as a string.
23
24 This is a wrapper for the C function :py:func:`RAND_bytes`.
25
26
27.. py:function:: cleanup()
28
29 Erase the memory used by the PRNG.
30
31 This is a wrapper for the C function :py:func:`RAND_cleanup`.
32
33
34.. py:function:: egd(path[, bytes])
35
Jonathan Ballet6381da32011-07-20 16:43:38 +090036 Query the `Entropy Gathering Daemon <http://www.lothar.com/tech/crypto/>`_ on
37 socket *path* for *bytes* bytes of random data and uses :py:func:`add` to
38 seed the PRNG. The default value of *bytes* is 255.
Jonathan Balletc9e066c2011-07-17 22:56:05 +090039
40
41.. py:function:: load_file(path[, bytes])
42
43 Read *bytes* bytes (or all of it, if *bytes* is negative) of data from the
44 file *path* to seed the PRNG. The default value of *bytes* is -1.
45
46
47.. py:function:: screen()
48
49 Add the current contents of the screen to the PRNG state.
50
51 Availability: Windows.
52
53
54.. py:function:: seed(string)
55
56 This is equivalent to calling :py:func:`add` with *entropy* as the length
57 of the string.
58
59
60.. py:function:: status()
61
62 Returns true if the PRNG has been seeded with enough data, and false otherwise.
63
64
65.. py:function:: write_file(path)
66
67 Write a number of random bytes (currently 1024) to the file *path*. This
68 file can then be used with :py:func:`load_file` to seed the PRNG again.
69
70
71.. py:exception:: Error
72
73 If the current RAND method supports any errors, this is raised when needed.
74 The default method does not raise this when the entropy pool is depleted.
75
76 Whenever this exception is raised directly, it has a list of error messages
77 from the OpenSSL error queue, where each item is a tuple *(lib, function,
78 reason)*. Here *lib*, *function* and *reason* are all strings, describing
79 where and what the problem is. See :manpage:`err(3)` for more information.