Completed
Push — master ( 6c40ed...665ea8 )
by sebastian
01:26
created

_RSAPrivateKeyPEMFormat::getFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
}