[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/web-token/jwt-core/Util/ -> BigInteger.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  /*
   6   * The MIT License (MIT)
   7   *
   8   * Copyright (c) 2014-2020 Spomky-Labs
   9   *
  10   * This software may be modified and distributed under the terms
  11   * of the MIT license.  See the LICENSE file for details.
  12   */
  13  
  14  namespace Jose\Component\Core\Util;
  15  
  16  use Brick\Math\BigInteger as BrickBigInteger;
  17  use function chr;
  18  use InvalidArgumentException;
  19  
  20  /**
  21   * @internal
  22   */
  23  class BigInteger
  24  {
  25      /**
  26       * Holds the BigInteger's value.
  27       *
  28       * @var BrickBigInteger
  29       */
  30      private $value;
  31  
  32      private function __construct(BrickBigInteger $value)
  33      {
  34          $this->value = $value;
  35      }
  36  
  37      /**
  38       * @return BigInteger
  39       */
  40      public static function createFromBinaryString(string $value): self
  41      {
  42          $res = unpack('H*', $value);
  43          if (false === $res) {
  44              throw new InvalidArgumentException('Unable to convert the value');
  45          }
  46          $data = current($res);
  47  
  48          return new self(BrickBigInteger::fromBase($data, 16));
  49      }
  50  
  51      /**
  52       * @return BigInteger
  53       */
  54      public static function createFromDecimal(int $value): self
  55      {
  56          return new self(BrickBigInteger::of($value));
  57      }
  58  
  59      /**
  60       * @return BigInteger
  61       */
  62      public static function createFromBigInteger(BrickBigInteger $value): self
  63      {
  64          return new self($value);
  65      }
  66  
  67      /**
  68       * Converts a BigInteger to a binary string.
  69       */
  70      public function toBytes(): string
  71      {
  72          if ($this->value->isEqualTo(BrickBigInteger::zero())) {
  73              return '';
  74          }
  75  
  76          $temp = $this->value->toBase(16);
  77          $temp = 0 !== (mb_strlen($temp, '8bit') & 1) ? '0'.$temp : $temp;
  78          $temp = hex2bin($temp);
  79          if (false === $temp) {
  80              throw new InvalidArgumentException('Unable to convert the value into bytes');
  81          }
  82  
  83          return ltrim($temp, chr(0));
  84      }
  85  
  86      /**
  87       * Adds two BigIntegers.
  88       *
  89       *  @param BigInteger $y
  90       *
  91       *  @return BigInteger
  92       */
  93      public function add(self $y): self
  94      {
  95          $value = $this->value->plus($y->value);
  96  
  97          return new self($value);
  98      }
  99  
 100      /**
 101       * Subtracts two BigIntegers.
 102       *
 103       *  @param BigInteger $y
 104       *
 105       *  @return BigInteger
 106       */
 107      public function subtract(self $y): self
 108      {
 109          $value = $this->value->minus($y->value);
 110  
 111          return new self($value);
 112      }
 113  
 114      /**
 115       * Multiplies two BigIntegers.
 116       *
 117       * @param BigInteger $x
 118       *
 119       *  @return BigInteger
 120       */
 121      public function multiply(self $x): self
 122      {
 123          $value = $this->value->multipliedBy($x->value);
 124  
 125          return new self($value);
 126      }
 127  
 128      /**
 129       * Divides two BigIntegers.
 130       *
 131       * @param BigInteger $x
 132       *
 133       *  @return BigInteger
 134       */
 135      public function divide(self $x): self
 136      {
 137          $value = $this->value->dividedBy($x->value);
 138  
 139          return new self($value);
 140      }
 141  
 142      /**
 143       * Performs modular exponentiation.
 144       *
 145       * @param BigInteger $e
 146       * @param BigInteger $n
 147       *
 148       * @return BigInteger
 149       */
 150      public function modPow(self $e, self $n): self
 151      {
 152          $value = $this->value->modPow($e->value, $n->value);
 153  
 154          return new self($value);
 155      }
 156  
 157      /**
 158       * Performs modular exponentiation.
 159       *
 160       * @param BigInteger $d
 161       *
 162       * @return BigInteger
 163       */
 164      public function mod(self $d): self
 165      {
 166          $value = $this->value->mod($d->value);
 167  
 168          return new self($value);
 169      }
 170  
 171      public function modInverse(BigInteger $m): BigInteger
 172      {
 173          return new self($this->value->modInverse($m->value));
 174      }
 175  
 176      /**
 177       * Compares two numbers.
 178       *
 179       * @param BigInteger $y
 180       */
 181      public function compare(self $y): int
 182      {
 183          return $this->value->compareTo($y->value);
 184      }
 185  
 186      /**
 187       * @param BigInteger $y
 188       */
 189      public function equals(self $y): bool
 190      {
 191          return $this->value->isEqualTo($y->value);
 192      }
 193  
 194      /**
 195       * @param BigInteger $y
 196       *
 197       * @return BigInteger
 198       */
 199      public static function random(self $y): self
 200      {
 201          return new self(BrickBigInteger::randomRange(0, $y->value));
 202      }
 203  
 204      /**
 205       * @param BigInteger $y
 206       *
 207       * @return BigInteger
 208       */
 209      public function gcd(self $y): self
 210      {
 211          return new self($this->value->gcd($y->value));
 212      }
 213  
 214      /**
 215       * @param BigInteger $y
 216       */
 217      public function lowerThan(self $y): bool
 218      {
 219          return $this->value->isLessThan($y->value);
 220      }
 221  
 222      public function isEven(): bool
 223      {
 224          return $this->value->isEven();
 225      }
 226  
 227      public function get(): BrickBigInteger
 228      {
 229          return $this->value;
 230      }
 231  }


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