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
*/?>