1
|
|
|
<?php namespace security\rsa; |
2
|
|
|
/** |
3
|
|
|
* Copyright 2015 OpenStack Foundation |
4
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
5
|
|
|
* you may not use this file except in compliance with the License. |
6
|
|
|
* You may obtain a copy of the License at |
7
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
8
|
|
|
* Unless required by applicable law or agreed to in writing, software |
9
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
10
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
11
|
|
|
* See the License for the specific language governing permissions and |
12
|
|
|
* limitations under the License. |
13
|
|
|
**/ |
14
|
|
|
use security\rsa\exceptions\RSABadPEMFormat; |
15
|
|
|
use phpseclib\Math\BigInteger; |
16
|
|
|
use phpseclib\Crypt\RSA; |
17
|
|
|
/** |
18
|
|
|
* Class _RSAPublicKeyPEMFornat |
19
|
|
|
* @package security\rsa |
20
|
|
|
*/ |
21
|
|
|
class _RSAPublicKeyPEMFormat |
22
|
|
|
extends _AbstractRSAKeyPEMFormat |
23
|
|
|
implements RSAPublicKey { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var BigInteger |
27
|
|
|
*/ |
28
|
|
|
protected $e; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param $pem_format |
32
|
|
|
* @param string $password |
33
|
|
|
* @throws RSABadPEMFormat |
34
|
|
|
*/ |
35
|
|
|
public function __construct($pem_format, $password = null){ |
36
|
|
|
parent::__construct($pem_format, $password); |
37
|
|
|
$this->e = $this->rsa_imp->publicExponent; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The "e" (exponent) |
42
|
|
|
* @return BigInteger |
43
|
|
|
*/ |
44
|
|
|
public function getPublicExponent() |
45
|
|
|
{ |
46
|
|
|
return $this->e; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getAlgorithm() |
53
|
|
|
{ |
54
|
|
|
return 'RSA'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getEncoded() |
61
|
|
|
{ |
62
|
|
|
return $this->rsa_imp->getPublicKey(RSA::PUBLIC_FORMAT_PKCS8); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getFormat() |
69
|
|
|
{ |
70
|
|
|
return 'PKCS8'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
|
|
public function getBitLength() |
77
|
|
|
{ |
78
|
|
|
return $this->rsa_imp->getSize(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function getStrippedEncoded(): string |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$pem = preg_replace('/\-+BEGIN PUBLIC KEY\-+/','', $this->getEncoded()); |
87
|
|
|
$pem = preg_replace('/\-+END PUBLIC KEY\-+/','',$pem); |
88
|
|
|
$pem = str_replace( array("\n","\r","\t"), '', trim($pem)); |
89
|
|
|
return $pem; |
90
|
|
|
} |
91
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.