def test_randombytes_uniform(self): self.assertIsInstance(libnacl.randombytes_uniform(200), int) freq = {libnacl.randombytes_uniform(256): 1 for _ in range(65536)} self.assertEqual(256, len(freq)) self.assertTrue(all(freq.values()))
def randint(a, b): """ Return a cryptographically secure random integer N such that ``a <= N <= b``. """ n = libnacl.randombytes_uniform(b) + a assert a <= n <= b return n
def randomStr(size): if not isinstance(size, int): raise PlenumTypeError('size', size, int) if not size > 0: raise PlenumValueError('size', size, '> 0') # Approach 1 rv = randombytes(size // 2).hex() return rv if size % 2 == 0 else rv + hex(randombytes_uniform(15))[-1]
def random_string(sz: int) -> str: assert (sz > 0), "Expected random string size cannot be less than 1" rv = libnacl.randombytes(sz // 2).hex() return rv if sz % 2 == 0 else rv + hex(libnacl.randombytes_uniform(15))[-1]
def randomStr(size): assert (size > 0), "Expected random string size cannot be less than 1" # Approach 1 rv = randombytes(size // 2).hex() return rv if size % 2 == 0 else rv + hex(randombytes_uniform(15))[-1]
def random_string(self, sz: int) -> str: assert (sz > 0), "Expected random string size cannot be less than 1" rv = libnacl.randombytes(sz // 2).hex() return rv if sz % 2 == 0 else rv + hex(libnacl.randombytes_uniform(15))[-1]