[issue27096] Ability to get random bytes from random.Random (as with os.urandom)
Campbell Barton
report at bugs.python.org
Tue May 24 17:01:49 EDT 2016
Campbell Barton added the comment:
@serhiy.storchaka, while a properly working function that uses getrandbits isn't so complex, its not trivial either.
It needs to create smaller chunks and join them (also check zero size case which raises an error if passed).
eg:
```
def urandom_from_random(rng, length):
if length == 0:
return b''
import sys
chunk_size = 65535
chunks = []
while length >= chunk_size:
chunks.append(rng.getrandbits(chunk_size * 8).to_bytes(chunk_size, sys.byteorder))
length -= chunk_size
if length:
chunks.append(rng.getrandbits(length * 8).to_bytes(length, sys.byteorder))
result = b''.join(chunks)
return result
```
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27096>
_______________________________________
More information about the Python-bugs-list
mailing list