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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the NzoUrlEncryptorBundle package.
  4.  *
  5.  * (c) Ala Eddine Khefifi <alakhefifi@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.     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.      * @return string
  49.      */
  50.     public function encrypt($plainText)
  51.     {
  52.         $encrypted openssl_encrypt($plainText$this->cipherAlgorithm$this->secretKeyOPENSSL_RAW_DATA$this->iv);
  53.         $encrypted $this->iv.$encrypted;
  54.         return $this->base64Encode $this->base64Encode($encrypted) : $encrypted;
  55.     }
  56.     /**
  57.      * @param string $encrypted
  58.      * @return string
  59.      */
  60.     public function decrypt($encrypted)
  61.     {
  62.         $ivLength openssl_cipher_iv_length($this->cipherAlgorithm);
  63.         $encrypted $this->base64Encode $this->base64Decode($encrypted) : $encrypted;
  64.         $iv substr($encrypted0$ivLength);
  65.         $raw substr($encrypted$ivLength);
  66.         $decrypted openssl_decrypt(
  67.             $raw,
  68.             $this->cipherAlgorithm,
  69.             $this->secretKey,
  70.             OPENSSL_RAW_DATA,
  71.             $iv
  72.         );
  73.         return trim($decrypted);
  74.     }
  75.     /**
  76.      * @param string $data
  77.      * @return string
  78.      */
  79.     private function base64Encode($data)
  80.     {
  81.         if ($this->formatBase64Output) {
  82.             return rtrim(strtr(base64_encode($data), '+/''-_'), '=');
  83.         }
  84.         return base64_encode($data);
  85.     }
  86.     /**
  87.      * @param string $data
  88.      * @return string
  89.      */
  90.     private function base64Decode($data)
  91.     {
  92.         if ($this->formatBase64Output) {
  93.             return base64_decode(str_pad(strtr($data'-_''+/'), strlen($data) % 4'='STR_PAD_RIGHT));
  94.         }
  95.         return base64_decode($data);
  96.     }
  97. }