3434except ImportError :
3535 CRYPTO = False
3636
37- from telegram import TelegramObject , TelegramError
37+ from telegram import TelegramObject
3838
3939
40- class TelegramDecryptionError ( TelegramError ):
40+ class _TelegramDecryptionError ( Exception ):
4141 pass
4242
4343
@@ -84,7 +84,7 @@ def decrypt(secret, hash, data):
8484 digest .update (data )
8585 data_hash = digest .finalize ()
8686 if data_hash != hash :
87- raise TelegramDecryptionError ("Hashes are not equal! {} != {}" .format (data_hash , hash ))
87+ raise _TelegramDecryptionError ("Hashes are not equal! {} != {}" .format (data_hash , hash ))
8888 return data [bord (data [0 ]):]
8989
9090
@@ -139,11 +139,15 @@ def de_json(cls, data, bot):
139139 if isinstance (data ['data' ], dict ):
140140 data ['data' ] = Credentials .de_json (data ['data' ], bot = bot )
141141 else :
142- data ['secret' ] = bot .private_key .decrypt (b64decode (data .get ('secret' )), OAEP (
143- mgf = MGF1 (algorithm = SHA1 ()),
144- algorithm = SHA1 (),
145- label = None
146- ))
142+ try :
143+ data ['secret' ] = bot .private_key .decrypt (b64decode (data .get ('secret' )), OAEP (
144+ mgf = MGF1 (algorithm = SHA1 ()),
145+ algorithm = SHA1 (),
146+ label = None
147+ ))
148+ except ValueError as e :
149+ raise _TelegramDecryptionError (e )
150+
147151 data ['data' ] = Credentials .de_json (decrypt_json (data .get ('secret' ),
148152 data .get ('hash' ),
149153 data .get ('data' )),
@@ -158,6 +162,7 @@ class Credentials(TelegramObject):
158162 secure_data (:class:`telegram.SecureData`): Credentials for encrypted data
159163 payload (:obj:`str`): Bot-specified payload
160164 """
165+
161166 def __init__ (self , secure_data , payload , bot = None , ** kwargs ):
162167 # Required
163168 self .secure_data = secure_data
@@ -202,6 +207,7 @@ class SecureData(TelegramObject):
202207 temporary_registration (:class:`telegram.SecureValue`, optional): Credentials for encrypted
203208 temporary registration
204209 """
210+
205211 def __init__ (self ,
206212 personal_details = None ,
207213 passport = None ,
@@ -315,6 +321,7 @@ def to_dict(self):
315321
316322class _CredentialsBase (TelegramObject ):
317323 """Base class for DataCredentials and FileCredentials."""
324+
318325 def __init__ (self , hash , secret , bot = None , ** kwargs ):
319326 self .hash = hash
320327 self .secret = secret
@@ -357,6 +364,7 @@ class DataCredentials(_CredentialsBase):
357364 hash (:obj:`str`): Checksum of encrypted data
358365 secret (:obj:`str`): Secret of encrypted data
359366 """
367+
360368 def __init__ (self , data_hash , secret , ** kwargs ):
361369 super (DataCredentials , self ).__init__ (data_hash , secret , ** kwargs )
362370
@@ -382,6 +390,7 @@ class FileCredentials(_CredentialsBase):
382390 hash (:obj:`str`): Checksum of encrypted file
383391 secret (:obj:`str`): Secret of encrypted file
384392 """
393+
385394 def __init__ (self , file_hash , secret , ** kwargs ):
386395 super (FileCredentials , self ).__init__ (file_hash , secret , ** kwargs )
387396
0 commit comments