1 | <?php |
||
19 | class CryptToolSodium extends CryptTool { |
||
20 | /** |
||
21 | * @param string $data |
||
22 | * @param string $nonce |
||
23 | * @param string $senderPrivateKey |
||
24 | * @param string $recipientPublicKey |
||
25 | * @return string encrypted box |
||
26 | */ |
||
27 | protected function makeBox($data, $nonce, $senderPrivateKey, $recipientPublicKey) { |
||
34 | |||
35 | /** |
||
36 | * make a secret box |
||
37 | * |
||
38 | * @param $data |
||
39 | * @param $nonce |
||
40 | * @param $key |
||
41 | * @return mixed |
||
42 | */ |
||
43 | protected function makeSecretBox($data, $nonce, $key) { |
||
47 | |||
48 | |||
49 | /** |
||
50 | * @param string $box |
||
51 | * @param string $recipientPrivateKey |
||
52 | * @param string $senderPublicKey |
||
53 | * @param string $nonce |
||
54 | * @return null|string |
||
55 | */ |
||
56 | protected function openBox($box, $recipientPrivateKey, $senderPublicKey, $nonce) { |
||
62 | |||
63 | /** |
||
64 | * decrypt a secret box |
||
65 | * |
||
66 | * @param string $box as binary |
||
67 | * @param string $nonce as binary |
||
68 | * @param string $key as binary |
||
69 | * @return string as binary |
||
70 | */ |
||
71 | protected function openSecretBox($box, $nonce, $key) { |
||
75 | |||
76 | /** |
||
77 | * Generate a new key pair. |
||
78 | * |
||
79 | * @return KeyPair the new key pair |
||
80 | */ |
||
81 | final public function generateKeyPair() { |
||
87 | |||
88 | /** |
||
89 | * @param int $size |
||
90 | * @return string |
||
91 | */ |
||
92 | protected function createRandom($size) { |
||
96 | |||
97 | /** |
||
98 | * Derive the public key |
||
99 | * |
||
100 | * @param string $privateKey in binary |
||
101 | * @return string public key as binary |
||
102 | */ |
||
103 | final public function derivePublicKey($privateKey) { |
||
107 | |||
108 | /** |
||
109 | * Converts a binary string to an hexdecimal string. |
||
110 | * |
||
111 | * This is the same as PHP's bin2hex() implementation, but it is resistant to |
||
112 | * timing attacks. |
||
113 | * |
||
114 | * @link https://paragonie.com/book/pecl-libsodium/read/03-utilities-helpers.md#bin2hex |
||
115 | * @param string $binaryString The binary string to convert |
||
116 | * @return string |
||
117 | */ |
||
118 | public function bin2hex($binaryString) |
||
123 | |||
124 | /** |
||
125 | * Converts an hexdecimal string to a binary string. |
||
126 | * |
||
127 | * This is the same as PHP's hex2bin() implementation, but it is resistant to |
||
128 | * timing attacks. |
||
129 | * |
||
130 | * @link https://paragonie.com/book/pecl-libsodium/read/03-utilities-helpers.md#hex2bin |
||
131 | * @param string $hexString The hex string to convert |
||
132 | * @param string|null $ignore (optional) Characters to ignore |
||
133 | * @throws \Threema\Core\Exception |
||
134 | * @return string |
||
135 | */ |
||
136 | public function hex2bin($hexString, $ignore = null) |
||
141 | |||
142 | |||
143 | /** |
||
144 | * Compares two strings in a secure way. |
||
145 | * |
||
146 | * This is the same as PHP's strcmp() implementation, but it is resistant to |
||
147 | * timing attacks. |
||
148 | * |
||
149 | * @link https://paragonie.com/book/pecl-libsodium/read/03-utilities-helpers.md#compare |
||
150 | * @param string $str1 The first string |
||
151 | * @param string $str2 The second string |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function stringCompare($str1, $str2) |
||
164 | |||
165 | /** |
||
166 | * Unsets/removes a variable. |
||
167 | * |
||
168 | * Important: When using PHPv7, make sure to have at least version 1.0.1 of |
||
169 | * the Libsodium PECL (libsodium-php) installed. Otherwise this falls back to |
||
170 | * the (insecure) PHP method of removing a variable. |
||
171 | * |
||
172 | * @link https://paragonie.com/book/pecl-libsodium/read/03-utilities-helpers.md#memzero |
||
173 | * @param string $var A variable, passed by reference |
||
174 | */ |
||
175 | public function removeVar(&$var) |
||
188 | |||
189 | /** |
||
190 | * Check if implementation supported |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function isSupported() { |
||
197 | |||
198 | /** |
||
199 | * Validate crypt tool |
||
200 | * |
||
201 | * @return bool |
||
202 | * @throws Exception |
||
203 | */ |
||
204 | public function validate() { |
||
210 | |||
211 | /** |
||
212 | * @return string |
||
213 | */ |
||
214 | public function getName() { |
||
217 | |||
218 | /** |
||
219 | * Description of the CryptTool |
||
220 | * @return string |
||
221 | */ |
||
222 | public function getDescription() { |
||
226 | } |
||
227 |