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

PublicKeyDelegate::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
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\Verifier;
18
use RuntimeException;
19
20
/**
21
 * Uses the OpenSSL extension or phpseclib library to verify a signed PHAR.
22
 */
23
final class PublicKeyDelegate implements Verifier
24
{
25
    private $hash;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function __construct(string $signature, string $path)
31
    {
32
        if (extension_loaded('openssl')) {
33
            $this->hash = new OpenSsl($signature, $path);
34
        } elseif (class_exists('Crypt_RSA')) {
35
            $this->hash = new PhpSeclib($signature, $path);
36
        } else {
37
            throw new RuntimeException('The "openssl" extension and "phpseclib" libraries are not available.');
38
        }
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function update(string $data): void
45
    {
46
        $this->hash->update($data);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function verify(string $signature): bool
53
    {
54
        return $this->hash->verify($signature);
55
    }
56
}
57