我一直试图找到有关是否password_verify()
使用长度 - 恒定时间比较来避免定时攻击的信息.
现在,简单的例子:
$hash = '$2y$10$HH3906lfby7HOy1N3duQh.Kju.84ct6AcMZm2p/SYZsZSXuYWvvT.';
$startTime = microtime(TRUE);
password_verify('rasmuslerdorf', $hash);
$endTime = microtime(TRUE);
$time = $endTime - $startTime;
Run Code Online (Sandbox Code Playgroud)
这总是产生稍微不同的输出,根据这篇文章("为什么这个页面上的散列码比较"长度常数"时间段中的散列?"段落),可以用于定时攻击来获取散列.我认为这些结果看起来有点随机,但它们肯定不会保持不变.
问题是,是否password_verify()
使用长度 - 恒定时间比较来避免定时攻击?在文档中没有关于它的信息,并且由于我的浅薄经验,我无法很好地解释函数处理时间结果.
sho*_*ild 10
答案是肯定它使用长度恒定的时间比较.
这是php的password_verify函数的摘录
/* We're using this method instead of == in order to provide
* resistance towards timing attacks. This is a constant time
* equality check that will always check every byte of both
* values. */
for (i = 0; i < hash_len; i++) {
status |= (ret->val[i] ^ hash[i]);
}
Run Code Online (Sandbox Code Playgroud)
您可以在https://github.com/php/php-src/blob/master/ext/standard/password.c上查看完整的源代码.