Skip to main content

Fast and Portable Cryptography Extension Library for Pyrogram, Rust rewrite of TgCrypto

Project description

TgCryptoX

Fast and Portable Cryptography Extension Library for Pyrogram.

Provides AES-256 encryption/decryption in IGE, CTR, and CBC modes, implemented in Rust with PyO3 bindings.

Installation

pip install TgCryptoX

Usage

import tgcrypto
import os

# AES-256-IGE
key = os.urandom(32)
iv = os.urandom(32)
data = os.urandom(64)

encrypted = tgcrypto.ige256_encrypt(data, key, iv)
decrypted = tgcrypto.ige256_decrypt(encrypted, key, iv)

# AES-256-CTR
key = os.urandom(32)
iv = bytearray(os.urandom(16))
state = bytearray(b"\x00")
data = os.urandom(100)

encrypted = tgcrypto.ctr256_encrypt(data, key, iv, state)

# AES-256-CBC
key = os.urandom(32)
iv = bytearray(os.urandom(16))
data = os.urandom(64)

encrypted = tgcrypto.cbc256_encrypt(data, key, iv)

Benchmark

Tested on Apple M-series (aarch64), Python 3.14, with ARMv8 hardware AES enabled.

Size Mode TgCrypto (C) TgCryptoX (Rust)
16B IGE Encrypt 409ns 418ns
256B IGE Encrypt 658ns 604ns
4KB IGE Encrypt 4.7us 4.8us
64KB IGE Encrypt 72.0us 71.8us
1MB IGE Encrypt 1.12ms 1.11ms
1MB IGE Decrypt 1.09ms 1.09ms
1MB CBC Encrypt 1.33ms 1.34ms
1MB CBC Decrypt 171.8us 162.8us
1MB CTR 1.28ms 1.37ms

Performance is on par with the original C implementation.

Why Rust?

TgCrypto (C) TgCryptoX (Rust)
AES core T-table lookup, vulnerable to cache-timing side-channel attacks RustCrypto aes crate: auto-detected hardware AES (AES-NI / ARMv8) + constant-time fixsliced software fallback, side-channel resistant
Memory safety Manual malloc/free, risk of leaks and buffer overflows Compile-time memory safety guaranteed by Rust's ownership system, no use-after-free or buffer overflows
Python bindings Hand-written Python C API (~244 lines), error-prone Auto-generated via PyO3 macros (~120 lines), type-safe
GIL release Manual Py_BEGIN/END_ALLOW_THREADS Managed by py.detach()
Build system setuptools + C compiler configuration Single-command build with maturin, no C toolchain needed
Cross-platform wheels Complex cibuildwheel configuration required Native multi-platform support via maturin
Code size ~900 lines C + ~80 lines setup.py ~250 lines Rust + ~30 lines TOML

Acknowledgements

This project is a Rust rewrite of TgCrypto by Pyrogram. Thanks to the original project for the API design, test vectors, and inspiration.

License

LGPL-3.0-or-later

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

tgcryptox-1.0.0.tar.gz (37.9 kB view details)

Uploaded Source

Built Distributions

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

tgcryptox-1.0.0-cp312-cp312-win_amd64.whl (117.8 kB view details)

Uploaded CPython 3.12Windows x86-64

tgcryptox-1.0.0-cp312-cp312-manylinux_2_34_x86_64.whl (221.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

tgcryptox-1.0.0-cp312-cp312-macosx_11_0_arm64.whl (187.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file tgcryptox-1.0.0.tar.gz.

File metadata

  • Download URL: tgcryptox-1.0.0.tar.gz
  • Upload date:
  • Size: 37.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tgcryptox-1.0.0.tar.gz
Algorithm Hash digest
SHA256 55c20e2e0ec3783c826bdcde34af90b485666fa5198dd480305b3b468153e915
MD5 256cfa859d4b8cd2035b9b58df41a611
BLAKE2b-256 d033ca6477a2bb5144f8ea986a0fb98af0d6548b81d2f01221ce84ff24f61c05

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcryptox-1.0.0.tar.gz:

Publisher: ci.yml on SychO3/tgcryptoX

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcryptox-1.0.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: tgcryptox-1.0.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 117.8 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tgcryptox-1.0.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c4836801477049ede6119bc1ddce06d28cce5778793611a5dd9f700818e81b92
MD5 728e6fa2936f0e1505563f37b7d42e3d
BLAKE2b-256 3e50b0d9e3f1840d492019d1c7203362ae22f9cfe34d7dc1488d469a2360761b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcryptox-1.0.0-cp312-cp312-win_amd64.whl:

Publisher: ci.yml on SychO3/tgcryptoX

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcryptox-1.0.0-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for tgcryptox-1.0.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 76403aaa6e3b2d2b5543e4a529c8b04a7b45e5ac28056de641431923622529dd
MD5 ae21afdbf22c514bf5a1ad54c3841218
BLAKE2b-256 e39df7786f46b5c1f059d8b93373c729a8292193daef57a48c3c463770406651

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcryptox-1.0.0-cp312-cp312-manylinux_2_34_x86_64.whl:

Publisher: ci.yml on SychO3/tgcryptoX

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcryptox-1.0.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tgcryptox-1.0.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9df241fa89154b1d41cefa9ade6e896eb28b70b0906a5d510b13fae29c597c79
MD5 687e67399cbe12ec7b3dc1e703e7025f
BLAKE2b-256 382957e8102fcf9c4ce833b4e094be3daac1f74dccde43278d4a3d74eca9601d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcryptox-1.0.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: ci.yml on SychO3/tgcryptoX

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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