<?php
function PBKDF1($pass, $salt, $count, $cb)
{
static $base;
static $extra;
static $extracount= 0;
static $hashno;
static $state = 0;
if ($state == 0)
{
$hashno = 0;
$state = 1;
$key = $pass . $salt;
$base = sha1($key, true); for($i = 2; $i < $count; $i++)
{
$base = sha1($base, true); }
}
$result = "";
if ($extracount > 0)
{
$rlen = strlen($extra) - $extracount; if ($rlen >= $cb)
{
$result = substr($extra, $extracount, $cb); if ($rlen > $cb)
{
$extracount += $cb;
}
else
{
$extra = null;
$extracount = 0;
}
return $result;
}
$result = substr($extra, $rlen, $rlen); }
$current = "";
$clen = 0;
$remain = $cb - strlen($result); while ($remain > $clen)
{
if ($hashno == 0)
{
$current = sha1($base, true); }
else if ($hashno < 1000)
{
$tmp = $n . $base;
$current .= sha1($tmp, true); }
$hashno++;
}
// $current now holds at least as many bytes as we need
$result .= substr($current, 0, $remain);
// Save any left over bytes for any future requests
if ($clen > $remain)
{
$extra = $current;
$extracount = $remain;
}
return $result;
}
PD9waHAKCmZ1bmN0aW9uIFBCS0RGMSgkcGFzcywgJHNhbHQsICRjb3VudCwgJGNiKQp7CiAgc3RhdGljICRiYXNlOwogIHN0YXRpYyAkZXh0cmE7CiAgc3RhdGljICRleHRyYWNvdW50PSAwOwogIHN0YXRpYyAkaGFzaG5vOwogIHN0YXRpYyAkc3RhdGUgPSAwOwoKICBpZiAoJHN0YXRlID09IDApCiAgewogICAgJGhhc2hubyA9IDA7CiAgICAkc3RhdGUgPSAxOwoKICAgICRrZXkgPSAkcGFzcyAuICRzYWx0OwogICAgJGJhc2UgPSBzaGExKCRrZXksIHRydWUpOwogICAgZm9yKCRpID0gMjsgJGkgPCAkY291bnQ7ICRpKyspCiAgICB7CiAgICAgICRiYXNlID0gc2hhMSgkYmFzZSwgdHJ1ZSk7CiAgICB9CiAgfQoKICAgICAgJHJlc3VsdCA9ICIiOwoKICAgICAgaWYgKCRleHRyYWNvdW50ID4gMCkKICAgICAgewogICAgICAgICRybGVuID0gc3RybGVuKCRleHRyYSkgLSAkZXh0cmFjb3VudDsKICAgICAgICBpZiAoJHJsZW4gPj0gJGNiKQogICAgICAgIHsKICAgICAgICAgICRyZXN1bHQgPSBzdWJzdHIoJGV4dHJhLCAkZXh0cmFjb3VudCwgJGNiKTsKICAgICAgICAgIGlmICgkcmxlbiA+ICRjYikKICAgICAgICAgIHsKICAgICAgICAgICAgJGV4dHJhY291bnQgKz0gJGNiOwogICAgICAgICAgfQogICAgICAgICAgZWxzZQogICAgICAgICAgewogICAgICAgICAgICAkZXh0cmEgPSBudWxsOwogICAgICAgICAgICAkZXh0cmFjb3VudCA9IDA7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gJHJlc3VsdDsKICAgICAgICB9CiAgICAgICAgJHJlc3VsdCA9IHN1YnN0cigkZXh0cmEsICRybGVuLCAkcmxlbik7CiAgICAgIH0KCiAgICAgICRjdXJyZW50ID0gIiI7CiAgICAgICRjbGVuID0gMDsKICAgICAgJHJlbWFpbiA9ICRjYiAtIHN0cmxlbigkcmVzdWx0KTsKICAgICAgd2hpbGUgKCRyZW1haW4gPiAkY2xlbikKICAgICAgewogICAgICAgIGlmICgkaGFzaG5vID09IDApCiAgICAgICAgewogICAgICAgICAgJGN1cnJlbnQgPSBzaGExKCRiYXNlLCB0cnVlKTsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZiAoJGhhc2hubyA8IDEwMDApCiAgICAgICAgewogICAgICAgICAgJG4gPSBzcHJpbnRmKCIlZCIsICRoYXNobm8pOwogICAgICAgICAgJHRtcCA9ICRuIC4gJGJhc2U7CiAgICAgICAgICAkY3VycmVudCAuPSBzaGExKCR0bXAsIHRydWUpOwogICAgICAgIH0KICAgICAgICAkaGFzaG5vKys7CiAgICAgICAgJGNsZW4gPSBzdHJsZW4oJGN1cnJlbnQpOyAgICAgCiAgICAgIH0KCiAgICAgIC8vICRjdXJyZW50IG5vdyBob2xkcyBhdCBsZWFzdCBhcyBtYW55IGJ5dGVzIGFzIHdlIG5lZWQKICAgICAgJHJlc3VsdCAuPSBzdWJzdHIoJGN1cnJlbnQsIDAsICRyZW1haW4pOwoKICAgICAgLy8gU2F2ZSBhbnkgbGVmdCBvdmVyIGJ5dGVzIGZvciBhbnkgZnV0dXJlIHJlcXVlc3RzCiAgICAgIGlmICgkY2xlbiA+ICRyZW1haW4pCiAgICAgIHsKICAgICAgICAkZXh0cmEgPSAkY3VycmVudDsKICAgICAgICAkZXh0cmFjb3VudCA9ICRyZW1haW47CiAgICAgIH0KCiAgICAgIHJldHVybiAkcmVzdWx0OyAKICAgIH0KICAgIAp2YXJfZHVtcChiaW4yaGV4KFBCS0RGMSgiU3lzdGVtIiwgIkc6TUZYNjJybEFCVzpJVVlBWChpIiwgMTAwLCAyNCkpKTs=