@@ -247,22 +247,21 @@ def __init__(self, hash_alg):
247
247
self .hash_alg = hash_alg
248
248
249
249
def prepare_key (self , key ):
250
- if isinstance (key , RSAPrivateKey ) or isinstance ( key , RSAPublicKey ):
250
+ if isinstance (key , ( RSAPrivateKey , RSAPublicKey ) ):
251
251
return key
252
252
253
- if isinstance (key , (bytes , str )):
254
- key = force_bytes (key )
255
-
256
- try :
257
- if key .startswith (b"ssh-rsa" ):
258
- key = load_ssh_public_key (key )
259
- else :
260
- key = load_pem_private_key (key , password = None )
261
- except ValueError :
262
- key = load_pem_public_key (key )
263
- else :
253
+ if not isinstance (key , (bytes , str )):
264
254
raise TypeError ("Expecting a PEM-formatted key." )
265
255
256
+ key = force_bytes (key )
257
+
258
+ try :
259
+ if key .startswith (b"ssh-rsa" ):
260
+ key = load_ssh_public_key (key )
261
+ else :
262
+ key = load_pem_private_key (key , password = None )
263
+ except ValueError :
264
+ key = load_pem_public_key (key )
266
265
return key
267
266
268
267
@staticmethod
@@ -399,28 +398,25 @@ def __init__(self, hash_alg):
399
398
self .hash_alg = hash_alg
400
399
401
400
def prepare_key (self , key ):
402
- if isinstance (key , EllipticCurvePrivateKey ) or isinstance (
403
- key , EllipticCurvePublicKey
404
- ):
401
+ if isinstance (key , (EllipticCurvePrivateKey , EllipticCurvePublicKey )):
405
402
return key
406
403
407
- if isinstance (key , (bytes , str )):
408
- key = force_bytes (key )
409
-
410
- # Attempt to load key. We don't know if it's
411
- # a Signing Key or a Verifying Key, so we try
412
- # the Verifying Key first.
413
- try :
414
- if key .startswith (b"ecdsa-sha2-" ):
415
- key = load_ssh_public_key (key )
416
- else :
417
- key = load_pem_public_key (key )
418
- except ValueError :
419
- key = load_pem_private_key (key , password = None )
420
-
421
- else :
404
+ if not isinstance (key , (bytes , str )):
422
405
raise TypeError ("Expecting a PEM-formatted key." )
423
406
407
+ key = force_bytes (key )
408
+
409
+ # Attempt to load key. We don't know if it's
410
+ # a Signing Key or a Verifying Key, so we try
411
+ # the Verifying Key first.
412
+ try :
413
+ if key .startswith (b"ecdsa-sha2-" ):
414
+ key = load_ssh_public_key (key )
415
+ else :
416
+ key = load_pem_public_key (key )
417
+ except ValueError :
418
+ key = load_pem_private_key (key , password = None )
419
+
424
420
return key
425
421
426
422
def sign (self , msg , key ):
0 commit comments