Package Crypto :: Package Util :: Module randpool :: Class RandomPool
[hide private]
[frames] | no frames]

Class RandomPool

Known Subclasses:

randpool.py : Cryptographically strong random number generation.

The implementation here is similar to the one in PGP.  To be
cryptographically strong, it must be difficult to determine the RNG's
output, whether in the future or the past.  This is done by using
a cryptographic hash function to "stir" the random data.

Entropy is gathered in the same fashion as PGP; the highest-resolution
clock around is read and the data is added to the random number pool.
A conservative estimate of the entropy is then kept.

If a cryptographically secure random source is available (/dev/urandom
on many Unixes, Windows CryptGenRandom on most Windows), then use
it.

Instance Attributes:
bits : int
  Maximum size of pool in bits
bytes : int
  Maximum size of pool in bytes
entropy : int
  Number of bits of entropy in this pool.

Methods:
add_event([s]) : add some entropy to the pool
get_bytes(int) : get N bytes of random data
randomize([N]) : get N bytes of randomness from external source

Instance Methods [hide private]
 
__init__(self, numbytes=160, cipher=None, hash=None)
 
_updateEntropyEstimate(self, nbits)
 
_randomize(self, N=0, devname='/dev/urandom')
_randomize(N, DEVNAME:device-filepath) collects N bits of randomness from some entropy source (e.g., /dev/urandom on Unixes that have it, Windows CryptoAPI CryptGenRandom, etc) DEVNAME is optional, defaults to /dev/urandom.
 
randomize(self, N=0)
randomize(N:int) use the class entropy source to get some entropy data.
 
stir_n(N)
stirs the random pool N times
 
stir(self, s='')
stir(s:string) Mix up the randomness pool.
 
get_bytes(self, N)
get_bytes(N:int) : string Return N bytes of random data.
 
add_event(self, s='')
add_event(s:string) Add an event to the random pool.
 
_noise(self)
 
_measureTickSize(self)
 
_addBytes(self, s)
XOR the contents of the string S into the random pool
 
getBytes(self, N)
 
addEvent(self, event, s='')
Method Details [hide private]

_randomize(self, N=0, devname='/dev/urandom')

 
_randomize(N, DEVNAME:device-filepath) collects N bits of randomness from some entropy source (e.g., /dev/urandom on Unixes that have it, Windows CryptoAPI CryptGenRandom, etc) DEVNAME is optional, defaults to /dev/urandom. You can change it to /dev/random if you want to block till you get enough entropy.

randomize(self, N=0)

 
randomize(N:int) use the class entropy source to get some entropy data. This is overridden by KeyboardRandomize().

stir(self, s='')

 
stir(s:string) Mix up the randomness pool. This will call add_event() twice, but out of paranoia the entropy attribute will not be increased. The optional 's' parameter is a string that will be hashed with the randomness pool.

add_event(self, s='')

 
add_event(s:string) Add an event to the random pool. The current time is stored between calls and used to estimate the entropy. The optional 's' parameter is a string that will also be XORed into the pool. Returns the estimated number of additional bits of entropy gain.