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
|
|
|
|