summaryrefslogtreecommitdiffstats
path: root/Lib/secrets.py
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-04-17 17:05:35 (GMT)
committerGitHub <noreply@github.com>2020-04-17 17:05:35 (GMT)
commit9f5fe7910f4a1bf5a425837d4915e332b945eb7b (patch)
tree5f652699332e33a81cf6baa5ff5b6fecadea1903 /Lib/secrets.py
parent22386bb4ef740ee92d34c87b8cb90d681423a853 (diff)
downloadcpython-9f5fe7910f4a1bf5a425837d4915e332b945eb7b.zip
cpython-9f5fe7910f4a1bf5a425837d4915e332b945eb7b.tar.gz
cpython-9f5fe7910f4a1bf5a425837d4915e332b945eb7b.tar.bz2
bpo-40286: Add randbytes() method to random.Random (GH-19527)
Add random.randbytes() function and random.Random.randbytes() method to generate random bytes. Modify secrets.token_bytes() to use SystemRandom.randbytes() rather than calling directly os.urandom(). Rename also genrand_int32() to genrand_uint32(), since it returns an unsigned 32-bit integer, not a signed integer. The _random module is now built with Py_BUILD_CORE_MODULE defined.
Diffstat (limited to 'Lib/secrets.py')
-rw-r--r--Lib/secrets.py3
1 files changed, 1 insertions, 2 deletions
diff --git a/Lib/secrets.py b/Lib/secrets.py
index 1304342..a546efb 100644
--- a/Lib/secrets.py
+++ b/Lib/secrets.py
@@ -14,7 +14,6 @@ __all__ = ['choice', 'randbelow', 'randbits', 'SystemRandom',
import base64
import binascii
-import os
from hmac import compare_digest
from random import SystemRandom
@@ -44,7 +43,7 @@ def token_bytes(nbytes=None):
"""
if nbytes is None:
nbytes = DEFAULT_ENTROPY
- return os.urandom(nbytes)
+ return _sysrand.randbytes(nbytes)
def token_hex(nbytes=None):
"""Return a random text string, in hexadecimal.