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.5.tar.gz (34.9 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.5-cp313-cp313t-musllinux_1_2_aarch64.whl (52.4 kB view details)

Uploaded CPython 3.13tmusllinux: musl 1.2+ ARM64

tgcryptomax-0.0.5-cp313-cp313t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (53.0 kB view details)

Uploaded CPython 3.13tmanylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

tgcryptomax-0.0.5-cp313-cp313-musllinux_1_2_aarch64.whl (52.2 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ ARM64

tgcryptomax-0.0.5-cp313-cp313-macosx_11_0_arm64.whl (34.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

tgcryptomax-0.0.5-cp312-cp312-musllinux_1_2_aarch64.whl (52.2 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ ARM64

tgcryptomax-0.0.5-cp312-cp312-macosx_11_0_arm64.whl (34.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

tgcryptomax-0.0.5-cp311-cp311-macosx_11_0_arm64.whl (34.7 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

tgcryptomax-0.0.5-cp310-cp310-musllinux_1_2_aarch64.whl (51.8 kB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ ARM64

tgcryptomax-0.0.5-cp310-cp310-macosx_11_0_arm64.whl (34.7 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

tgcryptomax-0.0.5-cp39-cp39-musllinux_1_2_aarch64.whl (51.6 kB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ ARM64

tgcryptomax-0.0.5-cp39-cp39-macosx_11_0_arm64.whl (34.7 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: tgcryptomax-0.0.5.tar.gz
  • Upload date:
  • Size: 34.9 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.5.tar.gz
Algorithm Hash digest
SHA256 dcf26880e08d3661179c1ce520dbc24b3c02925b8c9f54b87453c5709411b044
MD5 6417b60bf98237ceb6c9f40c78d6b01e
BLAKE2b-256 3fb7e2aa78607f3f886d020ec90b7ec35a21c96034079d2f294330595fab2f69

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.5-cp313-cp313t-musllinux_1_2_aarch64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.5-cp313-cp313t-musllinux_1_2_aarch64.whl
  • Upload date:
  • Size: 52.4 kB
  • Tags: CPython 3.13t, musllinux: musl 1.2+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","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.5-cp313-cp313t-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 2f13018b2f9c677dac4ca5beab1526c143fe56931806e179f79b0ab1f8fd7f60
MD5 eed5795f8eb455576347a6f34e6f93bb
BLAKE2b-256 f53fce5ab7abbc11f08d7ccccbdd189c814505d278e4a844f188fbcf654b7bea

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.5-cp313-cp313t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.5-cp313-cp313t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 53.0 kB
  • Tags: CPython 3.13t, manylinux: glibc 2.17+ ARM64, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","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.5-cp313-cp313t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 54c91044a551d7244e7196355799b4cdfbfde92d16dfcf1d6b89a76964ae3dd8
MD5 c5d57fee715fb3074555aaa033de001d
BLAKE2b-256 a4fa9907575e2c91b4cbb068a928338a545f78bf220ebb91ee6a4af32789a90e

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.5-cp313-cp313-musllinux_1_2_aarch64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.5-cp313-cp313-musllinux_1_2_aarch64.whl
  • Upload date:
  • Size: 52.2 kB
  • Tags: CPython 3.13, musllinux: musl 1.2+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","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.5-cp313-cp313-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 44a48bb81fff65f025195dcb65d268ed57a0c65821bc930cba087fb9dff0fd47
MD5 637d06badec9b82f92714d3c63962de8
BLAKE2b-256 94fbfecfd51a5bd80e0b63223156a73ce2e2f6b03bb5e6037371cda53bd10590

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.5-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.5-cp313-cp313-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: CPython 3.13, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"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.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7c063257e3ae573995aea5e43d9e8a799490b1920fc9d164bda6b679dbb276d7
MD5 346bbb29313302c36330896c6d6eefa8
BLAKE2b-256 b125166b6ea4fffd7fef4fb0a1787a6042bf311a3c0db361b9a966412c878946

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.5-cp312-cp312-musllinux_1_2_aarch64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.5-cp312-cp312-musllinux_1_2_aarch64.whl
  • Upload date:
  • Size: 52.2 kB
  • Tags: CPython 3.12, musllinux: musl 1.2+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","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.5-cp312-cp312-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 8f17a438d9f8aa88f50572ed629050de49981dadf28ec7b6de7334cdae7f21c7
MD5 32e30cc72d8c3772ffca02d7d909acf5
BLAKE2b-256 e750708f2eff3a0be15f9689604189799e95fd39daa59db3e23c4ffb189b88b0

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.5-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.5-cp312-cp312-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: CPython 3.12, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"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.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b56aa017085502923721e8de9e82bafdd0ff2115e4768f0875c1d1b9938e72ee
MD5 f9ee1fb867aa454aa31db51c27c0355c
BLAKE2b-256 f1ba2024283480c182293412d6b2cfc8d0ebddc5c22d72bc2c574bfd80e8243a

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.5-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.5-cp311-cp311-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: CPython 3.11, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"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.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 95003167edd383c7bbd449e3421e8c14a9ffdcb29d84aca4ab65bfd20dc6806a
MD5 b5ae415cc2a9b92436586d7fb7072b4a
BLAKE2b-256 898f228e4f77a11a3f1687a8e89727c4e596f57cca172ca6ee376e70812e4308

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.5-cp310-cp310-musllinux_1_2_aarch64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.5-cp310-cp310-musllinux_1_2_aarch64.whl
  • Upload date:
  • Size: 51.8 kB
  • Tags: CPython 3.10, musllinux: musl 1.2+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","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.5-cp310-cp310-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 e784c460a7a9930c233f5c06eee1c659f40681fba9bf36fa1c35dc70da718c21
MD5 ac4ae70ad86f1cb9f369066ff1a7741b
BLAKE2b-256 56cba96c655be1d02720a755ff8606b517682e9bd9fd7c99e24d1e68892162e8

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.5-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.5-cp310-cp310-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: CPython 3.10, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"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.5-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cb5072a87f63a8fbec41bc651135cbe789c16492fc436bd49428a6738918215c
MD5 9cf4e5a1eaa4c8ce69a4723164091e4c
BLAKE2b-256 15a074d7568eee2c5e141d8aa1cbd692d92991e90fc8f1005b50c032ae768d5b

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.5-cp39-cp39-musllinux_1_2_aarch64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.5-cp39-cp39-musllinux_1_2_aarch64.whl
  • Upload date:
  • Size: 51.6 kB
  • Tags: CPython 3.9, musllinux: musl 1.2+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","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.5-cp39-cp39-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 7327e254eb561b45cc336053e296a42e3ce487fcdb628dc7e7295d006e77662c
MD5 341671de3f2c62dd99dfc5b1052f50f3
BLAKE2b-256 eabd6fd6d8483ac44062ebb6ee73129178615e83da6b3b023638e0ae2e1ecd92

See more details on using hashes here.

File details

Details for the file tgcryptomax-0.0.5-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

  • Download URL: tgcryptomax-0.0.5-cp39-cp39-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: CPython 3.9, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"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.5-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 88db593324c7ddb8680adbb488dbcbd148319aa45c02e4c5e37d3e326dd256be
MD5 a47c2cd524824f96f34f18c80d7e65bf
BLAKE2b-256 22ad10cb359da808efa8b7c719490bd0e35cc024b67ceffa7912b3e4c3942592

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