Completed
Push — master ( abae97...fc5d0c )
by Breno
03:37 queued 01:48
created

PublicKey   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 35
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A verify() 0 8 2
A unformated() 0 5 1
1
<?php
2
3
namespace XmlSigner;
4
5
class PublicKey implements VerifierInterface
6
{
7
    const SIGNATURE_CORRECT = 1;
8
    const SIGNATURE_INCORRECT = 0;
9
    const SIGNATURE_ERROR = -1;
10
11
    private $key;
12
13 7
    public function __construct(string $key)
14
    {
15 7
        $this->key = $key;
16 7
    }
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 3
    public function verify($data, $signature, $algorithm = OPENSSL_ALGO_SHA1)
22
    {
23 3
        $verified = openssl_verify($data, $signature, $this->key, $algorithm);
24 3
        if ($verified === self::SIGNATURE_ERROR) {
25
            throw CertificateException::invalidSignature();
26
        }
27 3
        return $verified === self::SIGNATURE_CORRECT;
28
    }
29
30
    /**
31
     * Returns unformated public key
32
     * @return string
33
     */
34 2
    public function unformated()
35
    {
36 2
        $ret = preg_replace('/-----.*[\n]?/', '', $this->key);
37 2
        return preg_replace('/[\n\r]/', '', $ret);
38
    }
39
}
40