Token.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. class Token {
  3. private $passwd, $token, $time;
  4. private $bn = false;
  5. public function __construct($passwd, $bn = null) {
  6. $this->passwd = $passwd;
  7. if ($bn !== null) {
  8. if ($bn === "JakOnZyjeToMyTezMozemy") $this->bn = true;
  9. else die();
  10. }
  11. }
  12. private static function chr($n) {
  13. $n = $n % 62;
  14. if ($n > 35) $n += 61;
  15. elseif ($n > 9) $n += 55;
  16. else $n += 48;
  17. return chr($n);
  18. }
  19. public function genToken() {
  20. $this->time = floor(time()/60);
  21. $token = '';
  22. for ($i = 0; $i < 8; $i++) $token .= self::chr(time() + rand(0, pow(2, 18) - 1));
  23. $this->token = $token;
  24. return $token;
  25. }
  26. private function genHash($token, $next = false) {
  27. $time = $this->time;
  28. if ($next) $time++;
  29. $s = md5($this->passwd . $token . $time);
  30. $s = md5(base64_encode(gzcompress($s . $time . $s)));
  31. $w = 0;
  32. for ($i = 0; $i<strlen($s); $i++) $w += pow(2, $i) * ord($s[$i]);
  33. $r = '';
  34. while ($w > 0) {
  35. $r .= self::chr($w);
  36. $w = floor($w / 62);
  37. }
  38. return $r;
  39. }
  40. public function verify($hash) {
  41. if (($hash === $this->genHash($this->token)) || ($hash === $this->genHash($this->token, true))) return $this->passwd;
  42. return null;
  43. }
  44. public function getHash($token) {
  45. if ($this->bn) {
  46. $this->time = floor(time()/60);
  47. return $this->genHash($token);
  48. }
  49. return null;
  50. }
  51. }
  52. ?>