简体   繁体   English

如何通过 django 中的散列 CharField 获取项目

[英]How to get item by hashed CharField in django

I have Device model that has a field named token that stores a pbkdf2_sha256 hashed string.我有Device model,它有一个名为token的字段,用于存储 pbkdf2_sha256 散列字符串。

from django.contrib.auth.hashers import make_password
from django.models import Model, CharField

class Device(Model):
    name = CharField(max_length=200)
    token = CharField(unique=True, max_length=128)

    def save(self,*args,**kwargs):
        self.token = make_password(self.token)
        super().save(*args,**kwargs)

for example I have a Device object that it's token is hashed value of ABCD .例如,我有一个设备 object ,它的令牌是ABCD的哈希值。 now question is if I get ABCD from user as raw token, how i can find it's device from database?现在的问题是,如果我从用户那里获得ABCD作为原始令牌,我如何从数据库中找到它的设备? I tried this:我试过这个:

I hashed the ABCD that I got from user with make_password , but the new hashed value wasn't as same as the old one that was in db.我使用make_password对从用户那里获得的ABCD进行了哈希处理,但新的哈希值与 db 中的旧哈希值不同。

also I know that I can get device id from user and then check if user's entered token is same with check_password method.我也知道我可以从用户那里获取设备 ID,然后检查用户输入的令牌是否与check_password方法相同。 but I want only get token from user not device id and token.但我只想从用户那里获取令牌,而不是设备 ID 和令牌。

I don't think "pbkdf2_sha256" is the right algorithm for this case as even to check if password is correct, "check_password" doesn't do a simple string comparison.我不认为“pbkdf2_sha256”是这种情况下的正确算法,因为即使检查密码是否正确,“check_password”也不会进行简单的字符串比较。 You need a symetrical algo I think or a simple hash algo for which a simple string comparison will work我认为你需要一个对称算法或一个简单的 hash 算法,简单的字符串比较就可以了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM