|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class CryptoBoxAbstract |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource CryptoBoxAbstract.php |
|
6
|
|
|
* @created 25.01.2018 |
|
7
|
|
|
* @package chillerlan\Cryptobox |
|
8
|
|
|
* @author Smiley <[email protected]> |
|
9
|
|
|
* @copyright 2018 Smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace chillerlan\Cryptobox; |
|
14
|
|
|
|
|
15
|
|
|
use function property_exists, strlen, trim; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @link https://paragonie.com/book/pecl-libsodium/read/00-intro.md |
|
19
|
|
|
* @link https://paragonie.com/book/pecl-libsodium/read/01-quick-start.md |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class CryptoBoxAbstract implements CryptoBoxInterface{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \chillerlan\Cryptobox\CryptoKeypairInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $keypair; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $box; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $nonce; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $message; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* CryptoBoxAbstract constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param \chillerlan\Cryptobox\CryptoKeypairInterface|null $keypair |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(CryptoKeypairInterface $keypair = null){ |
|
49
|
|
|
$this->keypair = $keypair; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $property |
|
54
|
|
|
* |
|
55
|
|
|
* @return mixed|null |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __get(string $property){ |
|
58
|
|
|
return property_exists($this, $property) ? $this->{$property} : null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param int $secretLength |
|
63
|
|
|
* @param int $publicLength |
|
64
|
|
|
* |
|
65
|
|
|
* @return void |
|
66
|
|
|
* @throws \chillerlan\Cryptobox\CryptoException |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function checkKeypair(int $secretLength = null, int $publicLength = null):void{ |
|
69
|
|
|
|
|
70
|
|
|
if($secretLength !== null && (!$this->keypair->secret || strlen($this->keypair->secret) !== $secretLength)){ |
|
71
|
|
|
throw new CryptoException('invalid secret key'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if($publicLength !== null && (!$this->keypair->public || strlen($this->keypair->public) !== $publicLength)){ |
|
75
|
|
|
throw new CryptoException('invalid public key'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $message |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
* @throws \chillerlan\Cryptobox\CryptoException |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function checkMessage(string $message):string{ |
|
87
|
|
|
$message = trim($message); |
|
88
|
|
|
|
|
89
|
|
|
if(empty($message)){ |
|
90
|
|
|
throw new CryptoException('invalid message'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// @todo: padding? |
|
94
|
|
|
return $message; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|