mirror of
https://github.com/TeamPGM/pyrogram.git
synced 2024-11-17 13:21:52 +00:00
Merge branch 'master' into docs
This commit is contained in:
commit
e9817400a3
@ -18,9 +18,16 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
|
import sys
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
import socks
|
try:
|
||||||
|
import socks
|
||||||
|
except ImportError:
|
||||||
|
sys.exit(
|
||||||
|
"PySocks is missing and Pyrogram can't run without. "
|
||||||
|
"Please install it using \"pip3 install pysocks\"."
|
||||||
|
)
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -16,52 +16,34 @@
|
|||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import logging
|
import sys
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import tgcrypto
|
import tgcrypto
|
||||||
except ImportError:
|
except ImportError:
|
||||||
log.warning(
|
sys.exit(
|
||||||
"TgCrypto is missing! "
|
"TgCrypto is missing and Pyrogram can't run without. "
|
||||||
"Pyrogram will work the same, but at a much slower speed. "
|
"Please install it using \"pip3 install tgcrypto\". "
|
||||||
"More info: https://docs.pyrogram.ml/resources/TgCrypto"
|
"More info: https://docs.pyrogram.ml/resources/TgCrypto"
|
||||||
)
|
)
|
||||||
is_fast = False
|
|
||||||
import pyaes
|
|
||||||
else:
|
|
||||||
log.info("Using TgCrypto")
|
|
||||||
is_fast = True
|
|
||||||
|
|
||||||
|
|
||||||
# TODO: Ugly IFs
|
# TODO: Ugly IFs
|
||||||
class AES:
|
class AES:
|
||||||
@classmethod
|
@classmethod
|
||||||
def ige_encrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes:
|
def ige_encrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes:
|
||||||
if is_fast:
|
return tgcrypto.ige_encrypt(data, key, iv)
|
||||||
return tgcrypto.ige_encrypt(data, key, iv)
|
|
||||||
else:
|
|
||||||
return cls.ige(data, key, iv, True)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def ige_decrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes:
|
def ige_decrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes:
|
||||||
if is_fast:
|
return tgcrypto.ige_decrypt(data, key, iv)
|
||||||
return tgcrypto.ige_decrypt(data, key, iv)
|
|
||||||
else:
|
|
||||||
return cls.ige(data, key, iv, False)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def ctr_decrypt(data: bytes, key: bytes, iv: bytes, offset: int) -> bytes:
|
def ctr_decrypt(data: bytes, key: bytes, iv: bytes, offset: int) -> bytes:
|
||||||
replace = int.to_bytes(offset // 16, 4, "big")
|
replace = int.to_bytes(offset // 16, 4, "big")
|
||||||
iv = iv[:-4] + replace
|
iv = iv[:-4] + replace
|
||||||
|
|
||||||
if is_fast:
|
return tgcrypto.ctr_decrypt(data, key, iv)
|
||||||
return tgcrypto.ctr_decrypt(data, key, iv)
|
|
||||||
else:
|
|
||||||
ctr = pyaes.AESModeOfOperationCTR(key)
|
|
||||||
ctr._counter._counter = list(iv)
|
|
||||||
return ctr.decrypt(data)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def xor(a: bytes, b: bytes) -> bytes:
|
def xor(a: bytes, b: bytes) -> bytes:
|
||||||
@ -70,23 +52,3 @@ class AES:
|
|||||||
len(a),
|
len(a),
|
||||||
"big",
|
"big",
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def ige(cls, data: bytes, key: bytes, iv: bytes, encrypt: bool) -> bytes:
|
|
||||||
cipher = pyaes.AES(key)
|
|
||||||
|
|
||||||
iv_1 = iv[:16]
|
|
||||||
iv_2 = iv[16:]
|
|
||||||
|
|
||||||
data = [data[i: i + 16] for i in range(0, len(data), 16)]
|
|
||||||
|
|
||||||
if encrypt:
|
|
||||||
for i, chunk in enumerate(data):
|
|
||||||
iv_1 = data[i] = cls.xor(cipher.encrypt(cls.xor(chunk, iv_1)), iv_2)
|
|
||||||
iv_2 = chunk
|
|
||||||
else:
|
|
||||||
for i, chunk in enumerate(data):
|
|
||||||
iv_2 = data[i] = cls.xor(cipher.decrypt(cls.xor(chunk, iv_2)), iv_1)
|
|
||||||
iv_1 = chunk
|
|
||||||
|
|
||||||
return b"".join(data)
|
|
||||||
|
2
setup.py
2
setup.py
@ -72,7 +72,7 @@ setup(
|
|||||||
"Topic :: Software Development :: Libraries :: Python Modules",
|
"Topic :: Software Development :: Libraries :: Python Modules",
|
||||||
"Topic :: Software Development :: Libraries :: Application Frameworks"
|
"Topic :: Software Development :: Libraries :: Application Frameworks"
|
||||||
],
|
],
|
||||||
keywords="telegram mtproto api client library python",
|
keywords="telegram chat messenger mtproto api client library python",
|
||||||
project_urls={
|
project_urls={
|
||||||
"Tracker": "https://github.com/pyrogram/pyrogram/issues",
|
"Tracker": "https://github.com/pyrogram/pyrogram/issues",
|
||||||
"Community": "https://t.me/PyrogramChat",
|
"Community": "https://t.me/PyrogramChat",
|
||||||
|
Loading…
Reference in New Issue
Block a user