def _open_winrandom(): if _winrandom is None: raise error("Crypto.Util.winrandom module not found") # Check that we can open the winrandom module try: r0 = _winrandom.new() r1 = _winrandom.new() except Exception, exc: raise error("winrandom.new() failed: %s" % str(exc), exc)
def _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. """ data = '' if N <= 0: nbytes = int((self.bits - self.entropy) / 8 + 0.5) else: nbytes = int(N / 8 + 0.5) if winrandom: # Windows CryptGenRandom provides random data. data = winrandom.new().get_bytes(nbytes) elif os.path.exists(devname): # Many OSes support a /dev/urandom device try: f = open(devname) data = f.read(nbytes) f.close() except IOError, (num, msg): if num != 2: raise IOError, (num, msg)
def _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. """ data = '' if N <= 0: nbytes = int((self.bits - self.entropy)/8+0.5) else: nbytes = int(N/8+0.5) if winrandom: # Windows CryptGenRandom provides random data. data = winrandom.new().get_bytes(nbytes) elif os.path.exists(devname): # Many OSes support a /dev/urandom device try: f=open(devname) data=f.read(nbytes) f.close() except IOError, (num, msg): if num!=2: raise IOError, (num, msg)
def _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. """ data = '' if N <= 0: nbytes = int((self.bits - self.entropy) / 8 + 0.5) else: nbytes = int(N / 8 + 0.5) if winrandom: # Windows CryptGenRandom provides random data. data = winrandom.new().get_bytes(nbytes) # GAE fix, benadida # elif os.path.exists(devname): # Many OSes support a /dev/urandom device # try: # f=open(devname) # data=f.read(nbytes) # f.close() # except IOError, (num, msg): # if num!=2: raise IOError, (num, msg) # If the file wasn't found, ignore the error if data: self._addBytes(data) # Entropy estimate: The number of bits of # data obtained from the random source. self._updateEntropyEstimate(8 * len(data)) self.stir_n() # Wash the random pool
def _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. """ data = '' if N <= 0: nbytes = int((self.bits - self.entropy) / 8 + 0.5) else: nbytes = int(N / 8 + 0.5) if winrandom: # Windows CryptGenRandom provides random data. data = winrandom.new().get_bytes(nbytes) elif os.path.exists(devname): # Many OSes support a /dev/urandom device try: f = open(devname) data = f.read(nbytes) f.close() except IOError as xxx_todo_changeme: (num, msg) = xxx_todo_changeme.args if num != 2: raise IOError(num, msg) # If the file wasn't found, ignore the error if data: self._addBytes(data) # Entropy estimate: The number of bits of # data obtained from the random source. self._updateEntropyEstimate(8 * len(data)) self.stir_n() # Wash the random pool
def runTest(self): """winrandom: simple test""" # Import the winrandom module and try to use it from Crypto.Util import winrandom randobj = winrandom.new() x = randobj.get_bytes(16) y = randobj.get_bytes(16) self.assertNotEqual(x, y)
def __init__(self, numbytes=160, cipher=None, hash=None): self._wr = winrandom.new() self.get_bytes = self._wr.get_bytes self.randomize()