Exemple #1
0
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()
Exemple #2
0
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()
Exemple #3
0
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()
Exemple #4
0
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()
Exemple #5
0
    def generateSecretKey(self, keyLength):
        bytes = jarray.zeros(keyLength, "b")
        secureRandom = SecureRandom()
        secureRandom.nextBytes(bytes)

        return bytes
Exemple #6
0
 def generateNonce(keyLength):
     bytes = jarray.zeros(keyLength, "b")
     secureRandom = SecureRandom()
     secureRandom.nextBytes(bytes)
     return BaseEncoding.base64().omitPadding().encode(bytes)
 def generateSecretKey(self, keyLength):
     bytes = jarray.zeros(keyLength, "b")
     secureRandom = SecureRandom()
     secureRandom.nextBytes(bytes)
     
     return bytes