2014-07-18 27 views
0

我需要將原始的php auth驗證移動到python3。我有這樣的代碼:PHP Bcrypt移到Python3

// hash from db 
$hash = "$2y$14$00sMXcXPyTthEv9m5dszwuT8VUU6KK1HtCunemfutphrbCHZoIz0e"; 

// the hash is used as salt 
$result = crypt($password, $hash); 

//result should match the hash if pasword is correct 
$result = "$2y$14$00sMXcXPyTthEv9m5dszwuT8VUU6KK1HtCunemfutphrbCHZoIz0e"; 

它與參數做成本= 14

什麼來實現它在python3最好的lib?

編輯: 我使用了lib py-bcrypt

>>> import bcrypt 
>>> password = "ahoj" 
>>> db_hash = "$2y$14$00sMXcXPyTthEv9m5dszwuT8VUU6KK1HtCunemfutphrbCHZoIz0e" 
>>> computed_hash = bcrypt.hashpw(password, db_hash) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: Invalid salt 

爲什麼鹽無效?

在PHP它的工作原理:

password=ahoj 
hash=$2y$14$00sMXcXPyTthEv9m5dszwuT8VUU6KK1HtCunemfutphrbCHZoIz0e 
result=$2y$14$00sMXcXPyTthEv9m5dszwuT8VUU6KK1HtCunemfutphrbCHZoIz0e 

什麼是蟒蛇有什麼不同?

+0

這是一個標準的bcrypt散列,任何庫都會處理它。 –

回答

1

如果要比較的密碼,使用checkpw函數,而不是hashpw

hashed = bcrypt.hashpw(password, bcrypt.gensalt()) 

你可以找到更多的細節:只有當您生成一個新的哈希

if bcrypt.checkpw(passsword, db_hash): 
    print "It matches" 
else: 
    print "It does not match" 

鹽需要README文件。

+0

謝謝你的回答。它仍然沒有工作。我得到** ValueError:無效的hashed_pa​​ssword salt ** –

+1

@JoeBobson:將「$ 2y」替換爲「$ 2a」 - 「py-bcrypt」目前只能理解後者。對於大多數密碼這應該沒有什麼區別。 – hop

+0

Yeaah!而已。非常感謝你 :-) –