SHA-1 is built into php >= 4.0, this is what i made to force raw_output in older version of php:
Code:
<?php
function shr($x, $n)
{
return (0x80000000 & $x) ? (($x >> 1) & ~0x80000000 | 0x40000000) >> ($n - 1) : ($x >> $n);
}
function rotl($x, $n)
{
return ($x << $n) | shr($x, (32 - $n));
}
if(phpversion() >= '5.0')
{
function sha160($str, $raw_output = false)
{
return sha1($str, $raw_output);
}
}
else if(phpversion() >= '4.3')
{
function sha160($str, $raw_output = false)
{
$hash = sha1($str);
return ($raw_output) ? bin2hex($hash) : $hash;
}
}
else
{
function sha160($str, $raw_output = false)
{
$h0 = 0x67452301;
$h1 = 0xefcdaB89;
$h2 = 0x98badcfe;
$h3 = 0x10325476;
$h4 = 0xc3d2e1f0;
$l = strlen($str);
$str .= "\x80" . str_repeat("\x0", ($l & ~63) + (($l & 63 < 56) ? 60 : 124)) . pack('N', $l << 3);
for($i = 0; $i < strlen($str); $i += 64)
{
$w = array_values(unpack('N16', substr($str, $i, 64)));
$a = $h0;
$b = $h1;
$c = $h2;
$d = $h3;
$e = $h4;
for($t = 0; $t <= 79; $t++)
{
if(16 <= $t)
{
$w[] = rotl($w[$t - 3] ^ $w[$t - 8] ^ $w[$t - 14] ^ $w[$t - 16], 1);
}
if(0 <= $t && $t <= 19)
{
$f = ($b & $c) ^ (~$b & $d);
$k = 0x5a827999;
}
else if(20 <= $t && $t <= 39)
{
$f = $b ^ $c ^ $d;
$k = 0x6ed9eba1;
}
else if(40 <= $t && $t <= 59)
{
$f = ($b & $c) ^ ($b & $d) ^ ($c & $d);
$k = 0x8f1bbcdc;
}
else if(60 <= $t && $t <= 79)
{
$f = $b ^ $c ^ $d;
$k = 0xca62c1d6;
}
$tmp = intval(rotl($a, 5) + $f + $e + $k + $w[$t]);
$e = $d;
$d = $c;
$c = rotl($b, 30);
$b = $a;
$a = $tmp;
}
$h0 = $a + $h0;
$h1 = $b + $h1;
$h2 = $c + $h2;
$h3 = $d + $h3;
$h4 = $e + $h4;
}
$hash = pack('N*', $h0, $h1, $h2, $h3, $h4);
return ($raw_output) ? bin2hex($hash) : $hash;
}
}
?>
Bookmarks