[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Encrypt/AES/ -> AbstractAES.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2016 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\Encrypt\AES;
  11  
  12  // phpcs:disable PSR1.Files.SideEffects
  13  \defined('JPATH_PLATFORM') or die;
  14  // phpcs:enable PSR1.Files.SideEffects
  15  
  16  /**
  17   * Abstract AES encryption class
  18   *
  19   * @since    4.0.0
  20   */
  21  abstract class AbstractAES
  22  {
  23      /**
  24       * Trims or zero-pads a key / IV
  25       *
  26       * @param   string $key  The key or IV to treat
  27       * @param   int    $size The block size of the currently used algorithm
  28       *
  29       * @return  null|string  Null if $key is null, treated string of $size byte length otherwise
  30       */
  31      public function resizeKey($key, $size)
  32      {
  33          if (empty($key)) {
  34              return null;
  35          }
  36  
  37          $keyLength = \strlen($key);
  38  
  39          if (\function_exists('mb_strlen')) {
  40              $keyLength = mb_strlen($key, 'ASCII');
  41          }
  42  
  43          if ($keyLength == $size) {
  44              return $key;
  45          }
  46  
  47          if ($keyLength > $size) {
  48              if (\function_exists('mb_substr')) {
  49                  return mb_substr($key, 0, $size, 'ASCII');
  50              }
  51  
  52              return substr($key, 0, $size);
  53          }
  54  
  55          return $key . str_repeat("\0", ($size - $keyLength));
  56      }
  57  
  58      /**
  59       * Returns null bytes to append to the string so that it's zero padded to the specified block size
  60       *
  61       * @param   string $string    The binary string which will be zero padded
  62       * @param   int    $blockSize The block size
  63       *
  64       * @return  string  The zero bytes to append to the string to zero pad it to $blockSize
  65       */
  66      protected function getZeroPadding($string, $blockSize)
  67      {
  68          $stringSize = \strlen($string);
  69  
  70          if (\function_exists('mb_strlen')) {
  71              $stringSize = mb_strlen($string, 'ASCII');
  72          }
  73  
  74          if ($stringSize == $blockSize) {
  75              return '';
  76          }
  77  
  78          if ($stringSize < $blockSize) {
  79              return str_repeat("\0", $blockSize - $stringSize);
  80          }
  81  
  82          $paddingBytes = $stringSize % $blockSize;
  83  
  84          return str_repeat("\0", $blockSize - $paddingBytes);
  85      }
  86  }


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