php pwdhash,PHP: hash_hmac - Manual

with Tiger algorithm in PHP 5.1 - 5.3

and want to upgrade to PHP 5.4 (or newer?).

The problem occured because the order of bytes for Tiger

was changed to big endian since PHP 5.4.0.

The two functions below assert $algo is one of Tiger algorithms,

for example tiger160,4.

*/

# replaces hash('tiger...functionhash_tiger_rev($algo,$data,$raw_output=false) {$len=intval(substr($algo,5,3));# 128, 160 or 192 bits$times=substr($algo,9,1);# 3 or 4$revhash=implode("",array_map("strrev",str_split(hash('tiger192,'.$times,$data,true),8)));

if ($len<192)$revhash=substr($revhash,0,$len>>3);

return$raw_output?$revhash:bin2hex($revhash);

}# replaces hash_hmac('tiger...functionhash_hmac_tiger_rev($algo,$data,$key,$raw_output=false) {

if (strlen($key) >64)$key=hash_tiger_rev($algo,$key);$key=str_pad($key,64,chr(0));$o_pad=str_repeat("\\",64) ^$key;# "\" = chr(0x5C)$i_pad=str_repeat("6",64) ^$key;# "6" = chr(0x36)returnhash_tiger_rev($algo,$o_pad.hash_tiger_rev($algo,$i_pad.$data,true),$raw_output);

}# always the new version of tigerfunctionhash_hmac_new($algo,$data,$key,$raw_output=false) {

if (phpversion() >'5.4'|| !preg_match('/^tiger(128|160|192),(3|4)$/',$algo))

returnhash_hmac($algo,$data,$key,$raw_output);

else

returnhash_hmac_tiger_rev($algo,$data,$key,$raw_output);

}# always the old version of tigerfunctionhash_hmac_old($algo,$data,$key,$raw_output=false) {

if (phpversion()

returnhash_hmac($algo,$data,$key,$raw_output);

else

returnhash_hmac_tiger_rev($algo,$data,$key,$raw_output);

}# let's test it$algo='tiger160,4';$pwd='foo';$key='bar';

echohash_hmac($algo,$pwd,$key),"
";

echohash_hmac_tiger_rev($algo,$pwd,$key),"
";

echo"
";

echohash_hmac_old($algo,$pwd,$key),"
";

echohash_hmac_new($algo,$pwd,$key),"
";/* With PHP 5.4 output would be

590546d9f425188da35e5dfa53306ba3953571cc

bd6664330ed96b9b39ee063241b62e43f546a49d

bd6664330ed96b9b39ee063241b62e43f546a49d

590546d9f425188da35e5dfa53306ba3953571cc

With PHP 5.3

bd6664330ed96b9b39ee063241b62e43f546a49d

590546d9f425188da35e5dfa53306ba3953571cc

bd6664330ed96b9b39ee063241b62e43f546a49d

590546d9f425188da35e5dfa53306ba3953571cc

*/?>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值