Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55c96e8

Browse files
gh-99305: Speed up secrets.token_hex() ~2x (#99306)
simple code modernization. Co-authored-by: Pieter Eendebak <[email protected]>
1 parent f531b68 commit 55c96e8

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

Lib/secrets.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414

1515
import base64
16-
import binascii
1716

1817
from hmac import compare_digest
1918
from random import SystemRandom
@@ -56,7 +55,7 @@ def token_hex(nbytes=None):
5655
'f9bf78b9a18ce6d46a0cd2b0b86df9da'
5756
5857
"""
59-
return binascii.hexlify(token_bytes(nbytes)).decode('ascii')
58+
return token_bytes(nbytes).hex()
6059

6160
def token_urlsafe(nbytes=None):
6261
"""Return a random URL-safe text string, in Base64 encoding.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve performance of :func:`secrets.token_hex`.

0 commit comments

Comments
 (0)