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

_AbstractRSAKeyPEMFormat   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPassword() 0 3 1
A hasPassword() 0 3 1
A __construct() 0 16 3
A getModulus() 0 4 1
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\Crypt\RSA;
16
use phpseclib\Math\BigInteger;
17
/**
18
 * Class _AbstractRSAKeyPEMFormat
19
 * @package security\rsa
20
 */
21
abstract class _AbstractRSAKeyPEMFormat {
22
23
    /**
24
     * @var string
25
     */
26
    protected $pem_format;
27
28
    /**
29
     * @var RSA
30
     */
31
    protected $rsa_imp;
32
33
    /**
34
     * @var BigInteger
35
     */
36
    protected $n;
37
38
    /**
39
     * @var string
40
     */
41
    protected $password;
42
43
    /**
44
     * @return null|string
45
     */
46
    public function getPassword():?string{
47
        return $this->password;
48
    }
49
50
    /**
51
     * @return bool
52
     */
53
    public function hasPassword():bool{
54
        return !empty($this->password);
55
    }
56
57
    /**
58
     * @param string $pem_format
59
     * @param string $password
60
     * @throws RSABadPEMFormat
61
     */
62
    public function __construct($pem_format, $password = null){
63
64
        $this->pem_format = $pem_format;
65
        $this->rsa_imp    = new RSA();
66
67
        if(!empty($password)) {
68
            $this->password = trim($password);
69
            $this->rsa_imp->setPassword($this->password);
70
        }
71
72
        $res = $this->rsa_imp->loadKey($this->pem_format, RSA::PRIVATE_FORMAT_PKCS1);
73
74
        if(!$res) throw new RSABadPEMFormat(sprintf('pem %s',$pem_format ));
75
76
        $this->n = $this->rsa_imp->modulus;
77
    }
78
79
    /**
80
     * Returns The "n" (modulus)
81
     * @return BigInteger
82
     */
83
    public function getModulus()
84
    {
85
        return  $this->n;
86
    }
87
88
}