vendor/nzo/url-encryptor-bundle/Encryptor/Encryptor.php line 72

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the NzoUrlEncryptorBundle package.
  4.  *
  5.  * (c) Ala Eddine Khefifi <alakfpro@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Nzo\UrlEncryptorBundle\Encryptor;
  11. class Encryptor
  12. {
  13.     private const HASH_ALGORITHM 'sha256';
  14.     private $secretKey;
  15.     private $cipherAlgorithm;
  16.     private $base64Encode;
  17.     private $formatBase64Output;
  18.     private $randomPseudoBytes;
  19.     private $iv;
  20.     public function __construct(
  21.         string $secretKey,
  22.         string $cipherAlgorithm,
  23.         bool $base64Encode,
  24.         bool $formatBase64Output,
  25.         bool $randomPseudoBytes
  26.     ) {
  27.         $this->secretKey $secretKey;
  28.         $this->cipherAlgorithm $cipherAlgorithm;
  29.         $this->base64Encode $base64Encode;
  30.         $this->formatBase64Output $formatBase64Output;
  31.         $this->randomPseudoBytes $randomPseudoBytes;
  32.     }
  33.     /**
  34.      * @param string $secretIv
  35.      */
  36.     public function setSecretIv($secretIv)
  37.     {
  38.         $ivLength openssl_cipher_iv_length($this->cipherAlgorithm);
  39.         $secretIv $this->randomPseudoBytes openssl_random_pseudo_bytes($ivLength) : $secretIv;
  40.         $this->iv substr(
  41.             hash_hmac(self::HASH_ALGORITHM$secretIv$this->secretKeytrue),
  42.             0,
  43.             $ivLength
  44.         );
  45.     }
  46.     /**
  47.      * @param string $plainText
  48.      *
  49.      * @return string
  50.      */
  51.     public function encrypt($plainText)
  52.     {
  53.         $encrypted openssl_encrypt($plainText$this->cipherAlgorithm$this->secretKeyOPENSSL_RAW_DATA$this->iv);
  54.         $encrypted $this->iv $encrypted;
  55.         return $this->base64Encode $this->base64Encode($encrypted) : $encrypted;
  56.     }
  57.     /**
  58.      * @param string $encrypted
  59.      *
  60.      * @return string
  61.      */
  62.     public function decrypt($encrypted)
  63.     {
  64.         $ivLength openssl_cipher_iv_length($this->cipherAlgorithm);
  65.         $encrypted $this->base64Encode $this->base64Decode($encrypted) : $encrypted;
  66.         $iv substr($encrypted0$ivLength);
  67.         $raw substr($encrypted$ivLength);
  68.         $decrypted openssl_decrypt(
  69.             $raw,
  70.             $this->cipherAlgorithm,
  71.             $this->secretKey,
  72.             OPENSSL_RAW_DATA,
  73.             $iv
  74.         );
  75.         return trim($decrypted);
  76.     }
  77.     /**
  78.      * @param string $data
  79.      *
  80.      * @return string
  81.      */
  82.     private function base64Encode($data)
  83.     {
  84.         if ($this->formatBase64Output) {
  85.             return rtrim(strtr(base64_encode($data), '+/''-_'), '=');
  86.         }
  87.         return base64_encode($data);
  88.     }
  89.     /**
  90.      * @param string $data
  91.      *
  92.      * @return string
  93.      */
  94.     private function base64Decode($data)
  95.     {
  96.         if ($this->formatBase64Output) {
  97.             return base64_decode(str_pad(strtr($data'-_''+/'), strlen($data) % 4'='STR_PAD_RIGHT), true);
  98.         }
  99.         return base64_decode($datatrue);
  100.     }
  101. }