簡體   English   中英

Python中的哈希密碼驗證不起作用

[英]Hashed password authentication in Python not working

我有這段代碼可以對用戶的輸入加鹽,對其進行哈希處理,然后將哈希值和鹽寫入文件中:

def newhash(input):
    salt = uuid.uuid4().hex
    saltin = input + salt
    hashed_in = (hashlib.sha256(saltin.encode()).hexdigest())
    file.write(str(hashed_in) + '\n')
    file.write(str(salt) + '\n')
    file.close()

然后,我使用此代碼對用戶的新輸入進行加鹽和哈希處理(使用相同的鹽)並將其與文件中的輸入進行比較。

salt = linecache.getline(userin + '.userdat', 2)
saltin = newin + salt
hashed_newin = (hashlib.sha256(saltin.encode()).hexdigest())
realin = linecache.getline('file.dat', 1)
if hashed_newin == realin:
    return True

使用相同的鹽值對新輸入進行鹽析,並使用相同的函數對新輸入進行哈希處理。 因此,據我所知,它應該是相同的,第二段代碼應該返回True。 但是,它總是錯誤的。 有任何想法嗎? (我正在使用python 3.4.1)

編輯:再次通過調試器運行代碼。 事實證明,由於某種原因,新的哈希值變得不同。

linecache.getline返回'\\n'字符。 https://docs.python.org/2/library/linecache.html

此代碼應工作:

salt = linecache.getline(userin + '.userdat', 2).strip()
saltin = newin + salt
hashed_newin = (hashlib.sha256(saltin.encode()).hexdigest())
realin = linecache.getline('file.dat', 1).strip()
if hashed_newin == realin:
    return True

暫無
暫無

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

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