定时攻击安全字符串比较
原型
hash_equals( string $a, string $b )
描述
无论它们是否相等,使用相同的时间比较两个字符串。
参数
$a
(string)
(Required)
预期的字符串。
$b
(string)
(Required)
实际的,用户提供的字符串。
返回值
(bool)
字符串是否相等。
源文件
路径:wp-includes/compat.php
<?php
...
function hash_equals( $a, $b ) {
$a_length = strlen( $a );
if ( $a_length !== strlen( $b ) ) {
return false;
}
$result = 0;
// Do not attempt to "optimize" this.
for ( $i = 0; $i < $a_length; $i++ ) {
$result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
}
return $result === 0;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/hash_equals/