简体   繁体   中英

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.

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 . now question is if I get ABCD from user as raw token, how i can find it's device from database? 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.

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. but I want only get token from user not device id and token.

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. You need a symetrical algo I think or a simple hash algo for which a simple string comparison will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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