Skip to main content

A python xxxFile like ( ie GzipFile, BZ2File, ...) for manipulating Fernet encrypted files.

Project description

CircleCI codecov

FernetFile

A python xxxFile like (ie GzipFile, BZ2File, ...) for encrypting files with Fernet.

  • encrypting / decrypting data using chunks to reduce memory footprint
  • chainable with other python xxxFile interfaces (stream mode)
  • look at BENCHMARK.md ... and chain :)
  • look at tests for examples

Install

    pip install fernetfile

Create your encryption key

    from cryptography.fernet import Fernet

    key = Fernet.generate_key()

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 fernetfile

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

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

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

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

Binary files :

    import fernetfile

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

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

Use the FernetFile interface

    import fernetfile

    with fernetfile.FernetFile('test.dac', mode='wb', fernet_key=key) as ff:
        ff.write(data)

    with fernetfile.FernetFile('test.dac', mode='rb', fernet_key=key) as ff:
        data = ff.read()

Chain it to bz2

    import fernetfile
    import bz2

    class Bz2FernetFile(bz2.BZ2File):

        def __init__(self, name, mode='r', fernet_key=None, chunk_size=fernetfile.CHUNK_SIZE, **kwargs):
            compresslevel = kwargs.pop('compresslevel', 9)
            self.fernet_file = fernetfile.FernetFile(name, mode,
                fernet_key=fernet_key, chunk_size=chunk_size, **kwargs)
            try:
                super().__init__(self.fernet_file, mode=mode,
                    compresslevel=compresslevel, **kwargs)
            except Exception:
                self.fernet_file.close()
                raise

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


    with Bz2FernetFile('test.bzc', mode='wb', fernet_key=key) as ff:
        ff.write(data)

    with Bz2FernetFile('test.bzc', mode='rb', fernet_key=key) as ff:
        data = ff.read()

Chain it to tar and pyzstd

    import fernetfile
    import pyzstd
    import tarfile

    class TarZstdFernetFile(tarfile.TarFile):

        def __init__(self, name, mode='r', fernet_key=None, chunk_size=fernetfile.CHUNK_SIZE, **kwargs):
            level_or_option = kwargs.pop('level_or_option', None)
            zstd_dict = kwargs.pop('zstd_dict', None)
            self.fernet_file = fernetfile.FernetFile(name, mode,
                fernet_key=fernet_key, chunk_size=chunk_size, **kwargs)
            try:
                self.zstd_file = pyzstd.ZstdFile(self.fernet_file, mode=mode,
                    level_or_option=level_or_option, zstd_dict=zstd_dict, **kwargs)
                try:
                    super().__init__(fileobj=self.zstd_file, mode=mode, **kwargs)

                except Exception:
                    self.zstd_file.close()
                    raise

            except Exception:
                self.fernet_file.close()
                raise

        def close(self):
            try:
                super().close()
            finally:
                try:
                    if self.zstd_file is not None:
                        self.zstd_file.close()
                finally:
                    if self.fernet_file is not None:
                        self.fernet_file.close()


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

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

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

fernetfile-0.0.6.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

fernetfile-0.0.6-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file fernetfile-0.0.6.tar.gz.

File metadata

  • Download URL: fernetfile-0.0.6.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for fernetfile-0.0.6.tar.gz
Algorithm Hash digest
SHA256 77d68e8bc600fecc6c90a61abcaa442622af4b1ef6bb9c5699ef52484e8db670
MD5 e568c4526ec30a1c5238e9650df0c220
BLAKE2b-256 bed7fb28cd7ea24d6d2044faa51cf9b27aacc8725b9ca102335ada2d79348c06

See more details on using hashes here.

File details

Details for the file fernetfile-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: fernetfile-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for fernetfile-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 07e23d48421dbb66947923404f153a77120f80a6a9446c500d881e464a797671
MD5 837856e99a079e2993d371af2ead51b9
BLAKE2b-256 cfc1742ece28f13d68151be44e38a84524611372d79b1394d9797f49d5936c36

See more details on using hashes here.

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