9
9
from cryptography .hazmat .backends import default_backend
10
10
from cryptography .hazmat .primitives import serialization , hashes
11
11
from cryptography .hazmat .primitives .asymmetric import padding
12
+
12
13
_have_cryptography = True
13
14
except ImportError :
14
15
_have_cryptography = False
22
23
23
24
DEBUG = False
24
25
SCRAMBLE_LENGTH = 20
25
- sha1_new = partial (hashlib .new , ' sha1' )
26
+ sha1_new = partial (hashlib .new , " sha1" )
26
27
27
28
28
29
# mysql_native_password
32
33
def scramble_native_password (password , message ):
33
34
"""Scramble used for mysql_native_password"""
34
35
if not password :
35
- return b''
36
+ return b""
36
37
37
38
stage1 = sha1_new (password ).digest ()
38
39
stage2 = sha1_new (stage1 ).digest ()
@@ -59,7 +60,6 @@ def _my_crypt(message1, message2):
59
60
60
61
61
62
class RandStruct_323 :
62
-
63
63
def __init__ (self , seed1 , seed2 ):
64
64
self .max_value = 0x3FFFFFFF
65
65
self .seed1 = seed1 % self .max_value
@@ -73,8 +73,10 @@ def my_rnd(self):
73
73
74
74
def scramble_old_password (password , message ):
75
75
"""Scramble for old_password"""
76
- warnings .warn ("old password (for MySQL <4.1) is used. Upgrade your password with newer auth method.\n "
77
- "old password support will be removed in future PyMySQL version" )
76
+ warnings .warn (
77
+ "old password (for MySQL <4.1) is used. Upgrade your password with newer auth method.\n "
78
+ "old password support will be removed in future PyMySQL version"
79
+ )
78
80
hash_pass = _hash_password_323 (password )
79
81
hash_message = _hash_password_323 (message [:SCRAMBLE_LENGTH_323 ])
80
82
hash_pass_n = struct .unpack (">LL" , hash_pass )
@@ -100,7 +102,7 @@ def _hash_password_323(password):
100
102
nr2 = 0x12345671
101
103
102
104
# x in py3 is numbers, p27 is chars
103
- for c in [byte2int (x ) for x in password if x not in (' ' , ' \t ' , 32 , 9 )]:
105
+ for c in [byte2int (x ) for x in password if x not in (" " , " \t " , 32 , 9 )]:
104
106
nr ^= (((nr & 63 ) + add ) * c ) + (nr << 8 ) & 0xFFFFFFFF
105
107
nr2 = (nr2 + ((nr2 << 8 ) ^ nr )) & 0xFFFFFFFF
106
108
add = (add + c ) & 0xFFFFFFFF
@@ -120,9 +122,12 @@ def _init_nacl():
120
122
global _nacl_bindings
121
123
try :
122
124
from nacl import bindings
125
+
123
126
_nacl_bindings = bindings
124
127
except ImportError :
125
- raise RuntimeError ("'pynacl' package is required for ed25519_password auth method" )
128
+ raise RuntimeError (
129
+ "'pynacl' package is required for ed25519_password auth method"
130
+ )
126
131
127
132
128
133
def _scalar_clamp (s32 ):
@@ -185,7 +190,7 @@ def _xor_password(password, salt):
185
190
# See https://github.com/mysql/mysql-server/blob/7d10c82196c8e45554f27c00681474a9fb86d137/sql/auth/sha2_password.cc#L939-L945
186
191
salt = salt [:SCRAMBLE_LENGTH ]
187
192
password_bytes = bytearray (password )
188
- #salt = bytearray(salt) # for PY2 compat.
193
+ # salt = bytearray(salt) # for PY2 compat.
189
194
salt_len = len (salt )
190
195
for i in range (len (password_bytes )):
191
196
password_bytes [i ] ^= salt [i % salt_len ]
@@ -198,8 +203,10 @@ def sha2_rsa_encrypt(password, salt, public_key):
198
203
Used for sha256_password and caching_sha2_password.
199
204
"""
200
205
if not _have_cryptography :
201
- raise RuntimeError ("'cryptography' package is required for sha256_password or caching_sha2_password auth methods" )
202
- message = _xor_password (password + b'\0 ' , salt )
206
+ raise RuntimeError (
207
+ "'cryptography' package is required for sha256_password or caching_sha2_password auth methods"
208
+ )
209
+ message = _xor_password (password + b"\0 " , salt )
203
210
rsa_key = serialization .load_pem_public_key (public_key , default_backend ())
204
211
return rsa_key .encrypt (
205
212
message ,
@@ -215,7 +222,7 @@ def sha256_password_auth(conn, pkt):
215
222
if conn ._secure :
216
223
if DEBUG :
217
224
print ("sha256: Sending plain password" )
218
- data = conn .password + b' \0 '
225
+ data = conn .password + b" \0 "
219
226
return _roundtrip (conn , data )
220
227
221
228
if pkt .is_auth_switch_request ():
@@ -224,20 +231,20 @@ def sha256_password_auth(conn, pkt):
224
231
# Request server public key
225
232
if DEBUG :
226
233
print ("sha256: Requesting server public key" )
227
- pkt = _roundtrip (conn , b' \1 ' )
234
+ pkt = _roundtrip (conn , b" \1 " )
228
235
229
236
if pkt .is_extra_auth_data ():
230
237
conn .server_public_key = pkt ._data [1 :]
231
238
if DEBUG :
232
- print ("Received public key:\n " , conn .server_public_key .decode (' ascii' ))
239
+ print ("Received public key:\n " , conn .server_public_key .decode (" ascii" ))
233
240
234
241
if conn .password :
235
242
if not conn .server_public_key :
236
243
raise OperationalError ("Couldn't receive server's public key" )
237
244
238
245
data = sha2_rsa_encrypt (conn .password , conn .salt , conn .server_public_key )
239
246
else :
240
- data = b''
247
+ data = b""
241
248
242
249
return _roundtrip (conn , data )
243
250
@@ -249,7 +256,7 @@ def scramble_caching_sha2(password, nonce):
249
256
XOR(SHA256(password), SHA256(SHA256(SHA256(password)), nonce))
250
257
"""
251
258
if not password :
252
- return b''
259
+ return b""
253
260
254
261
p1 = hashlib .sha256 (password ).digest ()
255
262
p2 = hashlib .sha256 (p1 ).digest ()
@@ -265,7 +272,7 @@ def scramble_caching_sha2(password, nonce):
265
272
def caching_sha2_password_auth (conn , pkt ):
266
273
# No password fast path
267
274
if not conn .password :
268
- return _roundtrip (conn , b'' )
275
+ return _roundtrip (conn , b"" )
269
276
270
277
if pkt .is_auth_switch_request ():
271
278
# Try from fast auth
@@ -305,18 +312,18 @@ def caching_sha2_password_auth(conn, pkt):
305
312
if conn ._secure :
306
313
if DEBUG :
307
314
print ("caching sha2: Sending plain password via secure connection" )
308
- return _roundtrip (conn , conn .password + b' \0 ' )
315
+ return _roundtrip (conn , conn .password + b" \0 " )
309
316
310
317
if not conn .server_public_key :
311
- pkt = _roundtrip (conn , b' \x02 ' ) # Request public key
318
+ pkt = _roundtrip (conn , b" \x02 " ) # Request public key
312
319
if not pkt .is_extra_auth_data ():
313
320
raise OperationalError (
314
321
"caching sha2: Unknown packet for public key: %s" % pkt ._data [:1 ]
315
322
)
316
323
317
324
conn .server_public_key = pkt ._data [1 :]
318
325
if DEBUG :
319
- print (conn .server_public_key .decode (' ascii' ))
326
+ print (conn .server_public_key .decode (" ascii" ))
320
327
321
328
data = sha2_rsa_encrypt (conn .password , conn .salt , conn .server_public_key )
322
329
pkt = _roundtrip (conn , data )
0 commit comments