2018-01-28 00:44:38 +00:00
|
|
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
|
|
|
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
|
|
|
|
#
|
|
|
|
# This file is part of Pyrogram.
|
|
|
|
#
|
|
|
|
# Pyrogram is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Pyrogram is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
try:
|
|
|
|
import tgcrypto
|
2018-03-26 01:39:30 +00:00
|
|
|
except ImportError as e:
|
|
|
|
e.msg = (
|
2018-03-26 00:02:57 +00:00
|
|
|
"TgCrypto is missing and Pyrogram can't run without. "
|
|
|
|
"Please install it using \"pip3 install tgcrypto\". "
|
2018-02-17 14:01:09 +00:00
|
|
|
"More info: https://docs.pyrogram.ml/resources/TgCrypto"
|
|
|
|
)
|
2018-01-28 00:44:38 +00:00
|
|
|
|
2018-03-26 01:39:30 +00:00
|
|
|
raise e
|
|
|
|
|
2018-01-28 00:44:38 +00:00
|
|
|
|
|
|
|
class AES:
|
|
|
|
@classmethod
|
|
|
|
def ige_encrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes:
|
2018-03-26 00:02:57 +00:00
|
|
|
return tgcrypto.ige_encrypt(data, key, iv)
|
2018-01-28 00:44:38 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def ige_decrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes:
|
2018-03-26 00:02:57 +00:00
|
|
|
return tgcrypto.ige_decrypt(data, key, iv)
|
2018-01-28 00:44:38 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def ctr_decrypt(data: bytes, key: bytes, iv: bytes, offset: int) -> bytes:
|
2018-03-17 13:29:23 +00:00
|
|
|
replace = int.to_bytes(offset // 16, 4, "big")
|
2018-01-28 00:44:38 +00:00
|
|
|
iv = iv[:-4] + replace
|
|
|
|
|
2018-03-26 00:02:57 +00:00
|
|
|
return tgcrypto.ctr_decrypt(data, key, iv)
|
2018-01-28 00:44:38 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def xor(a: bytes, b: bytes) -> bytes:
|
|
|
|
return int.to_bytes(
|
|
|
|
int.from_bytes(a, "big") ^ int.from_bytes(b, "big"),
|
|
|
|
len(a),
|
|
|
|
"big",
|
|
|
|
)
|