Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
110 lines (96 sloc) 3.21 KB
#!/usr/bin/perl
use strict;
# http://forum.sources.ru/index.php?showtopic=225504
{
no strict;
sub str2blks_pvpgn { my ($str) = @_;
$nblk = ((length($str) + 8) >> 6) + 1;
for($i=0; $i < $nblk * 16; $i++) {$blks[$i] = 0;}
for($i=0; $i < length($str); $i++) {
$blks[$i >> 2] |= ord(substr($str, $i, 1)) << ( ($i % 4) * 8);
}
return @blks;
}
sub safe_add { my ($x, $y) = @_;
$lsw = ($x & 0xFFFF) + ($y & 0xFFFF);
$msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16);
return ($msw << 16) | ($lsw & 0xFFFF);
}
sub safe_not { my ( $num ) = @_;
$lsw = (~($num & 0xFFFF)) & 0xFFFF;
$msw = (~($num >> 16)) & 0xFFFF;
return ($msw << 16) | $lsw;
}
sub safe_rol { my ( $num, $amt ) = @_;
$leftmask = 0xffff | (0xffff << 16);
$leftmask <<= 32 - $amt;
$rightmask = 0xffff | (0xffff << 16);
$rightmask <<= $amt;
$rightmask = safe_not($rightmask);
$remains = $num & $leftmask;
$remains >>= 32 - $amt;
$remains &= $rightmask;
$res = ($num << $amt) | $remains;
return $res;
}
sub ft { my ($t, $b, $c, $d) = @_;
($t < 20) and return ($b & $c) | ((safe_not($b)) & $d);
($t < 40) and return $d ^ $c ^ $b;
($t < 60) and return ($c & $b) | ($d & $c) | ($d & $b);
return $d ^ $c ^ $b;
}
sub kt { my ($t) = @_;
if ($t < 20) {
return 0x5a82 << 16 | 0x7999;
} elsif ($t < 40) {
return 0x6ed9 << 16 | 0xeba1;
} elsif ($t < 60) {
return 0x8f1b << 16 | 0xbcdc;
} else {
return 0xca62 << 16 | 0xc1d6;
}
}
sub pvpgn_hash { my ($str) = @_;
@x = str2blks_pvpgn($str);
$a = 0x6745 << 16 | 0x2301;
$b = 0xefcd << 16 | 0xab89;
$c = 0x98ba << 16 | 0xdcfe;
$d = 0x1032 << 16 | 0x5476;
$e = 0xc3d2 << 16 | 0xe1f0;
for($i = 0; $i < scalar(@x); $i += 16) {
$olda = $a;
$oldb = $b;
$oldc = $c;
$oldd = $d;
$olde = $e;
for($j = 0; $j < 16; $j++) {
$w[$j] = $x[$i+$j];
}
for($j = 0; $j < 64; $j++) {
$ww = $w[$j] ^ $w[$j+8] ^ $w[$j+2] ^ $w[$j+13];
$w[$j+16] = 1 << ($ww%32);
}
for($j = 0; $j < 80; $j++) {
if ($j<20)
{
$t = safe_add(safe_add(safe_rol($a, 5), ft($j, $b, $c, $d)), safe_add(safe_add($e, $w[$j]), kt($j)));
}
else
{
$t = safe_add(safe_add(safe_rol($t, 5), ft($j, $b, $c, $d)), safe_add(safe_add($e, $w[$j]), kt($j)));
}
$e = $d;
$d = $c;
$c = safe_rol($b, 30);
$b = $a;
$a = $t;
}
$a = safe_add($t, $olda);
$b = safe_add($b, $oldb);
$c = safe_add($c, $oldc);
$d = safe_add($d, $oldd);
$e = safe_add($e, $olde);
}
return sprintf("%08x%08x%08x%08x%08x",$a,$b,$c,$d,$e);
}
}