From d4a387992fc0a233da3a23f222b36abd4bf3266c Mon Sep 17 00:00:00 2001 From: BlackCatDevel0per Date: Wed, 23 Mar 2022 01:26:19 +0500 Subject: [PATCH 1/7] Add pyaesni support --- telethon/_crypto/aes.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/telethon/_crypto/aes.py b/telethon/_crypto/aes.py index 3cfcc1af3..e3c93ac5c 100644 --- a/telethon/_crypto/aes.py +++ b/telethon/_crypto/aes.py @@ -2,6 +2,7 @@ AES IGE implementation in Python. If available, cryptg will be used instead, otherwise +If available, pyaesni will be used instead, otherwise if available, libssl will be used instead, otherwise the Python implementation will be used. """ @@ -13,10 +14,21 @@ __log__ = logging.getLogger(__name__) +try: + import pyaesni + __log__.info('pyaesni detected, it will be used for encryption') +except ImportError: + pyaesni = None + if libssl.encrypt_ige and libssl.decrypt_ige: + __log__.info('libssl detected, it will be used for encryption') + else: + __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 pyaesni: + import cryptg + __log__.info('cryptg detected, it will be used for encryption') except ImportError: cryptg = None if libssl.encrypt_ige and libssl.decrypt_ige: @@ -39,6 +51,8 @@ def decrypt_ige(cipher_text, key, iv): """ if 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) @@ -80,6 +94,8 @@ def encrypt_ige(plain_text, key, iv): if 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) From fe36ba0b9a9c27c8432abef839e2c8046c25e2c6 Mon Sep 17 00:00:00 2001 From: BlackCatDevel0per Date: Wed, 23 Mar 2022 19:55:40 +0500 Subject: [PATCH 2/7] Added tgcrypto lib from pyrogram project --- telethon/_crypto/aes.py | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/telethon/_crypto/aes.py b/telethon/_crypto/aes.py index e3c93ac5c..61e482b1c 100644 --- a/telethon/_crypto/aes.py +++ b/telethon/_crypto/aes.py @@ -2,7 +2,7 @@ AES IGE implementation in Python. If available, cryptg will be used instead, otherwise -If available, pyaesni 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. """ @@ -13,30 +13,35 @@ __log__ = logging.getLogger(__name__) - +# tgcrypto try: - import pyaesni + import tgcrypto __log__.info('pyaesni 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 - if libssl.encrypt_ige and libssl.decrypt_ige: - __log__.info('libssl detected, it will be used for encryption') - else: - __log__.info('pyaesni module not installed and libssl not found, ' - 'falling back to (slower) Python encryption') + __log__.info('pyaesni module not installed and libssl not found, ' + 'falling back to (slower) Python encryption') try: - if not pyaesni: + 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: """ @@ -49,7 +54,9 @@ 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) @@ -92,7 +99,9 @@ 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) From c6d3341cd51cadb3a025a83ea293b15fbbb51d70 Mon Sep 17 00:00:00 2001 From: BlackCatDevel0per Date: Wed, 23 Mar 2022 20:28:44 +0500 Subject: [PATCH 3/7] Rolled back --- telethon/_crypto/aes.py | 43 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 34 deletions(-) diff --git a/telethon/_crypto/aes.py b/telethon/_crypto/aes.py index 61e482b1c..3cfcc1af3 100644 --- a/telethon/_crypto/aes.py +++ b/telethon/_crypto/aes.py @@ -2,7 +2,6 @@ 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. """ @@ -13,35 +12,19 @@ __log__ = logging.getLogger(__name__) -# tgcrypto -try: - import tgcrypto - __log__.info('pyaesni 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: - if not tgcrypto and not pyaesni: - import cryptg - __log__.info('cryptg detected, it will be used for encryption') + 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: """ @@ -54,12 +37,8 @@ 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 tgcrypto: - return tgcrypto.ige256_decrypt(cipher_text, key, iv) - elif cryptg: + if 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) @@ -99,12 +78,8 @@ def encrypt_ige(plain_text, key, iv): if padding: plain_text += os.urandom(16 - padding) - if tgcrypto: - return tgcrypto.ige256_encrypt(plain_text, key, iv) - elif cryptg: + if 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) From 268e6643d07e4fa429726cc350ea4a6ce91ed991 Mon Sep 17 00:00:00 2001 From: BlackCatDevel0per Date: Wed, 23 Mar 2022 20:48:52 +0500 Subject: [PATCH 4/7] Added tgcrypto lib from pyrogram project --- telethon/_crypto/aes.py | 43 ++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/telethon/_crypto/aes.py b/telethon/_crypto/aes.py index 3cfcc1af3..61e482b1c 100644 --- a/telethon/_crypto/aes.py +++ b/telethon/_crypto/aes.py @@ -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. """ @@ -12,19 +13,35 @@ __log__ = logging.getLogger(__name__) - +# tgcrypto +try: + import tgcrypto + __log__.info('pyaesni 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: """ @@ -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) @@ -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) From d169d352388d136e73e38a6af66099bec40ba833 Mon Sep 17 00:00:00 2001 From: BlackCatDevel0per Date: Wed, 23 Mar 2022 21:57:23 +0500 Subject: [PATCH 5/7] Added tgcrypto lib from pyrogram project --- telethon/_crypto/aes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telethon/_crypto/aes.py b/telethon/_crypto/aes.py index 61e482b1c..11e41c8ac 100644 --- a/telethon/_crypto/aes.py +++ b/telethon/_crypto/aes.py @@ -16,7 +16,7 @@ # tgcrypto try: import tgcrypto - __log__.info('pyaesni detected, it will be used for encryption') + __log__.info('tgcrypto detected, it will be used for encryption') except ImportError: tgcrypto = None __log__.info('tgcrypto module not installed and libssl not found, ' From 872b6a723ad07bce731343fd9943ef758effb8d9 Mon Sep 17 00:00:00 2001 From: BlackCatDevel0per Date: Wed, 23 Mar 2022 22:06:27 +0500 Subject: [PATCH 6/7] Added tgcrypto lib from pyrogram project --- optional-requirements.txt | 2 ++ readthedocs/basic/installation.rst | 5 +++-- telethon/_client/telegramclient.py | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/optional-requirements.txt b/optional-requirements.txt index 30326da9a..34b50d436 100644 --- a/optional-requirements.txt +++ b/optional-requirements.txt @@ -1,4 +1,6 @@ +tgcrypto cryptg +pyaesni pysocks python-socks[asyncio] hachoir diff --git a/readthedocs/basic/installation.rst b/readthedocs/basic/installation.rst index 2f8fa8abe..a4fae888f 100644 --- a/readthedocs/basic/installation.rst +++ b/readthedocs/basic/installation.rst @@ -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 @@ -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 diff --git a/telethon/_client/telegramclient.py b/telethon/_client/telegramclient.py index 3d32dd3bc..af2e7dbfc 100644 --- a/telethon/_client/telegramclient.py +++ b/telethon/_client/telegramclient.py @@ -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() `. From b0545fb8e7bc4d96f975c7527b0b1ba4d619779d Mon Sep 17 00:00:00 2001 From: BlackCatDevel0per Date: Wed, 23 Mar 2022 22:08:59 +0500 Subject: [PATCH 7/7] Added tgcrypto lib from pyrogram project --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 373fc90c5..9bc034d30 100755 --- a/setup.py +++ b/setup.py @@ -234,7 +234,7 @@ def main(argv): ]), install_requires=['pyaes', 'rsa'], extras_require={ - 'cryptg': ['cryptg'] + 'tgcrypto': ['tgcrypto'] } )