Skip to content

Commit fea27ad

Browse files
committed
Add try/except for secrets back to scram.pyx
1 parent f1acdd8 commit fea27ad

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

asyncpg/protocol/scram.pyx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ import base64
99
import hashlib
1010
import hmac
1111
import re
12-
import secrets
1312
import stringprep
1413
import unicodedata
1514

15+
16+
# try to import the secrets library from Python 3.6+ for the
17+
# cryptographic token generator for generating nonces as part of SCRAM
18+
# Otherwise fall back on os.urandom
19+
try:
20+
from secrets import token_bytes as generate_token_bytes
21+
except ImportError:
22+
from os import urandom as generate_token_bytes
23+
1624
@cython.final
1725
cdef class SCRAMAuthentication:
1826
"""Contains the protocol for generating and a SCRAM hashed password.
@@ -190,7 +198,7 @@ cdef class SCRAMAuthentication:
190198
cdef:
191199
bytes token
192200

193-
token = secrets.generate_token_bytes(num_bytes)
201+
token = generate_token_bytes(num_bytes)
194202

195203
return base64.b64encode(token)
196204

0 commit comments

Comments
 (0)