django过滤器和django加密字段 [英] django filters and django-encrypted-fields

查看:538
本文介绍了django过滤器和django加密字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型有一个first_name字段

 从django.db导入模型
来自encrypted_fields导入EncryptedCharField

class Customer(models.Model):
first_name = EncryptedCharField(max_len = 30)

我使用 django-encrypted-fields 加密数据存储在我的数据库。至于(我想),我无法过滤结果。我想这样做

  customers = Customer.objects.all()。filter(first_name__icontains =George)#George存在作为客户

但我得到一个空列表。有没有办法,我可以这样做?

解决方案

这是不可能的,按设计。存储在数据库中的数据是不透明的blob,所以即使明文设置字段包含George,该文本不在数据库中。



再次加密数据并将存储的数据库值与该数据库值进行比较的想法是有趣的,但仍然无法正常工作。数据使用随机的初始化向量加密,因此您不能简单地重现结果。 / p>

可能的解决方案



您可能可以解决哈希问题。例如,如果您的用户提供用于身份验证的电子邮件和密码,但您希望将电子邮件存储在数据库中加密,则可以存储散列版本的电子邮件以及加密的电子邮件。然后,您可以重现哈希和查询,只有在找到您的用户后才能解密电子邮件。


I have the following model that has a first_name field

from django.db import models
from encrypted_fields import EncryptedCharField

class Customer(models.Model):
    first_name = EncryptedCharField(max_len=30)

I use django-encrypted-fields to encrypt the data stored in my db. As of this ( I think ) I am not able to filter results. I want to do this

customers = Customer.objects.all().filter(first_name__icontains="George") #George exists as customer

but I get an empty list. Is there a way I can go round that?

解决方案

This isn't possible, by design. The data stored in the database is an opaque blob, so even if the plaintext you set the field to contains "George", that text isn't in the database.

The idea regarding encrypting the data again and comparing the stored database value against that is an interesting one, but it still won't work. The data is encrypted with a random initialization vector, so you can't simply reproduce the results.

Possible solution

You may be able to work around the problem with hashing though. For example, if your user provides an email and password for authentication, but you want the email to be stored encrypted in your database, you could store a hashed version of the email in addition to the encrypted one. Then you can reproduce the hash and query against that, and only decrypt the email once you've found your user.

这篇关于django过滤器和django加密字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