我需要在php中检查密码的有效性。密码在django中使用pbkdf2 hasher进行哈希处理,哈希算法为sha256。
https://gist.github.com/staydecent/1019680 < - 这个人能够用更简单的sha1哈希做同样的事情。
Verifying Django Password in Ruby on Rails gives non-matching password < - 这个人在ruby中有类似的问题,而不是php。
据我了解,存储django密码,以便散列样式是第一个组件,接下来是复制的数量,然后是salt,然后是散列。
我已成功提取每个组件,但以下php不起作用。它返回正确长度的字符串,但不是正确的内容。我担心哈希码不起作用。我使用的哈希是来自这个网站: https://defuse.ca/php-pbkdf2.htm
这是我的php:
//$enc_password is the password from database
$pieces = explode('$', $enc_password);
$iterations = $pieces[1];
$salt = $pieces[2];
$hash = $pieces[3];
//raw_password is user's entered password
if ($hash == base64_encode(pbkdf2("sha256", $raw_password, $salt, $iterations, 32, true)))
{
echo ("SUCCESS");
}
else
{
echo ("FAILED");
}
任何和所有的帮助表示赞赏!
答案 0 :(得分:2)
我刚刚在Python和PHP中进行了一个简单的测试。
在Python中:
>>> from django.utils.crypto import pbkdf2
>>> import base64, hashlib
>>> hash = pbkdf2("test_password","myHash", 10000, 32, hashlib.sha256)
>>> hash.encode('base64').strip()
'lRKgL0zAmPwIMPNhTy1wJRXWZQwcHlbIW3glYA4LPU8='
在PHP中:
<?php
include 'pbkdf2.php';
echo base64_encode(pbkdf2('sha256', 'test_password', 'myHash', 10000, 32, true));
// result is: lRKgL0zAmPwIMPNhTy1wJRXWZQwcHlbIW3glYA4LPU8=
?>
两个测试的结果是相同的,因此哈希码没有任何问题。你确定你的Django测试使用的是pbkdf2算法吗?您的用户输入示例与测试密码相同吗?