2014-07-18 31 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!而已。非常感谢你 :-) –