gh-99305: Speed up secrets.token_hex() ~2x (#99306)

simple code modernization.

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
This commit is contained in:
NewUserHa 2022-11-12 06:45:24 +08:00 committed by GitHub
parent f531b6879b
commit 55c96e8053
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 2 deletions

View 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.

View File

@ -0,0 +1 @@
Improve performance of :func:`secrets.token_hex`.