[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Crypt/Cipher/ -> CryptoCipher.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Crypt\Cipher;
  11  
  12  use Joomla\Crypt\CipherInterface;
  13  use Joomla\Crypt\Key;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('JPATH_PLATFORM') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * Crypt cipher for encryption, decryption and key generation via the php-encryption library.
  21   *
  22   * @since       3.5
  23   * @deprecated  5.0   Without replacement use SodiumCipher
  24   */
  25  class CryptoCipher implements CipherInterface
  26  {
  27      /**
  28       * Method to decrypt a data string.
  29       *
  30       * @param   string  $data  The encrypted string to decrypt.
  31       * @param   Key     $key   The key object to use for decryption.
  32       *
  33       * @return  string  The decrypted data string.
  34       *
  35       * @since   3.5
  36       * @throws  \RuntimeException
  37       */
  38      public function decrypt($data, Key $key)
  39      {
  40          // Validate key.
  41          if ($key->getType() !== 'crypto') {
  42              throw new \InvalidArgumentException('Invalid key of type: ' . $key->getType() . '.  Expected crypto.');
  43          }
  44  
  45          // Decrypt the data.
  46          try {
  47              return \Crypto::Decrypt($data, $key->getPublic());
  48          } catch (\InvalidCiphertextException $ex) {
  49              throw new \RuntimeException('DANGER! DANGER! The ciphertext has been tampered with!', $ex->getCode(), $ex);
  50          } catch (\CryptoTestFailedException $ex) {
  51              throw new \RuntimeException('Cannot safely perform decryption', $ex->getCode(), $ex);
  52          } catch (\CannotPerformOperationException $ex) {
  53              throw new \RuntimeException('Cannot safely perform decryption', $ex->getCode(), $ex);
  54          }
  55      }
  56  
  57      /**
  58       * Method to encrypt a data string.
  59       *
  60       * @param   string  $data  The data string to encrypt.
  61       * @param   Key     $key   The key object to use for encryption.
  62       *
  63       * @return  string  The encrypted data string.
  64       *
  65       * @since   3.5
  66       * @throws  \RuntimeException
  67       */
  68      public function encrypt($data, Key $key)
  69      {
  70          // Validate key.
  71          if ($key->getType() !== 'crypto') {
  72              throw new \InvalidArgumentException('Invalid key of type: ' . $key->getType() . '.  Expected crypto.');
  73          }
  74  
  75          // Encrypt the data.
  76          try {
  77              return \Crypto::Encrypt($data, $key->getPublic());
  78          } catch (\CryptoTestFailedException $ex) {
  79              throw new \RuntimeException('Cannot safely perform encryption', $ex->getCode(), $ex);
  80          } catch (\CannotPerformOperationException $ex) {
  81              throw new \RuntimeException('Cannot safely perform encryption', $ex->getCode(), $ex);
  82          }
  83      }
  84  
  85      /**
  86       * Method to generate a new encryption key object.
  87       *
  88       * @param   array  $options  Key generation options.
  89       *
  90       * @return  Key
  91       *
  92       * @since   3.5
  93       * @throws  \RuntimeException
  94       */
  95      public function generateKey(array $options = array())
  96      {
  97          // Generate the encryption key.
  98          try {
  99              $public = \Crypto::CreateNewRandomKey();
 100          } catch (\CryptoTestFailedException $ex) {
 101              throw new \RuntimeException('Cannot safely create a key', $ex->getCode(), $ex);
 102          } catch (\CannotPerformOperationException $ex) {
 103              throw new \RuntimeException('Cannot safely create a key', $ex->getCode(), $ex);
 104          }
 105  
 106          // Explicitly flag the private as unused in this cipher.
 107          $private = 'unused';
 108  
 109          return new Key('crypto', $private, $public);
 110      }
 111  
 112      /**
 113       * Check if the cipher is supported in this environment.
 114       *
 115       * @return  boolean
 116       *
 117       * @since   4.0.0
 118       */
 119      public static function isSupported(): bool
 120      {
 121          try {
 122              \Crypto::RuntimeTest();
 123  
 124              return true;
 125          } catch (\CryptoTestFailedException $e) {
 126              return false;
 127          }
 128      }
 129  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer