Completed
Push — master ( bc7d7f...7a9596 )
by Théo
02:46
created

PublicKeyDelegate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 54
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 3 1
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\Signature;
16
17
use KevinGH\Box\Exception\SignatureException;
18
19
/**
20
 * Uses the OpenSSL extension or phpseclib library to verify a signature.
21
 *
22
 * @author Kevin Herrera <[email protected]>
23
 */
24
class PublicKeyDelegate implements VerifyInterface
25
{
26
    /**
27
     * The hashing class.
28
     *
29
     * @var VerifyInterface
30
     */
31
    private $hash;
32
33
    /**
34
     * Selects the appropriate hashing class.
35
     */
36
    public function __construct()
37
    {
38
        if (extension_loaded('openssl')) {
39
            $this->hash = new OpenSsl();
40
        } elseif (class_exists('Crypt_RSA')) {
41
            $this->hash = new PhpSeclib();
42
        } else {
43
            throw SignatureException::create(
44
                'The "openssl" extension and "phpseclib" libraries are not available.'
45
            );
46
        }
47
    }
48
49
    /**
50
     * @see VerifyInterface::init
51
     *
52
     * @param mixed $algorithm
53
     * @param mixed $path
54
     */
55
    public function init($algorithm, $path): void
56
    {
57
        $this->hash->init($algorithm, $path);
58
    }
59
60
    /**
61
     * @see VerifyInterface::update
62
     *
63
     * @param mixed $data
64
     */
65
    public function update($data): void
66
    {
67
        $this->hash->update($data);
68
    }
69
70
    /**
71
     * @see VerifyInterface::verify
72
     *
73
     * @param mixed $signature
74
     */
75
    public function verify($signature)
76
    {
77
        return $this->hash->verify($signature);
78
    }
79
}
80