1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class CryptoBox |
4
|
|
|
* |
5
|
|
|
* @filesource CryptoBox.php |
6
|
|
|
* @created 25.01.2018 |
7
|
|
|
* @package chillerlan\Traits\Crypto |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2018 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Traits\Crypto; |
14
|
|
|
|
15
|
|
|
use chillerlan\Traits\{ |
16
|
|
|
Container, ContainerInterface |
17
|
|
|
}; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @link https://paragonie.com/book/pecl-libsodium/read/00-intro.md |
21
|
|
|
* @link https://paragonie.com/book/pecl-libsodium/read/01-quick-start.md |
22
|
|
|
*/ |
23
|
|
|
abstract class CryptoBox implements CryptoBoxInterface, ContainerInterface{ |
24
|
|
|
use MemzeroDestructorTrait, Container{ |
25
|
|
|
__construct as containerConstruct; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \chillerlan\Traits\Crypto\CryptoKeyInterface |
30
|
|
|
*/ |
31
|
|
|
protected $keypair; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $box; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $nonce; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $message; |
47
|
|
|
|
48
|
|
|
/** @noinspection PhpMissingParentConstructorInspection */ |
49
|
|
|
/** |
50
|
|
|
* CryptoBox constructor. |
51
|
|
|
* |
52
|
|
|
* @param array|null $properties |
53
|
|
|
* |
54
|
|
|
* @throws \chillerlan\Traits\Crypto\CryptoException |
55
|
|
|
*/ |
56
|
|
|
public function __construct(array $properties = null){ |
57
|
|
|
|
58
|
|
|
if(!extension_loaded('sodium') || !function_exists('sodium_memzero')){ |
59
|
|
|
throw new CryptoException('sodium extension (PHP 7.2+) required!'); // @codeCoverageIgnore |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->containerConstruct($properties); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int $secretLength |
67
|
|
|
* @param int $PublicLength |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
* @throws \chillerlan\Traits\Crypto\CryptoException |
71
|
|
|
*/ |
72
|
|
|
protected function checkKeypair(int $secretLength = null, int $PublicLength = null){ |
73
|
|
|
|
74
|
|
|
if($secretLength !== null){ |
75
|
|
|
if(!$this->keypair->secret || strlen($this->keypair->secret) !== $secretLength){ |
|
|
|
|
76
|
|
|
throw new CryptoException('invalid secret key'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if($PublicLength !== null){ |
81
|
|
|
if(!$this->keypair->public || strlen($this->keypair->public) !== $PublicLength){ |
|
|
|
|
82
|
|
|
throw new CryptoException('invalid public key'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function checkMessage(string $message):string { |
89
|
|
|
$message = trim($message); |
90
|
|
|
|
91
|
|
|
if(strlen($message) < 1){ |
92
|
|
|
throw new CryptoException('invalid message'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// @todo: padding? |
96
|
|
|
return $message; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.