Issue
Here I am Trying to create Model where i can save Password, here my model:
class Server(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=20, null=True)
hostname = models.CharField(max_length=50, null=True, blank=True)
ip = models.GenericIPAddressField()
ip2 = models.GenericIPAddressField(null=True, blank=True)
user_name = models.CharField(max_length=20, null=True)
password = models.TextField(max_length=500, null=True, blank=True)
ssh_key = models.FileField(null=True, blank=True, upload_to='Keys/')
till now i read lot of blogs and posts but i haven't found any good way to save encrypted text in database
i was trying this method but it is also not working for me please check my View.py below,
from cryptography.fernet import Fernet
class HostCreate(CreateView):
model = Server
template_name = 'inventory/host_create.html'
form_class = HostForm
# after getting POST data of fields (name, hostname, ip, pass, key) adding user and saving
def form_valid(self, form):
host = form.save(commit=False)
host.user = User.objects.get(pk=self.request.user.pk)
host.password = self.ecrypt(host.password)
host.save()
return redirect('inventory:hosts')
def ecrypt(self, password): # need password and return cipher password
key = 'wgjSSyfVKgz0EjyTilqeJSaANLDu7TzHKdpAXUeZPbM='
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(password)
return cipher_text
here i am getting error,
Exception Type: TypeError
Exception Value: data must be bytes.
Exception Location: /usr/lib64/python2.7/site-packages/cryptography/fernet.py in _encrypt_from_parts, line 55
Is there any builtin django functionality for password field?
Solution
I solved it by using django-encrypted-fields Packages Steps are :
on Project Root directory open terminal and Execute commands.
Install Package django-encrypted-fields
$ pip install django-encrypted-fields
Create a basic keyczar keyset. AES-256 in this case.
$ mkdir fieldkeys $ keyczart create --location=fieldkeys --purpose=crypt $ keyczart addkey --location=fieldkeys --status=primary --size=256
Add settings In your settings.py
ENCRYPTED_FIELDS_KEYDIR = os.path.join(BASE_DIR, 'fieldkeys')
Now in models.py
from django.db import models import encrypted_fields class Server(models.Model): password = encrypted_fields.EncryptedCharField(max_length=500)
for more details please visit here
hope this will help someone in future
Answered By - Pankaj Jackson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.