Passed
Pull Request — master (#22)
by Théo
02:28
created

OpenSsl::verify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Verifier;
16
17
use KevinGH\Box\Exception\OpenSslExceptionFactory;
18
use RuntimeException;
19
20
/**
21
 * Uses OpenSSL to verify the signature.
22
 *
23
 * @author Kevin Herrera <[email protected]>
24
 */
25
final class OpenSsl extends PublicKey
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function verify(string $signature): bool
31
    {
32
        OpenSslExceptionFactory::reset();
33
34
        ob_start();
35
36
        $result = openssl_verify(
37
            $this->getBufferedData(),
38
            @pack('H*', $signature),
39
            $this->getKey()
40
        );
41
42
        $error = trim(ob_get_clean());
43
44
        if (-1 === $result) {
45
            throw OpenSslExceptionFactory::createForLastError();
46
        }
47
        if (!empty($error)) {
48
            throw new RuntimeException($error);
49
        }
50
51
        return 1 === $result;
52
    }
53
}
54