Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php hash_equals和strcmp函数之间的差异_Php - Fatal编程技术网

Php hash_equals和strcmp函数之间的差异

Php hash_equals和strcmp函数之间的差异,php,Php,hash_equals-定时攻击安全字符串比较。 我想知道什么是安全的 我看到了hash_equals的实现 $result = 0; for ($i = 0; $i < $len; $i++) { $result |= (ord($known_string[$i]) ^ ord($user_string[$i])); } // They are only identical strings if $result is exactly 0...

hash_equals-定时攻击安全字符串比较。 我想知道什么是安全的

我看到了hash_equals的实现

$result = 0;
    for ($i = 0; $i < $len; $i++) {
        $result |= (ord($known_string[$i]) ^ ord($user_string[$i]));
    }
    // They are only identical strings if $result is exactly 0...
    return 0 === $result;
$result=0;
对于($i=0;$i<$len;$i++){
$result |=(ord($known_string[$i])^ord($user_string[$i]);
}
//如果$result正好为0,则它们只是相同的字符串。。。
返回0=$result;

我想知道时间比较在哪里

通常,字符串比较(
strcmp
=
)会在第一个不匹配的字符上中断,因此如果您的密码是
12345
,攻击者提供
9xxxx
,然后提供
1xxxx
,她可以测量两个比较之间的时间差,并推断第二个字符串更正确(因为第二次比较花费了更多的时间)。
hash_equals
通过始终比较两个字符串的所有字符来消除这种类型的攻击,不管它们是否匹配。因此,越来越多的正确字符串将花费相同的时间