2015-07-05 65 views
0

我正在寫一個python腳本(在python3.4),它散列密碼,另一個腳本登錄用戶Python的登錄程序,用bcrypt

該散列密碼之一:

import bcrypt 
password1 = input("pass: ") 
password=b"password1" 
salt = bcrypt.gensalt() 
hashed = bcrypt.hashpw(password, salt) 

f=open("passwd.txt","w") 
print(hashed,file=f) 
f.close() 

f1=open("salty.txt","w") 
print(salt,file=f1) 
f1.close() 



if bcrypt.hashpw(password, hashed) == hashed: 
    print("It Matches") 
else: 
    print("It Does not Match") 

另:

import bcrypt 
password1 = input("pass: ") 
password=b"password1" 
f=open("passwd.txt","r") 
for i in f: 
    hashed1=i 
f.close() 

hashed=b"hashed1" 

f1=open("salty.txt","r") 
for j in f1: 
    salt=j 
f1.close() 

if bcrypt.hashpw(password, hashed) == hashed: 
    print("It Matches") 
else: 
    print("It Does not Match) 

,如果我嘗試使用第二個輸入密碼,並將它與壽另一方面,它告訴我:

ValueError: Invalid salt 

回答

0

它看起來像在第二種情況下傳遞hashpw函數的字符串文字hashed1。這是不正確的用法。據我瞭解,你應該傳遞用戶散列密碼的函數,因爲bcrypt會將salt作爲散列的一部分。

它看起來像你可能試圖將一個unicode字符串的散列錯誤地轉換爲一個字節串。我看到您有hashed=b"hashed1",它將文字字符串分配給變量hashed嘗試hashed = bytes(hashed1)

+0

那麼你會怎麼做soxnd部分? –

+0

請參閱我的回答,我敢肯定你錯誤地試圖將Unicode轉換爲字節串 –