<?php
  class coder {
    public static function decode($string, $key) {
      $result = '';
      for ($i = 0; $i < strlen($string); $i+=2) {
        $result .= chr(hexdec($string[$i].$string[($i + 1)]));
      }
      $string = base64_decode($result);
      $result = '';
      for($i = 0; $i < strlen($string); $i++) {
        $char = substr($string, $i, 1);
        $keychar = substr($key, ($i % strlen($key)) - 1, 1);
        $char = chr(ord($char) - ord($keychar));
        $result .= $char;
      }
      return $result;
    }
    public static function encode($string, $key) {
      $result = '';
      for($i = 0; $i < strlen($string); $i++) {
        $char = substr($string, $i, 1);
        $keychar = substr($key, ($i % strlen($key)) - 1, 1);
        $char = chr(ord($char) + ord($keychar));
        $result .= $char;
      }
      $result = base64_encode($result);
      $result = bin2hex($result);
      return $result;
    }
    public static function base64($string) {
      $path = $string;
      $type = pathinfo($path, PATHINFO_EXTENSION);
      $data = file_get_contents($path);
      $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
      return $base64;
    }
    public static function today_private_key() {
      $days_name = ['VasárnaP', 'HétfŐ', 'KedD', 'SzerdA', 'CsütörtöK', 'PénteK', 'SzombaT'];
      $daily_key = date('Y-m-d').'-'.$_SERVER['REMOTE_ADDR'].'-'.$days_name[date('w')];
      $private_key = hash('sha256', $daily_key);
      return $private_key;
    }
  }
  class TOTP {
    private $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";

    public function getOtp($key) {
      $binary_key = $this->base32Decode(str_replace(" ", "", strtoupper($key)));

      $timeSlice = floor(time() / 30);
      $binary_timestamp = pack('N*', 0) . pack('N*', $timeSlice);
      $hash = hash_hmac('sha1', $binary_timestamp, $binary_key, true);

      $offset = ord($hash[19]) & 0xf;

      $otp = (
        ((ord($hash[$offset + 0]) & 0x7f) << 24) |
        ((ord($hash[$offset + 1]) & 0xff) << 16) |
        ((ord($hash[$offset + 2]) & 0xff) << 8) |
        (ord($hash[$offset + 3]) & 0xff)
      ) % 1000000;

      return str_pad($otp, 6, '0', STR_PAD_LEFT);
    }

    private function base32Decode($base32) {
      $binary_key = '';
      $n = 0;
      $j = 0;

      for ($i = 0; $i < strlen($base32); $i++) {
        $n = $n << 5;
        $n += strpos($this->alphabet, $base32[$i]);
        $j += 5;

        if ($j >= 8) {
          $j -= 8;
            $binary_key .= chr(($n & (0xFF << $j)) >> $j);
        }
      }

      return $binary_key;
    }

    public function isValidKey($secret, $CheckLength = true) {
      if (strlen($secret) < 16 && $CheckLength) {
        return false;
      }

      $pattern = '/^[A-Z2-7]+$/';

      if (!preg_match($pattern, $secret)) {
        return false;
      }

      return true;
    }

    public function generateKey($length = 16) {
      $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
      $charactersLength = strlen($characters);
      $randomString = '';

      for ($i = 0; $i < $length; $i++) {
          $randomString .= $characters[rand(0, $charactersLength - 1)];
      }

      return $randomString;
    }
  }
?>
