Skip to main content

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

Project description

CircleCI codecov PyPI - Downloads

FernetFile

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

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 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()

Or compress and crypt them with pyzstd

    pip install fernetfile[zstd]
    from fernetfile.zstd import FernetFile

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

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

And chain it to tar and bz2

class TarBz2FernetFile(tarfile.TarFile):

    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:
            self.bz2_file = bz2.BZ2File(self.fernet_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.fernet_file.close()
            raise

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

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

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

Encrypt / decrypt existing files

Encrypt :

    import fernetfile

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

Decrypt :

    import fernetfile

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

Or

Encrypt :

    import fernetfile.zstd

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

Decrypt :

    import fernetfile.zstd

    with fernetfile.zstd.open(source, mode='rb', fernet_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/FernetFile/

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.1.5.tar.gz (7.2 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.1.5-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fernetfile-0.1.5.tar.gz
Algorithm Hash digest
SHA256 8822a6ed60b7d72884cd179fe0ca2df215e3be6206543b956ea9b317499dde8f
MD5 bed2a1bfe973ddb80cbef870c725432d
BLAKE2b-256 8e71f6919251c457bced1984255e233ae7d26ae8e991599447898aa35af736ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for fernetfile-0.1.5.tar.gz:

Publisher: python-publish.yml on bibi21000/FernetFile

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

File details

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

File metadata

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

File hashes

Hashes for fernetfile-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 dfa6959fb8ec8e9aba304c9e73c54cfe69b5d38a5c1c8a13cceee6317a59f318
MD5 1daa873db4a76a484a0058358353aacf
BLAKE2b-256 568cca84c9a3ab1a127021a4e2c2eb8fcb883529843cc570965162b0eaabad29

See more details on using hashes here.

Provenance

The following attestation bundles were made for fernetfile-0.1.5-py3-none-any.whl:

Publisher: python-publish.yml on bibi21000/FernetFile

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