[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 declare(strict_types=1); 4 5 /* 6 * The MIT License (MIT) 7 * 8 * Copyright (c) 2014-2019 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 Cose\Key; 15 16 use Assert\Assertion; 17 use FG\ASN1\Universal\BitString; 18 use FG\ASN1\Universal\Integer; 19 use FG\ASN1\Universal\NullObject; 20 use FG\ASN1\Universal\ObjectIdentifier; 21 use FG\ASN1\Universal\Sequence; 22 23 class RsaKey extends Key 24 { 25 public const DATA_N = -1; 26 public const DATA_E = -2; 27 public const DATA_D = -3; 28 public const DATA_P = -4; 29 public const DATA_Q = -5; 30 public const DATA_DP = -6; 31 public const DATA_DQ = -7; 32 public const DATA_QI = -8; 33 public const DATA_OTHER = -9; 34 public const DATA_RI = -10; 35 public const DATA_DI = -11; 36 public const DATA_TI = -12; 37 38 public function __construct(array $data) 39 { 40 parent::__construct($data); 41 Assertion::eq($data[self::TYPE], self::TYPE_RSA, 'Invalid RSA key. The key type does not correspond to a RSA key'); 42 Assertion::keyExists($data, self::DATA_N, 'Invalid RSA key. The modulus is missing'); 43 Assertion::keyExists($data, self::DATA_E, 'Invalid RSA key. The exponent is missing'); 44 } 45 46 public function n(): string 47 { 48 return $this->get(self::DATA_N); 49 } 50 51 public function e(): string 52 { 53 return $this->get(self::DATA_E); 54 } 55 56 public function d(): string 57 { 58 Assertion::true($this->isPrivate(), 'The key is not private.'); 59 60 return $this->get(self::DATA_D); 61 } 62 63 public function p(): string 64 { 65 Assertion::true($this->isPrivate(), 'The key is not private.'); 66 67 return $this->get(self::DATA_P); 68 } 69 70 public function q(): string 71 { 72 Assertion::true($this->isPrivate(), 'The key is not private.'); 73 74 return $this->get(self::DATA_Q); 75 } 76 77 public function dP(): string 78 { 79 Assertion::true($this->isPrivate(), 'The key is not private.'); 80 81 return $this->get(self::DATA_DP); 82 } 83 84 public function dQ(): string 85 { 86 Assertion::true($this->isPrivate(), 'The key is not private.'); 87 88 return $this->get(self::DATA_DQ); 89 } 90 91 public function QInv(): string 92 { 93 Assertion::true($this->isPrivate(), 'The key is not private.'); 94 95 return $this->get(self::DATA_QI); 96 } 97 98 public function other(): array 99 { 100 Assertion::true($this->isPrivate(), 'The key is not private.'); 101 102 return $this->get(self::DATA_OTHER); 103 } 104 105 public function rI(): string 106 { 107 Assertion::true($this->isPrivate(), 'The key is not private.'); 108 109 return $this->get(self::DATA_RI); 110 } 111 112 public function dI(): string 113 { 114 Assertion::true($this->isPrivate(), 'The key is not private.'); 115 116 return $this->get(self::DATA_DI); 117 } 118 119 public function tI(): string 120 { 121 Assertion::true($this->isPrivate(), 'The key is not private.'); 122 123 return $this->get(self::DATA_TI); 124 } 125 126 public function hasPrimes(): bool 127 { 128 return $this->has(self::DATA_P) && $this->has(self::DATA_Q); 129 } 130 131 public function primes(): array 132 { 133 return [ 134 $this->p(), 135 $this->q(), 136 ]; 137 } 138 139 public function hasExponents(): bool 140 { 141 return $this->has(self::DATA_DP) && $this->has(self::DATA_DQ); 142 } 143 144 public function exponents(): array 145 { 146 return [ 147 $this->dP(), 148 $this->dQ(), 149 ]; 150 } 151 152 public function hasCoefficient(): bool 153 { 154 return $this->has(self::DATA_QI); 155 } 156 157 public function isPublic(): bool 158 { 159 return !$this->isPrivate(); 160 } 161 162 public function isPrivate(): bool 163 { 164 return \array_key_exists(self::DATA_D, $this->getData()); 165 } 166 167 public function asPem(): string 168 { 169 Assertion::false($this->isPrivate(), 'Unsupported for private keys.'); 170 $bitSring = new Sequence( 171 new Integer($this->fromBase64ToInteger($this->n())), 172 new Integer($this->fromBase64ToInteger($this->e())) 173 ); 174 175 $der = new Sequence( 176 new Sequence( 177 new ObjectIdentifier('1.2.840.113549.1.1.1'), 178 new NullObject() 179 ), 180 new BitString(\bin2hex($bitSring->getBinary())) 181 ); 182 183 return $this->pem('PUBLIC KEY', $der->getBinary()); 184 } 185 186 private function fromBase64ToInteger(string $value): string 187 { 188 return gmp_strval(gmp_init(current(unpack('H*', $value)), 16), 10); 189 } 190 191 private function pem(string $type, string $der): string 192 { 193 return sprintf("-----BEGIN %s-----\n", mb_strtoupper($type)). 194 chunk_split(base64_encode($der), 64, "\n"). 195 sprintf("-----END %s-----\n", mb_strtoupper($type)); 196 } 197 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |