Skip to main content

Encrypted cloud storage Protocol based on a Telegram API

Project description

This repository contains a set of classes and functions used to manage TGBOX.
Try the tgbox-cli if you’re interested in working implementation!
from asyncio import run as asyncio_run
from getpass import getpass # Hidden input

from tgbox.api import TelegramClient, make_remotebox, make_localbox
from tgbox.keys import Phrase, make_basekey

# This two will not work. Get your own at https://my.telegram.org
API_ID, API_HASH = 1234567, '00000000000000000000000000000000'

# Simple progress callback to track upload/download state
PROGRESS_CALLBACK = lambda c,t: print(round(c/t*100),'%')

async def main():
    phone = input('Phone number: ')

    tc = TelegramClient(
        phone_number = phone,
        api_id = API_ID,
        api_hash = API_HASH
    )
    await tc.connect() # Connecting to Telegram
    await tc.send_code() # Requesting login code

    code = int(input('Login code: '))
    password = getpass('Your password: ')

    # Login to your Telegram account
    await tc.log_in(password, code)

    # Generate and show your Box phrase
    print(phrase := Phrase.generate())

    # WARNING: This will use 1GB of RAM for a
    # couple of seconds. See help(make_basekey)
    basekey = make_basekey(phrase)

    erb = await make_remotebox(tc) # Make EncryptedRemoteBox
    dlb = await make_localbox(erb, basekey) # Make DecryptedLocalBox
    drb = await erb.decrypt(dlb=dlb) # Obtain DecryptedRemoteBox

    # Write a file path to upload to your Box
    file_to_upload = input('File to upload (path): ')

    # Preparing for upload. Will return a PreparedFile object
    pf = await dlb.prepare_file(open(file_to_upload,'rb'))

    # Uploading PreparedFile to Remote and getting DecryptedRemoteBoxFile
    drbf = await drb.push_file(pf, progress_callback=PROGRESS_CALLBACK)

    # Retrieving some info from the RemoteBox file
    print('File size:', drbf.size, 'bytes')
    print('File name:', drbf.file_name)

    # You can also access all information about
    # the RemoteBoxFile you need from the LocalBox
    dlbf = await dlb.get_file(drbf.id)

    print('File size:', dlbf.size, 'bytes')
    print('File path:', dlbf.file_path)

    # Downloading your [already uploaded] file from Remote.
    await drbf.download(progress_callback=PROGRESS_CALLBACK)

    await drb.done() # Close all connections
    await dlb.done() # after work was done

asyncio_run(main())

Motivation

The Telegram is beautiful app. Not only by mean of features and Client API, but it’s also used to be good in cryptography and secure messaging. In the last years, core and client devs of Telegram mostly work for “social-network features”, i.e video chats and message reactions which is OK (until stories, wtf?), but there also can be plenty of “crypto-related” things implemented.

Target

This [unofficial] library targets to be a PoC of encrypted file storage inside the Telegram, and should be used as standalone Python library.

Abstract

We name “encrypted cloud storage” as Box and the API to it as tgbox. The Box splits into the RemoteBox and the LocalBox. They define a basic primitives. You can share your Box and separate Files with other people absolutely secure - only You and someone you want will have decryption key, even through insecure communication canals (e2e). You can make unlimited amount of Boxes, Upload & Download speed is faster than in official Telegram clients and maximum filesize is around 2GB and around 4GB for Premium users.

Documentation

See ReadTheDocs for main information and help.

You can also build docs from the source

git clone https://github.com/NonProject/tgbox --branch=indev
cd tgbox && python3 -m pip install .[doc] # Install with doc
cd docs && make html && firefox _build/html/index.html

Third party & thanks to

Resources

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

tgbox-1.4.tar.gz (108.6 kB view hashes)

Uploaded Source

Built Distribution

tgbox-1.4-py3-none-any.whl (108.7 kB view hashes)

Uploaded Python 3

Supported by

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