Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tgcrypto & pyaesni for faster work #3778

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions optional-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
tgcrypto
cryptg
pyaesni
pysocks
python-socks[asyncio]
hachoir
Expand Down
5 changes: 3 additions & 2 deletions readthedocs/basic/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ The version number of the library should show in the output.
Optional Dependencies
=====================

If cryptg_ is installed, **the library will work a lot faster**, since
If tgcrypto_ or cryptg_ or pyaesni(intel aes-ni) is installed, **the library will work a lot faster**, since
encryption and decryption will be made in C instead of Python. If your
code deals with a lot of updates or you are downloading/uploading a lot
of files, you will notice a considerable speed-up (from a hundred kilobytes
Expand Down Expand Up @@ -83,11 +83,12 @@ manually.
apt update
apt install clang lib{jpeg-turbo,webp}-dev python{,-dev} zlib-dev
pip install -U --user setuptools
pip install -U --user telethon cryptg pillow
pip install -U --user telethon tgcrypto pillow

Thanks to `@bb010g`_ for writing down this nice list.


.. _tgcrypto: https://github.com/pyrogram/tgcrypto
.. _cryptg: https://github.com/cher-nov/cryptg
.. _pyaes: https://github.com/ricmoo/pyaes
.. _pillow: https://python-pillow.org
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def main(argv):
]),
install_requires=['pyaes', 'rsa'],
extras_require={
'cryptg': ['cryptg']
'tgcrypto': ['tgcrypto']
}
)

Expand Down
2 changes: 1 addition & 1 deletion telethon/_client/telegramclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -1676,7 +1676,7 @@ async def download_media(
Downloads the given media from a message object.

Note that if the download is too slow, you should consider installing
``cryptg`` (through ``pip install cryptg``) so that decrypting the
``tgcrypto`` (through ``pip install tgcrypto``) so that decrypting the
received data is done in C instead of Python (much faster).

See also `Message.download_media() <telethon.tl._custom.message.Message.download_media>`.
Expand Down
43 changes: 34 additions & 9 deletions telethon/_crypto/aes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
AES IGE implementation in Python.

If available, cryptg will be used instead, otherwise
If available, pyaesni(intel aes-ni for python) will be used instead, otherwise
if available, libssl will be used instead, otherwise
the Python implementation will be used.
"""
Expand All @@ -12,19 +13,35 @@


__log__ = logging.getLogger(__name__)

# tgcrypto
try:
import tgcrypto
__log__.info('tgcrypto detected, it will be used for encryption')
except ImportError:
tgcrypto = None
__log__.info('tgcrypto module not installed and libssl not found, '
'falling back to (slower) Python encryption')
# pyaesni (intel aes-ni)
try:
if not tgcrypto:
import pyaesni
__log__.info('pyaesni detected, it will be used for encryption')
except ImportError:
pyaesni = None
__log__.info('pyaesni module not installed and libssl not found, '
'falling back to (slower) Python encryption')

try:
import cryptg
__log__.info('cryptg detected, it will be used for encryption')
if not tgcrypto and not pyaesni:
import cryptg
__log__.info('cryptg detected, it will be used for encryption')
except ImportError:
cryptg = None
__log__.info('cryptg module not installed and libssl not found, '
'falling back to (slower) Python encryption')
if not tgcrypto and not pyaesni and not cryptg:
if libssl.encrypt_ige and libssl.decrypt_ige:
__log__.info('libssl detected, it will be used for encryption')
else:
__log__.info('cryptg module not installed and libssl not found, '
'falling back to (slower) Python encryption')


class AES:
"""
Expand All @@ -37,8 +54,12 @@ def decrypt_ige(cipher_text, key, iv):
Decrypts the given text in 16-bytes blocks by using the
given key and 32-bytes initialization vector.
"""
if cryptg:
if tgcrypto:
return tgcrypto.ige256_decrypt(cipher_text, key, iv)
elif cryptg:
return cryptg.decrypt_ige(cipher_text, key, iv)
elif pyaesni:
return pyaesni.ige256_decrypt(cipher_text, key, iv)
if libssl.decrypt_ige:
return libssl.decrypt_ige(cipher_text, key, iv)

Expand Down Expand Up @@ -78,8 +99,12 @@ def encrypt_ige(plain_text, key, iv):
if padding:
plain_text += os.urandom(16 - padding)

if cryptg:
if tgcrypto:
return tgcrypto.ige256_encrypt(plain_text, key, iv)
elif cryptg:
return cryptg.encrypt_ige(plain_text, key, iv)
elif pyaesni:
return pyaesni.ige256_encrypt(plain_text, key, iv)
if libssl.encrypt_ige:
return libssl.encrypt_ige(plain_text, key, iv)

Expand Down