def AFSplit(data, stripes, digesttype='sha1'): """AF-Split data using digesttype. Returned data size will be len(data) * stripes""" blockSize = len(data) rand = RandomPool() bufblock = "\x00" * blockSize ret = "" for i in range(0, stripes - 1): # Get some random data rand.randomize() rand.stir() r = rand.get_bytes(blockSize) if rand.entropy < 0: print "Warning: RandomPool entropy dropped below 0" ret += r bufblock = _xor(r, bufblock) bufblock = _diffuse(bufblock, blockSize, digesttype) rand.add_event(bufblock) ret += _xor(bufblock, data) return ret
def get_random_bytes( nbytes ): nbits = nbytes * 8 random_pool = RandomPool( 1064 ) while random_pool.entropy < nbits: random_pool.add_event() random_pool.stir() return str( number.getRandomNumber( nbits, random_pool.get_bytes ) )
def get_random_bytes(nbytes): nbits = nbytes * 8 random_pool = RandomPool(1064) while random_pool.entropy < nbits: random_pool.add_event() random_pool.stir() return str(number.getRandomNumber(nbits, random_pool.get_bytes))
def AFSplit(data, stripes, digesttype='sha1'): """AF-Split data using digesttype. Returned data size will be len(data) * stripes""" blockSize = len(data) rand = RandomPool() bufblock = "\x00" * blockSize ret = "" for i in range(0, stripes-1): # Get some random data rand.randomize() rand.stir() r = rand.get_bytes(blockSize) if rand.entropy < 0: print "Warning: RandomPool entropy dropped below 0" ret += r bufblock = _xor(r, bufblock) bufblock = _diffuse(bufblock, blockSize, digesttype) rand.add_event(bufblock) ret += _xor(bufblock, data) return ret
def _generate_seed(self, size): rp = RandomPool() for i in range(7): m = SHA.new() tempseed = rp.get_bytes(size) m.update(tempseed) rp.add_event(m.hexdigest()) return rp.get_bytes(size)
def runTest(self): """Crypto.Util.randpool.RandomPool""" # Import the winrandom module and try to use it from Crypto.Util.randpool import RandomPool sys.stderr.write("SelfTest: You can ignore the RandomPool_DeprecationWarning that follows.\n") rpool = RandomPool() x = rpool.get_bytes(16) y = rpool.get_bytes(16) self.assertNotEqual(x, y) self.assertNotEqual(rpool.entropy, 0) rpool.randomize() rpool.stir('foo') rpool.add_event('foo')
class Random: def __init__(self): from Crypto.Util.randpool import RandomPool self.RandomPool = RandomPool() def getRandomString(self, N): """Returns a N-bit length random string.""" r = self.getRandomNumber(N) return number.long_to_bytes(r) def getRandomNumber(self, N): """Returns an N-bit length random number.""" if self.RandomPool.entropy < 2 * N: self.RandomPool.randomize(4 * N) self.RandomPool.add_event('') self.RandomPool.stir() random = number.getRandomNumber(N, self.RandomPool.get_bytes) self.RandomPool.stir() return random def getPrime(self, N): """Returns a N-bit length prime.""" if self.RandomPool.entropy < 2 * N: self.RandomPool.randomize(4 * N) self.RandomPool.add_event('') self.RandomPool.stir() prime = number.getPrime(N, self.RandomPool.get_bytes) self.RandomPool.stir() return prime def addEvent(self, text): """Adds a bit of random text to the pool as additional entropy. Use caution. The curreny implementation of this function just XORs the text over the entropy, probably giving it bias if we just roll through our messages. I'm not sure. """ self.RandomPool.add_event(text) self.RandomPool.stir() def verifyEntropy(self, N): """Verifies enough entropy is in the RandomPool. If we are close to no entropy, attempt to add some.""" if self.RandomPool.entropy < 2 * N: self.RandomPool.randomize(4 * N) self.RandomPool.add_event('') self.RandomPool.stir() if self.RandomPool.entropy < N: # if the stirring got rid of entropy, seed with more entropy self.verifyEntropy(2 * N) def get_bytes(self, num): """Get num bytes of randomness from the RandomPool.""" return self.RandomPool.get_bytes(num)