By using our website, you agree to our privacy policy

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.


Django Django Encrypt File Documentation

Share Your Thoughts
M ↓   Markdown

Steve
Friday, Jul 7, 2017

1: This does not seem to work: pip install djangoencryptfile 2: Bug here?: open('readme.md', 'rb') as inputfile: should this be: with open('readme.md', 'rb') as inputfile: 3: Another bug here usefile = File(inputfile, name='readme.md') encrypted_file = service.encrypt_file(useFile, password, extension='enc') # case mismatch between usefile and useFile