【发布时间】:2013-09-19 13:33:26
【问题描述】:
在学习密码哈希和保存在数据库中时,我发现了这篇文章:https://crackstation.net/hashing-security.htm#phpsourcecode
除了这个我无法理解的函数之外,一切都很清楚,为什么不使用普通相等?这意味着什么:比较两个字符串 $a 和 $b 在长度恒定的时间内。
// Compares two strings $a and $b in length-constant time.
function slow_equals($a, $b)
{
$diff = strlen($a) ^ strlen($b);
for($i = 0; $i < strlen($a) && $i < strlen($b); $i++)
{
$diff |= ord($a[$i]) ^ ord($b[$i]);
}
return $diff === 0;
}
【问题讨论】:
-
我假设它指的是一个“等于”函数,该函数具有恒定的执行时间以防止基于时间的攻击。 Length-constant 可能是指执行时间与字符串长度无关。当然,我可能是错的。