A python xxxFile like ( ie GzipFile, BZ2File, ...) for manipulating Fernet encrypted files.
Project description
FernetFile
This project is part of the CofferFile : https://github.com/bibi21000/CofferFile
A python xxxFile like (ie TarFile, GzipFile, BZ2File, pyzstd.ZstdFile, ...) for encrypting files with Fernet.
- encrypting / decrypting data using chunks to reduce memory footprint
- chainable with other python xxxFile interfaces (stream mode)
- interface to compress/encrypt and decrypt/decompress (with pyzstd) in stream mode
- a FernetStore with a TarFile like interface (work in progress)
- 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()
Or the fast and furious FernetFile with zstd compression
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 with the fast and furious FernetFile with zstd compression
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fernetfile-0.1.0.tar.gz.
File metadata
- Download URL: fernetfile-0.1.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5a0b2b58961cb613e8295c90f9d9820cdecc052515425e74e7e2ed747344c62
|
|
| MD5 |
cb9a0ffbcaa04bed2bcafec42b3c596d
|
|
| BLAKE2b-256 |
b2d9c787c964eb5b978ff41b9516dd97d05decb323d85f058e6bf56fce4ad627
|
File details
Details for the file fernetfile-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fernetfile-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09d9435c3f7170fbcf94bf99e279a083db4abee1e117dbeb5bacbaa649a39b88
|
|
| MD5 |
27b3ba4b98f02dfb3230350cc38f6e68
|
|
| BLAKE2b-256 |
3e182ae7dd9e4dad34ae1ddef9b3dd8cc2e02f16e3c07b209b46cd64aa0ba6f9
|