简体   繁体   中英

How to hash strings in python(django) and compare the hashed value with a given string

I'm working on a web app that allows users to sign up then login, I used the following functions to hash the password

from passlib.hash import pbkdf2_sha256
import math

def encrypt_password(pswd):
    encrypt_pswd = pbkdf2_sha256.encrypt(pswd, rounds=(int(math.pow(len(pswd),3))), salt_size=(len(pswd)*2))
    return encrypt_pswd

def verify_password(pswd, e_pswd):
    en_pswd = encrypt_password(pswd)
    if en_pswd == e_pswd:
        return True
    else:
        return False

my problem is that the string I hashed doesn't produce the same result when I hash it for a second time. How can I resolve this issue or what methods can I use hash the password, store in the database and compare that value with the one from the login form

You need to use the verify function

def verify_password(pswd, e_pswd):
    return pbkdf2_sha256.verify(pswd, e_pswd)

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