1616# You should have received a copy of the GNU Lesser General Public License
1717# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818
19- import logging
20-
21- log = logging .getLogger (__name__ )
19+ import sys
2220
2321try :
2422 import tgcrypto
2523except ImportError :
26- log . warning (
27- "TgCrypto is missing! "
28- "Pyrogram will work the same, but at a much slower speed . "
24+ sys . exit (
25+ "TgCrypto is missing and Pyrogram can't run without. "
26+ "Please install it using \" pip3 install tgcrypto \" . "
2927 "More info: https://docs.pyrogram.ml/resources/TgCrypto"
3028 )
31- is_fast = False
32- import pyaes
33- else :
34- log .info ("Using TgCrypto" )
35- is_fast = True
3629
3730
3831# TODO: Ugly IFs
3932class AES :
4033 @classmethod
4134 def ige_encrypt (cls , data : bytes , key : bytes , iv : bytes ) -> bytes :
42- if is_fast :
43- return tgcrypto .ige_encrypt (data , key , iv )
44- else :
45- return cls .ige (data , key , iv , True )
35+ return tgcrypto .ige_encrypt (data , key , iv )
4636
4737 @classmethod
4838 def ige_decrypt (cls , data : bytes , key : bytes , iv : bytes ) -> bytes :
49- if is_fast :
50- return tgcrypto .ige_decrypt (data , key , iv )
51- else :
52- return cls .ige (data , key , iv , False )
39+ return tgcrypto .ige_decrypt (data , key , iv )
5340
5441 @staticmethod
5542 def ctr_decrypt (data : bytes , key : bytes , iv : bytes , offset : int ) -> bytes :
5643 replace = int .to_bytes (offset // 16 , 4 , "big" )
5744 iv = iv [:- 4 ] + replace
5845
59- if is_fast :
60- return tgcrypto .ctr_decrypt (data , key , iv )
61- else :
62- ctr = pyaes .AESModeOfOperationCTR (key )
63- ctr ._counter ._counter = list (iv )
64- return ctr .decrypt (data )
46+ return tgcrypto .ctr_decrypt (data , key , iv )
6547
6648 @staticmethod
6749 def xor (a : bytes , b : bytes ) -> bytes :
@@ -70,23 +52,3 @@ def xor(a: bytes, b: bytes) -> bytes:
7052 len (a ),
7153 "big" ,
7254 )
73-
74- @classmethod
75- def ige (cls , data : bytes , key : bytes , iv : bytes , encrypt : bool ) -> bytes :
76- cipher = pyaes .AES (key )
77-
78- iv_1 = iv [:16 ]
79- iv_2 = iv [16 :]
80-
81- data = [data [i : i + 16 ] for i in range (0 , len (data ), 16 )]
82-
83- if encrypt :
84- for i , chunk in enumerate (data ):
85- iv_1 = data [i ] = cls .xor (cipher .encrypt (cls .xor (chunk , iv_1 )), iv_2 )
86- iv_2 = chunk
87- else :
88- for i , chunk in enumerate (data ):
89- iv_2 = data [i ] = cls .xor (cipher .decrypt (cls .xor (chunk , iv_2 )), iv_1 )
90- iv_1 = chunk
91-
92- return b"" .join (data )
0 commit comments