Skip to main content

Fast and Portable Cryptography Extension Library for Pyrogram

Project description

tgcryptomax

[!NOTE] The implementations of the algorithms presented in this repository are to be considered for educational purposes only.

Fast and Portable Cryptography Extension Library for Pyrogram

tgcryptomax is a Cryptography Library written in C as a Python extension. It is designed to be portable, fast, easy to install and use. tgcryptomax is intended for Pyrogram and implements the cryptographic algorithms Telegram requires, namely:

Requirements

  • Python 3.7 or higher.

Installation

$ pip3 install -U tgcryptomax

API

tgcryptomax API consists of these six methods:

def ige256_encrypt(data: bytes, key: bytes, iv: bytes) -> bytes: ...
def ige256_decrypt(data: bytes, key: bytes, iv: bytes) -> bytes: ...

def ctr256_encrypt(data: bytes, key: bytes, iv: bytes, state: bytes) -> bytes: ...
def ctr256_decrypt(data: bytes, key: bytes, iv: bytes, state: bytes) -> bytes: ...

def cbc256_encrypt(data: bytes, key: bytes, iv: bytes) -> bytes: ...
def cbc256_decrypt(data: bytes, key: bytes, iv: bytes) -> bytes: ...

Usage

IGE Mode

Note: Data must be padded to match a multiple of the block size (16 bytes).

import os

import tgcryptomax

data = os.urandom(10 * 1024 * 1024 + 7)  # 10 MB of random data + 7 bytes to show padding
key = os.urandom(32)  # Random Key
iv = os.urandom(32)  # Random IV

# Pad with zeroes: -7 % 16 = 9
data += bytes(-len(data) % 16)

ige_encrypted = tgcryptomax.ige256_encrypt(data, key, iv)
ige_decrypted = tgcryptomax.ige256_decrypt(ige_encrypted, key, iv)

print(data == ige_decrypted)  # True

CTR Mode (single chunk)

import os

import tgcryptomax

data = os.urandom(10 * 1024 * 1024)  # 10 MB of random data

key = os.urandom(32)  # Random Key

enc_iv = bytearray(os.urandom(16))  # Random IV
dec_iv = enc_iv.copy()  # Keep a copy for decryption

ctr_encrypted = tgcryptomax.ctr256_encrypt(data, key, enc_iv, bytes(1))
ctr_decrypted = tgcryptomax.ctr256_decrypt(ctr_encrypted, key, dec_iv, bytes(1))

print(data == ctr_decrypted)  # True

CTR Mode (stream)

import os
from io import BytesIO

import tgcryptomax

data = BytesIO(os.urandom(10 * 1024 * 1024))  # 10 MB of random data

key = os.urandom(32)  # Random Key

enc_iv = bytearray(os.urandom(16))  # Random IV
dec_iv = enc_iv.copy()  # Keep a copy for decryption

enc_state = bytes(1)  # Encryption state, starts from 0
dec_state = bytes(1)  # Decryption state, starts from 0

encrypted_data = BytesIO()  # Encrypted data buffer
decrypted_data = BytesIO()  # Decrypted data buffer

while True:
    chunk = data.read(1024)

    if not chunk:
        break

    # Write 1K encrypted bytes into the encrypted data buffer
    encrypted_data.write(tgcryptomax.ctr256_encrypt(chunk, key, enc_iv, enc_state))

# Reset position. We need to read it now
encrypted_data.seek(0)

while True:
    chunk = encrypted_data.read(1024)

    if not chunk:
        break

    # Write 1K decrypted bytes into the decrypted data buffer
    decrypted_data.write(tgcryptomax.ctr256_decrypt(chunk, key, dec_iv, dec_state))

print(data.getvalue() == decrypted_data.getvalue())  # True

CBC Mode

Note: Data must be padded to match a multiple of the block size (16 bytes).

import os

import tgcryptomax

data = os.urandom(10 * 1024 * 1024 + 7)  # 10 MB of random data + 7 bytes to show padding
key = os.urandom(32)  # Random Key

enc_iv = bytearray(os.urandom(16))  # Random IV
dec_iv = enc_iv.copy()  # Keep a copy for decryption

# Pad with zeroes: -7 % 16 = 9
data += bytes(-len(data) % 16)

cbc_encrypted = tgcryptomax.cbc256_encrypt(data, key, enc_iv)
cbc_decrypted = tgcryptomax.cbc256_decrypt(cbc_encrypted, key, dec_iv)

