Metadata-Version: 2.4
Name: fernetfile
Version: 0.1.7
Summary: A python xxxFile like ( ie GzipFile, BZ2File, ...) for manipulating Fernet encrypted files.
Project-URL: HomePage, https://github.com/bibi21000/FernetFile
Project-URL: Issues, https://github.com/bibi21000/FernetFile/issues
Project-URL: Changelog, https://github.com/bibi21000/FernetFile/blob/master/CHANGELOG.md
Project-URL: Documentation, https://bibi21000.github.io/FernetFile/
Author-email: bibi21000 <bibi21000@gmail.com>
Maintainer-email: bibi21000 <bibi21000@gmail.com>
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Requires-Dist: cofferfile
Requires-Dist: cryptography>=43.0.0
Provides-Extra: build
Requires-Dist: build; extra == 'build'
Requires-Dist: twine; extra == 'build'
Provides-Extra: doc
Requires-Dist: pdoc; extra == 'doc'
Provides-Extra: test
Requires-Dist: bandit; extra == 'test'
Requires-Dist: coverage[toml]; extra == 'test'
Requires-Dist: pytest; extra == 'test'
Requires-Dist: pytest-cov; extra == 'test'
Requires-Dist: pytest-mock; extra == 'test'
Requires-Dist: pytest-random-order; extra == 'test'
Requires-Dist: pytest-xdist; extra == 'test'
Requires-Dist: ruff; extra == 'test'
Provides-Extra: zstd
Requires-Dist: pyzstd; extra == 'zstd'
Description-Content-Type: text/markdown

[![CircleCI](https://dl.circleci.com/status-badge/img/gh/bibi21000/FernetFile/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/bibi21000/FernetFile/tree/main)
[![CodeQL](https://github.com/bibi21000/FernetFile/actions/workflows/codeql.yml/badge.svg)](https://github.com/bibi21000/FernetFile/actions/workflows/codeql.yml)
[![codecov](https://codecov.io/gh/bibi21000/FernetFile/graph/badge.svg?token=4124GIOJAK)](https://codecov.io/gh/bibi21000/FernetFile)
![PyPI - Downloads](https://img.shields.io/pypi/dm/fernetfile)

# 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

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

```
    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/

