These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 _RSAPrivateKeyPEMFornat |
||
19 | * @package security\rsa |
||
20 | */ |
||
21 | final class _RSAPrivateKeyPEMFormat |
||
22 | extends _RSAPublicKeyPEMFormat |
||
23 | implements RSAPrivateKey { |
||
24 | |||
25 | /** |
||
26 | * @var BigInteger |
||
27 | */ |
||
28 | private $d; |
||
29 | |||
30 | /** |
||
31 | * @param $pem_format |
||
32 | * @param string $password |
||
33 | * @throws RSABadPEMFormat |
||
34 | */ |
||
35 | public function __construct($pem_format, $password = null){ |
||
36 | |||
37 | parent::__construct($pem_format, $password); |
||
38 | $this->d = $this->rsa_imp->exponent; |
||
39 | if($this->d->toString() === $this->e->toString()) |
||
40 | throw new RSABadPEMFormat(sprintf('pem %s is a public key!', $pem_format)); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * The "d" (private exponent) |
||
45 | * |
||
46 | * @return BigInteger |
||
47 | */ |
||
48 | public function getPrivateExponent() |
||
49 | { |
||
50 | return $this->d; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getEncoded() |
||
57 | { |
||
58 | return $this->rsa_imp->getPrivateKey(RSA::PRIVATE_FORMAT_PKCS1); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @return string |
||
63 | */ |
||
64 | public function getFormat() |
||
65 | { |
||
66 | return 'PKCS1'; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @return string |
||
71 | */ |
||
72 | View Code Duplication | public function getStrippedEncoded(): string |
|
0 ignored issues
–
show
|
|||
73 | { |
||
74 | $pem = preg_replace('/\-+BEGIN RSA PRIVATE KEY\-+/','',$this->getEncoded()); |
||
75 | $pem = preg_replace('/\-+END RSA PRIVATE KEY\-+/','',$pem); |
||
76 | $pem = str_replace( array("\n","\r","\t"), '', trim($pem)); |
||
77 | return $pem; |
||
78 | } |
||
79 | |||
80 | } |
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.