Truly random numbers

John La Rooy nospampls.jlr at doctor.com
Tue Feb 11 20:57:17 EST 2003


On 11 Feb 2003 15:49:51 -0800
Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:

> Luka Milkovic <Luka.Milkovic at public.srce.hr> writes:
> > I am making a one time pad encryption program, and I have a little
> > problem. I somehow managed to make a small client which connects to
> > Forumilabs Hotbits random number generator, and I can download the
> > numbers. But the problem I am facing is that the connection is not
> > secured, it is actually plain-text protocol, and anyone can sniffit. Does
> > anyone have an idea how to solve this problem?
> 
> Use an SSL tunnel and hope that Fourmilabs isn't logging the random
> numbers?
> 
> > The second problem is much more important than the first... I don't know
> > how to obtain random numbers from /dev/random and since it is going to be
> > linux only application, I really need this as the source of my
> > entropy/random numbers. I was thinking that maybe I need to get data from
> > it, convert it into binary and then do it what ever I want. Anyway, what
> > I need is a list of n groups of XXXX ( four digits ) numbers, and I
> > solved it through the list, but can I ( I know I can, but I don't know
> > how ) somehow generate this list from /dev/random?
> 
> First of all you should use /dev/urandom rather than /dev/random,
> since /dev/random can block if it runs out of system entropy.
> 
> The simplest way to get a random 4 digit number from /dev/urandom
> is something like:
> 
>    urandom = open("/dev/urandom")
>    while 1:
>      n = struct.unpack('H', urandom.read (2))
>      if n < 10000: 
>         break

I don't think Luka wants pseudorandom numbers. To use /dev/random you can either
open it in nonblocking mode...

    import os
    dev_random=os.open("/dev/random",os.O_NONBLOCK)
    while 1:
        os.read(dev_random,1)     ### this will cause an exception if there is nothing to read

...or use select/poll


    import os,select

    number_of_groups=10000
    length_of_group=4

    dev_random=os.open("/dev/random",os.O_RDONLY)
    P=select.poll()
    P.register(dev_random,select.POLLIN)

    groups=[]

    while len(groups) < number_of_groups:
        group=""
        while len(group) < length_of_group:
            if P.poll(0.1):
                group+="%2.2X"%ord(os.read(dev_random,1))
        groups.append(group)
    print groups



John




More information about the Python-list mailing list