Skip to main content

CircleCI CodeQL codecov PyPI - Downloads

TinkFile

A python xxxFile like (ie TarFile, GzipFile, BZ2File, pyzstd.ZstdFile, ...) for encrypting files with cryptography Tink (experimental).

This project is part of the CofferFile : https://github.com/bibi21000/CofferFile

If you're looking for a more powerfull storage for your sensible datas, look at PyCoffer : https://github.com/bibi21000/PyCoffer.

Install

    pip install tinkfile

Create your encryption key in json format

    import tink
    from tink import aead
    from tink import secret_key_access

    aead.register()
    key_template = aead.aead_key_templates.AES128_GCM
    keyset_handle = tink.new_keyset_handle(key_template)
    key = tink.json_proto_keyset_format.serialize(
            keyset_handle, secret_key_access.TOKEN
        )

and store it in a safe place (disk, database, ...).

This key is essential to encrypt and decrypt data. Losing this key means losing the data.

"open" your encrypted files like normal files

Text files :

    import tinkfile

    with tinkfile.open('test.txc', mode='wt', tink_key=key, encoding="utf-8") as ff:
        ff.write(data)

    with tinkfile.open('test.txc', "rt", tink_key=key, encoding="utf-8") as ff:
        data = ff.read()

    with tinkfile.open('test.txc', mode='wt', tink_key=key, encoding="utf-8") as ff:
        ff.writelines(data)

    with tinkfile.open('test.txc', "rt", tink_key=key, encoding="utf-8") as ff:
        data = ff.readlines()

Binary files :

    import tinkfile

    with tinkfile.open('test.dac', mode='wb', tink_key=key) as ff:
        ff.write(data)

    with tinkfile.open('test.dac', "rb", tink_key=key) as ff:
        data = ff.read()

Or compress and crypt them with pyzstd

Look at https://github.com/bibi21000/CofferFile/blob/main/BENCHMARK.md.

    pip install tinkfile[zstd]
    from tinkfile.zstd import TinkFile

    with TinkFile('test.dac', mode='wb', tink_key=key) as ff:
        ff.write(data)

    with TinkFile('test.dac', mode='rb', tink_key=key) as ff:
        data = ff.read()

And chain it to tar and bz2

class TarBz2TinkFile(tarfile.TarFile):

    def __init__(self, name, mode='r', tink_key=None, chunk_size=tinkfile.CHUNK_SIZE, **kwargs):
        compresslevel = kwargs.pop('compresslevel', 9)
        self.tink_file = tinkfile.TinkFile(name, mode,
            tink_key=tink_key, chunk_size=chunk_size, **kwargs)
        try:
            self.bz2_file = bz2.BZ2File(self.tink_file, mode=mode,
                compresslevel=compresslevel, **kwargs)
            try:
                super().__init__(fileobj=self.bz2_file, mode=mode, **kwargs)

            except Exception:
                self.bz2_file.close()
                raise

        except Exception:
            self.tink_file.close()
            raise

    def close(self):
        try:
            super().close()
        finally:
            try:
                if self.tink_file is not None:
                    self.bz2_file.close()
            finally:
                if self.tink_file is not None:
                    self.tink_file.close()

    with TarBz2TinkFile('test.zsc', mode='wb', tink_key=key) as ff:
        ff.add(dataf1, 'file1.out')
        ff.add(dataf2, 'file2.out')

    with TarBz2TinkFile('test.zsc', mode='rb', tink_key=key) as ff:
        fdata1 = ff.extractfile('file1.out')
        fdata2 = ff.extractfile('file2.out')

Encrypt / decrypt existing files

Encrypt :

    import tinkfile

    with open(source, 'rb') as fin, tinkfile.open(destination, mode='wb', tink_key=key) as fout:
        while True:
            data = fin.read(7777)
            if not data:
                break
            fout.write(data)

Decrypt :

    import tinkfile

    with tinkfile.open(source, mode='rb', tink_key=key) as fin, open(destination, 'wb') as fout :
        while True:
            data = fin.read(8888)
            if not data:
                break
            fout.write(data)

Or to compress and crypt

    import tinkfile.zstd

    with open(source, 'rb') as fin, tinkfile.zstd.open(destination, mode='wb', tink_key=key) as fout:
        while True:
            data = fin.read(7777)
            if not data:
                break
            fout.write(data)

    with tinkfile.zstd.open(source, mode='rb', tink_key=key) as fin, open(destination, 'wb') as fout :
        while True:
            data = fin.read(8888)
            if not data:
                break
            fout.write(data)

Look at documentation : https://bibi21000.github.io/TinkFile/

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tinkfile-0.0.3.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

tinkfile-0.0.3-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file tinkfile-0.0.3.tar.gz.

File metadata

  • Download URL: tinkfile-0.0.3.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for tinkfile-0.0.3.tar.gz
Algorithm Hash digest
SHA256 2644439baeda0b9e95243fb244abe2065a0368680e0d950686431602c1edd41e
MD5 215923407fea590c0fa63f90d735b077
BLAKE2b-256 85267d1f5aff630269c2d95b5857c0286a95fa2a5d5d73b53b96f9d7ac8e65bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for tinkfile-0.0.3.tar.gz:

Publisher: python-publish.yml on bibi21000/TinkFile

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

File details

Details for the file tinkfile-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: tinkfile-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for tinkfile-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 084f278cb76532c8ebf9e469cb0570be1cefb25ffe2c060ede10071f84890072
MD5 f0206a220c842cc6147b9dfa89f8d706
BLAKE2b-256 3ae1d2076ad390a827a0af205657447bc9adfd73307d75867233a2fe0431ed9e

See more details on using hashes here.

Provenance

The following attestation bundles were made for tinkfile-0.0.3-py3-none-any.whl:

Publisher: python-publish.yml on bibi21000/TinkFile

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

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 files

0.0.2

2 files

0.0.1

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page