src/SecurityUtil/UrlSafeCipher.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\SecurityUtil;
  3. /**
  4.  * Cipher, this class can be used to encrypt string values to a format which is safe to use in urls.
  5.  */
  6. class UrlSafeCipher extends Cipher {
  7.     /**
  8.      * Encrypt the given value so that it's unreadable and that it can be used in an url.
  9.      *
  10.      * @param string $value
  11.      *
  12.      * @return string
  13.      */
  14.     public function encrypt($value) {
  15.         return bin2hex(parent::encrypt($value));
  16.     }
  17.     /**
  18.      * Decrypt the given value so that it's readable again.
  19.      *
  20.      * @param string $value
  21.      *
  22.      * @return string
  23.      */
  24.     public function decrypt($value) {
  25.         return parent::decrypt($this->hex2bin($value));
  26.     }
  27.     /**
  28.      * Decodes a hexadecimal encoded binary string.
  29.      * PHP version >= 5.4 has a function for this by default.
  30.      *
  31.      * @param String $hexString
  32.      *
  33.      * @return string
  34.      */
  35.     public function hex2bin($hexString) {
  36.         $pos 0;
  37.         $result '';
  38.         while ($pos strlen($hexString)) {
  39.             if (strpos(" \t\n\r"$hexString{$pos}) !== false) {
  40.                 $pos++;
  41.             } else {
  42.                 $code hexdec(substr($hexString$pos2));
  43.                 $pos $pos 2;
  44.                 $result .= chr($code);
  45.             }
  46.         }
  47.         return $result;
  48.     }
  49. }