test encrypt - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of test encrypt.php

  1. <?php
  2.  
  3. class AuthToken
  4. {
  5.         private static $key = "секретный ключ";
  6.         private static $iv = "должен быть каждый раз случайным, но для данного решения подойдёт просто секретный";
  7.        
  8.         private static function int2char($int)
  9.         {
  10.                 $char = "";
  11.                 $hex = sprintf("%08x", $int);
  12.                 for ($i =  0; $i < 4; $i++) {
  13.                         $char .= chr(hexdec(substr($hex, $i * 2, 2)));
  14.                 }
  15.                 return $char;
  16.         }
  17.         private static function char2int($char)
  18.         {
  19.                 $int =  0;
  20.                 $hex = "";
  21.                 for ($i = 0; $i < 4; $i++) {
  22.                         $hex .= sprintf(
  23.                                 "%02x",
  24.                                 ord(
  25.                                         $char {
  26.                                                 $i
  27.                                         }
  28.                                 )
  29.                         );
  30.                 }
  31.                 $int = hexdec($hex);
  32.                 return $int;
  33.         }
  34.         public static function create($id, $expire = 0, $mode = 0)
  35.         {
  36.                 $id = intval($id);
  37.                 $expire = intval($expire);
  38.                 $mode = intval($mode);
  39.                 if ($id < 0 || $expire < 0 || $mode < 0) {
  40.                         return null;
  41.                 }
  42.                 $info = array();
  43.                 $info["id"] = $id;
  44.                 $info["time"] = time();
  45.                 $info["expire"] = $expire;
  46.                 $info["mode"] = $mode;
  47.                 $info["rnd"] = ceil(mt_rand(0, 255));
  48.                 $info["sum"] = $info["time"] - $info["expire"] - $info["mode"] - $info["rnd"] - $info["id"];
  49.                 $info = self::int2char($info["id"]) . self::int2char($info["time"]) . self::int2char($info["expire"]) . chr($info["mode"]) . chr($info["rnd"]) . self::int2char($info["sum"]);
  50.                 $token = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5(self::$key), $info, MCRYPT_MODE_OFB, md5(self::$iv));
  51.                 $tokenHex = "";
  52.                 $tokenLength = strlen($token);
  53.                 for ($i =  0; $i < $tokenLength; $i++) {
  54.                         $tokenHex .= sprintf(
  55.                                 "%02x",
  56.                                 ord(
  57.                                         $token {
  58.                                                 $i
  59.                                         }
  60.                                 )
  61.                         );
  62.                 }
  63.                 return $tokenHex;
  64.         }
  65.         public static function check($tokenHex, $mode = null)
  66.         {
  67.                 $token = "";
  68.                 $tokenHexLength = strlen($tokenHex) / 2;
  69.                 for ($i = 0; $i < $tokenHexLength; $i++) {
  70.                         $token .= chr(hexdec(substr($tokenHex, $i * 2, 2)));
  71.                 }
  72.                 $info = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5(self::$key), $token, MCRYPT_MODE_OFB, md5(self::$iv));
  73.                 if (strlen($info) == 18) {
  74.                         $info = array(
  75.                                 "id" => self::char2int(substr($info, 0, 4)), "time" => self::char2int(substr($info, 4, 4)), "expire" => self::char2int(substr($info, 8, 4)),
  76.                                 "mode" => ord(
  77.                                         $info {
  78.                                                 12
  79.                                         }
  80.                                 ), "rnd" => ord(
  81.                                         $info {
  82.                                                 13
  83.                                         }
  84.                                 ), "sum" => self::char2int(substr($info, 14, 4))
  85.                         );
  86.                         if ($info["sum"] == $info["time"] - $info["expire"] - $info["mode"] - $info["rnd"] - $info["id"]) {
  87.                                 if ($info["expire"] > 0) {
  88.                                         if ($info["expire"] + $info["time"] < time()) {
  89.                                                 return false;
  90.                                         }
  91.                                 }
  92.                                 if ($info["mode"] > 0) {
  93.                                         if ($mode !== null) {
  94.                                                 if ($info["mode"] != $mode) {
  95.                                                         return false;
  96.                                                 }
  97.                                         }
  98.                                 }
  99.                                 return $info["id"];
  100.                         } else {
  101.                                 return false;
  102.                         }
  103.                 } else {
  104.                         return false;
  105.                 }
  106.         }
  107. }
  108.  
  109. $auth =  new AuthToken();
  110. echo $auth->create(54);
  111.  
File Description
  • test encrypt
  • PHP Code
  • 11 Feb-2020
  • 2.74 Kb
You can Share it: