Added tgcrypto lib from pyrogram project

This commit is contained in:
BlackCatDevel0per 2022-03-23 20:48:52 +05:00
parent c6d3341cd5
commit 268e6643d0

View File

@ -2,6 +2,7 @@
AES IGE implementation in Python. AES IGE implementation in Python.
If available, cryptg will be used instead, otherwise 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 if available, libssl will be used instead, otherwise
the Python implementation will be used. the Python implementation will be used.
""" """
@ -12,19 +13,35 @@ from . import libssl
__log__ = logging.getLogger(__name__) __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: try:
import cryptg if not tgcrypto and not pyaesni:
__log__.info('cryptg detected, it will be used for encryption') import cryptg
__log__.info('cryptg detected, it will be used for encryption')
except ImportError: except ImportError:
cryptg = None 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: if libssl.encrypt_ige and libssl.decrypt_ige:
__log__.info('libssl detected, it will be used for encryption') __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: class AES:
""" """
@ -37,8 +54,12 @@ class AES:
Decrypts the given text in 16-bytes blocks by using the Decrypts the given text in 16-bytes blocks by using the
given key and 32-bytes initialization vector. 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) return cryptg.decrypt_ige(cipher_text, key, iv)
elif pyaesni:
return pyaesni.ige256_decrypt(cipher_text, key, iv)
if libssl.decrypt_ige: if libssl.decrypt_ige:
return libssl.decrypt_ige(cipher_text, key, iv) return libssl.decrypt_ige(cipher_text, key, iv)
@ -78,8 +99,12 @@ class AES:
if padding: if padding:
plain_text += os.urandom(16 - 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) return cryptg.encrypt_ige(plain_text, key, iv)
elif pyaesni:
return pyaesni.ige256_encrypt(plain_text, key, iv)
if libssl.encrypt_ige: if libssl.encrypt_ige:
return libssl.encrypt_ige(plain_text, key, iv) return libssl.encrypt_ige(plain_text, key, iv)