def urandom(n): global urandom_source if urandom_source is None: urandom_source = SecureRandom() buffer = jarray.zeros(n, 'b') urandom_source.nextBytes(buffer) return buffer.tostring()
class _UserFriendlyRNG(object): def __init__(self): self.closed = False self.__securerandom = None self.reinit() def reinit(self): self.__securerandom = SecureRandom() def close(self): self.closed = True self.__securerandom = None def flush(self): pass def read(self, N): if self.closed: raise ValueError("I/O operation on closed file") if not isinstance(N, (long, int)): raise TypeError("an integer is required") if N < 0: raise ValueError("cannot read to end of infinite stream") result = jarray.zeros(N, 'b') self.__securerandom.nextBytes(result) return result.tostring()
def generateSecretKey(self, keyLength): bytes = jarray.zeros(keyLength, "b") secureRandom = SecureRandom() secureRandom.nextBytes(bytes) return bytes
def generateNonce(keyLength): bytes = jarray.zeros(keyLength, "b") secureRandom = SecureRandom() secureRandom.nextBytes(bytes) return BaseEncoding.base64().omitPadding().encode(bytes)