Skip to main content

Rust-based TgCrypto module for Pyrogram

Project description

tgcrypto-rs

A high-performance Rust implementation of the tgcrypto Python extension module for Pyrogram.

This module provides cryptographic primitives required for Telegram's MTProto protocol, implemented in Rust for optimal performance and security.

Features

  • AES-256-IGE encryption/decryption
  • AES-256-CTR encryption/decryption
  • AES-256-CBC encryption/decryption
  • SHA-1 hashing
  • SHA-256 hashing
  • RSA encryption with Telegram server public keys
  • Pollard's rho integer factorization for MTProto handshake
  • MTProto helpers (session ID generation)

Recent Updates

  • PyO3 0.28+: Updated to the latest PyO3 for better performance and support for Python 3.13.
  • Modern Dependencies: Updated all cryptographic and utility crates to their latest stable versions.
  • Enhanced Type Hints: Improved .pyi file for better IDE support.
  • Modern CI/CD: Updated GitHub Actions to use latest tools and secure publishing.

Performance Comparison

Benchmarks were performed on 64KB data chunks (Android, AArch64).

Operation Official (C) Rust (Current) Ratio
AES-IGE 0.53 ms 2.92 ms 0.18x
AES-CTR 0.66 ms 2.97 ms 0.22x
AES-CBC 0.50 ms 2.77 ms 0.18x
SHA1 N/A 0.15 ms -
SHA256 N/A 0.30 ms -
RSA Enc. N/A 0.64 ms -
Fact. N/A 0.004 ms -

Note: The official C implementation uses highly optimized assembly for AES on ARM, whereas this Rust implementation currently uses the standard aes crate. Future optimizations may close this gap.

Installation

Prerequisites

  • Rust toolchain (install via rustup)
  • Python 3.8+
  • maturin for building Python extensions
pip install maturin

Build and Install

cd pyrogram-tgcrypto
maturin develop --release

This will compile the Rust code and install the tgcrypto module into your current Python environment.

Usage

Once installed, the module can be imported directly in Python:

import tgcrypto

# AES-256-IGE
encrypted = tgcrypto.ige256_encrypt(data, key, iv)
decrypted = tgcrypto.ige256_decrypt(encrypted, key, iv)

# AES-256-CTR
encrypted = tgcrypto.ctr256_encrypt(data, key, iv, state)
decrypted = tgcrypto.ctr256_decrypt(encrypted, key, iv, state)

# AES-256-CBC
encrypted = tgcrypto.cbc256_encrypt(data, key, iv)
decrypted = tgcrypto.cbc256_decrypt(encrypted, key, iv)

# Hashing
sha1_hash = tgcrypto.sha1(data)
sha256_hash = tgcrypto.sha256(data)

# RSA encryption
encrypted = tgcrypto.rsa_encrypt(data, fingerprint)

# Factorization
factor = tgcrypto.factorize(pq)

# Session ID
session_id = tgcrypto.get_session_id(auth_key)

API Reference

ige256_encrypt(data: bytes, key: bytes, iv: bytes) -> bytes

Encrypt data using AES-256 in IGE mode.

  • data: Must be a multiple of 16 bytes
  • key: Must be 32 bytes
  • iv: Must be 32 bytes

ige256_decrypt(data: bytes, key: bytes, iv: bytes) -> bytes

Decrypt data using AES-256 in IGE mode.

ctr256_encrypt(data: bytes, key: bytes, iv: bytes, state: int) -> bytes

Encrypt data using AES-256 in CTR mode.

  • data: Any length
  • key: Must be 32 bytes
  • iv: Must be 16 bytes
  • state: Counter state offset

ctr256_decrypt(data: bytes, key: bytes, iv: bytes, state: int) -> bytes

Decrypt data using AES-256 in CTR mode.

cbc256_encrypt(data: bytes, key: bytes, iv: bytes) -> bytes

Encrypt data using AES-256 in CBC mode.

  • data: Must be a multiple of 16 bytes
  • key: Must be 32 bytes
  • iv: Must be 16 bytes

cbc256_decrypt(data: bytes, key: bytes, iv: bytes) -> bytes

Decrypt data using AES-256 in CBC mode.

sha1(data: bytes) -> bytes

Compute SHA-1 hash of data. Returns 20 bytes.

sha256(data: bytes) -> bytes

Compute SHA-256 hash of data. Returns 32 bytes.

rsa_encrypt(data: bytes, fingerprint: int) -> bytes

Encrypt data using RSA with Telegram server public key.

  • data: Data to encrypt
  • fingerprint: Telegram server key fingerprint (e.g., -4344800451088585951)

Returns 256-byte encrypted data.

factorize(pq: int) -> int

Find a non-trivial factor of a semiprime number using Pollard's rho algorithm. Used in MTProto key exchange.

get_session_id(auth_key: bytes) -> bytes

Generate session ID from authentication key. Returns 8 bytes.

Performance

