簡體   English   中英

如何在python(django)中hash字符串並將散列值與給定字符串進行比較

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

我正在開發一個 web 應用程序,該應用程序允許用戶注冊然后登錄,我使用以下功能 hash 密碼

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

我的問題是,當我第二次使用 hash 時,我散列的字符串不會產生相同的結果。 我該如何解決這個問題或者我可以使用什么方法 hash 密碼,存儲在數據庫中並將該值與登錄表單中的值進行比較

您需要使用驗證function

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM