| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- class Token {
- private $passwd, $token, $time;
- private $bn = false;
- public function __construct($passwd, $bn = null) {
- $this->passwd = $passwd;
- if ($bn !== null) {
- if ($bn === "JakOnZyjeToMyTezMozemy") $this->bn = true;
- else die();
- }
- }
- private static function chr($n) {
- $n = $n % 62;
- if ($n > 35) $n += 61;
- elseif ($n > 9) $n += 55;
- else $n += 48;
- return chr($n);
- }
- public function genToken() {
- $this->time = floor(time()/60);
- $token = '';
- for ($i = 0; $i < 8; $i++) $token .= self::chr(time() + rand(0, pow(2, 18) - 1));
- $this->token = $token;
- return $token;
- }
- private function genHash($token, $next = false) {
- $time = $this->time;
- if ($next) $time++;
- $s = md5($this->passwd . $token . $time);
- $s = md5(base64_encode(gzcompress($s . $time . $s)));
- $w = 0;
- for ($i = 0; $i<strlen($s); $i++) $w += pow(2, $i) * ord($s[$i]);
- $r = '';
- while ($w > 0) {
- $r .= self::chr($w);
- $w = floor($w / 62);
- }
- return $r;
- }
- public function verify($hash) {
- if (($hash === $this->genHash($this->token)) || ($hash === $this->genHash($this->token, true))) return $this->passwd;
- return null;
- }
- public function getHash($token) {
- if ($this->bn) {
- $this->time = floor(time()/60);
- return $this->genHash($token);
- }
- return null;
- }
- }
- ?>
|