SHA-based subclass for random module

Raymond Hettinger python at rcn.com
Fri Mar 19 04:05:12 EST 2004


[Paul Rubin] 
> This is intended to be less predicable/have fewer correlations than
> the default Mersenne Twister or Wichmann-Hill generators.  Comments
> are appreciated.

I offer this as an alternative:


from random import Random
from struct import unpack
import md5

class MD5Random(Random):
    def newrandom(self,    tofloat = 1.0 / 2 ** 53):
        plaintxt = str(Random.random(self))
        ciphertxt = md5.new(plaintxt).digest()
        randint = unpack('<Q', ciphertxt[:8])[0] >> 11  # grab 53 bits
        return randint * tofloat


Advantages over the original:

* Much faster

* Much shorter code

* More readable code

* Threadsafe

* Takes advantage of MT's proven equidistribution, of its passing
known tests for randomness, and of its known period (in contrast,
SHA-1 was designed as a digest that makes it computationally
intractable to find a different message giving the same signature --
that does *not* imply the non-existence of short cycles for some
keys).

* It uses both MD5 and the Mersenne Twister as they were designed (in
contrast, my google searches show *no* research on using SHA-1 in OFB
and reapplying SHA-1 again to conceal the output).


Raymond Hettinger



More information about the Python-list mailing list