Skip to content

Commit 4f48996

Browse files
committed
refactor: Hint typing, implicitly return default value, remove redundant except clause
1 parent 1ea07e2 commit 4f48996

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

pypinksign/pypinksign.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from hashlib import sha1
88
from os.path import expanduser
99
from sys import platform as _platform
10+
from typing import Optional, Callable, Dict
1011

1112
from cryptography import x509, __version__ as cryptography_version
1213
from cryptography.exceptions import InvalidSignature, UnsupportedAlgorithm
@@ -85,9 +86,9 @@ def __init__(self, pubkey_path: str = None, pubkey_data: bytes = None, prikey_pa
8586
self.p12_path = p12_path
8687
self.p12_data = p12_data
8788
self.pub_cert = None
88-
self.prikey: RSAPrivateKey = None
89-
self.pub_data: bytes = None
90-
self.pubkey: RSAPublicKey = None
89+
self.prikey: Optional[RSAPrivateKey] = None
90+
self.pub_data: Optional[bytes] = None
91+
self.pubkey: Optional[RSAPublicKey] = None
9192
self.rand_num = None
9293
if p12_path is not None:
9394
self.load_p12()
@@ -155,7 +156,7 @@ def load_prikey(self, prikey_path: str = None, prikey_data: bytes = None, prikey
155156
# check if correct K-PKI prikey file
156157
algorithm_type = der[0][0].asTuple()
157158

158-
private_key_decryption_key_functions = {
159+
private_key_decryption_key_functions: Dict[tuple, Callable] = {
159160
ID_SEED_CBC_WITH_SHA1: self.get_private_key_decryption_key_for_seed_cbc_with_sha1,
160161
ID_SEED_CBC: self.get_private_key_decryption_key_for_seed_cbc,
161162
ID_PBES2: self.get_private_key_decryption_key_for_pbes2,
@@ -214,7 +215,7 @@ def cn(self) -> str:
214215
return dn.rfc4514_string()[3:]
215216
return ''
216217

217-
def issuer(self) -> str:
218+
def issuer(self) -> Optional[str]:
218219
"""Get issuer value
219220
220221
p = PinkSign(pubkey_path="/some/path/signCert.der")
@@ -225,8 +226,9 @@ def issuer(self) -> str:
225226
for dn in self.pub_cert.issuer.rdns:
226227
if dn.rfc4514_string().startswith('O='):
227228
return dn.rfc4514_string()[2:]
229+
return None
228230

229-
def cert_class(self) -> str:
231+
def cert_class(self) -> Optional[str]:
230232
"""Get cert class
231233
232234
p = PinkSign(pubkey_path="/some/path/signCert.der")
@@ -237,8 +239,9 @@ def cert_class(self) -> str:
237239
for dn in self.pub_cert.issuer.rdns:
238240
if dn.rfc4514_string().startswith('CN='):
239241
return dn.rfc4514_string()[3:]
242+
return None
240243

241-
def cert_type_oid(self) -> str:
244+
def cert_type_oid(self) -> Optional[str]:
242245
"""Get cert type
243246
TODO: bad way to find value following oid. exception may occurred with certain certificate
244247
@@ -250,6 +253,7 @@ def cert_type_oid(self) -> str:
250253
for ext in self.pub_cert.extensions:
251254
if ext.oid.dotted_string == '2.5.29.32': #
252255
return ext.value[0].policy_identifier.dotted_string
256+
return None
253257

254258
def valid_date(self) -> (datetime, datetime):
255259
"""Get valid date range
@@ -296,8 +300,6 @@ def verify(self, signature: bytes, msg: bytes, algorithm=hashes.SHA256(), paddin
296300
return True
297301
except InvalidSignature:
298302
return False
299-
except Exception as e:
300-
raise e
301303

302304
def decrypt(self, msg, padding_=PKCS1v15()):
303305
"""Decrypt with private key - also used when signing.

0 commit comments

Comments
 (0)