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

digital_signatures/rsa/RSA_Algorithm.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\digital_signatures\rsa;
16
17
use jwa\cryptographic_algorithms\Abstract_RSA_Algorithm;
18
use jwa\cryptographic_algorithms\digital_signatures\DigitalSignatureAlgorithm;
19
use jwa\cryptographic_algorithms\exceptions\InvalidKeyLengthAlgorithmException;
20
use jwa\cryptographic_algorithms\exceptions\InvalidKeyTypeAlgorithmException;
21
use jwa\cryptographic_algorithms\HashFunctionAlgorithm;
22
use jwk\JSONWebKeyTypes;
23
use security\Key;
24
use security\PrivateKey;
25
use security\PublicKey;
26
use security\rsa\RSAPrivateKey;
27
use security\rsa\RSAPublicKey;
28
29
/**
30
 * Class RSA_Algorithm
31
 * @package jwa\cryptographic_algorithms\digital_signatures\rsa
32
 */
33
abstract class RSA_Algorithm
34
    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...
35
    implements DigitalSignatureAlgorithm, HashFunctionAlgorithm
0 ignored issues
show
The implements keyword must be on the same line as the class name
Loading history...
36
{
37
38
39
    /**
40
     * @param PrivateKey $private_key
41
     * @param string $message
42
     * @return string
43
     * @throws InvalidKeyLengthAlgorithmException
44
     * @throws InvalidKeyTypeAlgorithmException
45
     */
46 View Code Duplication
    public function sign(PrivateKey $private_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...
47
    {
48
        if(!($private_key instanceof RSAPrivateKey)) throw new InvalidKeyTypeAlgorithmException;
49
50
        if($this->getMinKeyLen() > $private_key->getBitLength())
51
            throw new InvalidKeyLengthAlgorithmException(sprintf('min len %s - cur len %s.',$this->getMinKeyLen(), $private_key->getBitLength()));
52
53
        $res = $this->rsa_impl->loadKey($private_key->getEncoded());
54
55
        if(!$res) throw new InvalidKeyTypeAlgorithmException;
56
57
        $this->rsa_impl->setHash($this->getHashingAlgorithm());
58
        $this->rsa_impl->setMGFHash($this->getHashingAlgorithm());
59
        $this->rsa_impl->setSignatureMode($this->getPaddingMode());
60
        return $this->rsa_impl->sign($message);
61
    }
62
63
    /**
64
     * @param Key $key
65
     * @param string $message
66
     * @param string $signature
67
     * @return bool
68
     * @throws InvalidKeyLengthAlgorithmException
69
     * @throws InvalidKeyTypeAlgorithmException
70
     */
71 View Code Duplication
    public function verify(Key $key, $message, $signature)
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...
72
    {
73
        if(!($key instanceof RSAPublicKey)) throw new InvalidKeyTypeAlgorithmException;
74
75
        if($this->getMinKeyLen() > $key->getBitLength())
76
            throw new InvalidKeyLengthAlgorithmException(sprintf('min len %s - cur len %s.',$this->getMinKeyLen(), $key->getBitLength()));
77
78
        $res = $this->rsa_impl->loadKey($key->getEncoded());
79
80
        if(!$res) throw new InvalidKeyTypeAlgorithmException;
81
82
        $this->rsa_impl->setHash($this->getHashingAlgorithm());
83
        $this->rsa_impl->setMGFHash($this->getHashingAlgorithm());
84
        $this->rsa_impl->setSignatureMode($this->getPaddingMode());
85
86
        return $this->rsa_impl->verify($message, $signature);
87
    }
88
89
    /**
90
     * @return int
91
     */
92
    abstract public function getPaddingMode();
93
}