PublicKey::verify()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
crap 2.032
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