egd.py

This module provides a simple implementation of a client-side interface to the EGD (entropy gathering daemon) protocol for obtaining cryptographically strong pseudo-random numbers on Unix-like systems that do not natively support a /dev/random device. Requires Python 2.4; probably not suitable for Python 3.x at this point. Also works with EGD-compatible entropy sources such as prngd.

Installation

Download the package tar file and extract it to a temporary directory. Then to install it into your Python (assuming you have proper Unix permissions), just type:

   python setup.py install

Optionally you can also just copy the "egd.py" file into your own project's directory.

This software is also published in PyPi, the Python Package Index (formerly known as the Python Cheeseshop). So you may be able to download and install it in one easy step by using:

   easy_install egd

Use

Using this module is easy. If you have an EGD (or prngd) daemon running, then to obtain random data is as simple as:

import egd

stuff = egd.get_random_data(16)  # ask for 16 bytes worth of random data

You can also use the egd.py script from the command line.

$ chmod +x egd.py   # Makes the script executable
$ egd.py --hex 16   # Prints 16 bytes worth of random data in hexadecimal

Other features are available, use the --help option:

Usage: egd.py [--path=path] [--blocking|--nonblocking] [--hex|--raw] [numbytes]

--path: specified path to the EGD socket.  May be a Unix path,
    or a TCP port number, or a "hostname:portnumber"

--hex | --raw: determines how the random bytes are output.
    The default (hex) is to output each byte in a hexadecimal
    encoded form.  With raw, each byte is output as-is.

--blocking | --nonblocking:  Determines which query command is made
    to the EGD daemon.  In blocking (the default), the process may
    have to wait until the EGD daemon has gathered enough entropy.
    In nonblocking, the process should return immediately although
    you may not get as many bytes (perhaps none) as you asked for.

See also

As a curiosity, python has an undocumented RAND_egd() function in the socket module, However, that function is only a simple wrapper around the underlying OpenSSL RAND_egd() function (asuming your Python was built with OpenSSL support) and only caused the internal entropy to be updated; it can not return actual random bytes to the caller.

License

I am releasing this code into the PUBLIC DOMAIN. It may be used by anybody for any purpose without any restriction.