Completed
Push — master ( 16bc05...356550 )
by sebastian
03:03
created

src/security/rsa/_RSAPrivateKeyPEMFornat.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
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
15
namespace security\rsa;
16
use security\rsa\exceptions\RSABadPEMFormat;
17
18
/**
19
 * Class _RSAPrivateKeyPEMFornat
20
 * @package security\rsa
21
 */
22
final class _RSAPrivateKeyPEMFornat
23
    extends _RSAPublicKeyPEMFornat
0 ignored issues
show
The extends keyword must be on the same line as the class name
Loading history...
Expected 0 spaces between "_RSAPublicKeyPEMFornat" and comma; 1 found
Loading history...
24
    implements RSAPrivateKey {
0 ignored issues
show
The implements keyword must be on the same line as the class name
Loading history...
25
26
    /**
27
     * @var \Math_BigInteger
28
     */
29
    private $d;
30
31
    /**
32
     * @param $pem_format
33
     * @param string $password
34
     * @throws RSABadPEMFormat
35
     */
36
    public function __construct($pem_format, $password = null){
37
38
        parent::__construct($pem_format, $password);
39
        $this->d = $this->rsa_imp->exponent;
40
        if($this->d->toString() === $this->e->toString())
41
            throw new RSABadPEMFormat(sprintf('pem %s is a public key!', $pem_format));
42
    }
43
44
    /**
45
     * The "d" (private exponent)
46
     *
47
     * @return \Math_BigInteger
48
     */
49
    public function getPrivateExponent()
50
    {
51
       return $this->d;
52
    }
53
54
    /**
55
     * @return string
56
     */
57 View Code Duplication
    public function getEncoded()
0 ignored issues
show
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...
58
    {
59
        $pem = $this->rsa_imp->getPrivateKey(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
60
        $pem = preg_replace('/\-+BEGIN RSA PRIVATE KEY\-+/','',$pem);
61
        $pem = preg_replace('/\-+END RSA PRIVATE KEY\-+/','',$pem);
62
        $pem = str_replace( array("\n","\r","\t"), '', trim($pem));
63
        return $pem;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getFormat()
70
    {
71
        return 'PKCS1';
72
    }
73
74
}