Completed
Branch master (356550)
by sebastian
02:47
created

key_management/rsa/RSA_KeyManagementAlgorithm.php (5 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 jwa\cryptographic_algorithms\key_management\rsa;
16
17
use jwa\cryptographic_algorithms\Abstract_RSA_Algorithm;
18
use jwa\cryptographic_algorithms\EncryptionAlgorithm;
19
use jwa\cryptographic_algorithms\exceptions\InvalidKeyTypeAlgorithmException;
20
use jwa\cryptographic_algorithms\key_management\modes\KeyEncryption;
21
use security\Key;
22
use security\rsa\RSAPrivateKey;
23
use security\rsa\RSAPublicKey;
24
25
/**
26
 * Class RSA_KeyManagementAlgorithm
27
 * @package jwa\cryptographic_algorithms\key_management\rsa
28
 */
29
abstract class RSA_KeyManagementAlgorithm
30
    extends Abstract_RSA_Algorithm
0 ignored issues
show
The extends keyword must be on the same line as the class name
Loading history...
Expected 0 spaces between "Abstract_RSA_Algorithm" and comma; 1 found
Loading history...
31
    implements EncryptionAlgorithm, KeyEncryption {
0 ignored issues
show
The implements keyword must be on the same line as the class name
Loading history...
32
33
    public function __construct(){
34
35
        parent::__construct();
36
        //configuration ...
37
        $this->rsa_impl->setEncryptionMode($this->getEncryptionMode());
38
        $this->rsa_impl->setHash($this->getHashingAlgorithm());
39
        $this->rsa_impl->setMGFHash($this->getMGFHash());
40
    }
41
42
    /**
43
     * @param Key $key
44
     * @param $message
45
     * @return string
46
     * @throws InvalidKeyTypeAlgorithmException
47
     */
48 View Code Duplication
    public function encrypt(Key $key, $message)
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...
49
    {
50
        if(!($key instanceof RSAPublicKey))
51
            throw new InvalidKeyTypeAlgorithmException('key is not public');
52
53
        if($key->getFormat() !== 'PKCS8')
54
            throw new InvalidKeyTypeAlgorithmException('keys is not on PKCS1 format');
55
56
        $res = $this->rsa_impl->loadKey($key->getEncoded());
57
58
        if(!$res)
59
            throw new InvalidKeyTypeAlgorithmException('could not parse the key');
60
61
        if($this->rsa_impl->getSize() < $this->getMinKeyLen())
62
            throw new InvalidKeyTypeAlgorithmException('len is invalid');
63
64
        return $this->rsa_impl->encrypt($message);
65
    }
66
67
    /**
68
     * @param Key $key
69
     * @param string $enc_message
70
     * @return string
71
     * @throws InvalidKeyTypeAlgorithmException
72
     */
73 View Code Duplication
    public function decrypt(Key $key, $enc_message){
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...
74
75
        if(!($key instanceof RSAPrivateKey))
76
            throw new InvalidKeyTypeAlgorithmException('key is not private');
77
78
79
        if($key->getFormat() !== 'PKCS1')
80
            throw new InvalidKeyTypeAlgorithmException('keys is not on PKCS1 format');
81
82
        $res = $this->rsa_impl->loadKey($key->getEncoded());
83
84
        if(!$res)
85
            throw new InvalidKeyTypeAlgorithmException('could not parse the key');
86
87
        if($this->rsa_impl->getSize() < $this->getMinKeyLen())
88
            throw new InvalidKeyTypeAlgorithmException('len is invalid');
89
90
        return $this->rsa_impl->decrypt($enc_message);
91
    }
92
93
    /**
94
     * @return int
95
     */
96
    abstract public function getEncryptionMode();
97
98
    /**
99
     * @return string
100
     */
101
    abstract public function getMGFHash();
102
103
}