summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNewUserHa <32261870+NewUserHa@users.noreply.github.com>2022-11-11 22:45:24 (GMT)
committerGitHub <noreply@github.com>2022-11-11 22:45:24 (GMT)
commit55c96e8053689c29ae28a9d2117ae37934eace68 (patch)
treee3c4b63e257d9eb31db654666d2a296657609cda
parentf531b6879b530515b009ac79767702829848cf07 (diff)
downloadcpython-55c96e8053689c29ae28a9d2117ae37934eace68.zip
cpython-55c96e8053689c29ae28a9d2117ae37934eace68.tar.gz
cpython-55c96e8053689c29ae28a9d2117ae37934eace68.tar.bz2
gh-99305: Speed up secrets.token_hex() ~2x (#99306)
simple code modernization. Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
-rw-r--r--Lib/secrets.py3
-rw-r--r--Misc/NEWS.d/next/Library/2022-11-10-11-51-39.gh-issue-99305.6LzQc3.rst1
2 files changed, 2 insertions, 2 deletions
diff --git a/Lib/secrets.py b/Lib/secrets.py
index 900381a..566a09b 100644
--- a/Lib/secrets.py
+++ b/Lib/secrets.py
@@ -13,7 +13,6 @@ __all__ = ['choice', 'randbelow', 'randbits', 'SystemRandom',
import base64
-import binascii
from hmac import compare_digest
from random import SystemRandom
@@ -56,7 +55,7 @@ def token_hex(nbytes=None):
'f9bf78b9a18ce6d46a0cd2b0b86df9da'
"""
- return binascii.hexlify(token_bytes(nbytes)).decode('ascii')
+ return token_bytes(nbytes).hex()
def token_urlsafe(nbytes=None):
"""Return a random URL-safe text string, in Base64 encoding.
diff --git a/Misc/NEWS.d/next/Library/2022-11-10-11-51-39.gh-issue-99305.6LzQc3.rst b/Misc/NEWS.d/next/Library/2022-11-10-11-51-39.gh-issue-99305.6LzQc3.rst
new file mode 100644
index 0000000..32e18e5
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-11-10-11-51-39.gh-issue-99305.6LzQc3.rst
@@ -0,0 +1 @@
+Improve performance of :func:`secrets.token_hex`.