This Rust implementation provides significant performance improvements over pure Python implementations:

  • AES operations: ~10-50x faster
  • Hashing: ~5-20x faster
  • Factorization: ~100x+ faster for large numbers

The GIL is released during heavy cryptographic operations, allowing true parallelism in multi-threaded applications.

Security

This implementation uses well-audited cryptographic crates:

  • aes - AES block cipher
  • ctr - CTR mode
  • cbc - CBC mode
  • sha1 - SHA-1 hash
  • sha2 - SHA-2 family hashes
  • num-bigint - Big integer arithmetic

No unsafe code is used for cryptographic operations.

License

LGPL-3.0-or-later (same as original tgcrypto)

Acknowledgments

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

tgcrypto_rs-2.1.6.tar.gz (66.3 kB view details)

Uploaded Source

Built Distributions

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

tgcrypto_rs-2.1.6-cp38-abi3-win_amd64.whl (229.0 kB view details)

Uploaded CPython 3.8+Windows x86-64

tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_34_x86_64.whl (331.4 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.34+ x86-64

tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (357.5 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (457.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (315.8 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (312.8 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

tgcrypto_rs-2.1.6-cp38-abi3-macosx_11_0_arm64.whl (291.0 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

tgcrypto_rs-2.1.6-cp38-abi3-macosx_10_12_x86_64.whl (302.9 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file tgcrypto_rs-2.1.6.tar.gz.

File metadata

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

File hashes

Hashes for tgcrypto_rs-2.1.6.tar.gz
Algorithm Hash digest
SHA256 af199b65a5c74c328432422b53d591ab60c792c6656d1268bb72115e5d35fa42
MD5 f846eef6b2b3105d52d59b6974ad305a
BLAKE2b-256 c7b46bc13a030fda90710ea74b276589ff66dc4136728f3828646481af8b25c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.6.tar.gz:

Publisher: release.yaml on troublescope/tgcrypto-rs

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

File details

Details for the file tgcrypto_rs-2.1.6-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: tgcrypto_rs-2.1.6-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 229.0 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tgcrypto_rs-2.1.6-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 36b9cc6867205b4641bf8185bcd82cc03f590ea94031e5bbcb138d48522a9416
MD5 748fdc9b44eb9f5c0a82077185861ba6
BLAKE2b-256 0b7118ae1d186ded84ca870473d15072275e4c73cdd3ed3645ca68a02fc0c25e

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.6-cp38-abi3-win_amd64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

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

File details

Details for the file tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 421097d8f95891a87a964902f495972f770fe19f10e46789b1d24359852cb61c
MD5 c8623d76be83e55638a52a3b815aa443
BLAKE2b-256 365753476d5e102578eb0e3256cc6fd21ade769057f2e63c44383f3888f46b58

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_34_x86_64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

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

File details

Details for the file tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 6a32305784a6923802f173356032fb70e512cc21e8bafc95d37c15c76cf909da
MD5 71023a086a07f4e82fad2c9a75190b3c
BLAKE2b-256 880bc9da56f4781649a90bd94a3866382afb5e5036c3aad15b9c1eeb088e12f1

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

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

File details

Details for the file tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 547544cdd874cafd75c055a4feb1495e1ad8874da335ee7389e7e6726ccfbc43
MD5 76c135b175ffa7adc083135faf548d78
BLAKE2b-256 bdb5d7216985471d5ad655612d63728d20865837a97837798354ea837280ad71

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

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

File details

Details for the file tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 e14ea8b18c8efefb02a12f2d85a63f8542209771725be3176a348197b70e888b
MD5 08f822d8bdef720fabb913c485f34b1b
BLAKE2b-256 3987f8c4191d957784dee7b341f08c4a242e6c407bc0a9b0794def232039a17d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

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

File details

Details for the file tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c50c3ea15ec25ec545dc31273795221ec3be50f0b8388f0f7845920d5cc51a0b
MD5 be9c21e98930bd5b9ddd60af820b90e7
BLAKE2b-256 4fc019abdf95235dad8402f199108f11c5afb4bdf0c6dbdadb9b79cb24550acc

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.6-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

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

File details

Details for the file tgcrypto_rs-2.1.6-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.6-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 456d507204c4ce58d7441adbdf7658ba1160ca10a9f05dbc1e34dd771f3e39ea
MD5 5a1cdee2e524c825bd3bac44092f6b28
BLAKE2b-256 e6596df88aeea77939d186e1c3f4ce282bdac2c9a4f2aab793e31377d4ca49f0

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.6-cp38-abi3-macosx_11_0_arm64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

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

File details

Details for the file tgcrypto_rs-2.1.6-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.6-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7600c5ee417b6da4245938bf0952f996919d72770d9590b724b3e18d2bfd2387
MD5 dcae1f91e56f5b89bea8685b4fcfd528
BLAKE2b-256 5da32787d522c9c5270727b88fcafcaeb499c77831641f5c9803ab9dacc35eb0

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.6-cp38-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

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