|
25 | 25 | from base64 import b64decode |
26 | 26 | from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union, no_type_check |
27 | 27 |
|
28 | | -from cryptography.hazmat.backends import default_backend |
29 | | -from cryptography.hazmat.primitives.asymmetric.padding import MGF1, OAEP |
30 | | -from cryptography.hazmat.primitives.ciphers import Cipher |
31 | | -from cryptography.hazmat.primitives.ciphers.algorithms import AES |
32 | | -from cryptography.hazmat.primitives.ciphers.modes import CBC |
33 | | -from cryptography.hazmat.primitives.hashes import SHA1, SHA256, SHA512, Hash |
| 28 | +try: |
| 29 | + from cryptography.hazmat.backends import default_backend |
| 30 | + from cryptography.hazmat.primitives.asymmetric.padding import MGF1, OAEP |
| 31 | + from cryptography.hazmat.primitives.ciphers import Cipher |
| 32 | + from cryptography.hazmat.primitives.ciphers.algorithms import AES |
| 33 | + from cryptography.hazmat.primitives.ciphers.modes import CBC |
| 34 | + from cryptography.hazmat.primitives.hashes import SHA1, SHA256, SHA512, Hash |
| 35 | + |
| 36 | + CRYPTO_INSTALLED = True |
| 37 | +except ImportError: |
| 38 | + default_backend = None |
| 39 | + MGF1, OAEP, Cipher, AES, CBC = (None, None, None, None, None) # type: ignore[misc] |
| 40 | + SHA1, SHA256, SHA512, Hash = (None, None, None, None) # type: ignore[misc] |
| 41 | + |
| 42 | + CRYPTO_INSTALLED = False |
34 | 43 |
|
35 | 44 | from telegram import TelegramError, TelegramObject |
36 | 45 | from telegram.utils.types import JSONDict |
@@ -74,6 +83,11 @@ def decrypt(secret, hash, data): |
74 | 83 | :obj:`bytes`: The decrypted data as bytes. |
75 | 84 |
|
76 | 85 | """ |
| 86 | + if not CRYPTO_INSTALLED: |
| 87 | + raise RuntimeError( |
| 88 | + 'To use Telegram Passports, PTB must be installed via `pip install ' |
| 89 | + 'python-telegram-bot[passport]`.' |
| 90 | + ) |
77 | 91 | # Make a SHA512 hash of secret + update |
78 | 92 | digest = Hash(SHA512(), backend=default_backend()) |
79 | 93 | digest.update(secret + hash) |
@@ -153,6 +167,11 @@ def decrypted_secret(self) -> str: |
153 | 167 | private/public key but can also suggest malformed/tampered data. |
154 | 168 | """ |
155 | 169 | if self._decrypted_secret is None: |
| 170 | + if not CRYPTO_INSTALLED: |
| 171 | + raise RuntimeError( |
| 172 | + 'To use Telegram Passports, PTB must be installed via `pip install ' |
| 173 | + 'python-telegram-bot[passport]`.' |
| 174 | + ) |
156 | 175 | # Try decrypting according to step 1 at |
157 | 176 | # https://core.telegram.org/passport#decrypting-data |
158 | 177 | # We make sure to base64 decode the secret first. |
|
0 commit comments