php hash equals,hash_equals

{

if (func_num_args() !==2) {// handle wrong parameter count as the native implentationtrigger_error('hash_equals() expects exactly 2 parameters, '.func_num_args() .' given',E_USER_WARNING);

returnnull;

}

if (is_string($known_string) !==true) {trigger_error('hash_equals(): Expected known_string to be a string, '.gettype($known_string) .' given',E_USER_WARNING);

returnfalse;

}$known_string_len=strlen($known_string);$user_string_type_error='hash_equals(): Expected user_string to be a string, '.gettype($user_string) .' given';// prepare wrong type error message now to reduce the impact of string concatenation and the gettype callif (is_string($user_string) !==true) {trigger_error($user_string_type_error,E_USER_WARNING);// prevention of timing attacks might be still possible if we handle $user_string as a string of diffent length (the trigger_error() call increases the execution time a bit)$user_string_len=strlen($user_string);$user_string_len=$known_string_len+1;

} else {$user_string_len=$known_string_len+1;$user_string_len=strlen($user_string);

}

if ($known_string_len!==$user_string_len) {$res=$known_string^$known_string;// use $known_string instead of $user_string to handle strings of diffrent length.$ret=1;// set $ret to 1 to make sure false is returned} else {$res=$known_string^$user_string;$ret=0;

}

for ($i=strlen($res) -1;$i>=0;$i--) {$ret|=ord($res[$i]);

}

return$ret===0;

}

}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值