2017-12-18 207 views
0

我正在開發一個使用python和flask的webapp。它有一個用戶系統,當然還有一個註冊表。我正在使用,加密要註冊的用戶的密碼,passlib.hash.sha256。以下是我在做什麼:sha256_crypt.encrypt總是返回另一個散列

from passlib.hash import sha256_crypt as sha256 
[...] 
if request.method == "POST" and form.validate(): 
    username = request.form['username'] 
    password = request.form['password'] 
    confirm_password = request.form['confirm_password'] 
    email = request.form['email'] 

    password = sha256.encrypt(password) #Encryption. 



    c, conn = connection('accounts') #Connection to the database 


    x = c.execute("SELECT * FROM accounts WHERE username = '%s' OR email = '%s'" %(thwart(username), thwart(email))) 

    if x: 
     flash("We are very sorry, but this Username/Email-address is already taken. Please try again") 
    else: 
     c.execute('INSERT INTO accounts VALUES ("%s", "%s", "%s")' %(thwart(username), thwart(password), thwart(email))) 
     conn.commit() 
     flash('Succesfully Registered!') 

在數據庫中,即使輸入了相同的密碼,散列總是變化。有人知道爲什麼嗎?我究竟做錯了什麼?

+0

你已經發現了這個概念鹽https://en.wikipedia.org/wiki/Salt_(cryptography)。你確定你有足夠的資格來處理認證嗎? –

+0

你是什麼意思「合格」 – MisterMM23

+0

我明白了。但是我沒有編寫任何可以添加隨機數據的程序。這是python的sha256的新功能嗎? – MisterMM23

回答

1

Fristly,請注意自1.7版本sha256_crypt.encrypt(..)被棄用,而是改名爲sha256_crypt.hash(..)所以你必須

hash = sha256_crypt.hash("password") 

創建哈希值。作爲哈希包括隨機鹽,可以不重新計算哈希和比較,而不是你應該查找表中的散列值,然後在使用它sha256_crypt.verify(),如:

sha256_crypt.verify("password", hash) 
+0

是_hash_表示從數據庫或內置類型'hash'中提取的散列? – MisterMM23

+0

聲明:'hash = sha256_crypt.hash(「password」)'我直接從文檔中取出。第一個'hash'是由'hash(..)'方法調用完成的計算的結果,並且是數據庫中需要保留的內容。 –

+0

好的。有效!非常感謝你! – MisterMM23

-1

嘗試pycrypto .sha256相反,passlib似乎不是您的要求計算未消隱哈希的正確解決方案。

相關問題