print(data == cbc_decrypted)  # True

Testing

  1. Clone this repository: git clone https://github.com/5hojib/tgcryptomax.
  2. Enter the directory: cd tgcryptomax.
  3. Install tox: pip3 install tox
  4. Run tests: tox.

License

LGPLv3+ © 2017-present Dan

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tgcryptomax-0.0.1.tar.gz (35.2 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tgcryptomax-0.0.1-cp314-cp314-musllinux_1_2_x86_64.whl (51.2 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

tgcryptomax-0.0.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (51.1 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

tgcryptomax-0.0.1-cp313-cp313t-musllinux_1_2_x86_64.whl (51.3 kB view details)

Uploaded CPython 3.13tmusllinux: musl 1.2+ x86-64

tgcryptomax-0.0.1-cp312-cp312-musllinux_1_2_x86_64.whl (51.0 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

tgcryptomax-0.0.1-cp311-cp311-musllinux_1_2_x86_64.whl (51.4 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

tgcryptomax-0.0.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (50.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file tgcryptomax-0.0.1.tar.gz.

File metadata

  • Download URL: tgcryptomax-0.0.1.tar.gz
  • Upload date:
  • Size: 35.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for tgcryptomax-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d4952fca218dd69acce595dea6a3e7d6c5d2254880eef8b2a461429e25060219
MD5 adbeccc6213f90511412a126add5b570
BLAKE2b-256 61920f10dceae2389ca61eb9632b8ca5bf3f056b3bf27c8296b4172ea69c781e

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.1-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.1-cp314-cp314-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 51.2 kB
  • Tags: CPython 3.14, musllinux: musl 1.2+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for tgcryptomax-0.0.1-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 49a60ba5d77b07e88116f5905797707ee87d1cda9e7fe8f411ea9f318b8778cc
MD5 55e20291572ad69f8a8597c4db5221ed
BLAKE2b-256 845528ab89d3d55ed9632af8d3b2fb530cc79aa134f319dc01c2f21eb9876d2c

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 51.1 kB
  • Tags: CPython 3.14, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for tgcryptomax-0.0.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fa5e5caf3241142362ea18bd6454b5e8afbae88347b0635fa55f52a205708b3d
MD5 da79a48440f7f344a8eb6c3a9bc9ce46
BLAKE2b-256 17e95c228284ba263d83d7a920aa9fe1fa3c9b4a06cc610fe2e8a49e494d7826

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.1-cp313-cp313t-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.1-cp313-cp313t-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 51.3 kB
  • Tags: CPython 3.13t, musllinux: musl 1.2+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for tgcryptomax-0.0.1-cp313-cp313t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 eba0998ee1fcf149cf747d6f75f5c7e1ca1ee412b361512524db4725150342de
MD5 ce2f24c1a98b2cacabd5acdcbecd0ec2
BLAKE2b-256 62a1526dd6e5b19de0705a3eaa25d5e7eb31890a0739ff6303912dfb3cbb5dfd

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.1-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.1-cp312-cp312-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 51.0 kB
  • Tags: CPython 3.12, musllinux: musl 1.2+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for tgcryptomax-0.0.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e4df7ca1580fc9347a2133c3d69e8ad4f521a4bd21191d79cc459995074dbc89
MD5 7d0826120069387772848fc7a0b0604c
BLAKE2b-256 6b8ac835c1878525c667cecd0aff42cf90350b5389a9a66dd96ffdfdfb1b92d5

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.1-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.1-cp311-cp311-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 51.4 kB
  • Tags: CPython 3.11, musllinux: musl 1.2+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for tgcryptomax-0.0.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 fd946de569deb9bf7fb3e368935c9a882473f5dcabc7cf06156e99e7f72420ba
MD5 c89a952b2c5202117bf385a0343fd656
BLAKE2b-256 8291e6775e5d819fb1be8391409dd7a7ab921bad3eed6562c0987bcf15ec8260

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 50.5 kB
  • Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for tgcryptomax-0.0.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d43d47290d0014cf6913315ef78fd7e08b48281d2debd16733f46d68d5443c71
MD5 baee5632d8be1a778d58a9f4e2f61bc3
BLAKE2b-256 22bdc55228344b846f7214d11183b812bacd922c05adf29bd98a3d4039ddbd1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page