]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-99305: Speed up secrets.token_hex() ~2x (#99306)
authorNewUserHa <32261870+NewUserHa@users.noreply.github.com>
Fri, 11 Nov 2022 22:45:24 +0000 (06:45 +0800)
committerGitHub <noreply@github.com>
Fri, 11 Nov 2022 22:45:24 +0000 (14:45 -0800)
simple code modernization.

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Lib/secrets.py
Misc/NEWS.d/next/Library/2022-11-10-11-51-39.gh-issue-99305.6LzQc3.rst [new file with mode: 0644]

index 900381a89f53ec44b5b0a7e8e4481fce2ab931ad..566a09b7311154a1814bacb69b9ac8bdf07641f9 100644 (file)
@@ -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 (file)
index 0000000..32e18e5
--- /dev/null
@@ -0,0 +1 @@
+Improve performance of :func:`secrets.token_hex`.