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

PublicKeyDelegate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 34
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 3 1
A __construct() 0 9 3
A verify() 0 3 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\SignatureException;
18
use KevinGH\Box\Verifier;
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 SignatureException::create(
38
                'The "openssl" extension and "phpseclib" libraries are not available.'
39
            );
40
        }
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function update(string $data): void
47
    {
48
        $this->hash->update($data);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function verify(string $signature): bool
55
    {
56
        return $this->hash->verify($signature);
57
    }
58
}
59