Fast Telegram Crypto Library for Python
Project description
TgCrypto
Fast and Portable Telegram Crypto Library for Python
TgCrypto is a Telegram Crypto Library written in C89 as a Python extension. It is designed to be portable, fast, easy to install and use. TgCrypto is intended for Pyrogram and implements the crypto algorithms Telegram requires, namely:
AES256-IGE
- used in MTProto v2.0.AES256-CTR
- used for CDN encrypted files.AES256-CBC
- used for encrypted passport credentials.
Python wheels are available for hassle-free installations; they are automatically built and tested using Travis CI (Linux, macOS) and AppVeyor (Windows), for both 32-bit and 64-bit architectures.
Even though TgCrypto is primarily intended for use with Pyrogram, you are free and welcome to use it for any other Python project too, as it's shipped as standalone package.
More info: https://docs.pyrogram.org/topics/tgcrypto
Requirements
- Python 3.4 or higher.
Installation
$ pip3 install -U tgcrypto
API
TgCrypto 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 tgcrypto
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 = tgcrypto.ige256_encrypt(data, key, iv)
ige_decrypted = tgcrypto.ige256_decrypt(ige_encrypted, key, iv)
print(data == ige_decrypted) # True
CTR Mode (single chunk)
import os
import tgcrypto
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 = tgcrypto.ctr256_encrypt(data, key, enc_iv, bytes(1))
ctr_decrypted = tgcrypto.ctr256_decrypt(ctr_encrypted, key, dec_iv, bytes(1))
print(data == ctr_decrypted) # True
CTR Mode (stream)
import os
from io import BytesIO
import tgcrypto
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(tgcrypto.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(tgcrypto.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 tgcrypto
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 = tgcrypto.cbc256_encrypt(data, key, enc_iv)
cbc_decrypted = tgcrypto.cbc256_decrypt(cbc_encrypted, key, dec_iv)
print(data == cbc_decrypted) # True
Testing
- Clone this repository:
git clone https://github.com/pyrogram/tgcrypto
. - Enter the directory:
cd tgcrypto
. - Run tests:
python3 setup.py test
.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for TgCrypto-1.2.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 822222ee462f0770a825deecbba459f74bcbba451b9f4b516590260e627b1494 |
|
MD5 | 677a024f5da77d4f4b9d4dda99d7e05c |
|
BLAKE2b-256 | 9ba989ec2a629a893a2deedce5db3e8d88b1e7b488cc46e887a06f2c5aefa445 |
Hashes for TgCrypto-1.2.1-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b7790be25c1ea5cb7c05b2efb1d40f427efe6aa31ecde080136ea5eb8406870 |
|
MD5 | d208b1ce6f1c8ba3a2e0783f57a5c2f5 |
|
BLAKE2b-256 | e0c8957f4c65b9d2b47c7cbb3956dc69b07eff1de3a34d97f60dc0005008c6ff |
Hashes for TgCrypto-1.2.1-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf6a07f5fe29df5d84d25b9d944e0d986c7f9a8b837137de8682f43f0d9f0bc3 |
|
MD5 | 126c838b9f0623ccd27c5988cfa452a3 |
|
BLAKE2b-256 | f8247c3e949808fd881703d77a416da2abf8fd6b50fdcc1892cb51ff505d1ff1 |
Hashes for TgCrypto-1.2.1-cp38-cp38-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c3c94f5683787de8168ba94dd5f8bccc87b9c84ad316169a23eb273f8614273 |
|
MD5 | 1e92a579683b7919732a5318d789170e |
|
BLAKE2b-256 | 692891e2a955e138e54271edf0a8d338ac913c18be2be762e435baf68e1a430a |
Hashes for TgCrypto-1.2.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c085e268cc7675f6c13f10789ed369a1554e8238322caeb0ead29591a52a3d8 |
|
MD5 | 05aa0a5bd47f1e73cc1e3d460572e4ce |
|
BLAKE2b-256 | ef4b4aba757723730a56b6e05f3c0140a271b62cf27e0182abbc96dd3dfc4d7c |
Hashes for TgCrypto-1.2.1-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e93d810570109d4f5b8ceaa24d88d60c778627c3e0c1a90561a003c9add8fa5c |
|
MD5 | 8bb849cece115e43599b992844cbd08c |
|
BLAKE2b-256 | 182f5395564fca7fc4cf01eeff15ccda3117be20698f2cdaa584a2fb85665ced |
Hashes for TgCrypto-1.2.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1703ce724a55254815a97430479805ad172d727435ca93f9012b4b8000517aa |
|
MD5 | d4c9c098909769635460720a2f9118c5 |
|
BLAKE2b-256 | afcd7d1b16aacf5c129b8cc8d77ad8d126b737a721c11ff53188e0f907dfdf75 |
Hashes for TgCrypto-1.2.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0809363d53b02684ac2b6f3940360ea82b0a8bbe01591f1d1fddc1adbd0b393 |
|
MD5 | 5113be772239b0d8451a30edb25cd365 |
|
BLAKE2b-256 | c5552e468cb496c2532d6bf3bcda99d1a28942078522dbd2832a89cb0641498a |
Hashes for TgCrypto-1.2.1-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | efc3dd2380eec99f1172416de1fba569815f5b2b570b658e77871c8087cc85d7 |
|
MD5 | 545cf474d42057507bd513d1e7299a18 |
|
BLAKE2b-256 | 432ef897befdde62c8c5a094cbdfbc27b449834ed7e181bc3b758dc7862c5d07 |
Hashes for TgCrypto-1.2.1-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 414024b421bd429bc0fb681f1fd94982381cddc2fecf16629551d5982221928b |
|
MD5 | 0f7f452c61032919c1a1aa57434f0b1b |
|
BLAKE2b-256 | e48b8b0aac2851d008050a7ea075a07ec41b91af44dad47d02826adfde70d3b3 |
Hashes for TgCrypto-1.2.1-cp37-cp37m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e81df8b6295ea121cf62eea89b997a25baac2765f6324e325ebc4400d030429 |
|
MD5 | f342e5a3d6f4fee3453121734747e5e3 |
|
BLAKE2b-256 | e912afa369a6265752887dc81d39b172bc0530c2cc17da92c9e3b0673a3c3ced |
Hashes for TgCrypto-1.2.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 59929e440c12606cede6be57fb34c5abf8d0eeac8d5539635d3dd1b81a8c59d5 |
|
MD5 | 06faf54c824d0d4037e79b0e79028603 |
|
BLAKE2b-256 | caef0c12218f6671370923bc90f533f22da2c568995c1fcf9e2ea5a8e4383ace |
Hashes for TgCrypto-1.2.1-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f05d540b2da1cbd2c2b8ea01659388897ff256e9bf32267061f71058851928b5 |
|
MD5 | cfa7b7b0fc9348fbde970621411cfa2e |
|
BLAKE2b-256 | 2c54d8a8d5e20e120142322c010fc6ff8220be3d2dc157f342c33b889f35a960 |
Hashes for TgCrypto-1.2.1-cp37-cp37m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5631a700546284ed9348c7f4de2fe98ce9bf4f69659fe37eec3421d8ae586a9 |
|
MD5 | 1c6e48a08d33ae714f3350e0f899eb52 |
|
BLAKE2b-256 | 7816154020192bb8ee52976df6031608a037e41fcf4cc17ab405ce78d1509735 |
Hashes for TgCrypto-1.2.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7bb91e047aae498b0463cc1d9edec1f1c11b1cf5a3401941c52b790c2875bde |
|
MD5 | 4b3fbed029086bffd0d0985849220eb1 |
|
BLAKE2b-256 | 79e06b641678749a53797298595f680acf36f261e40394b5ff3653d107d2085f |
Hashes for TgCrypto-1.2.1-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03a30eea8c705453063ccd717571760419b7c23837760a41d84fb9b4579867a0 |
|
MD5 | a344204e3293cd7d8fb1b69789b70437 |
|
BLAKE2b-256 | 7b8816985353a3c9976ba19db99a907b1988f993eb4895941ecc729636b6fbc5 |
Hashes for TgCrypto-1.2.1-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e36c668de9e24dad4d599f9b354199d82efe56d4712083dc4993de2fed87b2d |
|
MD5 | 3c6cbb957f86f4f870c9808c5d3139bd |
|
BLAKE2b-256 | 2e33ff30166284ba14ceddc8fe2ef37f7d5f8a7a619d532fe699b1187cc00878 |
Hashes for TgCrypto-1.2.1-cp36-cp36m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 319a8a58ac2a3d09d05574008a4d29f15d0446a7b930a932f119b709d4ad9423 |
|
MD5 | 3c964972660528430633837f7881f0e9 |
|
BLAKE2b-256 | e379c14105f112fd61c8c822a5575173430b697c9660949172ae3e8588fbcb5d |
Hashes for TgCrypto-1.2.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d184b99792bfc29cd51675287241f715cc2959c3b5c21e616b0a2fcb2eeeec39 |
|
MD5 | 53df217255cf1aae0cac28cec66ad77c |
|
BLAKE2b-256 | a2b3d3ba321103022b8d1711be9a4a341d163f9cdf9ba8224bc0f26c90542001 |
Hashes for TgCrypto-1.2.1-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b46f2bb3945a92d976da65a82941543cc907ee30a3b29327bfa78ff1f33867f |
|
MD5 | 709a3337849637f54845721498048274 |
|
BLAKE2b-256 | 0d77a651ab93f10ab7313227fb975307cd7b9e79e41753aa102818f68438d901 |
Hashes for TgCrypto-1.2.1-cp36-cp36m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bbb43e52b89b4aeb8502b05901ca2a84b27c97f609dd82d14d74e65ec61c8d38 |
|
MD5 | 2b19914080ebca7a0156686b99eb3caa |
|
BLAKE2b-256 | f9971142e834ee529db85df5363d9f9df428dad899800e19914c9940532d1d3f |
Hashes for TgCrypto-1.2.1-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 610fedcd99eedc6873c71af50af10645eeb1e7d2e5b41b515f070513ce90647b |
|
MD5 | 725cf8cc9e4f10a91eb0e31094f8d0bd |
|
BLAKE2b-256 | 80170b8b72ce10ef7a67579700ca1c7e3b5600ac8328df10f3d5e78b12854a88 |
Hashes for TgCrypto-1.2.1-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e423a03a2468325cde3e7b27a67898d6d360908915f9c4ae8465fcc025a18936 |
|
MD5 | b4390af801b88a8a57048bb1e331ae13 |
|
BLAKE2b-256 | 72b0ffe3454464f420dde1d94fd8f9a007934ed9aae08c48789dbcbb2442d6e3 |
Hashes for TgCrypto-1.2.1-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2776c7b746acbb8a5349ff40bfe506738e60a07da2886c481fcdd92fc0b937ef |
|
MD5 | f52965a657229898722624c9ab29d8a1 |
|
BLAKE2b-256 | ad6f56fb73e03df53731e884a0bee919fde7fe083dd58f96aff7dcca89df89b3 |
Hashes for TgCrypto-1.2.1-cp35-cp35m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9467a3b56337fcb5f0c6bc752b2d04a04de3fb2807920f311200822cb5bf5b6e |
|
MD5 | d670a58bed60793f7a9719be3d93e33a |
|
BLAKE2b-256 | 526fe8e59ae2d5cadacec486a8c5f696744bc0038c409b33f31848f2549d86b7 |
Hashes for TgCrypto-1.2.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aefc5fc6d4b3ddc976b69c7f50dabd2dc2406cbc37607421d104376487ee38c3 |
|
MD5 | d624201996cda3f4e132c2c9f733df6e |
|
BLAKE2b-256 | 9de4708f66ab44df7585cd3541aaf4ece9486b4c83fdde664f78d6e9c69ac6fe |
Hashes for TgCrypto-1.2.1-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b7fae9d66dea83829607172248f00b0f4891e13b6356fdaa655403bdfaa4949 |
|
MD5 | 5b5c5f0d87f7f8e0fa6e92d3813bd91d |
|
BLAKE2b-256 | d993dfab8422260265362acb94c0a3afb6496ee23ab3d0b246621227568ea9a1 |
Hashes for TgCrypto-1.2.1-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 597d24debc4ef95bb14e0406e41951cfc5472853446344911214bcdd9a123b89 |
|
MD5 | f3b21fe524e8ce2af1b34812d945519f |
|
BLAKE2b-256 | ac4c797fc9a34f4d77f552be0b4a6ad9244ab9b3f43061fd359d747e2de5afff |