Ruddra.com

Documentation Of Django Encrypt File

Documentation Of Django Encrypt File

This is no longer maintained. Use it at your own risk.

Django Encrypt File is a simple Library which can be used to encrypt uploaded files and store them.

Installation

Use pip to install it:

pip install djangoencryptfile

Or

pip install https://github.com/ruddra/django-encrypt-file/tarball/0.7

Basic usage

from djangoencryptfile import EncryptionService
from django.core.files import File

password = '1234'
service = EncryptionService(raise_exception=False)

open('readme.md', 'rb') as inputfile:
    usefile = File(inputfile, name='readme.md')
    encrypted_file = service.encrypt_file(useFile, password, extension='enc')  # it will save readme.md.enc
    decrypt_file = service.decrypt_file(encrypted_file, password, extension='.enc') # it will remove .enc extension

Using in the view

from django_encrypt_file import EncryptionService, ValidationError

def some_view(request):
   try:
       myfile = request.FILES.get('myfile', None)
       password = request.POST.get('password', None)
       encrypted_file = EncryptionService().encrypt_file(myfile, password, extension='enc')
       decrypt_file = service.decrypt_file(encrypted_file, password, extension='enc') # it will remove .enc extension
   except ValidationError as e:
       print(e)

Advance example(with models)

Model defination

class MyModel(models.Model):
    upload_file = models.FileField(
        upload_to='tuploaded_file/%Y/%m/%d'
    )

Encrypt view

from django_encrypt_file import EncryptionService, ValidationError

def encrypt_view(request):
   try:
       myfile = request.FILES.get('myfile', None)
       password = request.POST.get('password', None)
       encrypted_file = EncryptionService().encrypt_file(myfile, password, extension='enc')
       mymodel = MyModel.objects.create(uploaded_file=encrypted_file)
   except ValidationError as e:
       print(e)

Decrypt view

from django_encrypt_file import EncryptionService, ValidationError

def decrypt_view(request):
   try:
       my_object = MyModel.objects.get(pk=1)
       myfile = my_object.uploaded_file
       password = request.POST.get('password', None)

       decrypt_file = service.decrypt_file(encrypted_file, password, extension='enc')
       my_object.uploaded_file = decrypt_file
       my_object.save()
   except ValidationError as e:
       print(e)

Notes

Input file here can be any kind of Django File Object like models.FileField or forms.FileFiled. raise_exception = True will throw ValidationError error which can be imported from django_encrypt_file import ValidationError.

Last updated: Jul 13, 2024


← Previous
Docker: Use Celery in Django(Redis as Broker)

In previous two posts, we have deployed Django with Postgres, Nginx, now its time to do some async …

Next →
Python: Selenium with PhantomJs to capture Screenshots

This article is deprecated as PhantomJs has been deprecated from Selenium as driver. PhantomJS is a …

Share Your Thoughts
M↓ Markdown