在这里,我试图创建模型,在这里我可以保存密码,这里是我的模型:
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/')
到目前为止,我读了很多博客和帖子,但我还没有找到在数据库中保存加密文本的好方法。
我正在尝试这个方法,但它也不适用于我,请查看下面的View.py,
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
我犯了错误,
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
密码字段有任何内置django功能吗?
发布于 2016-06-10 09:37:08
我通过使用django加密的字段包来解决这个问题,步骤如下:
在Project根目录上,打开终端并执行命令。
欲知更多详情,请访问这里
希望这能对将来的人有所帮助
发布于 2016-06-10 08:12:56
你可以用两种可能的方法来做。
BaseEncryptedField
中查看这里。您还可以使用这包。
https://stackoverflow.com/questions/37741339
复制相似问题