Completed
Pull Request — develop (#1726)
by
unknown
19:16
created
vendor/paragonie/sodium_compat/src/Crypto.php 3 patches
Indentation   +1639 added lines, -1639 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 if (class_exists('ParagonIE_Sodium_Crypto', false)) {
4
-    return;
4
+	return;
5 5
 }
6 6
 
7 7
 /**
@@ -14,1642 +14,1642 @@  discard block
 block discarded – undo
14 14
  */
15 15
 abstract class ParagonIE_Sodium_Crypto
16 16
 {
17
-    const aead_chacha20poly1305_KEYBYTES = 32;
18
-    const aead_chacha20poly1305_NSECBYTES = 0;
19
-    const aead_chacha20poly1305_NPUBBYTES = 8;
20
-    const aead_chacha20poly1305_ABYTES = 16;
21
-
22
-    const aead_chacha20poly1305_IETF_KEYBYTES = 32;
23
-    const aead_chacha20poly1305_IETF_NSECBYTES = 0;
24
-    const aead_chacha20poly1305_IETF_NPUBBYTES = 12;
25
-    const aead_chacha20poly1305_IETF_ABYTES = 16;
26
-
27
-    const aead_xchacha20poly1305_IETF_KEYBYTES = 32;
28
-    const aead_xchacha20poly1305_IETF_NSECBYTES = 0;
29
-    const aead_xchacha20poly1305_IETF_NPUBBYTES = 24;
30
-    const aead_xchacha20poly1305_IETF_ABYTES = 16;
31
-
32
-    const box_curve25519xsalsa20poly1305_SEEDBYTES = 32;
33
-    const box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32;
34
-    const box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32;
35
-    const box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32;
36
-    const box_curve25519xsalsa20poly1305_NONCEBYTES = 24;
37
-    const box_curve25519xsalsa20poly1305_MACBYTES = 16;
38
-    const box_curve25519xsalsa20poly1305_BOXZEROBYTES = 16;
39
-    const box_curve25519xsalsa20poly1305_ZEROBYTES = 32;
40
-
41
-    const onetimeauth_poly1305_BYTES = 16;
42
-    const onetimeauth_poly1305_KEYBYTES = 32;
43
-
44
-    const secretbox_xsalsa20poly1305_KEYBYTES = 32;
45
-    const secretbox_xsalsa20poly1305_NONCEBYTES = 24;
46
-    const secretbox_xsalsa20poly1305_MACBYTES = 16;
47
-    const secretbox_xsalsa20poly1305_BOXZEROBYTES = 16;
48
-    const secretbox_xsalsa20poly1305_ZEROBYTES = 32;
49
-
50
-    const secretbox_xchacha20poly1305_KEYBYTES = 32;
51
-    const secretbox_xchacha20poly1305_NONCEBYTES = 24;
52
-    const secretbox_xchacha20poly1305_MACBYTES = 16;
53
-    const secretbox_xchacha20poly1305_BOXZEROBYTES = 16;
54
-    const secretbox_xchacha20poly1305_ZEROBYTES = 32;
55
-
56
-    const stream_salsa20_KEYBYTES = 32;
57
-
58
-    /**
59
-     * AEAD Decryption with ChaCha20-Poly1305
60
-     *
61
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
62
-     *
63
-     * @param string $message
64
-     * @param string $ad
65
-     * @param string $nonce
66
-     * @param string $key
67
-     * @return string
68
-     * @throws SodiumException
69
-     * @throws TypeError
70
-     */
71
-    public static function aead_chacha20poly1305_decrypt(
72
-        $message = '',
73
-        $ad = '',
74
-        $nonce = '',
75
-        $key = ''
76
-    ) {
77
-        /** @var int $len - Length of message (ciphertext + MAC) */
78
-        $len = ParagonIE_Sodium_Core_Util::strlen($message);
79
-
80
-        /** @var int  $clen - Length of ciphertext */
81
-        $clen = $len - self::aead_chacha20poly1305_ABYTES;
82
-
83
-        /** @var int $adlen - Length of associated data */
84
-        $adlen = ParagonIE_Sodium_Core_Util::strlen($ad);
85
-
86
-        /** @var string $mac - Message authentication code */
87
-        $mac = ParagonIE_Sodium_Core_Util::substr(
88
-            $message,
89
-            $clen,
90
-            self::aead_chacha20poly1305_ABYTES
91
-        );
92
-
93
-        /** @var string $ciphertext - The encrypted message (sans MAC) */
94
-        $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 0, $clen);
95
-
96
-        /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
97
-        $block0 = ParagonIE_Sodium_Core_ChaCha20::stream(
98
-            32,
99
-            $nonce,
100
-            $key
101
-        );
102
-
103
-        /* Recalculate the Poly1305 authentication tag (MAC): */
104
-        $state = new ParagonIE_Sodium_Core_Poly1305_State($block0);
105
-        try {
106
-            ParagonIE_Sodium_Compat::memzero($block0);
107
-        } catch (SodiumException $ex) {
108
-            $block0 = null;
109
-        }
110
-        $state->update($ad);
111
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen));
112
-        $state->update($ciphertext);
113
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen));
114
-        $computed_mac = $state->finish();
115
-
116
-        /* Compare the given MAC with the recalculated MAC: */
117
-        if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) {
118
-            throw new SodiumException('Invalid MAC');
119
-        }
120
-
121
-        // Here, we know that the MAC is valid, so we decrypt and return the plaintext
122
-        return ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
123
-            $ciphertext,
124
-            $nonce,
125
-            $key,
126
-            ParagonIE_Sodium_Core_Util::store64_le(1)
127
-        );
128
-    }
129
-
130
-    /**
131
-     * AEAD Encryption with ChaCha20-Poly1305
132
-     *
133
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
134
-     *
135
-     * @param string $message
136
-     * @param string $ad
137
-     * @param string $nonce
138
-     * @param string $key
139
-     * @return string
140
-     * @throws SodiumException
141
-     * @throws TypeError
142
-     */
143
-    public static function aead_chacha20poly1305_encrypt(
144
-        $message = '',
145
-        $ad = '',
146
-        $nonce = '',
147
-        $key = ''
148
-    ) {
149
-        /** @var int $len - Length of the plaintext message */
150
-        $len = ParagonIE_Sodium_Core_Util::strlen($message);
151
-
152
-        /** @var int $adlen - Length of the associated data */
153
-        $adlen = ParagonIE_Sodium_Core_Util::strlen($ad);
154
-
155
-        /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
156
-        $block0 = ParagonIE_Sodium_Core_ChaCha20::stream(
157
-            32,
158
-            $nonce,
159
-            $key
160
-        );
161
-        $state = new ParagonIE_Sodium_Core_Poly1305_State($block0);
162
-        try {
163
-            ParagonIE_Sodium_Compat::memzero($block0);
164
-        } catch (SodiumException $ex) {
165
-            $block0 = null;
166
-        }
167
-
168
-        /** @var string $ciphertext - Raw encrypted data */
169
-        $ciphertext = ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
170
-            $message,
171
-            $nonce,
172
-            $key,
173
-            ParagonIE_Sodium_Core_Util::store64_le(1)
174
-        );
175
-
176
-        $state->update($ad);
177
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen));
178
-        $state->update($ciphertext);
179
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($len));
180
-        return $ciphertext . $state->finish();
181
-    }
182
-
183
-    /**
184
-     * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
185
-     *
186
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
187
-     *
188
-     * @param string $message
189
-     * @param string $ad
190
-     * @param string $nonce
191
-     * @param string $key
192
-     * @return string
193
-     * @throws SodiumException
194
-     * @throws TypeError
195
-     */
196
-    public static function aead_chacha20poly1305_ietf_decrypt(
197
-        $message = '',
198
-        $ad = '',
199
-        $nonce = '',
200
-        $key = ''
201
-    ) {
202
-        /** @var int $adlen - Length of associated data */
203
-        $adlen = ParagonIE_Sodium_Core_Util::strlen($ad);
204
-
205
-        /** @var int $len - Length of message (ciphertext + MAC) */
206
-        $len = ParagonIE_Sodium_Core_Util::strlen($message);
207
-
208
-        /** @var int  $clen - Length of ciphertext */
209
-        $clen = $len - self::aead_chacha20poly1305_IETF_ABYTES;
210
-
211
-        /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
212
-        $block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream(
213
-            32,
214
-            $nonce,
215
-            $key
216
-        );
217
-
218
-        /** @var string $mac - Message authentication code */
219
-        $mac = ParagonIE_Sodium_Core_Util::substr(
220
-            $message,
221
-            $len - self::aead_chacha20poly1305_IETF_ABYTES,
222
-            self::aead_chacha20poly1305_IETF_ABYTES
223
-        );
224
-
225
-        /** @var string $ciphertext - The encrypted message (sans MAC) */
226
-        $ciphertext = ParagonIE_Sodium_Core_Util::substr(
227
-            $message,
228
-            0,
229
-            $len - self::aead_chacha20poly1305_IETF_ABYTES
230
-        );
231
-
232
-        /* Recalculate the Poly1305 authentication tag (MAC): */
233
-        $state = new ParagonIE_Sodium_Core_Poly1305_State($block0);
234
-        try {
235
-            ParagonIE_Sodium_Compat::memzero($block0);
236
-        } catch (SodiumException $ex) {
237
-            $block0 = null;
238
-        }
239
-        $state->update($ad);
240
-        $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
241
-        $state->update($ciphertext);
242
-        $state->update(str_repeat("\x00", (0x10 - $clen) & 0xf));
243
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen));
244
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen));
245
-        $computed_mac = $state->finish();
246
-
247
-        /* Compare the given MAC with the recalculated MAC: */
248
-        if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) {
249
-            throw new SodiumException('Invalid MAC');
250
-        }
251
-
252
-        // Here, we know that the MAC is valid, so we decrypt and return the plaintext
253
-        return ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
254
-            $ciphertext,
255
-            $nonce,
256
-            $key,
257
-            ParagonIE_Sodium_Core_Util::store64_le(1)
258
-        );
259
-    }
260
-
261
-    /**
262
-     * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
263
-     *
264
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
265
-     *
266
-     * @param string $message
267
-     * @param string $ad
268
-     * @param string $nonce
269
-     * @param string $key
270
-     * @return string
271
-     * @throws SodiumException
272
-     * @throws TypeError
273
-     */
274
-    public static function aead_chacha20poly1305_ietf_encrypt(
275
-        $message = '',
276
-        $ad = '',
277
-        $nonce = '',
278
-        $key = ''
279
-    ) {
280
-        /** @var int $len - Length of the plaintext message */
281
-        $len = ParagonIE_Sodium_Core_Util::strlen($message);
282
-
283
-        /** @var int $adlen - Length of the associated data */
284
-        $adlen = ParagonIE_Sodium_Core_Util::strlen($ad);
285
-
286
-        /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
287
-        $block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream(
288
-            32,
289
-            $nonce,
290
-            $key
291
-        );
292
-        $state = new ParagonIE_Sodium_Core_Poly1305_State($block0);
293
-        try {
294
-            ParagonIE_Sodium_Compat::memzero($block0);
295
-        } catch (SodiumException $ex) {
296
-            $block0 = null;
297
-        }
298
-
299
-        /** @var string $ciphertext - Raw encrypted data */
300
-        $ciphertext = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
301
-            $message,
302
-            $nonce,
303
-            $key,
304
-            ParagonIE_Sodium_Core_Util::store64_le(1)
305
-        );
306
-
307
-        $state->update($ad);
308
-        $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
309
-        $state->update($ciphertext);
310
-        $state->update(str_repeat("\x00", ((0x10 - $len) & 0xf)));
311
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen));
312
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($len));
313
-        return $ciphertext . $state->finish();
314
-    }
315
-
316
-    /**
317
-     * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
318
-     *
319
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
320
-     *
321
-     * @param string $message
322
-     * @param string $ad
323
-     * @param string $nonce
324
-     * @param string $key
325
-     * @return string
326
-     * @throws SodiumException
327
-     * @throws TypeError
328
-     */
329
-    public static function aead_xchacha20poly1305_ietf_decrypt(
330
-        $message = '',
331
-        $ad = '',
332
-        $nonce = '',
333
-        $key = ''
334
-    ) {
335
-        $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
336
-            ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16),
337
-            $key
338
-        );
339
-        $nonceLast = "\x00\x00\x00\x00" .
340
-            ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
341
-
342
-        return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey);
343
-    }
344
-
345
-    /**
346
-     * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
347
-     *
348
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
349
-     *
350
-     * @param string $message
351
-     * @param string $ad
352
-     * @param string $nonce
353
-     * @param string $key
354
-     * @return string
355
-     * @throws SodiumException
356
-     * @throws TypeError
357
-     */
358
-    public static function aead_xchacha20poly1305_ietf_encrypt(
359
-        $message = '',
360
-        $ad = '',
361
-        $nonce = '',
362
-        $key = ''
363
-    ) {
364
-        $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
365
-            ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16),
366
-            $key
367
-        );
368
-        $nonceLast = "\x00\x00\x00\x00" .
369
-            ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
370
-
371
-        return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey);
372
-    }
373
-
374
-    /**
375
-     * HMAC-SHA-512-256 (a.k.a. the leftmost 256 bits of HMAC-SHA-512)
376
-     *
377
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
378
-     *
379
-     * @param string $message
380
-     * @param string $key
381
-     * @return string
382
-     * @throws TypeError
383
-     */
384
-    public static function auth($message, $key)
385
-    {
386
-        return ParagonIE_Sodium_Core_Util::substr(
387
-            hash_hmac('sha512', $message, $key, true),
388
-            0,
389
-            32
390
-        );
391
-    }
392
-
393
-    /**
394
-     * HMAC-SHA-512-256 validation. Constant-time via hash_equals().
395
-     *
396
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
397
-     *
398
-     * @param string $mac
399
-     * @param string $message
400
-     * @param string $key
401
-     * @return bool
402
-     * @throws SodiumException
403
-     * @throws TypeError
404
-     */
405
-    public static function auth_verify($mac, $message, $key)
406
-    {
407
-        return ParagonIE_Sodium_Core_Util::hashEquals(
408
-            $mac,
409
-            self::auth($message, $key)
410
-        );
411
-    }
412
-
413
-    /**
414
-     * X25519 key exchange followed by XSalsa20Poly1305 symmetric encryption
415
-     *
416
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
417
-     *
418
-     * @param string $plaintext
419
-     * @param string $nonce
420
-     * @param string $keypair
421
-     * @return string
422
-     * @throws SodiumException
423
-     * @throws TypeError
424
-     */
425
-    public static function box($plaintext, $nonce, $keypair)
426
-    {
427
-        $c = self::secretbox(
428
-            $plaintext,
429
-            $nonce,
430
-            self::box_beforenm(
431
-                self::box_secretkey($keypair),
432
-                self::box_publickey($keypair)
433
-            )
434
-        );
435
-        return $c;
436
-    }
437
-
438
-    /**
439
-     * X25519-XSalsa20-Poly1305 with one ephemeral X25519 keypair.
440
-     *
441
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
442
-     *
443
-     * @param string $message
444
-     * @param string $publicKey
445
-     * @return string
446
-     * @throws SodiumException
447
-     * @throws TypeError
448
-     */
449
-    public static function box_seal($message, $publicKey)
450
-    {
451
-        /** @var string $ephemeralKeypair */
452
-        $ephemeralKeypair = self::box_keypair();
453
-
454
-        /** @var string $ephemeralSK */
455
-        $ephemeralSK = self::box_secretkey($ephemeralKeypair);
456
-
457
-        /** @var string $ephemeralPK */
458
-        $ephemeralPK = self::box_publickey($ephemeralKeypair);
459
-
460
-        /** @var string $nonce */
461
-        $nonce = self::generichash(
462
-            $ephemeralPK . $publicKey,
463
-            '',
464
-            24
465
-        );
466
-
467
-        /** @var string $keypair - The combined keypair used in crypto_box() */
468
-        $keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey);
469
-
470
-        /** @var string $ciphertext Ciphertext + MAC from crypto_box */
471
-        $ciphertext = self::box($message, $nonce, $keypair);
472
-        try {
473
-            ParagonIE_Sodium_Compat::memzero($ephemeralKeypair);
474
-            ParagonIE_Sodium_Compat::memzero($ephemeralSK);
475
-            ParagonIE_Sodium_Compat::memzero($nonce);
476
-        } catch (SodiumException $ex) {
477
-            $ephemeralKeypair = null;
478
-            $ephemeralSK = null;
479
-            $nonce = null;
480
-        }
481
-        return $ephemeralPK . $ciphertext;
482
-    }
483
-
484
-    /**
485
-     * Opens a message encrypted via box_seal().
486
-     *
487
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
488
-     *
489
-     * @param string $message
490
-     * @param string $keypair
491
-     * @return string
492
-     * @throws SodiumException
493
-     * @throws TypeError
494
-     */
495
-    public static function box_seal_open($message, $keypair)
496
-    {
497
-        /** @var string $ephemeralPK */
498
-        $ephemeralPK = ParagonIE_Sodium_Core_Util::substr($message, 0, 32);
499
-
500
-        /** @var string $ciphertext (ciphertext + MAC) */
501
-        $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 32);
502
-
503
-        /** @var string $secretKey */
504
-        $secretKey = self::box_secretkey($keypair);
505
-
506
-        /** @var string $publicKey */
507
-        $publicKey = self::box_publickey($keypair);
508
-
509
-        /** @var string $nonce */
510
-        $nonce = self::generichash(
511
-            $ephemeralPK . $publicKey,
512
-            '',
513
-            24
514
-        );
515
-
516
-        /** @var string $keypair */
517
-        $keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK);
518
-
519
-        /** @var string $m */
520
-        $m = self::box_open($ciphertext, $nonce, $keypair);
521
-        try {
522
-            ParagonIE_Sodium_Compat::memzero($secretKey);
523
-            ParagonIE_Sodium_Compat::memzero($ephemeralPK);
524
-            ParagonIE_Sodium_Compat::memzero($nonce);
525
-        } catch (SodiumException $ex) {
526
-            $secretKey = null;
527
-            $ephemeralPK = null;
528
-            $nonce = null;
529
-        }
530
-        return $m;
531
-    }
532
-
533
-    /**
534
-     * Used by crypto_box() to get the crypto_secretbox() key.
535
-     *
536
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
537
-     *
538
-     * @param string $sk
539
-     * @param string $pk
540
-     * @return string
541
-     * @throws SodiumException
542
-     * @throws TypeError
543
-     */
544
-    public static function box_beforenm($sk, $pk)
545
-    {
546
-        return ParagonIE_Sodium_Core_HSalsa20::hsalsa20(
547
-            str_repeat("\x00", 16),
548
-            self::scalarmult($sk, $pk)
549
-        );
550
-    }
551
-
552
-    /**
553
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
554
-     *
555
-     * @return string
556
-     * @throws Exception
557
-     * @throws SodiumException
558
-     * @throws TypeError
559
-     */
560
-    public static function box_keypair()
561
-    {
562
-        $sKey = random_bytes(32);
563
-        $pKey = self::scalarmult_base($sKey);
564
-        return $sKey . $pKey;
565
-    }
566
-
567
-    /**
568
-     * @param string $seed
569
-     * @return string
570
-     * @throws SodiumException
571
-     * @throws TypeError
572
-     */
573
-    public static function box_seed_keypair($seed)
574
-    {
575
-        $sKey = ParagonIE_Sodium_Core_Util::substr(
576
-            hash('sha512', $seed, true),
577
-            0,
578
-            32
579
-        );
580
-        $pKey = self::scalarmult_base($sKey);
581
-        return $sKey . $pKey;
582
-    }
583
-
584
-    /**
585
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
586
-     *
587
-     * @param string $sKey
588
-     * @param string $pKey
589
-     * @return string
590
-     * @throws TypeError
591
-     */
592
-    public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey)
593
-    {
594
-        return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) .
595
-            ParagonIE_Sodium_Core_Util::substr($pKey, 0, 32);
596
-    }
597
-
598
-    /**
599
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
600
-     *
601
-     * @param string $keypair
602
-     * @return string
603
-     * @throws RangeException
604
-     * @throws TypeError
605
-     */
606
-    public static function box_secretkey($keypair)
607
-    {
608
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) {
609
-            throw new RangeException(
610
-                'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
611
-            );
612
-        }
613
-        return ParagonIE_Sodium_Core_Util::substr($keypair, 0, 32);
614
-    }
615
-
616
-    /**
617
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
618
-     *
619
-     * @param string $keypair
620
-     * @return string
621
-     * @throws RangeException
622
-     * @throws TypeError
623
-     */
624
-    public static function box_publickey($keypair)
625
-    {
626
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
627
-            throw new RangeException(
628
-                'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
629
-            );
630
-        }
631
-        return ParagonIE_Sodium_Core_Util::substr($keypair, 32, 32);
632
-    }
633
-
634
-    /**
635
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
636
-     *
637
-     * @param string $sKey
638
-     * @return string
639
-     * @throws RangeException
640
-     * @throws SodiumException
641
-     * @throws TypeError
642
-     */
643
-    public static function box_publickey_from_secretkey($sKey)
644
-    {
645
-        if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) {
646
-            throw new RangeException(
647
-                'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.'
648
-            );
649
-        }
650
-        return self::scalarmult_base($sKey);
651
-    }
652
-
653
-    /**
654
-     * Decrypt a message encrypted with box().
655
-     *
656
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
657
-     *
658
-     * @param string $ciphertext
659
-     * @param string $nonce
660
-     * @param string $keypair
661
-     * @return string
662
-     * @throws SodiumException
663
-     * @throws TypeError
664
-     */
665
-    public static function box_open($ciphertext, $nonce, $keypair)
666
-    {
667
-        return self::secretbox_open(
668
-            $ciphertext,
669
-            $nonce,
670
-            self::box_beforenm(
671
-                self::box_secretkey($keypair),
672
-                self::box_publickey($keypair)
673
-            )
674
-        );
675
-    }
676
-
677
-    /**
678
-     * Calculate a BLAKE2b hash.
679
-     *
680
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
681
-     *
682
-     * @param string $message
683
-     * @param string|null $key
684
-     * @param int $outlen
685
-     * @return string
686
-     * @throws RangeException
687
-     * @throws SodiumException
688
-     * @throws TypeError
689
-     */
690
-    public static function generichash($message, $key = '', $outlen = 32)
691
-    {
692
-        // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
693
-        ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
694
-
695
-        $k = null;
696
-        if (!empty($key)) {
697
-            /** @var SplFixedArray $k */
698
-            $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key);
699
-            if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) {
700
-                throw new RangeException('Invalid key size');
701
-            }
702
-        }
703
-
704
-        /** @var SplFixedArray $in */
705
-        $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message);
706
-
707
-        /** @var SplFixedArray $ctx */
708
-        $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outlen);
709
-        ParagonIE_Sodium_Core_BLAKE2b::update($ctx, $in, $in->count());
710
-
711
-        /** @var SplFixedArray $out */
712
-        $out = new SplFixedArray($outlen);
713
-        $out = ParagonIE_Sodium_Core_BLAKE2b::finish($ctx, $out);
714
-
715
-        /** @var array<int, int> */
716
-        $outArray = $out->toArray();
717
-        return ParagonIE_Sodium_Core_Util::intArrayToString($outArray);
718
-    }
719
-
720
-    /**
721
-     * Finalize a BLAKE2b hashing context, returning the hash.
722
-     *
723
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
724
-     *
725
-     * @param string $ctx
726
-     * @param int $outlen
727
-     * @return string
728
-     * @throws SodiumException
729
-     * @throws TypeError
730
-     */
731
-    public static function generichash_final($ctx, $outlen = 32)
732
-    {
733
-        if (!is_string($ctx)) {
734
-            throw new TypeError('Context must be a string');
735
-        }
736
-        $out = new SplFixedArray($outlen);
737
-
738
-        /** @var SplFixedArray $context */
739
-        $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx);
740
-
741
-        /** @var SplFixedArray $out */
742
-        $out = ParagonIE_Sodium_Core_BLAKE2b::finish($context, $out);
743
-
744
-        /** @var array<int, int> */
745
-        $outArray = $out->toArray();
746
-        return ParagonIE_Sodium_Core_Util::intArrayToString($outArray);
747
-    }
748
-
749
-    /**
750
-     * Initialize a hashing context for BLAKE2b.
751
-     *
752
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
753
-     *
754
-     * @param string $key
755
-     * @param int $outputLength
756
-     * @return string
757
-     * @throws RangeException
758
-     * @throws SodiumException
759
-     * @throws TypeError
760
-     */
761
-    public static function generichash_init($key = '', $outputLength = 32)
762
-    {
763
-        // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
764
-        ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
765
-
766
-        $k = null;
767
-        if (!empty($key)) {
768
-            $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key);
769
-            if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) {
770
-                throw new RangeException('Invalid key size');
771
-            }
772
-        }
773
-
774
-        /** @var SplFixedArray $ctx */
775
-        $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength);
776
-
777
-        return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx);
778
-    }
779
-
780
-    /**
781
-     * Initialize a hashing context for BLAKE2b.
782
-     *
783
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
784
-     *
785
-     * @param string $key
786
-     * @param int $outputLength
787
-     * @param string $salt
788
-     * @param string $personal
789
-     * @return string
790
-     * @throws RangeException
791
-     * @throws SodiumException
792
-     * @throws TypeError
793
-     */
794
-    public static function generichash_init_salt_personal(
795
-        $key = '',
796
-        $outputLength = 32,
797
-        $salt = '',
798
-        $personal = ''
799
-    ) {
800
-        // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
801
-        ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
802
-
803
-        $k = null;
804
-        if (!empty($key)) {
805
-            $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key);
806
-            if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) {
807
-                throw new RangeException('Invalid key size');
808
-            }
809
-        }
810
-        if (!empty($salt)) {
811
-            $s = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($salt);
812
-        } else {
813
-            $s = null;
814
-        }
815
-        if (!empty($salt)) {
816
-            $p = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($personal);
817
-        } else {
818
-            $p = null;
819
-        }
820
-
821
-        /** @var SplFixedArray $ctx */
822
-        $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength, $s, $p);
823
-
824
-        return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx);
825
-    }
826
-
827
-    /**
828
-     * Update a hashing context for BLAKE2b with $message
829
-     *
830
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
831
-     *
832
-     * @param string $ctx
833
-     * @param string $message
834
-     * @return string
835
-     * @throws SodiumException
836
-     * @throws TypeError
837
-     */
838
-    public static function generichash_update($ctx, $message)
839
-    {
840
-        // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
841
-        ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
842
-
843
-        /** @var SplFixedArray $context */
844
-        $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx);
845
-
846
-        /** @var SplFixedArray $in */
847
-        $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message);
848
-
849
-        ParagonIE_Sodium_Core_BLAKE2b::update($context, $in, $in->count());
850
-
851
-        return ParagonIE_Sodium_Core_BLAKE2b::contextToString($context);
852
-    }
853
-
854
-    /**
855
-     * Libsodium's crypto_kx().
856
-     *
857
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
858
-     *
859
-     * @param string $my_sk
860
-     * @param string $their_pk
861
-     * @param string $client_pk
862
-     * @param string $server_pk
863
-     * @return string
864
-     * @throws SodiumException
865
-     * @throws TypeError
866
-     */
867
-    public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk)
868
-    {
869
-        return ParagonIE_Sodium_Compat::crypto_generichash(
870
-            ParagonIE_Sodium_Compat::crypto_scalarmult($my_sk, $their_pk) .
871
-            $client_pk .
872
-            $server_pk
873
-        );
874
-    }
875
-
876
-    /**
877
-     * ECDH over Curve25519
878
-     *
879
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
880
-     *
881
-     * @param string $sKey
882
-     * @param string $pKey
883
-     * @return string
884
-     *
885
-     * @throws SodiumException
886
-     * @throws TypeError
887
-     */
888
-    public static function scalarmult($sKey, $pKey)
889
-    {
890
-        $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey);
891
-        self::scalarmult_throw_if_zero($q);
892
-        return $q;
893
-    }
894
-
895
-    /**
896
-     * ECDH over Curve25519, using the basepoint.
897
-     * Used to get a secret key from a public key.
898
-     *
899
-     * @param string $secret
900
-     * @return string
901
-     *
902
-     * @throws SodiumException
903
-     * @throws TypeError
904
-     */
905
-    public static function scalarmult_base($secret)
906
-    {
907
-        $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret);
908
-        self::scalarmult_throw_if_zero($q);
909
-        return $q;
910
-    }
911
-
912
-    /**
913
-     * This throws an Error if a zero public key was passed to the function.
914
-     *
915
-     * @param string $q
916
-     * @return void
917
-     * @throws SodiumException
918
-     * @throws TypeError
919
-     */
920
-    protected static function scalarmult_throw_if_zero($q)
921
-    {
922
-        $d = 0;
923
-        for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) {
924
-            $d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]);
925
-        }
926
-
927
-        /* branch-free variant of === 0 */
928
-        if (-(1 & (($d - 1) >> 8))) {
929
-            throw new SodiumException('Zero public key is not allowed');
930
-        }
931
-    }
932
-
933
-    /**
934
-     * XSalsa20-Poly1305 authenticated symmetric-key encryption.
935
-     *
936
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
937
-     *
938
-     * @param string $plaintext
939
-     * @param string $nonce
940
-     * @param string $key
941
-     * @return string
942
-     * @throws SodiumException
943
-     * @throws TypeError
944
-     */
945
-    public static function secretbox($plaintext, $nonce, $key)
946
-    {
947
-        /** @var string $subkey */
948
-        $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
949
-
950
-        /** @var string $block0 */
951
-        $block0 = str_repeat("\x00", 32);
952
-
953
-        /** @var int $mlen - Length of the plaintext message */
954
-        $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext);
955
-        $mlen0 = $mlen;
956
-        if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) {
957
-            $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES;
958
-        }
959
-        $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0);
960
-
961
-        /** @var string $block0 */
962
-        $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor(
963
-            $block0,
964
-            ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
965
-            $subkey
966
-        );
967
-
968
-        /** @var string $c */
969
-        $c = ParagonIE_Sodium_Core_Util::substr(
970
-            $block0,
971
-            self::secretbox_xsalsa20poly1305_ZEROBYTES
972
-        );
973
-        if ($mlen > $mlen0) {
974
-            $c .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
975
-                ParagonIE_Sodium_Core_Util::substr(
976
-                    $plaintext,
977
-                    self::secretbox_xsalsa20poly1305_ZEROBYTES
978
-                ),
979
-                ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
980
-                1,
981
-                $subkey
982
-            );
983
-        }
984
-        $state = new ParagonIE_Sodium_Core_Poly1305_State(
985
-            ParagonIE_Sodium_Core_Util::substr(
986
-                $block0,
987
-                0,
988
-                self::onetimeauth_poly1305_KEYBYTES
989
-            )
990
-        );
991
-        try {
992
-            ParagonIE_Sodium_Compat::memzero($block0);
993
-            ParagonIE_Sodium_Compat::memzero($subkey);
994
-        } catch (SodiumException $ex) {
995
-            $block0 = null;
996
-            $subkey = null;
997
-        }
998
-
999
-        $state->update($c);
1000
-
1001
-        /** @var string $c - MAC || ciphertext */
1002
-        $c = $state->finish() . $c;
1003
-        unset($state);
1004
-
1005
-        return $c;
1006
-    }
1007
-
1008
-    /**
1009
-     * Decrypt a ciphertext generated via secretbox().
1010
-     *
1011
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1012
-     *
1013
-     * @param string $ciphertext
1014
-     * @param string $nonce
1015
-     * @param string $key
1016
-     * @return string
1017
-     * @throws SodiumException
1018
-     * @throws TypeError
1019
-     */
1020
-    public static function secretbox_open($ciphertext, $nonce, $key)
1021
-    {
1022
-        /** @var string $mac */
1023
-        $mac = ParagonIE_Sodium_Core_Util::substr(
1024
-            $ciphertext,
1025
-            0,
1026
-            self::secretbox_xsalsa20poly1305_MACBYTES
1027
-        );
1028
-
1029
-        /** @var string $c */
1030
-        $c = ParagonIE_Sodium_Core_Util::substr(
1031
-            $ciphertext,
1032
-            self::secretbox_xsalsa20poly1305_MACBYTES
1033
-        );
1034
-
1035
-        /** @var int $clen */
1036
-        $clen = ParagonIE_Sodium_Core_Util::strlen($c);
1037
-
1038
-        /** @var string $subkey */
1039
-        $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
1040
-
1041
-        /** @var string $block0 */
1042
-        $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20(
1043
-            64,
1044
-            ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
1045
-            $subkey
1046
-        );
1047
-        $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify(
1048
-            $mac,
1049
-            $c,
1050
-            ParagonIE_Sodium_Core_Util::substr($block0, 0, 32)
1051
-        );
1052
-        if (!$verified) {
1053
-            try {
1054
-                ParagonIE_Sodium_Compat::memzero($subkey);
1055
-            } catch (SodiumException $ex) {
1056
-                $subkey = null;
1057
-            }
1058
-            throw new SodiumException('Invalid MAC');
1059
-        }
1060
-
1061
-        /** @var string $m - Decrypted message */
1062
-        $m = ParagonIE_Sodium_Core_Util::xorStrings(
1063
-            ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES),
1064
-            ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES)
1065
-        );
1066
-        if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) {
1067
-            // We had more than 1 block, so let's continue to decrypt the rest.
1068
-            $m .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
1069
-                ParagonIE_Sodium_Core_Util::substr(
1070
-                    $c,
1071
-                    self::secretbox_xsalsa20poly1305_ZEROBYTES
1072
-                ),
1073
-                ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
1074
-                1,
1075
-                (string) $subkey
1076
-            );
1077
-        }
1078
-        return $m;
1079
-    }
1080
-
1081
-    /**
1082
-     * XChaCha20-Poly1305 authenticated symmetric-key encryption.
1083
-     *
1084
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1085
-     *
1086
-     * @param string $plaintext
1087
-     * @param string $nonce
1088
-     * @param string $key
1089
-     * @return string
1090
-     * @throws SodiumException
1091
-     * @throws TypeError
1092
-     */
1093
-    public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key)
1094
-    {
1095
-        /** @var string $subkey */
1096
-        $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
1097
-            ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16),
1098
-            $key
1099
-        );
1100
-        $nonceLast = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
1101
-
1102
-        /** @var string $block0 */
1103
-        $block0 = str_repeat("\x00", 32);
1104
-
1105
-        /** @var int $mlen - Length of the plaintext message */
1106
-        $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext);
1107
-        $mlen0 = $mlen;
1108
-        if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) {
1109
-            $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES;
1110
-        }
1111
-        $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0);
1112
-
1113
-        /** @var string $block0 */
1114
-        $block0 = ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
1115
-            $block0,
1116
-            $nonceLast,
1117
-            $subkey
1118
-        );
1119
-
1120
-        /** @var string $c */
1121
-        $c = ParagonIE_Sodium_Core_Util::substr(
1122
-            $block0,
1123
-            self::secretbox_xchacha20poly1305_ZEROBYTES
1124
-        );
1125
-        if ($mlen > $mlen0) {
1126
-            $c .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
1127
-                ParagonIE_Sodium_Core_Util::substr(
1128
-                    $plaintext,
1129
-                    self::secretbox_xchacha20poly1305_ZEROBYTES
1130
-                ),
1131
-                $nonceLast,
1132
-                $subkey,
1133
-                ParagonIE_Sodium_Core_Util::store64_le(1)
1134
-            );
1135
-        }
1136
-        $state = new ParagonIE_Sodium_Core_Poly1305_State(
1137
-            ParagonIE_Sodium_Core_Util::substr(
1138
-                $block0,
1139
-                0,
1140
-                self::onetimeauth_poly1305_KEYBYTES
1141
-            )
1142
-        );
1143
-        try {
1144
-            ParagonIE_Sodium_Compat::memzero($block0);
1145
-            ParagonIE_Sodium_Compat::memzero($subkey);
1146
-        } catch (SodiumException $ex) {
1147
-            $block0 = null;
1148
-            $subkey = null;
1149
-        }
1150
-
1151
-        $state->update($c);
1152
-
1153
-        /** @var string $c - MAC || ciphertext */
1154
-        $c = $state->finish() . $c;
1155
-        unset($state);
1156
-
1157
-        return $c;
1158
-    }
1159
-
1160
-    /**
1161
-     * Decrypt a ciphertext generated via secretbox_xchacha20poly1305().
1162
-     *
1163
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1164
-     *
1165
-     * @param string $ciphertext
1166
-     * @param string $nonce
1167
-     * @param string $key
1168
-     * @return string
1169
-     * @throws SodiumException
1170
-     * @throws TypeError
1171
-     */
1172
-    public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
1173
-    {
1174
-        /** @var string $mac */
1175
-        $mac = ParagonIE_Sodium_Core_Util::substr(
1176
-            $ciphertext,
1177
-            0,
1178
-            self::secretbox_xchacha20poly1305_MACBYTES
1179
-        );
1180
-
1181
-        /** @var string $c */
1182
-        $c = ParagonIE_Sodium_Core_Util::substr(
1183
-            $ciphertext,
1184
-            self::secretbox_xchacha20poly1305_MACBYTES
1185
-        );
1186
-
1187
-        /** @var int $clen */
1188
-        $clen = ParagonIE_Sodium_Core_Util::strlen($c);
1189
-
1190
-        /** @var string $subkey */
1191
-        $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20($nonce, $key);
1192
-
1193
-        /** @var string $block0 */
1194
-        $block0 = ParagonIE_Sodium_Core_ChaCha20::stream(
1195
-            64,
1196
-            ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
1197
-            $subkey
1198
-        );
1199
-        $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify(
1200
-            $mac,
1201
-            $c,
1202
-            ParagonIE_Sodium_Core_Util::substr($block0, 0, 32)
1203
-        );
1204
-
1205
-        if (!$verified) {
1206
-            try {
1207
-                ParagonIE_Sodium_Compat::memzero($subkey);
1208
-            } catch (SodiumException $ex) {
1209
-                $subkey = null;
1210
-            }
1211
-            throw new SodiumException('Invalid MAC');
1212
-        }
1213
-
1214
-        /** @var string $m - Decrypted message */
1215
-        $m = ParagonIE_Sodium_Core_Util::xorStrings(
1216
-            ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES),
1217
-            ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES)
1218
-        );
1219
-
1220
-        if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) {
1221
-            // We had more than 1 block, so let's continue to decrypt the rest.
1222
-            $m .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
1223
-                ParagonIE_Sodium_Core_Util::substr(
1224
-                    $c,
1225
-                    self::secretbox_xchacha20poly1305_ZEROBYTES
1226
-                ),
1227
-                ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
1228
-                (string) $subkey,
1229
-                ParagonIE_Sodium_Core_Util::store64_le(1)
1230
-            );
1231
-        }
1232
-        return $m;
1233
-    }
1234
-
1235
-    /**
1236
-     * @param string $key
1237
-     * @return array<int, string> Returns a state and a header.
1238
-     * @throws Exception
1239
-     * @throws SodiumException
1240
-     */
1241
-    public static function secretstream_xchacha20poly1305_init_push($key)
1242
-    {
1243
-        # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES);
1244
-        $out = random_bytes(24);
1245
-
1246
-        # crypto_core_hchacha20(state->k, out, k, NULL);
1247
-        $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20($out, $key);
1248
-        $state = new ParagonIE_Sodium_Core_SecretStream_State(
1249
-            $subkey,
1250
-            ParagonIE_Sodium_Core_Util::substr($out, 16, 8) . str_repeat("\0", 4)
1251
-        );
1252
-
1253
-        # _crypto_secretstream_xchacha20poly1305_counter_reset(state);
1254
-        $state->counterReset();
1255
-
1256
-        # memcpy(STATE_INONCE(state), out + crypto_core_hchacha20_INPUTBYTES,
1257
-        #        crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1258
-        # memset(state->_pad, 0, sizeof state->_pad);
1259
-        return array(
1260
-            $state->toString(),
1261
-            $out
1262
-        );
1263
-    }
1264
-
1265
-    /**
1266
-     * @param string $key
1267
-     * @param string $header
1268
-     * @return string Returns a state.
1269
-     * @throws Exception
1270
-     */
1271
-    public static function secretstream_xchacha20poly1305_init_pull($key, $header)
1272
-    {
1273
-        # crypto_core_hchacha20(state->k, in, k, NULL);
1274
-        $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
1275
-            ParagonIE_Sodium_Core_Util::substr($header, 0, 16),
1276
-            $key
1277
-        );
1278
-        $state = new ParagonIE_Sodium_Core_SecretStream_State(
1279
-            $subkey,
1280
-            ParagonIE_Sodium_Core_Util::substr($header, 16)
1281
-        );
1282
-        $state->counterReset();
1283
-        # memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES,
1284
-        #     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1285
-        # memset(state->_pad, 0, sizeof state->_pad);
1286
-        # return 0;
1287
-        return $state->toString();
1288
-    }
1289
-
1290
-    /**
1291
-     * @param string $state
1292
-     * @param string $msg
1293
-     * @param string $aad
1294
-     * @param int $tag
1295
-     * @return string
1296
-     * @throws SodiumException
1297
-     */
1298
-    public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
1299
-    {
1300
-        $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
1301
-        # crypto_onetimeauth_poly1305_state poly1305_state;
1302
-        # unsigned char                     block[64U];
1303
-        # unsigned char                     slen[8U];
1304
-        # unsigned char                    *c;
1305
-        # unsigned char                    *mac;
1306
-
1307
-        $msglen = ParagonIE_Sodium_Core_Util::strlen($msg);
1308
-        $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad);
1309
-
1310
-        if ((($msglen + 63) >> 6) > 0xfffffffe) {
1311
-            throw new SodiumException(
1312
-                'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
1313
-            );
1314
-        }
1315
-
1316
-        # if (outlen_p != NULL) {
1317
-        #     *outlen_p = 0U;
1318
-        # }
1319
-        # if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
1320
-        #     sodium_misuse();
1321
-        # }
1322
-
1323
-        # crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
1324
-        # crypto_onetimeauth_poly1305_init(&poly1305_state, block);
1325
-        # sodium_memzero(block, sizeof block);
1326
-        $auth = new ParagonIE_Sodium_Core_Poly1305_State(
1327
-            ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
1328
-        );
1329
-
1330
-        # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
1331
-        $auth->update($aad);
1332
-
1333
-        # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
1334
-        #     (0x10 - adlen) & 0xf);
1335
-        $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
1336
-
1337
-        # memset(block, 0, sizeof block);
1338
-        # block[0] = tag;
1339
-        # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
1340
-        #                                    state->nonce, 1U, state->k);
1341
-        $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1342
-            ParagonIE_Sodium_Core_Util::intToChr($tag) . str_repeat("\0", 63),
1343
-            $st->getCombinedNonce(),
1344
-            $st->getKey(),
1345
-            ParagonIE_Sodium_Core_Util::store64_le(1)
1346
-        );
1347
-
1348
-        # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
1349
-        $auth->update($block);
1350
-
1351
-        # out[0] = block[0];
1352
-        $out = $block[0];
1353
-        # c = out + (sizeof tag);
1354
-        # crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k);
1355
-        $cipher = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1356
-            $msg,
1357
-            $st->getCombinedNonce(),
1358
-            $st->getKey(),
1359
-            ParagonIE_Sodium_Core_Util::store64_le(2)
1360
-        );
1361
-
1362
-        # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
1363
-        $auth->update($cipher);
1364
-
1365
-        $out .= $cipher;
1366
-        unset($cipher);
1367
-
1368
-        # crypto_onetimeauth_poly1305_update
1369
-        # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
1370
-        $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
1371
-
1372
-        # STORE64_LE(slen, (uint64_t) adlen);
1373
-        $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen);
1374
-
1375
-        # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1376
-        $auth->update($slen);
1377
-
1378
-        # STORE64_LE(slen, (sizeof block) + mlen);
1379
-        $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen);
1380
-
1381
-        # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1382
-        $auth->update($slen);
1383
-
1384
-        # mac = c + mlen;
1385
-        # crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
1386
-        $mac = $auth->finish();
1387
-        $out .= $mac;
1388
-
1389
-        # sodium_memzero(&poly1305_state, sizeof poly1305_state);
1390
-        unset($auth);
1391
-
1392
-
1393
-        # XOR_BUF(STATE_INONCE(state), mac,
1394
-        #     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1395
-        $st->xorNonce($mac);
1396
-
1397
-        # sodium_increment(STATE_COUNTER(state),
1398
-        #     crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
1399
-        $st->incrementCounter();
1400
-        // Overwrite by reference:
1401
-        $state = $st->toString();
1402
-
1403
-        /** @var bool $rekey */
1404
-        $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
1405
-        # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
1406
-        #     sodium_is_zero(STATE_COUNTER(state),
1407
-        #         crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
1408
-        #     crypto_secretstream_xchacha20poly1305_rekey(state);
1409
-        # }
1410
-        if ($rekey || $st->needsRekey()) {
1411
-            // DO REKEY
1412
-            self::secretstream_xchacha20poly1305_rekey($state);
1413
-        }
1414
-        # if (outlen_p != NULL) {
1415
-        #     *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen;
1416
-        # }
1417
-        return $out;
1418
-    }
1419
-
1420
-    /**
1421
-     * @param string $state
1422
-     * @param string $cipher
1423
-     * @param string $aad
1424
-     * @return bool|array{0: string, 1: int}
1425
-     * @throws SodiumException
1426
-     */
1427
-    public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '')
1428
-    {
1429
-        $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
1430
-
1431
-        $cipherlen = ParagonIE_Sodium_Core_Util::strlen($cipher);
1432
-        #     mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES;
1433
-        $msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES;
1434
-        $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad);
1435
-
1436
-        #     if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
1437
-        #         sodium_misuse();
1438
-        #     }
1439
-        if ((($msglen + 63) >> 6) > 0xfffffffe) {
1440
-            throw new SodiumException(
1441
-                'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
1442
-            );
1443
-        }
1444
-
1445
-        #     crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
1446
-        #     crypto_onetimeauth_poly1305_init(&poly1305_state, block);
1447
-        #     sodium_memzero(block, sizeof block);
1448
-        $auth = new ParagonIE_Sodium_Core_Poly1305_State(
1449
-            ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
1450
-        );
1451
-
1452
-        #     crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
1453
-        $auth->update($aad);
1454
-
1455
-        #     crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
1456
-        #         (0x10 - adlen) & 0xf);
1457
-        $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
1458
-
1459
-
1460
-        #     memset(block, 0, sizeof block);
1461
-        #     block[0] = in[0];
1462
-        #     crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
1463
-        #                                        state->nonce, 1U, state->k);
1464
-        $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1465
-            $cipher[0] . str_repeat("\0", 63),
1466
-            $st->getCombinedNonce(),
1467
-            $st->getKey(),
1468
-            ParagonIE_Sodium_Core_Util::store64_le(1)
1469
-        );
1470
-        #     tag = block[0];
1471
-        #     block[0] = in[0];
1472
-        #     crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
1473
-        $tag = ParagonIE_Sodium_Core_Util::chrToInt($block[0]);
1474
-        $block[0] = $cipher[0];
1475
-        $auth->update($block);
1476
-
1477
-
1478
-        #     c = in + (sizeof tag);
1479
-        #     crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
1480
-        $auth->update(ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen));
1481
-
1482
-        #     crypto_onetimeauth_poly1305_update
1483
-        #     (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
1484
-        $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
1485
-
1486
-        #     STORE64_LE(slen, (uint64_t) adlen);
1487
-        #     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1488
-        $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen);
1489
-        $auth->update($slen);
1490
-
1491
-        #     STORE64_LE(slen, (sizeof block) + mlen);
1492
-        #     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1493
-        $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen);
1494
-        $auth->update($slen);
1495
-
1496
-        #     crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
1497
-        #     sodium_memzero(&poly1305_state, sizeof poly1305_state);
1498
-        $mac = $auth->finish();
1499
-
1500
-        #     stored_mac = c + mlen;
1501
-        #     if (sodium_memcmp(mac, stored_mac, sizeof mac) != 0) {
1502
-        #     sodium_memzero(mac, sizeof mac);
1503
-        #         return -1;
1504
-        #     }
1505
-
1506
-        $stored = ParagonIE_Sodium_Core_Util::substr($cipher, $msglen + 1, 16);
1507
-        if (!ParagonIE_Sodium_Core_Util::hashEquals($mac, $stored)) {
1508
-            return false;
1509
-        }
1510
-
1511
-        #     crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k);
1512
-        $out = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1513
-            ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen),
1514
-            $st->getCombinedNonce(),
1515
-            $st->getKey(),
1516
-            ParagonIE_Sodium_Core_Util::store64_le(2)
1517
-        );
1518
-
1519
-        #     XOR_BUF(STATE_INONCE(state), mac,
1520
-        #         crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1521
-        $st->xorNonce($mac);
1522
-
1523
-        #     sodium_increment(STATE_COUNTER(state),
1524
-        #         crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
1525
-        $st->incrementCounter();
1526
-
1527
-        #     if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
1528
-        #         sodium_is_zero(STATE_COUNTER(state),
1529
-        #             crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
1530
-        #         crypto_secretstream_xchacha20poly1305_rekey(state);
1531
-        #     }
1532
-
1533
-        // Overwrite by reference:
1534
-        $state = $st->toString();
1535
-
1536
-        /** @var bool $rekey */
1537
-        $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
1538
-        if ($rekey || $st->needsRekey()) {
1539
-            // DO REKEY
1540
-            self::secretstream_xchacha20poly1305_rekey($state);
1541
-        }
1542
-        return array($out, $tag);
1543
-    }
1544
-
1545
-    /**
1546
-     * @param string $state
1547
-     * @return void
1548
-     * @throws SodiumException
1549
-     */
1550
-    public static function secretstream_xchacha20poly1305_rekey(&$state)
1551
-    {
1552
-        $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
1553
-        # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES +
1554
-        # crypto_secretstream_xchacha20poly1305_INONCEBYTES];
1555
-        # size_t        i;
1556
-        # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
1557
-        #     new_key_and_inonce[i] = state->k[i];
1558
-        # }
1559
-        $new_key_and_inonce = $st->getKey();
1560
-
1561
-        # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
1562
-        #     new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] =
1563
-        #         STATE_INONCE(state)[i];
1564
-        # }
1565
-        $new_key_and_inonce .= ParagonIE_Sodium_Core_Util::substR($st->getNonce(), 0, 8);
1566
-
1567
-        # crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce,
1568
-        #                                 sizeof new_key_and_inonce,
1569
-        #                                 state->nonce, state->k);
1570
-
1571
-        $st->rekey(ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1572
-            $new_key_and_inonce,
1573
-            $st->getCombinedNonce(),
1574
-            $st->getKey(),
1575
-            ParagonIE_Sodium_Core_Util::store64_le(0)
1576
-        ));
1577
-
1578
-        # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
1579
-        #     state->k[i] = new_key_and_inonce[i];
1580
-        # }
1581
-        # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
1582
-        #     STATE_INONCE(state)[i] =
1583
-        #          new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i];
1584
-        # }
1585
-        # _crypto_secretstream_xchacha20poly1305_counter_reset(state);
1586
-        $st->counterReset();
1587
-
1588
-        $state = $st->toString();
1589
-    }
1590
-
1591
-    /**
1592
-     * Detached Ed25519 signature.
1593
-     *
1594
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1595
-     *
1596
-     * @param string $message
1597
-     * @param string $sk
1598
-     * @return string
1599
-     * @throws SodiumException
1600
-     * @throws TypeError
1601
-     */
1602
-    public static function sign_detached($message, $sk)
1603
-    {
1604
-        return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk);
1605
-    }
1606
-
1607
-    /**
1608
-     * Attached Ed25519 signature. (Returns a signed message.)
1609
-     *
1610
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1611
-     *
1612
-     * @param string $message
1613
-     * @param string $sk
1614
-     * @return string
1615
-     * @throws SodiumException
1616
-     * @throws TypeError
1617
-     */
1618
-    public static function sign($message, $sk)
1619
-    {
1620
-        return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk);
1621
-    }
1622
-
1623
-    /**
1624
-     * Opens a signed message. If valid, returns the message.
1625
-     *
1626
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1627
-     *
1628
-     * @param string $signedMessage
1629
-     * @param string $pk
1630
-     * @return string
1631
-     * @throws SodiumException
1632
-     * @throws TypeError
1633
-     */
1634
-    public static function sign_open($signedMessage, $pk)
1635
-    {
1636
-        return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk);
1637
-    }
1638
-
1639
-    /**
1640
-     * Verify a detached signature of a given message and public key.
1641
-     *
1642
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1643
-     *
1644
-     * @param string $signature
1645
-     * @param string $message
1646
-     * @param string $pk
1647
-     * @return bool
1648
-     * @throws SodiumException
1649
-     * @throws TypeError
1650
-     */
1651
-    public static function sign_verify_detached($signature, $message, $pk)
1652
-    {
1653
-        return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk);
1654
-    }
17
+	const aead_chacha20poly1305_KEYBYTES = 32;
18
+	const aead_chacha20poly1305_NSECBYTES = 0;
19
+	const aead_chacha20poly1305_NPUBBYTES = 8;
20
+	const aead_chacha20poly1305_ABYTES = 16;
21
+
22
+	const aead_chacha20poly1305_IETF_KEYBYTES = 32;
23
+	const aead_chacha20poly1305_IETF_NSECBYTES = 0;
24
+	const aead_chacha20poly1305_IETF_NPUBBYTES = 12;
25
+	const aead_chacha20poly1305_IETF_ABYTES = 16;
26
+
27
+	const aead_xchacha20poly1305_IETF_KEYBYTES = 32;
28
+	const aead_xchacha20poly1305_IETF_NSECBYTES = 0;
29
+	const aead_xchacha20poly1305_IETF_NPUBBYTES = 24;
30
+	const aead_xchacha20poly1305_IETF_ABYTES = 16;
31
+
32
+	const box_curve25519xsalsa20poly1305_SEEDBYTES = 32;
33
+	const box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32;
34
+	const box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32;
35
+	const box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32;
36
+	const box_curve25519xsalsa20poly1305_NONCEBYTES = 24;
37
+	const box_curve25519xsalsa20poly1305_MACBYTES = 16;
38
+	const box_curve25519xsalsa20poly1305_BOXZEROBYTES = 16;
39
+	const box_curve25519xsalsa20poly1305_ZEROBYTES = 32;
40
+
41
+	const onetimeauth_poly1305_BYTES = 16;
42
+	const onetimeauth_poly1305_KEYBYTES = 32;
43
+
44
+	const secretbox_xsalsa20poly1305_KEYBYTES = 32;
45
+	const secretbox_xsalsa20poly1305_NONCEBYTES = 24;
46
+	const secretbox_xsalsa20poly1305_MACBYTES = 16;
47
+	const secretbox_xsalsa20poly1305_BOXZEROBYTES = 16;
48
+	const secretbox_xsalsa20poly1305_ZEROBYTES = 32;
49
+
50
+	const secretbox_xchacha20poly1305_KEYBYTES = 32;
51
+	const secretbox_xchacha20poly1305_NONCEBYTES = 24;
52
+	const secretbox_xchacha20poly1305_MACBYTES = 16;
53
+	const secretbox_xchacha20poly1305_BOXZEROBYTES = 16;
54
+	const secretbox_xchacha20poly1305_ZEROBYTES = 32;
55
+
56
+	const stream_salsa20_KEYBYTES = 32;
57
+
58
+	/**
59
+	 * AEAD Decryption with ChaCha20-Poly1305
60
+	 *
61
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
62
+	 *
63
+	 * @param string $message
64
+	 * @param string $ad
65
+	 * @param string $nonce
66
+	 * @param string $key
67
+	 * @return string
68
+	 * @throws SodiumException
69
+	 * @throws TypeError
70
+	 */
71
+	public static function aead_chacha20poly1305_decrypt(
72
+		$message = '',
73
+		$ad = '',
74
+		$nonce = '',
75
+		$key = ''
76
+	) {
77
+		/** @var int $len - Length of message (ciphertext + MAC) */
78
+		$len = ParagonIE_Sodium_Core_Util::strlen($message);
79
+
80
+		/** @var int  $clen - Length of ciphertext */
81
+		$clen = $len - self::aead_chacha20poly1305_ABYTES;
82
+
83
+		/** @var int $adlen - Length of associated data */
84
+		$adlen = ParagonIE_Sodium_Core_Util::strlen($ad);
85
+
86
+		/** @var string $mac - Message authentication code */
87
+		$mac = ParagonIE_Sodium_Core_Util::substr(
88
+			$message,
89
+			$clen,
90
+			self::aead_chacha20poly1305_ABYTES
91
+		);
92
+
93
+		/** @var string $ciphertext - The encrypted message (sans MAC) */
94
+		$ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 0, $clen);
95
+
96
+		/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
97
+		$block0 = ParagonIE_Sodium_Core_ChaCha20::stream(
98
+			32,
99
+			$nonce,
100
+			$key
101
+		);
102
+
103
+		/* Recalculate the Poly1305 authentication tag (MAC): */
104
+		$state = new ParagonIE_Sodium_Core_Poly1305_State($block0);
105
+		try {
106
+			ParagonIE_Sodium_Compat::memzero($block0);
107
+		} catch (SodiumException $ex) {
108
+			$block0 = null;
109
+		}
110
+		$state->update($ad);
111
+		$state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen));
112
+		$state->update($ciphertext);
113
+		$state->update(ParagonIE_Sodium_Core_Util::store64_le($clen));
114
+		$computed_mac = $state->finish();
115
+
116
+		/* Compare the given MAC with the recalculated MAC: */
117
+		if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) {
118
+			throw new SodiumException('Invalid MAC');
119
+		}
120
+
121
+		// Here, we know that the MAC is valid, so we decrypt and return the plaintext
122
+		return ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
123
+			$ciphertext,
124
+			$nonce,
125
+			$key,
126
+			ParagonIE_Sodium_Core_Util::store64_le(1)
127
+		);
128
+	}
129
+
130
+	/**
131
+	 * AEAD Encryption with ChaCha20-Poly1305
132
+	 *
133
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
134
+	 *
135
+	 * @param string $message
136
+	 * @param string $ad
137
+	 * @param string $nonce
138
+	 * @param string $key
139
+	 * @return string
140
+	 * @throws SodiumException
141
+	 * @throws TypeError
142
+	 */
143
+	public static function aead_chacha20poly1305_encrypt(
144
+		$message = '',
145
+		$ad = '',
146
+		$nonce = '',
147
+		$key = ''
148
+	) {
149
+		/** @var int $len - Length of the plaintext message */
150
+		$len = ParagonIE_Sodium_Core_Util::strlen($message);
151
+
152
+		/** @var int $adlen - Length of the associated data */
153
+		$adlen = ParagonIE_Sodium_Core_Util::strlen($ad);
154
+
155
+		/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
156
+		$block0 = ParagonIE_Sodium_Core_ChaCha20::stream(
157
+			32,
158
+			$nonce,
159
+			$key
160
+		);
161
+		$state = new ParagonIE_Sodium_Core_Poly1305_State($block0);
162
+		try {
163
+			ParagonIE_Sodium_Compat::memzero($block0);
164
+		} catch (SodiumException $ex) {
165
+			$block0 = null;
166
+		}
167
+
168
+		/** @var string $ciphertext - Raw encrypted data */
169
+		$ciphertext = ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
170
+			$message,
171
+			$nonce,
172
+			$key,
173
+			ParagonIE_Sodium_Core_Util::store64_le(1)
174
+		);
175
+
176
+		$state->update($ad);
177
+		$state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen));
178
+		$state->update($ciphertext);
179
+		$state->update(ParagonIE_Sodium_Core_Util::store64_le($len));
180
+		return $ciphertext . $state->finish();
181
+	}
182
+
183
+	/**
184
+	 * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
185
+	 *
186
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
187
+	 *
188
+	 * @param string $message
189
+	 * @param string $ad
190
+	 * @param string $nonce
191
+	 * @param string $key
192
+	 * @return string
193
+	 * @throws SodiumException
194
+	 * @throws TypeError
195
+	 */
196
+	public static function aead_chacha20poly1305_ietf_decrypt(
197
+		$message = '',
198
+		$ad = '',
199
+		$nonce = '',
200
+		$key = ''
201
+	) {
202
+		/** @var int $adlen - Length of associated data */
203
+		$adlen = ParagonIE_Sodium_Core_Util::strlen($ad);
204
+
205
+		/** @var int $len - Length of message (ciphertext + MAC) */
206
+		$len = ParagonIE_Sodium_Core_Util::strlen($message);
207
+
208
+		/** @var int  $clen - Length of ciphertext */
209
+		$clen = $len - self::aead_chacha20poly1305_IETF_ABYTES;
210
+
211
+		/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
212
+		$block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream(
213
+			32,
214
+			$nonce,
215
+			$key
216
+		);
217
+
218
+		/** @var string $mac - Message authentication code */
219
+		$mac = ParagonIE_Sodium_Core_Util::substr(
220
+			$message,
221
+			$len - self::aead_chacha20poly1305_IETF_ABYTES,
222
+			self::aead_chacha20poly1305_IETF_ABYTES
223
+		);
224
+
225
+		/** @var string $ciphertext - The encrypted message (sans MAC) */
226
+		$ciphertext = ParagonIE_Sodium_Core_Util::substr(
227
+			$message,
228
+			0,
229
+			$len - self::aead_chacha20poly1305_IETF_ABYTES
230
+		);
231
+
232
+		/* Recalculate the Poly1305 authentication tag (MAC): */
233
+		$state = new ParagonIE_Sodium_Core_Poly1305_State($block0);
234
+		try {
235
+			ParagonIE_Sodium_Compat::memzero($block0);
236
+		} catch (SodiumException $ex) {
237
+			$block0 = null;
238
+		}
239
+		$state->update($ad);
240
+		$state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
241
+		$state->update($ciphertext);
242
+		$state->update(str_repeat("\x00", (0x10 - $clen) & 0xf));
243
+		$state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen));
244
+		$state->update(ParagonIE_Sodium_Core_Util::store64_le($clen));
245
+		$computed_mac = $state->finish();
246
+
247
+		/* Compare the given MAC with the recalculated MAC: */
248
+		if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) {
249
+			throw new SodiumException('Invalid MAC');
250
+		}
251
+
252
+		// Here, we know that the MAC is valid, so we decrypt and return the plaintext
253
+		return ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
254
+			$ciphertext,
255
+			$nonce,
256
+			$key,
257
+			ParagonIE_Sodium_Core_Util::store64_le(1)
258
+		);
259
+	}
260
+
261
+	/**
262
+	 * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
263
+	 *
264
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
265
+	 *
266
+	 * @param string $message
267
+	 * @param string $ad
268
+	 * @param string $nonce
269
+	 * @param string $key
270
+	 * @return string
271
+	 * @throws SodiumException
272
+	 * @throws TypeError
273
+	 */
274
+	public static function aead_chacha20poly1305_ietf_encrypt(
275
+		$message = '',
276
+		$ad = '',
277
+		$nonce = '',
278
+		$key = ''
279
+	) {
280
+		/** @var int $len - Length of the plaintext message */
281
+		$len = ParagonIE_Sodium_Core_Util::strlen($message);
282
+
283
+		/** @var int $adlen - Length of the associated data */
284
+		$adlen = ParagonIE_Sodium_Core_Util::strlen($ad);
285
+
286
+		/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
287
+		$block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream(
288
+			32,
289
+			$nonce,
290
+			$key
291
+		);
292
+		$state = new ParagonIE_Sodium_Core_Poly1305_State($block0);
293
+		try {
294
+			ParagonIE_Sodium_Compat::memzero($block0);
295
+		} catch (SodiumException $ex) {
296
+			$block0 = null;
297
+		}
298
+
299
+		/** @var string $ciphertext - Raw encrypted data */
300
+		$ciphertext = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
301
+			$message,
302
+			$nonce,
303
+			$key,
304
+			ParagonIE_Sodium_Core_Util::store64_le(1)
305
+		);
306
+
307
+		$state->update($ad);
308
+		$state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
309
+		$state->update($ciphertext);
310
+		$state->update(str_repeat("\x00", ((0x10 - $len) & 0xf)));
311
+		$state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen));
312
+		$state->update(ParagonIE_Sodium_Core_Util::store64_le($len));
313
+		return $ciphertext . $state->finish();
314
+	}
315
+
316
+	/**
317
+	 * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
318
+	 *
319
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
320
+	 *
321
+	 * @param string $message
322
+	 * @param string $ad
323
+	 * @param string $nonce
324
+	 * @param string $key
325
+	 * @return string
326
+	 * @throws SodiumException
327
+	 * @throws TypeError
328
+	 */
329
+	public static function aead_xchacha20poly1305_ietf_decrypt(
330
+		$message = '',
331
+		$ad = '',
332
+		$nonce = '',
333
+		$key = ''
334
+	) {
335
+		$subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
336
+			ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16),
337
+			$key
338
+		);
339
+		$nonceLast = "\x00\x00\x00\x00" .
340
+			ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
341
+
342
+		return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey);
343
+	}
344
+
345
+	/**
346
+	 * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
347
+	 *
348
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
349
+	 *
350
+	 * @param string $message
351
+	 * @param string $ad
352
+	 * @param string $nonce
353
+	 * @param string $key
354
+	 * @return string
355
+	 * @throws SodiumException
356
+	 * @throws TypeError
357
+	 */
358
+	public static function aead_xchacha20poly1305_ietf_encrypt(
359
+		$message = '',
360
+		$ad = '',
361
+		$nonce = '',
362
+		$key = ''
363
+	) {
364
+		$subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
365
+			ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16),
366
+			$key
367
+		);
368
+		$nonceLast = "\x00\x00\x00\x00" .
369
+			ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
370
+
371
+		return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey);
372
+	}
373
+
374
+	/**
375
+	 * HMAC-SHA-512-256 (a.k.a. the leftmost 256 bits of HMAC-SHA-512)
376
+	 *
377
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
378
+	 *
379
+	 * @param string $message
380
+	 * @param string $key
381
+	 * @return string
382
+	 * @throws TypeError
383
+	 */
384
+	public static function auth($message, $key)
385
+	{
386
+		return ParagonIE_Sodium_Core_Util::substr(
387
+			hash_hmac('sha512', $message, $key, true),
388
+			0,
389
+			32
390
+		);
391
+	}
392
+
393
+	/**
394
+	 * HMAC-SHA-512-256 validation. Constant-time via hash_equals().
395
+	 *
396
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
397
+	 *
398
+	 * @param string $mac
399
+	 * @param string $message
400
+	 * @param string $key
401
+	 * @return bool
402
+	 * @throws SodiumException
403
+	 * @throws TypeError
404
+	 */
405
+	public static function auth_verify($mac, $message, $key)
406
+	{
407
+		return ParagonIE_Sodium_Core_Util::hashEquals(
408
+			$mac,
409
+			self::auth($message, $key)
410
+		);
411
+	}
412
+
413
+	/**
414
+	 * X25519 key exchange followed by XSalsa20Poly1305 symmetric encryption
415
+	 *
416
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
417
+	 *
418
+	 * @param string $plaintext
419
+	 * @param string $nonce
420
+	 * @param string $keypair
421
+	 * @return string
422
+	 * @throws SodiumException
423
+	 * @throws TypeError
424
+	 */
425
+	public static function box($plaintext, $nonce, $keypair)
426
+	{
427
+		$c = self::secretbox(
428
+			$plaintext,
429
+			$nonce,
430
+			self::box_beforenm(
431
+				self::box_secretkey($keypair),
432
+				self::box_publickey($keypair)
433
+			)
434
+		);
435
+		return $c;
436
+	}
437
+
438
+	/**
439
+	 * X25519-XSalsa20-Poly1305 with one ephemeral X25519 keypair.
440
+	 *
441
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
442
+	 *
443
+	 * @param string $message
444
+	 * @param string $publicKey
445
+	 * @return string
446
+	 * @throws SodiumException
447
+	 * @throws TypeError
448
+	 */
449
+	public static function box_seal($message, $publicKey)
450
+	{
451
+		/** @var string $ephemeralKeypair */
452
+		$ephemeralKeypair = self::box_keypair();
453
+
454
+		/** @var string $ephemeralSK */
455
+		$ephemeralSK = self::box_secretkey($ephemeralKeypair);
456
+
457
+		/** @var string $ephemeralPK */
458
+		$ephemeralPK = self::box_publickey($ephemeralKeypair);
459
+
460
+		/** @var string $nonce */
461
+		$nonce = self::generichash(
462
+			$ephemeralPK . $publicKey,
463
+			'',
464
+			24
465
+		);
466
+
467
+		/** @var string $keypair - The combined keypair used in crypto_box() */
468
+		$keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey);
469
+
470
+		/** @var string $ciphertext Ciphertext + MAC from crypto_box */
471
+		$ciphertext = self::box($message, $nonce, $keypair);
472
+		try {
473
+			ParagonIE_Sodium_Compat::memzero($ephemeralKeypair);
474
+			ParagonIE_Sodium_Compat::memzero($ephemeralSK);
475
+			ParagonIE_Sodium_Compat::memzero($nonce);
476
+		} catch (SodiumException $ex) {
477
+			$ephemeralKeypair = null;
478
+			$ephemeralSK = null;
479
+			$nonce = null;
480
+		}
481
+		return $ephemeralPK . $ciphertext;
482
+	}
483
+
484
+	/**
485
+	 * Opens a message encrypted via box_seal().
486
+	 *
487
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
488
+	 *
489
+	 * @param string $message
490
+	 * @param string $keypair
491
+	 * @return string
492
+	 * @throws SodiumException
493
+	 * @throws TypeError
494
+	 */
495
+	public static function box_seal_open($message, $keypair)
496
+	{
497
+		/** @var string $ephemeralPK */
498
+		$ephemeralPK = ParagonIE_Sodium_Core_Util::substr($message, 0, 32);
499
+
500
+		/** @var string $ciphertext (ciphertext + MAC) */
501
+		$ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 32);
502
+
503
+		/** @var string $secretKey */
504
+		$secretKey = self::box_secretkey($keypair);
505
+
506
+		/** @var string $publicKey */
507
+		$publicKey = self::box_publickey($keypair);
508
+
509
+		/** @var string $nonce */
510
+		$nonce = self::generichash(
511
+			$ephemeralPK . $publicKey,
512
+			'',
513
+			24
514
+		);
515
+
516
+		/** @var string $keypair */
517
+		$keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK);
518
+
519
+		/** @var string $m */
520
+		$m = self::box_open($ciphertext, $nonce, $keypair);
521
+		try {
522
+			ParagonIE_Sodium_Compat::memzero($secretKey);
523
+			ParagonIE_Sodium_Compat::memzero($ephemeralPK);
524
+			ParagonIE_Sodium_Compat::memzero($nonce);
525
+		} catch (SodiumException $ex) {
526
+			$secretKey = null;
527
+			$ephemeralPK = null;
528
+			$nonce = null;
529
+		}
530
+		return $m;
531
+	}
532
+
533
+	/**
534
+	 * Used by crypto_box() to get the crypto_secretbox() key.
535
+	 *
536
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
537
+	 *
538
+	 * @param string $sk
539
+	 * @param string $pk
540
+	 * @return string
541
+	 * @throws SodiumException
542
+	 * @throws TypeError
543
+	 */
544
+	public static function box_beforenm($sk, $pk)
545
+	{
546
+		return ParagonIE_Sodium_Core_HSalsa20::hsalsa20(
547
+			str_repeat("\x00", 16),
548
+			self::scalarmult($sk, $pk)
549
+		);
550
+	}
551
+
552
+	/**
553
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
554
+	 *
555
+	 * @return string
556
+	 * @throws Exception
557
+	 * @throws SodiumException
558
+	 * @throws TypeError
559
+	 */
560
+	public static function box_keypair()
561
+	{
562
+		$sKey = random_bytes(32);
563
+		$pKey = self::scalarmult_base($sKey);
564
+		return $sKey . $pKey;
565
+	}
566
+
567
+	/**
568
+	 * @param string $seed
569
+	 * @return string
570
+	 * @throws SodiumException
571
+	 * @throws TypeError
572
+	 */
573
+	public static function box_seed_keypair($seed)
574
+	{
575
+		$sKey = ParagonIE_Sodium_Core_Util::substr(
576
+			hash('sha512', $seed, true),
577
+			0,
578
+			32
579
+		);
580
+		$pKey = self::scalarmult_base($sKey);
581
+		return $sKey . $pKey;
582
+	}
583
+
584
+	/**
585
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
586
+	 *
587
+	 * @param string $sKey
588
+	 * @param string $pKey
589
+	 * @return string
590
+	 * @throws TypeError
591
+	 */
592
+	public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey)
593
+	{
594
+		return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) .
595
+			ParagonIE_Sodium_Core_Util::substr($pKey, 0, 32);
596
+	}
597
+
598
+	/**
599
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
600
+	 *
601
+	 * @param string $keypair
602
+	 * @return string
603
+	 * @throws RangeException
604
+	 * @throws TypeError
605
+	 */
606
+	public static function box_secretkey($keypair)
607
+	{
608
+		if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) {
609
+			throw new RangeException(
610
+				'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
611
+			);
612
+		}
613
+		return ParagonIE_Sodium_Core_Util::substr($keypair, 0, 32);
614
+	}
615
+
616
+	/**
617
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
618
+	 *
619
+	 * @param string $keypair
620
+	 * @return string
621
+	 * @throws RangeException
622
+	 * @throws TypeError
623
+	 */
624
+	public static function box_publickey($keypair)
625
+	{
626
+		if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
627
+			throw new RangeException(
628
+				'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
629
+			);
630
+		}
631
+		return ParagonIE_Sodium_Core_Util::substr($keypair, 32, 32);
632
+	}
633
+
634
+	/**
635
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
636
+	 *
637
+	 * @param string $sKey
638
+	 * @return string
639
+	 * @throws RangeException
640
+	 * @throws SodiumException
641
+	 * @throws TypeError
642
+	 */
643
+	public static function box_publickey_from_secretkey($sKey)
644
+	{
645
+		if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) {
646
+			throw new RangeException(
647
+				'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.'
648
+			);
649
+		}
650
+		return self::scalarmult_base($sKey);
651
+	}
652
+
653
+	/**
654
+	 * Decrypt a message encrypted with box().
655
+	 *
656
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
657
+	 *
658
+	 * @param string $ciphertext
659
+	 * @param string $nonce
660
+	 * @param string $keypair
661
+	 * @return string
662
+	 * @throws SodiumException
663
+	 * @throws TypeError
664
+	 */
665
+	public static function box_open($ciphertext, $nonce, $keypair)
666
+	{
667
+		return self::secretbox_open(
668
+			$ciphertext,
669
+			$nonce,
670
+			self::box_beforenm(
671
+				self::box_secretkey($keypair),
672
+				self::box_publickey($keypair)
673
+			)
674
+		);
675
+	}
676
+
677
+	/**
678
+	 * Calculate a BLAKE2b hash.
679
+	 *
680
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
681
+	 *
682
+	 * @param string $message
683
+	 * @param string|null $key
684
+	 * @param int $outlen
685
+	 * @return string
686
+	 * @throws RangeException
687
+	 * @throws SodiumException
688
+	 * @throws TypeError
689
+	 */
690
+	public static function generichash($message, $key = '', $outlen = 32)
691
+	{
692
+		// This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
693
+		ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
694
+
695
+		$k = null;
696
+		if (!empty($key)) {
697
+			/** @var SplFixedArray $k */
698
+			$k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key);
699
+			if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) {
700
+				throw new RangeException('Invalid key size');
701
+			}
702
+		}
703
+
704
+		/** @var SplFixedArray $in */
705
+		$in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message);
706
+
707
+		/** @var SplFixedArray $ctx */
708
+		$ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outlen);
709
+		ParagonIE_Sodium_Core_BLAKE2b::update($ctx, $in, $in->count());
710
+
711
+		/** @var SplFixedArray $out */
712
+		$out = new SplFixedArray($outlen);
713
+		$out = ParagonIE_Sodium_Core_BLAKE2b::finish($ctx, $out);
714
+
715
+		/** @var array<int, int> */
716
+		$outArray = $out->toArray();
717
+		return ParagonIE_Sodium_Core_Util::intArrayToString($outArray);
718
+	}
719
+
720
+	/**
721
+	 * Finalize a BLAKE2b hashing context, returning the hash.
722
+	 *
723
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
724
+	 *
725
+	 * @param string $ctx
726
+	 * @param int $outlen
727
+	 * @return string
728
+	 * @throws SodiumException
729
+	 * @throws TypeError
730
+	 */
731
+	public static function generichash_final($ctx, $outlen = 32)
732
+	{
733
+		if (!is_string($ctx)) {
734
+			throw new TypeError('Context must be a string');
735
+		}
736
+		$out = new SplFixedArray($outlen);
737
+
738
+		/** @var SplFixedArray $context */
739
+		$context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx);
740
+
741
+		/** @var SplFixedArray $out */
742
+		$out = ParagonIE_Sodium_Core_BLAKE2b::finish($context, $out);
743
+
744
+		/** @var array<int, int> */
745
+		$outArray = $out->toArray();
746
+		return ParagonIE_Sodium_Core_Util::intArrayToString($outArray);
747
+	}
748
+
749
+	/**
750
+	 * Initialize a hashing context for BLAKE2b.
751
+	 *
752
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
753
+	 *
754
+	 * @param string $key
755
+	 * @param int $outputLength
756
+	 * @return string
757
+	 * @throws RangeException
758
+	 * @throws SodiumException
759
+	 * @throws TypeError
760
+	 */
761
+	public static function generichash_init($key = '', $outputLength = 32)
762
+	{
763
+		// This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
764
+		ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
765
+
766
+		$k = null;
767
+		if (!empty($key)) {
768
+			$k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key);
769
+			if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) {
770
+				throw new RangeException('Invalid key size');
771
+			}
772
+		}
773
+
774
+		/** @var SplFixedArray $ctx */
775
+		$ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength);
776
+
777
+		return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx);
778
+	}
779
+
780
+	/**
781
+	 * Initialize a hashing context for BLAKE2b.
782
+	 *
783
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
784
+	 *
785
+	 * @param string $key
786
+	 * @param int $outputLength
787
+	 * @param string $salt
788
+	 * @param string $personal
789
+	 * @return string
790
+	 * @throws RangeException
791
+	 * @throws SodiumException
792
+	 * @throws TypeError
793
+	 */
794
+	public static function generichash_init_salt_personal(
795
+		$key = '',
796
+		$outputLength = 32,
797
+		$salt = '',
798
+		$personal = ''
799
+	) {
800
+		// This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
801
+		ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
802
+
803
+		$k = null;
804
+		if (!empty($key)) {
805
+			$k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key);
806
+			if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) {
807
+				throw new RangeException('Invalid key size');
808
+			}
809
+		}
810
+		if (!empty($salt)) {
811
+			$s = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($salt);
812
+		} else {
813
+			$s = null;
814
+		}
815
+		if (!empty($salt)) {
816
+			$p = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($personal);
817
+		} else {
818
+			$p = null;
819
+		}
820
+
821
+		/** @var SplFixedArray $ctx */
822
+		$ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength, $s, $p);
823
+
824
+		return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx);
825
+	}
826
+
827
+	/**
828
+	 * Update a hashing context for BLAKE2b with $message
829
+	 *
830
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
831
+	 *
832
+	 * @param string $ctx
833
+	 * @param string $message
834
+	 * @return string
835
+	 * @throws SodiumException
836
+	 * @throws TypeError
837
+	 */
838
+	public static function generichash_update($ctx, $message)
839
+	{
840
+		// This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
841
+		ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
842
+
843
+		/** @var SplFixedArray $context */
844
+		$context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx);
845
+
846
+		/** @var SplFixedArray $in */
847
+		$in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message);
848
+
849
+		ParagonIE_Sodium_Core_BLAKE2b::update($context, $in, $in->count());
850
+
851
+		return ParagonIE_Sodium_Core_BLAKE2b::contextToString($context);
852
+	}
853
+
854
+	/**
855
+	 * Libsodium's crypto_kx().
856
+	 *
857
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
858
+	 *
859
+	 * @param string $my_sk
860
+	 * @param string $their_pk
861
+	 * @param string $client_pk
862
+	 * @param string $server_pk
863
+	 * @return string
864
+	 * @throws SodiumException
865
+	 * @throws TypeError
866
+	 */
867
+	public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk)
868
+	{
869
+		return ParagonIE_Sodium_Compat::crypto_generichash(
870
+			ParagonIE_Sodium_Compat::crypto_scalarmult($my_sk, $their_pk) .
871
+			$client_pk .
872
+			$server_pk
873
+		);
874
+	}
875
+
876
+	/**
877
+	 * ECDH over Curve25519
878
+	 *
879
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
880
+	 *
881
+	 * @param string $sKey
882
+	 * @param string $pKey
883
+	 * @return string
884
+	 *
885
+	 * @throws SodiumException
886
+	 * @throws TypeError
887
+	 */
888
+	public static function scalarmult($sKey, $pKey)
889
+	{
890
+		$q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey);
891
+		self::scalarmult_throw_if_zero($q);
892
+		return $q;
893
+	}
894
+
895
+	/**
896
+	 * ECDH over Curve25519, using the basepoint.
897
+	 * Used to get a secret key from a public key.
898
+	 *
899
+	 * @param string $secret
900
+	 * @return string
901
+	 *
902
+	 * @throws SodiumException
903
+	 * @throws TypeError
904
+	 */
905
+	public static function scalarmult_base($secret)
906
+	{
907
+		$q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret);
908
+		self::scalarmult_throw_if_zero($q);
909
+		return $q;
910
+	}
911
+
912
+	/**
913
+	 * This throws an Error if a zero public key was passed to the function.
914
+	 *
915
+	 * @param string $q
916
+	 * @return void
917
+	 * @throws SodiumException
918
+	 * @throws TypeError
919
+	 */
920
+	protected static function scalarmult_throw_if_zero($q)
921
+	{
922
+		$d = 0;
923
+		for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) {
924
+			$d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]);
925
+		}
926
+
927
+		/* branch-free variant of === 0 */
928
+		if (-(1 & (($d - 1) >> 8))) {
929
+			throw new SodiumException('Zero public key is not allowed');
930
+		}
931
+	}
932
+
933
+	/**
934
+	 * XSalsa20-Poly1305 authenticated symmetric-key encryption.
935
+	 *
936
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
937
+	 *
938
+	 * @param string $plaintext
939
+	 * @param string $nonce
940
+	 * @param string $key
941
+	 * @return string
942
+	 * @throws SodiumException
943
+	 * @throws TypeError
944
+	 */
945
+	public static function secretbox($plaintext, $nonce, $key)
946
+	{
947
+		/** @var string $subkey */
948
+		$subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
949
+
950
+		/** @var string $block0 */
951
+		$block0 = str_repeat("\x00", 32);
952
+
953
+		/** @var int $mlen - Length of the plaintext message */
954
+		$mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext);
955
+		$mlen0 = $mlen;
956
+		if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) {
957
+			$mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES;
958
+		}
959
+		$block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0);
960
+
961
+		/** @var string $block0 */
962
+		$block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor(
963
+			$block0,
964
+			ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
965
+			$subkey
966
+		);
967
+
968
+		/** @var string $c */
969
+		$c = ParagonIE_Sodium_Core_Util::substr(
970
+			$block0,
971
+			self::secretbox_xsalsa20poly1305_ZEROBYTES
972
+		);
973
+		if ($mlen > $mlen0) {
974
+			$c .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
975
+				ParagonIE_Sodium_Core_Util::substr(
976
+					$plaintext,
977
+					self::secretbox_xsalsa20poly1305_ZEROBYTES
978
+				),
979
+				ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
980
+				1,
981
+				$subkey
982
+			);
983
+		}
984
+		$state = new ParagonIE_Sodium_Core_Poly1305_State(
985
+			ParagonIE_Sodium_Core_Util::substr(
986
+				$block0,
987
+				0,
988
+				self::onetimeauth_poly1305_KEYBYTES
989
+			)
990
+		);
991
+		try {
992
+			ParagonIE_Sodium_Compat::memzero($block0);
993
+			ParagonIE_Sodium_Compat::memzero($subkey);
994
+		} catch (SodiumException $ex) {
995
+			$block0 = null;
996
+			$subkey = null;
997
+		}
998
+
999
+		$state->update($c);
1000
+
1001
+		/** @var string $c - MAC || ciphertext */
1002
+		$c = $state->finish() . $c;
1003
+		unset($state);
1004
+
1005
+		return $c;
1006
+	}
1007
+
1008
+	/**
1009
+	 * Decrypt a ciphertext generated via secretbox().
1010
+	 *
1011
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1012
+	 *
1013
+	 * @param string $ciphertext
1014
+	 * @param string $nonce
1015
+	 * @param string $key
1016
+	 * @return string
1017
+	 * @throws SodiumException
1018
+	 * @throws TypeError
1019
+	 */
1020
+	public static function secretbox_open($ciphertext, $nonce, $key)
1021
+	{
1022
+		/** @var string $mac */
1023
+		$mac = ParagonIE_Sodium_Core_Util::substr(
1024
+			$ciphertext,
1025
+			0,
1026
+			self::secretbox_xsalsa20poly1305_MACBYTES
1027
+		);
1028
+
1029
+		/** @var string $c */
1030
+		$c = ParagonIE_Sodium_Core_Util::substr(
1031
+			$ciphertext,
1032
+			self::secretbox_xsalsa20poly1305_MACBYTES
1033
+		);
1034
+
1035
+		/** @var int $clen */
1036
+		$clen = ParagonIE_Sodium_Core_Util::strlen($c);
1037
+
1038
+		/** @var string $subkey */
1039
+		$subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
1040
+
1041
+		/** @var string $block0 */
1042
+		$block0 = ParagonIE_Sodium_Core_Salsa20::salsa20(
1043
+			64,
1044
+			ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
1045
+			$subkey
1046
+		);
1047
+		$verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify(
1048
+			$mac,
1049
+			$c,
1050
+			ParagonIE_Sodium_Core_Util::substr($block0, 0, 32)
1051
+		);
1052
+		if (!$verified) {
1053
+			try {
1054
+				ParagonIE_Sodium_Compat::memzero($subkey);
1055
+			} catch (SodiumException $ex) {
1056
+				$subkey = null;
1057
+			}
1058
+			throw new SodiumException('Invalid MAC');
1059
+		}
1060
+
1061
+		/** @var string $m - Decrypted message */
1062
+		$m = ParagonIE_Sodium_Core_Util::xorStrings(
1063
+			ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES),
1064
+			ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES)
1065
+		);
1066
+		if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) {
1067
+			// We had more than 1 block, so let's continue to decrypt the rest.
1068
+			$m .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
1069
+				ParagonIE_Sodium_Core_Util::substr(
1070
+					$c,
1071
+					self::secretbox_xsalsa20poly1305_ZEROBYTES
1072
+				),
1073
+				ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
1074
+				1,
1075
+				(string) $subkey
1076
+			);
1077
+		}
1078
+		return $m;
1079
+	}
1080
+
1081
+	/**
1082
+	 * XChaCha20-Poly1305 authenticated symmetric-key encryption.
1083
+	 *
1084
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1085
+	 *
1086
+	 * @param string $plaintext
1087
+	 * @param string $nonce
1088
+	 * @param string $key
1089
+	 * @return string
1090
+	 * @throws SodiumException
1091
+	 * @throws TypeError
1092
+	 */
1093
+	public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key)
1094
+	{
1095
+		/** @var string $subkey */
1096
+		$subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
1097
+			ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16),
1098
+			$key
1099
+		);
1100
+		$nonceLast = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
1101
+
1102
+		/** @var string $block0 */
1103
+		$block0 = str_repeat("\x00", 32);
1104
+
1105
+		/** @var int $mlen - Length of the plaintext message */
1106
+		$mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext);
1107
+		$mlen0 = $mlen;
1108
+		if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) {
1109
+			$mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES;
1110
+		}
1111
+		$block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0);
1112
+
1113
+		/** @var string $block0 */
1114
+		$block0 = ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
1115
+			$block0,
1116
+			$nonceLast,
1117
+			$subkey
1118
+		);
1119
+
1120
+		/** @var string $c */
1121
+		$c = ParagonIE_Sodium_Core_Util::substr(
1122
+			$block0,
1123
+			self::secretbox_xchacha20poly1305_ZEROBYTES
1124
+		);
1125
+		if ($mlen > $mlen0) {
1126
+			$c .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
1127
+				ParagonIE_Sodium_Core_Util::substr(
1128
+					$plaintext,
1129
+					self::secretbox_xchacha20poly1305_ZEROBYTES
1130
+				),
1131
+				$nonceLast,
1132
+				$subkey,
1133
+				ParagonIE_Sodium_Core_Util::store64_le(1)
1134
+			);
1135
+		}
1136
+		$state = new ParagonIE_Sodium_Core_Poly1305_State(
1137
+			ParagonIE_Sodium_Core_Util::substr(
1138
+				$block0,
1139
+				0,
1140
+				self::onetimeauth_poly1305_KEYBYTES
1141
+			)
1142
+		);
1143
+		try {
1144
+			ParagonIE_Sodium_Compat::memzero($block0);
1145
+			ParagonIE_Sodium_Compat::memzero($subkey);
1146
+		} catch (SodiumException $ex) {
1147
+			$block0 = null;
1148
+			$subkey = null;
1149
+		}
1150
+
1151
+		$state->update($c);
1152
+
1153
+		/** @var string $c - MAC || ciphertext */
1154
+		$c = $state->finish() . $c;
1155
+		unset($state);
1156
+
1157
+		return $c;
1158
+	}
1159
+
1160
+	/**
1161
+	 * Decrypt a ciphertext generated via secretbox_xchacha20poly1305().
1162
+	 *
1163
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1164
+	 *
1165
+	 * @param string $ciphertext
1166
+	 * @param string $nonce
1167
+	 * @param string $key
1168
+	 * @return string
1169
+	 * @throws SodiumException
1170
+	 * @throws TypeError
1171
+	 */
1172
+	public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
1173
+	{
1174
+		/** @var string $mac */
1175
+		$mac = ParagonIE_Sodium_Core_Util::substr(
1176
+			$ciphertext,
1177
+			0,
1178
+			self::secretbox_xchacha20poly1305_MACBYTES
1179
+		);
1180
+
1181
+		/** @var string $c */
1182
+		$c = ParagonIE_Sodium_Core_Util::substr(
1183
+			$ciphertext,
1184
+			self::secretbox_xchacha20poly1305_MACBYTES
1185
+		);
1186
+
1187
+		/** @var int $clen */
1188
+		$clen = ParagonIE_Sodium_Core_Util::strlen($c);
1189
+
1190
+		/** @var string $subkey */
1191
+		$subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20($nonce, $key);
1192
+
1193
+		/** @var string $block0 */
1194
+		$block0 = ParagonIE_Sodium_Core_ChaCha20::stream(
1195
+			64,
1196
+			ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
1197
+			$subkey
1198
+		);
1199
+		$verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify(
1200
+			$mac,
1201
+			$c,
1202
+			ParagonIE_Sodium_Core_Util::substr($block0, 0, 32)
1203
+		);
1204
+
1205
+		if (!$verified) {
1206
+			try {
1207
+				ParagonIE_Sodium_Compat::memzero($subkey);
1208
+			} catch (SodiumException $ex) {
1209
+				$subkey = null;
1210
+			}
1211
+			throw new SodiumException('Invalid MAC');
1212
+		}
1213
+
1214
+		/** @var string $m - Decrypted message */
1215
+		$m = ParagonIE_Sodium_Core_Util::xorStrings(
1216
+			ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES),
1217
+			ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES)
1218
+		);
1219
+
1220
+		if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) {
1221
+			// We had more than 1 block, so let's continue to decrypt the rest.
1222
+			$m .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
1223
+				ParagonIE_Sodium_Core_Util::substr(
1224
+					$c,
1225
+					self::secretbox_xchacha20poly1305_ZEROBYTES
1226
+				),
1227
+				ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
1228
+				(string) $subkey,
1229
+				ParagonIE_Sodium_Core_Util::store64_le(1)
1230
+			);
1231
+		}
1232
+		return $m;
1233
+	}
1234
+
1235
+	/**
1236
+	 * @param string $key
1237
+	 * @return array<int, string> Returns a state and a header.
1238
+	 * @throws Exception
1239
+	 * @throws SodiumException
1240
+	 */
1241
+	public static function secretstream_xchacha20poly1305_init_push($key)
1242
+	{
1243
+		# randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES);
1244
+		$out = random_bytes(24);
1245
+
1246
+		# crypto_core_hchacha20(state->k, out, k, NULL);
1247
+		$subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20($out, $key);
1248
+		$state = new ParagonIE_Sodium_Core_SecretStream_State(
1249
+			$subkey,
1250
+			ParagonIE_Sodium_Core_Util::substr($out, 16, 8) . str_repeat("\0", 4)
1251
+		);
1252
+
1253
+		# _crypto_secretstream_xchacha20poly1305_counter_reset(state);
1254
+		$state->counterReset();
1255
+
1256
+		# memcpy(STATE_INONCE(state), out + crypto_core_hchacha20_INPUTBYTES,
1257
+		#        crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1258
+		# memset(state->_pad, 0, sizeof state->_pad);
1259
+		return array(
1260
+			$state->toString(),
1261
+			$out
1262
+		);
1263
+	}
1264
+
1265
+	/**
1266
+	 * @param string $key
1267
+	 * @param string $header
1268
+	 * @return string Returns a state.
1269
+	 * @throws Exception
1270
+	 */
1271
+	public static function secretstream_xchacha20poly1305_init_pull($key, $header)
1272
+	{
1273
+		# crypto_core_hchacha20(state->k, in, k, NULL);
1274
+		$subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
1275
+			ParagonIE_Sodium_Core_Util::substr($header, 0, 16),
1276
+			$key
1277
+		);
1278
+		$state = new ParagonIE_Sodium_Core_SecretStream_State(
1279
+			$subkey,
1280
+			ParagonIE_Sodium_Core_Util::substr($header, 16)
1281
+		);
1282
+		$state->counterReset();
1283
+		# memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES,
1284
+		#     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1285
+		# memset(state->_pad, 0, sizeof state->_pad);
1286
+		# return 0;
1287
+		return $state->toString();
1288
+	}
1289
+
1290
+	/**
1291
+	 * @param string $state
1292
+	 * @param string $msg
1293
+	 * @param string $aad
1294
+	 * @param int $tag
1295
+	 * @return string
1296
+	 * @throws SodiumException
1297
+	 */
1298
+	public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
1299
+	{
1300
+		$st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
1301
+		# crypto_onetimeauth_poly1305_state poly1305_state;
1302
+		# unsigned char                     block[64U];
1303
+		# unsigned char                     slen[8U];
1304
+		# unsigned char                    *c;
1305
+		# unsigned char                    *mac;
1306
+
1307
+		$msglen = ParagonIE_Sodium_Core_Util::strlen($msg);
1308
+		$aadlen = ParagonIE_Sodium_Core_Util::strlen($aad);
1309
+
1310
+		if ((($msglen + 63) >> 6) > 0xfffffffe) {
1311
+			throw new SodiumException(
1312
+				'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
1313
+			);
1314
+		}
1315
+
1316
+		# if (outlen_p != NULL) {
1317
+		#     *outlen_p = 0U;
1318
+		# }
1319
+		# if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
1320
+		#     sodium_misuse();
1321
+		# }
1322
+
1323
+		# crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
1324
+		# crypto_onetimeauth_poly1305_init(&poly1305_state, block);
1325
+		# sodium_memzero(block, sizeof block);
1326
+		$auth = new ParagonIE_Sodium_Core_Poly1305_State(
1327
+			ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
1328
+		);
1329
+
1330
+		# crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
1331
+		$auth->update($aad);
1332
+
1333
+		# crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
1334
+		#     (0x10 - adlen) & 0xf);
1335
+		$auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
1336
+
1337
+		# memset(block, 0, sizeof block);
1338
+		# block[0] = tag;
1339
+		# crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
1340
+		#                                    state->nonce, 1U, state->k);
1341
+		$block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1342
+			ParagonIE_Sodium_Core_Util::intToChr($tag) . str_repeat("\0", 63),
1343
+			$st->getCombinedNonce(),
1344
+			$st->getKey(),
1345
+			ParagonIE_Sodium_Core_Util::store64_le(1)
1346
+		);
1347
+
1348
+		# crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
1349
+		$auth->update($block);
1350
+
1351
+		# out[0] = block[0];
1352
+		$out = $block[0];
1353
+		# c = out + (sizeof tag);
1354
+		# crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k);
1355
+		$cipher = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1356
+			$msg,
1357
+			$st->getCombinedNonce(),
1358
+			$st->getKey(),
1359
+			ParagonIE_Sodium_Core_Util::store64_le(2)
1360
+		);
1361
+
1362
+		# crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
1363
+		$auth->update($cipher);
1364
+
1365
+		$out .= $cipher;
1366
+		unset($cipher);
1367
+
1368
+		# crypto_onetimeauth_poly1305_update
1369
+		# (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
1370
+		$auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
1371
+
1372
+		# STORE64_LE(slen, (uint64_t) adlen);
1373
+		$slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen);
1374
+
1375
+		# crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1376
+		$auth->update($slen);
1377
+
1378
+		# STORE64_LE(slen, (sizeof block) + mlen);
1379
+		$slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen);
1380
+
1381
+		# crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1382
+		$auth->update($slen);
1383
+
1384
+		# mac = c + mlen;
1385
+		# crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
1386
+		$mac = $auth->finish();
1387
+		$out .= $mac;
1388
+
1389
+		# sodium_memzero(&poly1305_state, sizeof poly1305_state);
1390
+		unset($auth);
1391
+
1392
+
1393
+		# XOR_BUF(STATE_INONCE(state), mac,
1394
+		#     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1395
+		$st->xorNonce($mac);
1396
+
1397
+		# sodium_increment(STATE_COUNTER(state),
1398
+		#     crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
1399
+		$st->incrementCounter();
1400
+		// Overwrite by reference:
1401
+		$state = $st->toString();
1402
+
1403
+		/** @var bool $rekey */
1404
+		$rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
1405
+		# if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
1406
+		#     sodium_is_zero(STATE_COUNTER(state),
1407
+		#         crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
1408
+		#     crypto_secretstream_xchacha20poly1305_rekey(state);
1409
+		# }
1410
+		if ($rekey || $st->needsRekey()) {
1411
+			// DO REKEY
1412
+			self::secretstream_xchacha20poly1305_rekey($state);
1413
+		}
1414
+		# if (outlen_p != NULL) {
1415
+		#     *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen;
1416
+		# }
1417
+		return $out;
1418
+	}
1419
+
1420
+	/**
1421
+	 * @param string $state
1422
+	 * @param string $cipher
1423
+	 * @param string $aad
1424
+	 * @return bool|array{0: string, 1: int}
1425
+	 * @throws SodiumException
1426
+	 */
1427
+	public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '')
1428
+	{
1429
+		$st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
1430
+
1431
+		$cipherlen = ParagonIE_Sodium_Core_Util::strlen($cipher);
1432
+		#     mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES;
1433
+		$msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES;
1434
+		$aadlen = ParagonIE_Sodium_Core_Util::strlen($aad);
1435
+
1436
+		#     if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
1437
+		#         sodium_misuse();
1438
+		#     }
1439
+		if ((($msglen + 63) >> 6) > 0xfffffffe) {
1440
+			throw new SodiumException(
1441
+				'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
1442
+			);
1443
+		}
1444
+
1445
+		#     crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
1446
+		#     crypto_onetimeauth_poly1305_init(&poly1305_state, block);
1447
+		#     sodium_memzero(block, sizeof block);
1448
+		$auth = new ParagonIE_Sodium_Core_Poly1305_State(
1449
+			ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
1450
+		);
1451
+
1452
+		#     crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
1453
+		$auth->update($aad);
1454
+
1455
+		#     crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
1456
+		#         (0x10 - adlen) & 0xf);
1457
+		$auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
1458
+
1459
+
1460
+		#     memset(block, 0, sizeof block);
1461
+		#     block[0] = in[0];
1462
+		#     crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
1463
+		#                                        state->nonce, 1U, state->k);
1464
+		$block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1465
+			$cipher[0] . str_repeat("\0", 63),
1466
+			$st->getCombinedNonce(),
1467
+			$st->getKey(),
1468
+			ParagonIE_Sodium_Core_Util::store64_le(1)
1469
+		);
1470
+		#     tag = block[0];
1471
+		#     block[0] = in[0];
1472
+		#     crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
1473
+		$tag = ParagonIE_Sodium_Core_Util::chrToInt($block[0]);
1474
+		$block[0] = $cipher[0];
1475
+		$auth->update($block);
1476
+
1477
+
1478
+		#     c = in + (sizeof tag);
1479
+		#     crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
1480
+		$auth->update(ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen));
1481
+
1482
+		#     crypto_onetimeauth_poly1305_update
1483
+		#     (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
1484
+		$auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
1485
+
1486
+		#     STORE64_LE(slen, (uint64_t) adlen);
1487
+		#     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1488
+		$slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen);
1489
+		$auth->update($slen);
1490
+
1491
+		#     STORE64_LE(slen, (sizeof block) + mlen);
1492
+		#     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1493
+		$slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen);
1494
+		$auth->update($slen);
1495
+
1496
+		#     crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
1497
+		#     sodium_memzero(&poly1305_state, sizeof poly1305_state);
1498
+		$mac = $auth->finish();
1499
+
1500
+		#     stored_mac = c + mlen;
1501
+		#     if (sodium_memcmp(mac, stored_mac, sizeof mac) != 0) {
1502
+		#     sodium_memzero(mac, sizeof mac);
1503
+		#         return -1;
1504
+		#     }
1505
+
1506
+		$stored = ParagonIE_Sodium_Core_Util::substr($cipher, $msglen + 1, 16);
1507
+		if (!ParagonIE_Sodium_Core_Util::hashEquals($mac, $stored)) {
1508
+			return false;
1509
+		}
1510
+
1511
+		#     crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k);
1512
+		$out = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1513
+			ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen),
1514
+			$st->getCombinedNonce(),
1515
+			$st->getKey(),
1516
+			ParagonIE_Sodium_Core_Util::store64_le(2)
1517
+		);
1518
+
1519
+		#     XOR_BUF(STATE_INONCE(state), mac,
1520
+		#         crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1521
+		$st->xorNonce($mac);
1522
+
1523
+		#     sodium_increment(STATE_COUNTER(state),
1524
+		#         crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
1525
+		$st->incrementCounter();
1526
+
1527
+		#     if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
1528
+		#         sodium_is_zero(STATE_COUNTER(state),
1529
+		#             crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
1530
+		#         crypto_secretstream_xchacha20poly1305_rekey(state);
1531
+		#     }
1532
+
1533
+		// Overwrite by reference:
1534
+		$state = $st->toString();
1535
+
1536
+		/** @var bool $rekey */
1537
+		$rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
1538
+		if ($rekey || $st->needsRekey()) {
1539
+			// DO REKEY
1540
+			self::secretstream_xchacha20poly1305_rekey($state);
1541
+		}
1542
+		return array($out, $tag);
1543
+	}
1544
+
1545
+	/**
1546
+	 * @param string $state
1547
+	 * @return void
1548
+	 * @throws SodiumException
1549
+	 */
1550
+	public static function secretstream_xchacha20poly1305_rekey(&$state)
1551
+	{
1552
+		$st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
1553
+		# unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES +
1554
+		# crypto_secretstream_xchacha20poly1305_INONCEBYTES];
1555
+		# size_t        i;
1556
+		# for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
1557
+		#     new_key_and_inonce[i] = state->k[i];
1558
+		# }
1559
+		$new_key_and_inonce = $st->getKey();
1560
+
1561
+		# for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
1562
+		#     new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] =
1563
+		#         STATE_INONCE(state)[i];
1564
+		# }
1565
+		$new_key_and_inonce .= ParagonIE_Sodium_Core_Util::substR($st->getNonce(), 0, 8);
1566
+
1567
+		# crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce,
1568
+		#                                 sizeof new_key_and_inonce,
1569
+		#                                 state->nonce, state->k);
1570
+
1571
+		$st->rekey(ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1572
+			$new_key_and_inonce,
1573
+			$st->getCombinedNonce(),
1574
+			$st->getKey(),
1575
+			ParagonIE_Sodium_Core_Util::store64_le(0)
1576
+		));
1577
+
1578
+		# for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
1579
+		#     state->k[i] = new_key_and_inonce[i];
1580
+		# }
1581
+		# for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
1582
+		#     STATE_INONCE(state)[i] =
1583
+		#          new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i];
1584
+		# }
1585
+		# _crypto_secretstream_xchacha20poly1305_counter_reset(state);
1586
+		$st->counterReset();
1587
+
1588
+		$state = $st->toString();
1589
+	}
1590
+
1591
+	/**
1592
+	 * Detached Ed25519 signature.
1593
+	 *
1594
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1595
+	 *
1596
+	 * @param string $message
1597
+	 * @param string $sk
1598
+	 * @return string
1599
+	 * @throws SodiumException
1600
+	 * @throws TypeError
1601
+	 */
1602
+	public static function sign_detached($message, $sk)
1603
+	{
1604
+		return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk);
1605
+	}
1606
+
1607
+	/**
1608
+	 * Attached Ed25519 signature. (Returns a signed message.)
1609
+	 *
1610
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1611
+	 *
1612
+	 * @param string $message
1613
+	 * @param string $sk
1614
+	 * @return string
1615
+	 * @throws SodiumException
1616
+	 * @throws TypeError
1617
+	 */
1618
+	public static function sign($message, $sk)
1619
+	{
1620
+		return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk);
1621
+	}
1622
+
1623
+	/**
1624
+	 * Opens a signed message. If valid, returns the message.
1625
+	 *
1626
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1627
+	 *
1628
+	 * @param string $signedMessage
1629
+	 * @param string $pk
1630
+	 * @return string
1631
+	 * @throws SodiumException
1632
+	 * @throws TypeError
1633
+	 */
1634
+	public static function sign_open($signedMessage, $pk)
1635
+	{
1636
+		return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk);
1637
+	}
1638
+
1639
+	/**
1640
+	 * Verify a detached signature of a given message and public key.
1641
+	 *
1642
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1643
+	 *
1644
+	 * @param string $signature
1645
+	 * @param string $message
1646
+	 * @param string $pk
1647
+	 * @return bool
1648
+	 * @throws SodiumException
1649
+	 * @throws TypeError
1650
+	 */
1651
+	public static function sign_verify_detached($signature, $message, $pk)
1652
+	{
1653
+		return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk);
1654
+	}
1655 1655
 }
Please login to merge, or discard this patch.
Spacing   +290 added lines, -290 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-if (class_exists('ParagonIE_Sodium_Crypto', false)) {
3
+if ( class_exists( 'ParagonIE_Sodium_Crypto', false ) ) {
4 4
     return;
5 5
 }
6 6
 
@@ -75,13 +75,13 @@  discard block
 block discarded – undo
75 75
         $key = ''
76 76
     ) {
77 77
         /** @var int $len - Length of message (ciphertext + MAC) */
78
-        $len = ParagonIE_Sodium_Core_Util::strlen($message);
78
+        $len = ParagonIE_Sodium_Core_Util::strlen( $message );
79 79
 
80 80
         /** @var int  $clen - Length of ciphertext */
81 81
         $clen = $len - self::aead_chacha20poly1305_ABYTES;
82 82
 
83 83
         /** @var int $adlen - Length of associated data */
84
-        $adlen = ParagonIE_Sodium_Core_Util::strlen($ad);
84
+        $adlen = ParagonIE_Sodium_Core_Util::strlen( $ad );
85 85
 
86 86
         /** @var string $mac - Message authentication code */
87 87
         $mac = ParagonIE_Sodium_Core_Util::substr(
@@ -91,7 +91,7 @@  discard block
 block discarded – undo
91 91
         );
92 92
 
93 93
         /** @var string $ciphertext - The encrypted message (sans MAC) */
94
-        $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 0, $clen);
94
+        $ciphertext = ParagonIE_Sodium_Core_Util::substr( $message, 0, $clen );
95 95
 
96 96
         /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
97 97
         $block0 = ParagonIE_Sodium_Core_ChaCha20::stream(
@@ -101,21 +101,21 @@  discard block
 block discarded – undo
101 101
         );
102 102
 
103 103
         /* Recalculate the Poly1305 authentication tag (MAC): */
104
-        $state = new ParagonIE_Sodium_Core_Poly1305_State($block0);
104
+        $state = new ParagonIE_Sodium_Core_Poly1305_State( $block0 );
105 105
         try {
106
-            ParagonIE_Sodium_Compat::memzero($block0);
107
-        } catch (SodiumException $ex) {
106
+            ParagonIE_Sodium_Compat::memzero( $block0 );
107
+        } catch ( SodiumException $ex ) {
108 108
             $block0 = null;
109 109
         }
110
-        $state->update($ad);
111
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen));
112
-        $state->update($ciphertext);
113
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen));
110
+        $state->update( $ad );
111
+        $state->update( ParagonIE_Sodium_Core_Util::store64_le( $adlen ) );
112
+        $state->update( $ciphertext );
113
+        $state->update( ParagonIE_Sodium_Core_Util::store64_le( $clen ) );
114 114
         $computed_mac = $state->finish();
115 115
 
116 116
         /* Compare the given MAC with the recalculated MAC: */
117
-        if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) {
118
-            throw new SodiumException('Invalid MAC');
117
+        if ( ! ParagonIE_Sodium_Core_Util::verify_16( $computed_mac, $mac ) ) {
118
+            throw new SodiumException( 'Invalid MAC' );
119 119
         }
120 120
 
121 121
         // Here, we know that the MAC is valid, so we decrypt and return the plaintext
@@ -123,7 +123,7 @@  discard block
 block discarded – undo
123 123
             $ciphertext,
124 124
             $nonce,
125 125
             $key,
126
-            ParagonIE_Sodium_Core_Util::store64_le(1)
126
+            ParagonIE_Sodium_Core_Util::store64_le( 1 )
127 127
         );
128 128
     }
129 129
 
@@ -147,10 +147,10 @@  discard block
 block discarded – undo
147 147
         $key = ''
148 148
     ) {
149 149
         /** @var int $len - Length of the plaintext message */
150
-        $len = ParagonIE_Sodium_Core_Util::strlen($message);
150
+        $len = ParagonIE_Sodium_Core_Util::strlen( $message );
151 151
 
152 152
         /** @var int $adlen - Length of the associated data */
153
-        $adlen = ParagonIE_Sodium_Core_Util::strlen($ad);
153
+        $adlen = ParagonIE_Sodium_Core_Util::strlen( $ad );
154 154
 
155 155
         /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
156 156
         $block0 = ParagonIE_Sodium_Core_ChaCha20::stream(
@@ -158,10 +158,10 @@  discard block
 block discarded – undo
158 158
             $nonce,
159 159
             $key
160 160
         );
161
-        $state = new ParagonIE_Sodium_Core_Poly1305_State($block0);
161
+        $state = new ParagonIE_Sodium_Core_Poly1305_State( $block0 );
162 162
         try {
163
-            ParagonIE_Sodium_Compat::memzero($block0);
164
-        } catch (SodiumException $ex) {
163
+            ParagonIE_Sodium_Compat::memzero( $block0 );
164
+        } catch ( SodiumException $ex ) {
165 165
             $block0 = null;
166 166
         }
167 167
 
@@ -170,13 +170,13 @@  discard block
 block discarded – undo
170 170
             $message,
171 171
             $nonce,
172 172
             $key,
173
-            ParagonIE_Sodium_Core_Util::store64_le(1)
173
+            ParagonIE_Sodium_Core_Util::store64_le( 1 )
174 174
         );
175 175
 
176
-        $state->update($ad);
177
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen));
178
-        $state->update($ciphertext);
179
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($len));
176
+        $state->update( $ad );
177
+        $state->update( ParagonIE_Sodium_Core_Util::store64_le( $adlen ) );
178
+        $state->update( $ciphertext );
179
+        $state->update( ParagonIE_Sodium_Core_Util::store64_le( $len ) );
180 180
         return $ciphertext . $state->finish();
181 181
     }
182 182
 
@@ -200,10 +200,10 @@  discard block
 block discarded – undo
200 200
         $key = ''
201 201
     ) {
202 202
         /** @var int $adlen - Length of associated data */
203
-        $adlen = ParagonIE_Sodium_Core_Util::strlen($ad);
203
+        $adlen = ParagonIE_Sodium_Core_Util::strlen( $ad );
204 204
 
205 205
         /** @var int $len - Length of message (ciphertext + MAC) */
206
-        $len = ParagonIE_Sodium_Core_Util::strlen($message);
206
+        $len = ParagonIE_Sodium_Core_Util::strlen( $message );
207 207
 
208 208
         /** @var int  $clen - Length of ciphertext */
209 209
         $clen = $len - self::aead_chacha20poly1305_IETF_ABYTES;
@@ -230,23 +230,23 @@  discard block
 block discarded – undo
230 230
         );
231 231
 
232 232
         /* Recalculate the Poly1305 authentication tag (MAC): */
233
-        $state = new ParagonIE_Sodium_Core_Poly1305_State($block0);
233
+        $state = new ParagonIE_Sodium_Core_Poly1305_State( $block0 );
234 234
         try {
235
-            ParagonIE_Sodium_Compat::memzero($block0);
236
-        } catch (SodiumException $ex) {
235
+            ParagonIE_Sodium_Compat::memzero( $block0 );
236
+        } catch ( SodiumException $ex ) {
237 237
             $block0 = null;
238 238
         }
239
-        $state->update($ad);
240
-        $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
241
-        $state->update($ciphertext);
242
-        $state->update(str_repeat("\x00", (0x10 - $clen) & 0xf));
243
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen));
244
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($clen));
239
+        $state->update( $ad );
240
+        $state->update( str_repeat( "\x00", ( ( 0x10 - $adlen ) & 0xf ) ) );
241
+        $state->update( $ciphertext );
242
+        $state->update( str_repeat( "\x00", ( 0x10 - $clen ) & 0xf ) );
243
+        $state->update( ParagonIE_Sodium_Core_Util::store64_le( $adlen ) );
244
+        $state->update( ParagonIE_Sodium_Core_Util::store64_le( $clen ) );
245 245
         $computed_mac = $state->finish();
246 246
 
247 247
         /* Compare the given MAC with the recalculated MAC: */
248
-        if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) {
249
-            throw new SodiumException('Invalid MAC');
248
+        if ( ! ParagonIE_Sodium_Core_Util::verify_16( $computed_mac, $mac ) ) {
249
+            throw new SodiumException( 'Invalid MAC' );
250 250
         }
251 251
 
252 252
         // Here, we know that the MAC is valid, so we decrypt and return the plaintext
@@ -254,7 +254,7 @@  discard block
 block discarded – undo
254 254
             $ciphertext,
255 255
             $nonce,
256 256
             $key,
257
-            ParagonIE_Sodium_Core_Util::store64_le(1)
257
+            ParagonIE_Sodium_Core_Util::store64_le( 1 )
258 258
         );
259 259
     }
260 260
 
@@ -278,10 +278,10 @@  discard block
 block discarded – undo
278 278
         $key = ''
279 279
     ) {
280 280
         /** @var int $len - Length of the plaintext message */
281
-        $len = ParagonIE_Sodium_Core_Util::strlen($message);
281
+        $len = ParagonIE_Sodium_Core_Util::strlen( $message );
282 282
 
283 283
         /** @var int $adlen - Length of the associated data */
284
-        $adlen = ParagonIE_Sodium_Core_Util::strlen($ad);
284
+        $adlen = ParagonIE_Sodium_Core_Util::strlen( $ad );
285 285
 
286 286
         /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
287 287
         $block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream(
@@ -289,10 +289,10 @@  discard block
 block discarded – undo
289 289
             $nonce,
290 290
             $key
291 291
         );
292
-        $state = new ParagonIE_Sodium_Core_Poly1305_State($block0);
292
+        $state = new ParagonIE_Sodium_Core_Poly1305_State( $block0 );
293 293
         try {
294
-            ParagonIE_Sodium_Compat::memzero($block0);
295
-        } catch (SodiumException $ex) {
294
+            ParagonIE_Sodium_Compat::memzero( $block0 );
295
+        } catch ( SodiumException $ex ) {
296 296
             $block0 = null;
297 297
         }
298 298
 
@@ -301,15 +301,15 @@  discard block
 block discarded – undo
301 301
             $message,
302 302
             $nonce,
303 303
             $key,
304
-            ParagonIE_Sodium_Core_Util::store64_le(1)
304
+            ParagonIE_Sodium_Core_Util::store64_le( 1 )
305 305
         );
306 306
 
307
-        $state->update($ad);
308
-        $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
309
-        $state->update($ciphertext);
310
-        $state->update(str_repeat("\x00", ((0x10 - $len) & 0xf)));
311
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen));
312
-        $state->update(ParagonIE_Sodium_Core_Util::store64_le($len));
307
+        $state->update( $ad );
308
+        $state->update( str_repeat( "\x00", ( ( 0x10 - $adlen ) & 0xf ) ) );
309
+        $state->update( $ciphertext );
310
+        $state->update( str_repeat( "\x00", ( ( 0x10 - $len ) & 0xf ) ) );
311
+        $state->update( ParagonIE_Sodium_Core_Util::store64_le( $adlen ) );
312
+        $state->update( ParagonIE_Sodium_Core_Util::store64_le( $len ) );
313 313
         return $ciphertext . $state->finish();
314 314
     }
315 315
 
@@ -333,13 +333,13 @@  discard block
 block discarded – undo
333 333
         $key = ''
334 334
     ) {
335 335
         $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
336
-            ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16),
336
+            ParagonIE_Sodium_Core_Util::substr( $nonce, 0, 16 ),
337 337
             $key
338 338
         );
339 339
         $nonceLast = "\x00\x00\x00\x00" .
340
-            ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
340
+            ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 );
341 341
 
342
-        return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey);
342
+        return self::aead_chacha20poly1305_ietf_decrypt( $message, $ad, $nonceLast, $subkey );
343 343
     }
344 344
 
345 345
     /**
@@ -362,13 +362,13 @@  discard block
 block discarded – undo
362 362
         $key = ''
363 363
     ) {
364 364
         $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
365
-            ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16),
365
+            ParagonIE_Sodium_Core_Util::substr( $nonce, 0, 16 ),
366 366
             $key
367 367
         );
368 368
         $nonceLast = "\x00\x00\x00\x00" .
369
-            ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
369
+            ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 );
370 370
 
371
-        return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey);
371
+        return self::aead_chacha20poly1305_ietf_encrypt( $message, $ad, $nonceLast, $subkey );
372 372
     }
373 373
 
374 374
     /**
@@ -381,10 +381,10 @@  discard block
 block discarded – undo
381 381
      * @return string
382 382
      * @throws TypeError
383 383
      */
384
-    public static function auth($message, $key)
384
+    public static function auth( $message, $key )
385 385
     {
386 386
         return ParagonIE_Sodium_Core_Util::substr(
387
-            hash_hmac('sha512', $message, $key, true),
387
+            hash_hmac( 'sha512', $message, $key, true ),
388 388
             0,
389 389
             32
390 390
         );
@@ -402,11 +402,11 @@  discard block
 block discarded – undo
402 402
      * @throws SodiumException
403 403
      * @throws TypeError
404 404
      */
405
-    public static function auth_verify($mac, $message, $key)
405
+    public static function auth_verify( $mac, $message, $key )
406 406
     {
407 407
         return ParagonIE_Sodium_Core_Util::hashEquals(
408 408
             $mac,
409
-            self::auth($message, $key)
409
+            self::auth( $message, $key )
410 410
         );
411 411
     }
412 412
 
@@ -422,14 +422,14 @@  discard block
 block discarded – undo
422 422
      * @throws SodiumException
423 423
      * @throws TypeError
424 424
      */
425
-    public static function box($plaintext, $nonce, $keypair)
425
+    public static function box( $plaintext, $nonce, $keypair )
426 426
     {
427 427
         $c = self::secretbox(
428 428
             $plaintext,
429 429
             $nonce,
430 430
             self::box_beforenm(
431
-                self::box_secretkey($keypair),
432
-                self::box_publickey($keypair)
431
+                self::box_secretkey( $keypair ),
432
+                self::box_publickey( $keypair )
433 433
             )
434 434
         );
435 435
         return $c;
@@ -446,16 +446,16 @@  discard block
 block discarded – undo
446 446
      * @throws SodiumException
447 447
      * @throws TypeError
448 448
      */
449
-    public static function box_seal($message, $publicKey)
449
+    public static function box_seal( $message, $publicKey )
450 450
     {
451 451
         /** @var string $ephemeralKeypair */
452 452
         $ephemeralKeypair = self::box_keypair();
453 453
 
454 454
         /** @var string $ephemeralSK */
455
-        $ephemeralSK = self::box_secretkey($ephemeralKeypair);
455
+        $ephemeralSK = self::box_secretkey( $ephemeralKeypair );
456 456
 
457 457
         /** @var string $ephemeralPK */
458
-        $ephemeralPK = self::box_publickey($ephemeralKeypair);
458
+        $ephemeralPK = self::box_publickey( $ephemeralKeypair );
459 459
 
460 460
         /** @var string $nonce */
461 461
         $nonce = self::generichash(
@@ -465,15 +465,15 @@  discard block
 block discarded – undo
465 465
         );
466 466
 
467 467
         /** @var string $keypair - The combined keypair used in crypto_box() */
468
-        $keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey);
468
+        $keypair = self::box_keypair_from_secretkey_and_publickey( $ephemeralSK, $publicKey );
469 469
 
470 470
         /** @var string $ciphertext Ciphertext + MAC from crypto_box */
471
-        $ciphertext = self::box($message, $nonce, $keypair);
471
+        $ciphertext = self::box( $message, $nonce, $keypair );
472 472
         try {
473
-            ParagonIE_Sodium_Compat::memzero($ephemeralKeypair);
474
-            ParagonIE_Sodium_Compat::memzero($ephemeralSK);
475
-            ParagonIE_Sodium_Compat::memzero($nonce);
476
-        } catch (SodiumException $ex) {
473
+            ParagonIE_Sodium_Compat::memzero( $ephemeralKeypair );
474
+            ParagonIE_Sodium_Compat::memzero( $ephemeralSK );
475
+            ParagonIE_Sodium_Compat::memzero( $nonce );
476
+        } catch ( SodiumException $ex ) {
477 477
             $ephemeralKeypair = null;
478 478
             $ephemeralSK = null;
479 479
             $nonce = null;
@@ -492,19 +492,19 @@  discard block
 block discarded – undo
492 492
      * @throws SodiumException
493 493
      * @throws TypeError
494 494
      */
495
-    public static function box_seal_open($message, $keypair)
495
+    public static function box_seal_open( $message, $keypair )
496 496
     {
497 497
         /** @var string $ephemeralPK */
498
-        $ephemeralPK = ParagonIE_Sodium_Core_Util::substr($message, 0, 32);
498
+        $ephemeralPK = ParagonIE_Sodium_Core_Util::substr( $message, 0, 32 );
499 499
 
500 500
         /** @var string $ciphertext (ciphertext + MAC) */
501
-        $ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 32);
501
+        $ciphertext = ParagonIE_Sodium_Core_Util::substr( $message, 32 );
502 502
 
503 503
         /** @var string $secretKey */
504
-        $secretKey = self::box_secretkey($keypair);
504
+        $secretKey = self::box_secretkey( $keypair );
505 505
 
506 506
         /** @var string $publicKey */
507
-        $publicKey = self::box_publickey($keypair);
507
+        $publicKey = self::box_publickey( $keypair );
508 508
 
509 509
         /** @var string $nonce */
510 510
         $nonce = self::generichash(
@@ -514,15 +514,15 @@  discard block
 block discarded – undo
514 514
         );
515 515
 
516 516
         /** @var string $keypair */
517
-        $keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK);
517
+        $keypair = self::box_keypair_from_secretkey_and_publickey( $secretKey, $ephemeralPK );
518 518
 
519 519
         /** @var string $m */
520
-        $m = self::box_open($ciphertext, $nonce, $keypair);
520
+        $m = self::box_open( $ciphertext, $nonce, $keypair );
521 521
         try {
522
-            ParagonIE_Sodium_Compat::memzero($secretKey);
523
-            ParagonIE_Sodium_Compat::memzero($ephemeralPK);
524
-            ParagonIE_Sodium_Compat::memzero($nonce);
525
-        } catch (SodiumException $ex) {
522
+            ParagonIE_Sodium_Compat::memzero( $secretKey );
523
+            ParagonIE_Sodium_Compat::memzero( $ephemeralPK );
524
+            ParagonIE_Sodium_Compat::memzero( $nonce );
525
+        } catch ( SodiumException $ex ) {
526 526
             $secretKey = null;
527 527
             $ephemeralPK = null;
528 528
             $nonce = null;
@@ -541,11 +541,11 @@  discard block
 block discarded – undo
541 541
      * @throws SodiumException
542 542
      * @throws TypeError
543 543
      */
544
-    public static function box_beforenm($sk, $pk)
544
+    public static function box_beforenm( $sk, $pk )
545 545
     {
546 546
         return ParagonIE_Sodium_Core_HSalsa20::hsalsa20(
547
-            str_repeat("\x00", 16),
548
-            self::scalarmult($sk, $pk)
547
+            str_repeat( "\x00", 16 ),
548
+            self::scalarmult( $sk, $pk )
549 549
         );
550 550
     }
551 551
 
@@ -559,8 +559,8 @@  discard block
 block discarded – undo
559 559
      */
560 560
     public static function box_keypair()
561 561
     {
562
-        $sKey = random_bytes(32);
563
-        $pKey = self::scalarmult_base($sKey);
562
+        $sKey = random_bytes( 32 );
563
+        $pKey = self::scalarmult_base( $sKey );
564 564
         return $sKey . $pKey;
565 565
     }
566 566
 
@@ -570,14 +570,14 @@  discard block
 block discarded – undo
570 570
      * @throws SodiumException
571 571
      * @throws TypeError
572 572
      */
573
-    public static function box_seed_keypair($seed)
573
+    public static function box_seed_keypair( $seed )
574 574
     {
575 575
         $sKey = ParagonIE_Sodium_Core_Util::substr(
576
-            hash('sha512', $seed, true),
576
+            hash( 'sha512', $seed, true ),
577 577
             0,
578 578
             32
579 579
         );
580
-        $pKey = self::scalarmult_base($sKey);
580
+        $pKey = self::scalarmult_base( $sKey );
581 581
         return $sKey . $pKey;
582 582
     }
583 583
 
@@ -589,10 +589,10 @@  discard block
 block discarded – undo
589 589
      * @return string
590 590
      * @throws TypeError
591 591
      */
592
-    public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey)
592
+    public static function box_keypair_from_secretkey_and_publickey( $sKey, $pKey )
593 593
     {
594
-        return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) .
595
-            ParagonIE_Sodium_Core_Util::substr($pKey, 0, 32);
594
+        return ParagonIE_Sodium_Core_Util::substr( $sKey, 0, 32 ) .
595
+            ParagonIE_Sodium_Core_Util::substr( $pKey, 0, 32 );
596 596
     }
597 597
 
598 598
     /**
@@ -603,14 +603,14 @@  discard block
 block discarded – undo
603 603
      * @throws RangeException
604 604
      * @throws TypeError
605 605
      */
606
-    public static function box_secretkey($keypair)
606
+    public static function box_secretkey( $keypair )
607 607
     {
608
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) {
608
+        if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== 64 ) {
609 609
             throw new RangeException(
610 610
                 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
611 611
             );
612 612
         }
613
-        return ParagonIE_Sodium_Core_Util::substr($keypair, 0, 32);
613
+        return ParagonIE_Sodium_Core_Util::substr( $keypair, 0, 32 );
614 614
     }
615 615
 
616 616
     /**
@@ -621,14 +621,14 @@  discard block
 block discarded – undo
621 621
      * @throws RangeException
622 622
      * @throws TypeError
623 623
      */
624
-    public static function box_publickey($keypair)
624
+    public static function box_publickey( $keypair )
625 625
     {
626
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
626
+        if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) {
627 627
             throw new RangeException(
628 628
                 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
629 629
             );
630 630
         }
631
-        return ParagonIE_Sodium_Core_Util::substr($keypair, 32, 32);
631
+        return ParagonIE_Sodium_Core_Util::substr( $keypair, 32, 32 );
632 632
     }
633 633
 
634 634
     /**
@@ -640,14 +640,14 @@  discard block
 block discarded – undo
640 640
      * @throws SodiumException
641 641
      * @throws TypeError
642 642
      */
643
-    public static function box_publickey_from_secretkey($sKey)
643
+    public static function box_publickey_from_secretkey( $sKey )
644 644
     {
645
-        if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) {
645
+        if ( ParagonIE_Sodium_Core_Util::strlen( $sKey ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES ) {
646 646
             throw new RangeException(
647 647
                 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.'
648 648
             );
649 649
         }
650
-        return self::scalarmult_base($sKey);
650
+        return self::scalarmult_base( $sKey );
651 651
     }
652 652
 
653 653
     /**
@@ -662,14 +662,14 @@  discard block
 block discarded – undo
662 662
      * @throws SodiumException
663 663
      * @throws TypeError
664 664
      */
665
-    public static function box_open($ciphertext, $nonce, $keypair)
665
+    public static function box_open( $ciphertext, $nonce, $keypair )
666 666
     {
667 667
         return self::secretbox_open(
668 668
             $ciphertext,
669 669
             $nonce,
670 670
             self::box_beforenm(
671
-                self::box_secretkey($keypair),
672
-                self::box_publickey($keypair)
671
+                self::box_secretkey( $keypair ),
672
+                self::box_publickey( $keypair )
673 673
             )
674 674
         );
675 675
     }
@@ -687,34 +687,34 @@  discard block
 block discarded – undo
687 687
      * @throws SodiumException
688 688
      * @throws TypeError
689 689
      */
690
-    public static function generichash($message, $key = '', $outlen = 32)
690
+    public static function generichash( $message, $key = '', $outlen = 32 )
691 691
     {
692 692
         // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
693 693
         ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
694 694
 
695 695
         $k = null;
696
-        if (!empty($key)) {
696
+        if ( ! empty( $key ) ) {
697 697
             /** @var SplFixedArray $k */
698
-            $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key);
699
-            if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) {
700
-                throw new RangeException('Invalid key size');
698
+            $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $key );
699
+            if ( $k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES ) {
700
+                throw new RangeException( 'Invalid key size' );
701 701
             }
702 702
         }
703 703
 
704 704
         /** @var SplFixedArray $in */
705
-        $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message);
705
+        $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $message );
706 706
 
707 707
         /** @var SplFixedArray $ctx */
708
-        $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outlen);
709
-        ParagonIE_Sodium_Core_BLAKE2b::update($ctx, $in, $in->count());
708
+        $ctx = ParagonIE_Sodium_Core_BLAKE2b::init( $k, $outlen );
709
+        ParagonIE_Sodium_Core_BLAKE2b::update( $ctx, $in, $in->count() );
710 710
 
711 711
         /** @var SplFixedArray $out */
712
-        $out = new SplFixedArray($outlen);
713
-        $out = ParagonIE_Sodium_Core_BLAKE2b::finish($ctx, $out);
712
+        $out = new SplFixedArray( $outlen );
713
+        $out = ParagonIE_Sodium_Core_BLAKE2b::finish( $ctx, $out );
714 714
 
715 715
         /** @var array<int, int> */
716 716
         $outArray = $out->toArray();
717
-        return ParagonIE_Sodium_Core_Util::intArrayToString($outArray);
717
+        return ParagonIE_Sodium_Core_Util::intArrayToString( $outArray );
718 718
     }
719 719
 
720 720
     /**
@@ -728,22 +728,22 @@  discard block
 block discarded – undo
728 728
      * @throws SodiumException
729 729
      * @throws TypeError
730 730
      */
731
-    public static function generichash_final($ctx, $outlen = 32)
731
+    public static function generichash_final( $ctx, $outlen = 32 )
732 732
     {
733
-        if (!is_string($ctx)) {
734
-            throw new TypeError('Context must be a string');
733
+        if ( ! is_string( $ctx ) ) {
734
+            throw new TypeError( 'Context must be a string' );
735 735
         }
736
-        $out = new SplFixedArray($outlen);
736
+        $out = new SplFixedArray( $outlen );
737 737
 
738 738
         /** @var SplFixedArray $context */
739
-        $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx);
739
+        $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext( $ctx );
740 740
 
741 741
         /** @var SplFixedArray $out */
742
-        $out = ParagonIE_Sodium_Core_BLAKE2b::finish($context, $out);
742
+        $out = ParagonIE_Sodium_Core_BLAKE2b::finish( $context, $out );
743 743
 
744 744
         /** @var array<int, int> */
745 745
         $outArray = $out->toArray();
746
-        return ParagonIE_Sodium_Core_Util::intArrayToString($outArray);
746
+        return ParagonIE_Sodium_Core_Util::intArrayToString( $outArray );
747 747
     }
748 748
 
749 749
     /**
@@ -758,23 +758,23 @@  discard block
 block discarded – undo
758 758
      * @throws SodiumException
759 759
      * @throws TypeError
760 760
      */
761
-    public static function generichash_init($key = '', $outputLength = 32)
761
+    public static function generichash_init( $key = '', $outputLength = 32 )
762 762
     {
763 763
         // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
764 764
         ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
765 765
 
766 766
         $k = null;
767
-        if (!empty($key)) {
768
-            $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key);
769
-            if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) {
770
-                throw new RangeException('Invalid key size');
767
+        if ( ! empty( $key ) ) {
768
+            $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $key );
769
+            if ( $k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES ) {
770
+                throw new RangeException( 'Invalid key size' );
771 771
             }
772 772
         }
773 773
 
774 774
         /** @var SplFixedArray $ctx */
775
-        $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength);
775
+        $ctx = ParagonIE_Sodium_Core_BLAKE2b::init( $k, $outputLength );
776 776
 
777
-        return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx);
777
+        return ParagonIE_Sodium_Core_BLAKE2b::contextToString( $ctx );
778 778
     }
779 779
 
780 780
     /**
@@ -801,27 +801,27 @@  discard block
 block discarded – undo
801 801
         ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
802 802
 
803 803
         $k = null;
804
-        if (!empty($key)) {
805
-            $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key);
806
-            if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) {
807
-                throw new RangeException('Invalid key size');
804
+        if ( ! empty( $key ) ) {
805
+            $k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $key );
806
+            if ( $k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES ) {
807
+                throw new RangeException( 'Invalid key size' );
808 808
             }
809 809
         }
810
-        if (!empty($salt)) {
811
-            $s = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($salt);
810
+        if ( ! empty( $salt ) ) {
811
+            $s = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $salt );
812 812
         } else {
813 813
             $s = null;
814 814
         }
815
-        if (!empty($salt)) {
816
-            $p = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($personal);
815
+        if ( ! empty( $salt ) ) {
816
+            $p = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $personal );
817 817
         } else {
818 818
             $p = null;
819 819
         }
820 820
 
821 821
         /** @var SplFixedArray $ctx */
822
-        $ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength, $s, $p);
822
+        $ctx = ParagonIE_Sodium_Core_BLAKE2b::init( $k, $outputLength, $s, $p );
823 823
 
824
-        return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx);
824
+        return ParagonIE_Sodium_Core_BLAKE2b::contextToString( $ctx );
825 825
     }
826 826
 
827 827
     /**
@@ -835,20 +835,20 @@  discard block
 block discarded – undo
835 835
      * @throws SodiumException
836 836
      * @throws TypeError
837 837
      */
838
-    public static function generichash_update($ctx, $message)
838
+    public static function generichash_update( $ctx, $message )
839 839
     {
840 840
         // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
841 841
         ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
842 842
 
843 843
         /** @var SplFixedArray $context */
844
-        $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx);
844
+        $context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext( $ctx );
845 845
 
846 846
         /** @var SplFixedArray $in */
847
-        $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message);
847
+        $in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray( $message );
848 848
 
849
-        ParagonIE_Sodium_Core_BLAKE2b::update($context, $in, $in->count());
849
+        ParagonIE_Sodium_Core_BLAKE2b::update( $context, $in, $in->count() );
850 850
 
851
-        return ParagonIE_Sodium_Core_BLAKE2b::contextToString($context);
851
+        return ParagonIE_Sodium_Core_BLAKE2b::contextToString( $context );
852 852
     }
853 853
 
854 854
     /**
@@ -864,10 +864,10 @@  discard block
 block discarded – undo
864 864
      * @throws SodiumException
865 865
      * @throws TypeError
866 866
      */
867
-    public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk)
867
+    public static function keyExchange( $my_sk, $their_pk, $client_pk, $server_pk )
868 868
     {
869 869
         return ParagonIE_Sodium_Compat::crypto_generichash(
870
-            ParagonIE_Sodium_Compat::crypto_scalarmult($my_sk, $their_pk) .
870
+            ParagonIE_Sodium_Compat::crypto_scalarmult( $my_sk, $their_pk ) .
871 871
             $client_pk .
872 872
             $server_pk
873 873
         );
@@ -885,10 +885,10 @@  discard block
 block discarded – undo
885 885
      * @throws SodiumException
886 886
      * @throws TypeError
887 887
      */
888
-    public static function scalarmult($sKey, $pKey)
888
+    public static function scalarmult( $sKey, $pKey )
889 889
     {
890
-        $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey);
891
-        self::scalarmult_throw_if_zero($q);
890
+        $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10( $sKey, $pKey );
891
+        self::scalarmult_throw_if_zero( $q );
892 892
         return $q;
893 893
     }
894 894
 
@@ -902,10 +902,10 @@  discard block
 block discarded – undo
902 902
      * @throws SodiumException
903 903
      * @throws TypeError
904 904
      */
905
-    public static function scalarmult_base($secret)
905
+    public static function scalarmult_base( $secret )
906 906
     {
907
-        $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret);
908
-        self::scalarmult_throw_if_zero($q);
907
+        $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base( $secret );
908
+        self::scalarmult_throw_if_zero( $q );
909 909
         return $q;
910 910
     }
911 911
 
@@ -917,16 +917,16 @@  discard block
 block discarded – undo
917 917
      * @throws SodiumException
918 918
      * @throws TypeError
919 919
      */
920
-    protected static function scalarmult_throw_if_zero($q)
920
+    protected static function scalarmult_throw_if_zero( $q )
921 921
     {
922 922
         $d = 0;
923
-        for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) {
924
-            $d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]);
923
+        for ( $i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i ) {
924
+            $d |= ParagonIE_Sodium_Core_Util::chrToInt( $q[ $i ] );
925 925
         }
926 926
 
927 927
         /* branch-free variant of === 0 */
928
-        if (-(1 & (($d - 1) >> 8))) {
929
-            throw new SodiumException('Zero public key is not allowed');
928
+        if (-( 1 & ( ( $d - 1 ) >> 8 ) )) {
929
+            throw new SodiumException( 'Zero public key is not allowed' );
930 930
         }
931 931
     }
932 932
 
@@ -942,26 +942,26 @@  discard block
 block discarded – undo
942 942
      * @throws SodiumException
943 943
      * @throws TypeError
944 944
      */
945
-    public static function secretbox($plaintext, $nonce, $key)
945
+    public static function secretbox( $plaintext, $nonce, $key )
946 946
     {
947 947
         /** @var string $subkey */
948
-        $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
948
+        $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20( $nonce, $key );
949 949
 
950 950
         /** @var string $block0 */
951
-        $block0 = str_repeat("\x00", 32);
951
+        $block0 = str_repeat( "\x00", 32 );
952 952
 
953 953
         /** @var int $mlen - Length of the plaintext message */
954
-        $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext);
954
+        $mlen = ParagonIE_Sodium_Core_Util::strlen( $plaintext );
955 955
         $mlen0 = $mlen;
956
-        if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) {
956
+        if ( $mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES ) {
957 957
             $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES;
958 958
         }
959
-        $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0);
959
+        $block0 .= ParagonIE_Sodium_Core_Util::substr( $plaintext, 0, $mlen0 );
960 960
 
961 961
         /** @var string $block0 */
962 962
         $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor(
963 963
             $block0,
964
-            ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
964
+            ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ),
965 965
             $subkey
966 966
         );
967 967
 
@@ -970,13 +970,13 @@  discard block
 block discarded – undo
970 970
             $block0,
971 971
             self::secretbox_xsalsa20poly1305_ZEROBYTES
972 972
         );
973
-        if ($mlen > $mlen0) {
973
+        if ( $mlen > $mlen0 ) {
974 974
             $c .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
975 975
                 ParagonIE_Sodium_Core_Util::substr(
976 976
                     $plaintext,
977 977
                     self::secretbox_xsalsa20poly1305_ZEROBYTES
978 978
                 ),
979
-                ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
979
+                ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ),
980 980
                 1,
981 981
                 $subkey
982 982
             );
@@ -989,18 +989,18 @@  discard block
 block discarded – undo
989 989
             )
990 990
         );
991 991
         try {
992
-            ParagonIE_Sodium_Compat::memzero($block0);
993
-            ParagonIE_Sodium_Compat::memzero($subkey);
994
-        } catch (SodiumException $ex) {
992
+            ParagonIE_Sodium_Compat::memzero( $block0 );
993
+            ParagonIE_Sodium_Compat::memzero( $subkey );
994
+        } catch ( SodiumException $ex ) {
995 995
             $block0 = null;
996 996
             $subkey = null;
997 997
         }
998 998
 
999
-        $state->update($c);
999
+        $state->update( $c );
1000 1000
 
1001 1001
         /** @var string $c - MAC || ciphertext */
1002 1002
         $c = $state->finish() . $c;
1003
-        unset($state);
1003
+        unset( $state );
1004 1004
 
1005 1005
         return $c;
1006 1006
     }
@@ -1017,7 +1017,7 @@  discard block
 block discarded – undo
1017 1017
      * @throws SodiumException
1018 1018
      * @throws TypeError
1019 1019
      */
1020
-    public static function secretbox_open($ciphertext, $nonce, $key)
1020
+    public static function secretbox_open( $ciphertext, $nonce, $key )
1021 1021
     {
1022 1022
         /** @var string $mac */
1023 1023
         $mac = ParagonIE_Sodium_Core_Util::substr(
@@ -1033,46 +1033,46 @@  discard block
 block discarded – undo
1033 1033
         );
1034 1034
 
1035 1035
         /** @var int $clen */
1036
-        $clen = ParagonIE_Sodium_Core_Util::strlen($c);
1036
+        $clen = ParagonIE_Sodium_Core_Util::strlen( $c );
1037 1037
 
1038 1038
         /** @var string $subkey */
1039
-        $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
1039
+        $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20( $nonce, $key );
1040 1040
 
1041 1041
         /** @var string $block0 */
1042 1042
         $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20(
1043 1043
             64,
1044
-            ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
1044
+            ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ),
1045 1045
             $subkey
1046 1046
         );
1047 1047
         $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify(
1048 1048
             $mac,
1049 1049
             $c,
1050
-            ParagonIE_Sodium_Core_Util::substr($block0, 0, 32)
1050
+            ParagonIE_Sodium_Core_Util::substr( $block0, 0, 32 )
1051 1051
         );
1052
-        if (!$verified) {
1052
+        if ( ! $verified ) {
1053 1053
             try {
1054
-                ParagonIE_Sodium_Compat::memzero($subkey);
1055
-            } catch (SodiumException $ex) {
1054
+                ParagonIE_Sodium_Compat::memzero( $subkey );
1055
+            } catch ( SodiumException $ex ) {
1056 1056
                 $subkey = null;
1057 1057
             }
1058
-            throw new SodiumException('Invalid MAC');
1058
+            throw new SodiumException( 'Invalid MAC' );
1059 1059
         }
1060 1060
 
1061 1061
         /** @var string $m - Decrypted message */
1062 1062
         $m = ParagonIE_Sodium_Core_Util::xorStrings(
1063
-            ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES),
1064
-            ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES)
1063
+            ParagonIE_Sodium_Core_Util::substr( $block0, self::secretbox_xsalsa20poly1305_ZEROBYTES ),
1064
+            ParagonIE_Sodium_Core_Util::substr( $c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES )
1065 1065
         );
1066
-        if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) {
1066
+        if ( $clen > self::secretbox_xsalsa20poly1305_ZEROBYTES ) {
1067 1067
             // We had more than 1 block, so let's continue to decrypt the rest.
1068 1068
             $m .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
1069 1069
                 ParagonIE_Sodium_Core_Util::substr(
1070 1070
                     $c,
1071 1071
                     self::secretbox_xsalsa20poly1305_ZEROBYTES
1072 1072
                 ),
1073
-                ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
1073
+                ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ),
1074 1074
                 1,
1075
-                (string) $subkey
1075
+                (string)$subkey
1076 1076
             );
1077 1077
         }
1078 1078
         return $m;
@@ -1090,25 +1090,25 @@  discard block
 block discarded – undo
1090 1090
      * @throws SodiumException
1091 1091
      * @throws TypeError
1092 1092
      */
1093
-    public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key)
1093
+    public static function secretbox_xchacha20poly1305( $plaintext, $nonce, $key )
1094 1094
     {
1095 1095
         /** @var string $subkey */
1096 1096
         $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
1097
-            ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16),
1097
+            ParagonIE_Sodium_Core_Util::substr( $nonce, 0, 16 ),
1098 1098
             $key
1099 1099
         );
1100
-        $nonceLast = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
1100
+        $nonceLast = ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 );
1101 1101
 
1102 1102
         /** @var string $block0 */
1103
-        $block0 = str_repeat("\x00", 32);
1103
+        $block0 = str_repeat( "\x00", 32 );
1104 1104
 
1105 1105
         /** @var int $mlen - Length of the plaintext message */
1106
-        $mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext);
1106
+        $mlen = ParagonIE_Sodium_Core_Util::strlen( $plaintext );
1107 1107
         $mlen0 = $mlen;
1108
-        if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) {
1108
+        if ( $mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES ) {
1109 1109
             $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES;
1110 1110
         }
1111
-        $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0);
1111
+        $block0 .= ParagonIE_Sodium_Core_Util::substr( $plaintext, 0, $mlen0 );
1112 1112
 
1113 1113
         /** @var string $block0 */
1114 1114
         $block0 = ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
@@ -1122,7 +1122,7 @@  discard block
 block discarded – undo
1122 1122
             $block0,
1123 1123
             self::secretbox_xchacha20poly1305_ZEROBYTES
1124 1124
         );
1125
-        if ($mlen > $mlen0) {
1125
+        if ( $mlen > $mlen0 ) {
1126 1126
             $c .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
1127 1127
                 ParagonIE_Sodium_Core_Util::substr(
1128 1128
                     $plaintext,
@@ -1130,7 +1130,7 @@  discard block
 block discarded – undo
1130 1130
                 ),
1131 1131
                 $nonceLast,
1132 1132
                 $subkey,
1133
-                ParagonIE_Sodium_Core_Util::store64_le(1)
1133
+                ParagonIE_Sodium_Core_Util::store64_le( 1 )
1134 1134
             );
1135 1135
         }
1136 1136
         $state = new ParagonIE_Sodium_Core_Poly1305_State(
@@ -1141,18 +1141,18 @@  discard block
 block discarded – undo
1141 1141
             )
1142 1142
         );
1143 1143
         try {
1144
-            ParagonIE_Sodium_Compat::memzero($block0);
1145
-            ParagonIE_Sodium_Compat::memzero($subkey);
1146
-        } catch (SodiumException $ex) {
1144
+            ParagonIE_Sodium_Compat::memzero( $block0 );
1145
+            ParagonIE_Sodium_Compat::memzero( $subkey );
1146
+        } catch ( SodiumException $ex ) {
1147 1147
             $block0 = null;
1148 1148
             $subkey = null;
1149 1149
         }
1150 1150
 
1151
-        $state->update($c);
1151
+        $state->update( $c );
1152 1152
 
1153 1153
         /** @var string $c - MAC || ciphertext */
1154 1154
         $c = $state->finish() . $c;
1155
-        unset($state);
1155
+        unset( $state );
1156 1156
 
1157 1157
         return $c;
1158 1158
     }
@@ -1169,7 +1169,7 @@  discard block
 block discarded – undo
1169 1169
      * @throws SodiumException
1170 1170
      * @throws TypeError
1171 1171
      */
1172
-    public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
1172
+    public static function secretbox_xchacha20poly1305_open( $ciphertext, $nonce, $key )
1173 1173
     {
1174 1174
         /** @var string $mac */
1175 1175
         $mac = ParagonIE_Sodium_Core_Util::substr(
@@ -1185,48 +1185,48 @@  discard block
 block discarded – undo
1185 1185
         );
1186 1186
 
1187 1187
         /** @var int $clen */
1188
-        $clen = ParagonIE_Sodium_Core_Util::strlen($c);
1188
+        $clen = ParagonIE_Sodium_Core_Util::strlen( $c );
1189 1189
 
1190 1190
         /** @var string $subkey */
1191
-        $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20($nonce, $key);
1191
+        $subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20( $nonce, $key );
1192 1192
 
1193 1193
         /** @var string $block0 */
1194 1194
         $block0 = ParagonIE_Sodium_Core_ChaCha20::stream(
1195 1195
             64,
1196
-            ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
1196
+            ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ),
1197 1197
             $subkey
1198 1198
         );
1199 1199
         $verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify(
1200 1200
             $mac,
1201 1201
             $c,
1202
-            ParagonIE_Sodium_Core_Util::substr($block0, 0, 32)
1202
+            ParagonIE_Sodium_Core_Util::substr( $block0, 0, 32 )
1203 1203
         );
1204 1204
 
1205
-        if (!$verified) {
1205
+        if ( ! $verified ) {
1206 1206
             try {
1207
-                ParagonIE_Sodium_Compat::memzero($subkey);
1208
-            } catch (SodiumException $ex) {
1207
+                ParagonIE_Sodium_Compat::memzero( $subkey );
1208
+            } catch ( SodiumException $ex ) {
1209 1209
                 $subkey = null;
1210 1210
             }
1211
-            throw new SodiumException('Invalid MAC');
1211
+            throw new SodiumException( 'Invalid MAC' );
1212 1212
         }
1213 1213
 
1214 1214
         /** @var string $m - Decrypted message */
1215 1215
         $m = ParagonIE_Sodium_Core_Util::xorStrings(
1216
-            ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES),
1217
-            ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES)
1216
+            ParagonIE_Sodium_Core_Util::substr( $block0, self::secretbox_xchacha20poly1305_ZEROBYTES ),
1217
+            ParagonIE_Sodium_Core_Util::substr( $c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES )
1218 1218
         );
1219 1219
 
1220
-        if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) {
1220
+        if ( $clen > self::secretbox_xchacha20poly1305_ZEROBYTES ) {
1221 1221
             // We had more than 1 block, so let's continue to decrypt the rest.
1222 1222
             $m .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc(
1223 1223
                 ParagonIE_Sodium_Core_Util::substr(
1224 1224
                     $c,
1225 1225
                     self::secretbox_xchacha20poly1305_ZEROBYTES
1226 1226
                 ),
1227
-                ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
1228
-                (string) $subkey,
1229
-                ParagonIE_Sodium_Core_Util::store64_le(1)
1227
+                ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ),
1228
+                (string)$subkey,
1229
+                ParagonIE_Sodium_Core_Util::store64_le( 1 )
1230 1230
             );
1231 1231
         }
1232 1232
         return $m;
@@ -1238,16 +1238,16 @@  discard block
 block discarded – undo
1238 1238
      * @throws Exception
1239 1239
      * @throws SodiumException
1240 1240
      */
1241
-    public static function secretstream_xchacha20poly1305_init_push($key)
1241
+    public static function secretstream_xchacha20poly1305_init_push( $key )
1242 1242
     {
1243 1243
         # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES);
1244
-        $out = random_bytes(24);
1244
+        $out = random_bytes( 24 );
1245 1245
 
1246 1246
         # crypto_core_hchacha20(state->k, out, k, NULL);
1247
-        $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20($out, $key);
1247
+        $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( $out, $key );
1248 1248
         $state = new ParagonIE_Sodium_Core_SecretStream_State(
1249 1249
             $subkey,
1250
-            ParagonIE_Sodium_Core_Util::substr($out, 16, 8) . str_repeat("\0", 4)
1250
+            ParagonIE_Sodium_Core_Util::substr( $out, 16, 8 ) . str_repeat( "\0", 4 )
1251 1251
         );
1252 1252
 
1253 1253
         # _crypto_secretstream_xchacha20poly1305_counter_reset(state);
@@ -1268,16 +1268,16 @@  discard block
 block discarded – undo
1268 1268
      * @return string Returns a state.
1269 1269
      * @throws Exception
1270 1270
      */
1271
-    public static function secretstream_xchacha20poly1305_init_pull($key, $header)
1271
+    public static function secretstream_xchacha20poly1305_init_pull( $key, $header )
1272 1272
     {
1273 1273
         # crypto_core_hchacha20(state->k, in, k, NULL);
1274 1274
         $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
1275
-            ParagonIE_Sodium_Core_Util::substr($header, 0, 16),
1275
+            ParagonIE_Sodium_Core_Util::substr( $header, 0, 16 ),
1276 1276
             $key
1277 1277
         );
1278 1278
         $state = new ParagonIE_Sodium_Core_SecretStream_State(
1279 1279
             $subkey,
1280
-            ParagonIE_Sodium_Core_Util::substr($header, 16)
1280
+            ParagonIE_Sodium_Core_Util::substr( $header, 16 )
1281 1281
         );
1282 1282
         $state->counterReset();
1283 1283
         # memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES,
@@ -1295,19 +1295,19 @@  discard block
 block discarded – undo
1295 1295
      * @return string
1296 1296
      * @throws SodiumException
1297 1297
      */
1298
-    public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
1298
+    public static function secretstream_xchacha20poly1305_push( &$state, $msg, $aad = '', $tag = 0 )
1299 1299
     {
1300
-        $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
1300
+        $st = ParagonIE_Sodium_Core_SecretStream_State::fromString( $state );
1301 1301
         # crypto_onetimeauth_poly1305_state poly1305_state;
1302 1302
         # unsigned char                     block[64U];
1303 1303
         # unsigned char                     slen[8U];
1304 1304
         # unsigned char                    *c;
1305 1305
         # unsigned char                    *mac;
1306 1306
 
1307
-        $msglen = ParagonIE_Sodium_Core_Util::strlen($msg);
1308
-        $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad);
1307
+        $msglen = ParagonIE_Sodium_Core_Util::strlen( $msg );
1308
+        $aadlen = ParagonIE_Sodium_Core_Util::strlen( $aad );
1309 1309
 
1310
-        if ((($msglen + 63) >> 6) > 0xfffffffe) {
1310
+        if ( ( ( $msglen + 63 ) >> 6 ) > 0xfffffffe ) {
1311 1311
             throw new SodiumException(
1312 1312
                 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
1313 1313
             );
@@ -1324,62 +1324,62 @@  discard block
 block discarded – undo
1324 1324
         # crypto_onetimeauth_poly1305_init(&poly1305_state, block);
1325 1325
         # sodium_memzero(block, sizeof block);
1326 1326
         $auth = new ParagonIE_Sodium_Core_Poly1305_State(
1327
-            ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
1327
+            ParagonIE_Sodium_Core_ChaCha20::ietfStream( 32, $st->getCombinedNonce(), $st->getKey() )
1328 1328
         );
1329 1329
 
1330 1330
         # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
1331
-        $auth->update($aad);
1331
+        $auth->update( $aad );
1332 1332
 
1333 1333
         # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
1334 1334
         #     (0x10 - adlen) & 0xf);
1335
-        $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
1335
+        $auth->update( str_repeat( "\0", ( ( 0x10 - $aadlen ) & 0xf ) ) );
1336 1336
 
1337 1337
         # memset(block, 0, sizeof block);
1338 1338
         # block[0] = tag;
1339 1339
         # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
1340 1340
         #                                    state->nonce, 1U, state->k);
1341 1341
         $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1342
-            ParagonIE_Sodium_Core_Util::intToChr($tag) . str_repeat("\0", 63),
1342
+            ParagonIE_Sodium_Core_Util::intToChr( $tag ) . str_repeat( "\0", 63 ),
1343 1343
             $st->getCombinedNonce(),
1344 1344
             $st->getKey(),
1345
-            ParagonIE_Sodium_Core_Util::store64_le(1)
1345
+            ParagonIE_Sodium_Core_Util::store64_le( 1 )
1346 1346
         );
1347 1347
 
1348 1348
         # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
1349
-        $auth->update($block);
1349
+        $auth->update( $block );
1350 1350
 
1351 1351
         # out[0] = block[0];
1352
-        $out = $block[0];
1352
+        $out = $block[ 0 ];
1353 1353
         # c = out + (sizeof tag);
1354 1354
         # crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k);
1355 1355
         $cipher = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1356 1356
             $msg,
1357 1357
             $st->getCombinedNonce(),
1358 1358
             $st->getKey(),
1359
-            ParagonIE_Sodium_Core_Util::store64_le(2)
1359
+            ParagonIE_Sodium_Core_Util::store64_le( 2 )
1360 1360
         );
1361 1361
 
1362 1362
         # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
1363
-        $auth->update($cipher);
1363
+        $auth->update( $cipher );
1364 1364
 
1365 1365
         $out .= $cipher;
1366
-        unset($cipher);
1366
+        unset( $cipher );
1367 1367
 
1368 1368
         # crypto_onetimeauth_poly1305_update
1369 1369
         # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
1370
-        $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
1370
+        $auth->update( str_repeat( "\0", ( ( 0x10 - 64 + $msglen ) & 0xf ) ) );
1371 1371
 
1372 1372
         # STORE64_LE(slen, (uint64_t) adlen);
1373
-        $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen);
1373
+        $slen = ParagonIE_Sodium_Core_Util::store64_le( $aadlen );
1374 1374
 
1375 1375
         # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1376
-        $auth->update($slen);
1376
+        $auth->update( $slen );
1377 1377
 
1378 1378
         # STORE64_LE(slen, (sizeof block) + mlen);
1379
-        $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen);
1379
+        $slen = ParagonIE_Sodium_Core_Util::store64_le( 64 + $msglen );
1380 1380
 
1381 1381
         # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1382
-        $auth->update($slen);
1382
+        $auth->update( $slen );
1383 1383
 
1384 1384
         # mac = c + mlen;
1385 1385
         # crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
@@ -1387,12 +1387,12 @@  discard block
 block discarded – undo
1387 1387
         $out .= $mac;
1388 1388
 
1389 1389
         # sodium_memzero(&poly1305_state, sizeof poly1305_state);
1390
-        unset($auth);
1390
+        unset( $auth );
1391 1391
 
1392 1392
 
1393 1393
         # XOR_BUF(STATE_INONCE(state), mac,
1394 1394
         #     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1395
-        $st->xorNonce($mac);
1395
+        $st->xorNonce( $mac );
1396 1396
 
1397 1397
         # sodium_increment(STATE_COUNTER(state),
1398 1398
         #     crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
@@ -1401,15 +1401,15 @@  discard block
 block discarded – undo
1401 1401
         $state = $st->toString();
1402 1402
 
1403 1403
         /** @var bool $rekey */
1404
-        $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
1404
+        $rekey = ( $tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY ) !== 0;
1405 1405
         # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
1406 1406
         #     sodium_is_zero(STATE_COUNTER(state),
1407 1407
         #         crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
1408 1408
         #     crypto_secretstream_xchacha20poly1305_rekey(state);
1409 1409
         # }
1410
-        if ($rekey || $st->needsRekey()) {
1410
+        if ( $rekey || $st->needsRekey() ) {
1411 1411
             // DO REKEY
1412
-            self::secretstream_xchacha20poly1305_rekey($state);
1412
+            self::secretstream_xchacha20poly1305_rekey( $state );
1413 1413
         }
1414 1414
         # if (outlen_p != NULL) {
1415 1415
         #     *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen;
@@ -1424,19 +1424,19 @@  discard block
 block discarded – undo
1424 1424
      * @return bool|array{0: string, 1: int}
1425 1425
      * @throws SodiumException
1426 1426
      */
1427
-    public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '')
1427
+    public static function secretstream_xchacha20poly1305_pull( &$state, $cipher, $aad = '' )
1428 1428
     {
1429
-        $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
1429
+        $st = ParagonIE_Sodium_Core_SecretStream_State::fromString( $state );
1430 1430
 
1431
-        $cipherlen = ParagonIE_Sodium_Core_Util::strlen($cipher);
1431
+        $cipherlen = ParagonIE_Sodium_Core_Util::strlen( $cipher );
1432 1432
         #     mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES;
1433 1433
         $msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES;
1434
-        $aadlen = ParagonIE_Sodium_Core_Util::strlen($aad);
1434
+        $aadlen = ParagonIE_Sodium_Core_Util::strlen( $aad );
1435 1435
 
1436 1436
         #     if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
1437 1437
         #         sodium_misuse();
1438 1438
         #     }
1439
-        if ((($msglen + 63) >> 6) > 0xfffffffe) {
1439
+        if ( ( ( $msglen + 63 ) >> 6 ) > 0xfffffffe ) {
1440 1440
             throw new SodiumException(
1441 1441
                 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
1442 1442
             );
@@ -1446,15 +1446,15 @@  discard block
 block discarded – undo
1446 1446
         #     crypto_onetimeauth_poly1305_init(&poly1305_state, block);
1447 1447
         #     sodium_memzero(block, sizeof block);
1448 1448
         $auth = new ParagonIE_Sodium_Core_Poly1305_State(
1449
-            ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
1449
+            ParagonIE_Sodium_Core_ChaCha20::ietfStream( 32, $st->getCombinedNonce(), $st->getKey() )
1450 1450
         );
1451 1451
 
1452 1452
         #     crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
1453
-        $auth->update($aad);
1453
+        $auth->update( $aad );
1454 1454
 
1455 1455
         #     crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
1456 1456
         #         (0x10 - adlen) & 0xf);
1457
-        $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
1457
+        $auth->update( str_repeat( "\0", ( ( 0x10 - $aadlen ) & 0xf ) ) );
1458 1458
 
1459 1459
 
1460 1460
         #     memset(block, 0, sizeof block);
@@ -1462,36 +1462,36 @@  discard block
 block discarded – undo
1462 1462
         #     crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
1463 1463
         #                                        state->nonce, 1U, state->k);
1464 1464
         $block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1465
-            $cipher[0] . str_repeat("\0", 63),
1465
+            $cipher[ 0 ] . str_repeat( "\0", 63 ),
1466 1466
             $st->getCombinedNonce(),
1467 1467
             $st->getKey(),
1468
-            ParagonIE_Sodium_Core_Util::store64_le(1)
1468
+            ParagonIE_Sodium_Core_Util::store64_le( 1 )
1469 1469
         );
1470 1470
         #     tag = block[0];
1471 1471
         #     block[0] = in[0];
1472 1472
         #     crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
1473
-        $tag = ParagonIE_Sodium_Core_Util::chrToInt($block[0]);
1474
-        $block[0] = $cipher[0];
1475
-        $auth->update($block);
1473
+        $tag = ParagonIE_Sodium_Core_Util::chrToInt( $block[ 0 ] );
1474
+        $block[ 0 ] = $cipher[ 0 ];
1475
+        $auth->update( $block );
1476 1476
 
1477 1477
 
1478 1478
         #     c = in + (sizeof tag);
1479 1479
         #     crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
1480
-        $auth->update(ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen));
1480
+        $auth->update( ParagonIE_Sodium_Core_Util::substr( $cipher, 1, $msglen ) );
1481 1481
 
1482 1482
         #     crypto_onetimeauth_poly1305_update
1483 1483
         #     (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
1484
-        $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
1484
+        $auth->update( str_repeat( "\0", ( ( 0x10 - 64 + $msglen ) & 0xf ) ) );
1485 1485
 
1486 1486
         #     STORE64_LE(slen, (uint64_t) adlen);
1487 1487
         #     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1488
-        $slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen);
1489
-        $auth->update($slen);
1488
+        $slen = ParagonIE_Sodium_Core_Util::store64_le( $aadlen );
1489
+        $auth->update( $slen );
1490 1490
 
1491 1491
         #     STORE64_LE(slen, (sizeof block) + mlen);
1492 1492
         #     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1493
-        $slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen);
1494
-        $auth->update($slen);
1493
+        $slen = ParagonIE_Sodium_Core_Util::store64_le( 64 + $msglen );
1494
+        $auth->update( $slen );
1495 1495
 
1496 1496
         #     crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
1497 1497
         #     sodium_memzero(&poly1305_state, sizeof poly1305_state);
@@ -1503,22 +1503,22 @@  discard block
 block discarded – undo
1503 1503
         #         return -1;
1504 1504
         #     }
1505 1505
 
1506
-        $stored = ParagonIE_Sodium_Core_Util::substr($cipher, $msglen + 1, 16);
1507
-        if (!ParagonIE_Sodium_Core_Util::hashEquals($mac, $stored)) {
1506
+        $stored = ParagonIE_Sodium_Core_Util::substr( $cipher, $msglen + 1, 16 );
1507
+        if ( ! ParagonIE_Sodium_Core_Util::hashEquals( $mac, $stored ) ) {
1508 1508
             return false;
1509 1509
         }
1510 1510
 
1511 1511
         #     crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k);
1512 1512
         $out = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1513
-            ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen),
1513
+            ParagonIE_Sodium_Core_Util::substr( $cipher, 1, $msglen ),
1514 1514
             $st->getCombinedNonce(),
1515 1515
             $st->getKey(),
1516
-            ParagonIE_Sodium_Core_Util::store64_le(2)
1516
+            ParagonIE_Sodium_Core_Util::store64_le( 2 )
1517 1517
         );
1518 1518
 
1519 1519
         #     XOR_BUF(STATE_INONCE(state), mac,
1520 1520
         #         crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1521
-        $st->xorNonce($mac);
1521
+        $st->xorNonce( $mac );
1522 1522
 
1523 1523
         #     sodium_increment(STATE_COUNTER(state),
1524 1524
         #         crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
@@ -1534,12 +1534,12 @@  discard block
 block discarded – undo
1534 1534
         $state = $st->toString();
1535 1535
 
1536 1536
         /** @var bool $rekey */
1537
-        $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
1538
-        if ($rekey || $st->needsRekey()) {
1537
+        $rekey = ( $tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY ) !== 0;
1538
+        if ( $rekey || $st->needsRekey() ) {
1539 1539
             // DO REKEY
1540
-            self::secretstream_xchacha20poly1305_rekey($state);
1540
+            self::secretstream_xchacha20poly1305_rekey( $state );
1541 1541
         }
1542
-        return array($out, $tag);
1542
+        return array( $out, $tag );
1543 1543
     }
1544 1544
 
1545 1545
     /**
@@ -1547,9 +1547,9 @@  discard block
 block discarded – undo
1547 1547
      * @return void
1548 1548
      * @throws SodiumException
1549 1549
      */
1550
-    public static function secretstream_xchacha20poly1305_rekey(&$state)
1550
+    public static function secretstream_xchacha20poly1305_rekey( &$state )
1551 1551
     {
1552
-        $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
1552
+        $st = ParagonIE_Sodium_Core_SecretStream_State::fromString( $state );
1553 1553
         # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES +
1554 1554
         # crypto_secretstream_xchacha20poly1305_INONCEBYTES];
1555 1555
         # size_t        i;
@@ -1562,18 +1562,18 @@  discard block
 block discarded – undo
1562 1562
         #     new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] =
1563 1563
         #         STATE_INONCE(state)[i];
1564 1564
         # }
1565
-        $new_key_and_inonce .= ParagonIE_Sodium_Core_Util::substR($st->getNonce(), 0, 8);
1565
+        $new_key_and_inonce .= ParagonIE_Sodium_Core_Util::substR( $st->getNonce(), 0, 8 );
1566 1566
 
1567 1567
         # crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce,
1568 1568
         #                                 sizeof new_key_and_inonce,
1569 1569
         #                                 state->nonce, state->k);
1570 1570
 
1571
-        $st->rekey(ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1571
+        $st->rekey( ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc(
1572 1572
             $new_key_and_inonce,
1573 1573
             $st->getCombinedNonce(),
1574 1574
             $st->getKey(),
1575
-            ParagonIE_Sodium_Core_Util::store64_le(0)
1576
-        ));
1575
+            ParagonIE_Sodium_Core_Util::store64_le( 0 )
1576
+        ) );
1577 1577
 
1578 1578
         # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
1579 1579
         #     state->k[i] = new_key_and_inonce[i];
@@ -1599,9 +1599,9 @@  discard block
 block discarded – undo
1599 1599
      * @throws SodiumException
1600 1600
      * @throws TypeError
1601 1601
      */
1602
-    public static function sign_detached($message, $sk)
1602
+    public static function sign_detached( $message, $sk )
1603 1603
     {
1604
-        return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk);
1604
+        return ParagonIE_Sodium_Core_Ed25519::sign_detached( $message, $sk );
1605 1605
     }
1606 1606
 
1607 1607
     /**
@@ -1615,9 +1615,9 @@  discard block
 block discarded – undo
1615 1615
      * @throws SodiumException
1616 1616
      * @throws TypeError
1617 1617
      */
1618
-    public static function sign($message, $sk)
1618
+    public static function sign( $message, $sk )
1619 1619
     {
1620
-        return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk);
1620
+        return ParagonIE_Sodium_Core_Ed25519::sign( $message, $sk );
1621 1621
     }
1622 1622
 
1623 1623
     /**
@@ -1631,9 +1631,9 @@  discard block
 block discarded – undo
1631 1631
      * @throws SodiumException
1632 1632
      * @throws TypeError
1633 1633
      */
1634
-    public static function sign_open($signedMessage, $pk)
1634
+    public static function sign_open( $signedMessage, $pk )
1635 1635
     {
1636
-        return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk);
1636
+        return ParagonIE_Sodium_Core_Ed25519::sign_open( $signedMessage, $pk );
1637 1637
     }
1638 1638
 
1639 1639
     /**
@@ -1648,8 +1648,8 @@  discard block
 block discarded – undo
1648 1648
      * @throws SodiumException
1649 1649
      * @throws TypeError
1650 1650
      */
1651
-    public static function sign_verify_detached($signature, $message, $pk)
1651
+    public static function sign_verify_detached( $signature, $message, $pk )
1652 1652
     {
1653
-        return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk);
1653
+        return ParagonIE_Sodium_Core_Ed25519::verify_detached( $signature, $message, $pk );
1654 1654
     }
1655 1655
 }
Please login to merge, or discard this patch.
Braces   +35 added lines, -70 removed lines patch added patch discarded remove patch
@@ -12,8 +12,7 @@  discard block
 block discarded – undo
12 12
  * If you are using this library, you should be using
13 13
  * ParagonIE_Sodium_Compat in your code, not this class.
14 14
  */
15
-abstract class ParagonIE_Sodium_Crypto
16
-{
15
+abstract class ParagonIE_Sodium_Crypto {
17 16
     const aead_chacha20poly1305_KEYBYTES = 32;
18 17
     const aead_chacha20poly1305_NSECBYTES = 0;
19 18
     const aead_chacha20poly1305_NPUBBYTES = 8;
@@ -381,8 +380,7 @@  discard block
 block discarded – undo
381 380
      * @return string
382 381
      * @throws TypeError
383 382
      */
384
-    public static function auth($message, $key)
385
-    {
383
+    public static function auth($message, $key) {
386 384
         return ParagonIE_Sodium_Core_Util::substr(
387 385
             hash_hmac('sha512', $message, $key, true),
388 386
             0,
@@ -402,8 +400,7 @@  discard block
 block discarded – undo
402 400
      * @throws SodiumException
403 401
      * @throws TypeError
404 402
      */
405
-    public static function auth_verify($mac, $message, $key)
406
-    {
403
+    public static function auth_verify($mac, $message, $key) {
407 404
         return ParagonIE_Sodium_Core_Util::hashEquals(
408 405
             $mac,
409 406
             self::auth($message, $key)
@@ -422,8 +419,7 @@  discard block
 block discarded – undo
422 419
      * @throws SodiumException
423 420
      * @throws TypeError
424 421
      */
425
-    public static function box($plaintext, $nonce, $keypair)
426
-    {
422
+    public static function box($plaintext, $nonce, $keypair) {
427 423
         $c = self::secretbox(
428 424
             $plaintext,
429 425
             $nonce,
@@ -446,8 +442,7 @@  discard block
 block discarded – undo
446 442
      * @throws SodiumException
447 443
      * @throws TypeError
448 444
      */
449
-    public static function box_seal($message, $publicKey)
450
-    {
445
+    public static function box_seal($message, $publicKey) {
451 446
         /** @var string $ephemeralKeypair */
452 447
         $ephemeralKeypair = self::box_keypair();
453 448
 
@@ -492,8 +487,7 @@  discard block
 block discarded – undo
492 487
      * @throws SodiumException
493 488
      * @throws TypeError
494 489
      */
495
-    public static function box_seal_open($message, $keypair)
496
-    {
490
+    public static function box_seal_open($message, $keypair) {
497 491
         /** @var string $ephemeralPK */
498 492
         $ephemeralPK = ParagonIE_Sodium_Core_Util::substr($message, 0, 32);
499 493
 
@@ -541,8 +535,7 @@  discard block
 block discarded – undo
541 535
      * @throws SodiumException
542 536
      * @throws TypeError
543 537
      */
544
-    public static function box_beforenm($sk, $pk)
545
-    {
538
+    public static function box_beforenm($sk, $pk) {
546 539
         return ParagonIE_Sodium_Core_HSalsa20::hsalsa20(
547 540
             str_repeat("\x00", 16),
548 541
             self::scalarmult($sk, $pk)
@@ -557,8 +550,7 @@  discard block
 block discarded – undo
557 550
      * @throws SodiumException
558 551
      * @throws TypeError
559 552
      */
560
-    public static function box_keypair()
561
-    {
553
+    public static function box_keypair() {
562 554
         $sKey = random_bytes(32);
563 555
         $pKey = self::scalarmult_base($sKey);
564 556
         return $sKey . $pKey;
@@ -570,8 +562,7 @@  discard block
 block discarded – undo
570 562
      * @throws SodiumException
571 563
      * @throws TypeError
572 564
      */
573
-    public static function box_seed_keypair($seed)
574
-    {
565
+    public static function box_seed_keypair($seed) {
575 566
         $sKey = ParagonIE_Sodium_Core_Util::substr(
576 567
             hash('sha512', $seed, true),
577 568
             0,
@@ -589,8 +580,7 @@  discard block
 block discarded – undo
589 580
      * @return string
590 581
      * @throws TypeError
591 582
      */
592
-    public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey)
593
-    {
583
+    public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) {
594 584
         return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) .
595 585
             ParagonIE_Sodium_Core_Util::substr($pKey, 0, 32);
596 586
     }
@@ -603,8 +593,7 @@  discard block
 block discarded – undo
603 593
      * @throws RangeException
604 594
      * @throws TypeError
605 595
      */
606
-    public static function box_secretkey($keypair)
607
-    {
596
+    public static function box_secretkey($keypair) {
608 597
         if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) {
609 598
             throw new RangeException(
610 599
                 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
@@ -621,8 +610,7 @@  discard block
 block discarded – undo
621 610
      * @throws RangeException
622 611
      * @throws TypeError
623 612
      */
624
-    public static function box_publickey($keypair)
625
-    {
613
+    public static function box_publickey($keypair) {
626 614
         if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
627 615
             throw new RangeException(
628 616
                 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
@@ -640,8 +628,7 @@  discard block
 block discarded – undo
640 628
      * @throws SodiumException
641 629
      * @throws TypeError
642 630
      */
643
-    public static function box_publickey_from_secretkey($sKey)
644
-    {
631
+    public static function box_publickey_from_secretkey($sKey) {
645 632
         if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) {
646 633
             throw new RangeException(
647 634
                 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.'
@@ -662,8 +649,7 @@  discard block
 block discarded – undo
662 649
      * @throws SodiumException
663 650
      * @throws TypeError
664 651
      */
665
-    public static function box_open($ciphertext, $nonce, $keypair)
666
-    {
652
+    public static function box_open($ciphertext, $nonce, $keypair) {
667 653
         return self::secretbox_open(
668 654
             $ciphertext,
669 655
             $nonce,
@@ -687,8 +673,7 @@  discard block
 block discarded – undo
687 673
      * @throws SodiumException
688 674
      * @throws TypeError
689 675
      */
690
-    public static function generichash($message, $key = '', $outlen = 32)
691
-    {
676
+    public static function generichash($message, $key = '', $outlen = 32) {
692 677
         // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
693 678
         ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
694 679
 
@@ -728,8 +713,7 @@  discard block
 block discarded – undo
728 713
      * @throws SodiumException
729 714
      * @throws TypeError
730 715
      */
731
-    public static function generichash_final($ctx, $outlen = 32)
732
-    {
716
+    public static function generichash_final($ctx, $outlen = 32) {
733 717
         if (!is_string($ctx)) {
734 718
             throw new TypeError('Context must be a string');
735 719
         }
@@ -758,8 +742,7 @@  discard block
 block discarded – undo
758 742
      * @throws SodiumException
759 743
      * @throws TypeError
760 744
      */
761
-    public static function generichash_init($key = '', $outputLength = 32)
762
-    {
745
+    public static function generichash_init($key = '', $outputLength = 32) {
763 746
         // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
764 747
         ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
765 748
 
@@ -835,8 +818,7 @@  discard block
 block discarded – undo
835 818
      * @throws SodiumException
836 819
      * @throws TypeError
837 820
      */
838
-    public static function generichash_update($ctx, $message)
839
-    {
821
+    public static function generichash_update($ctx, $message) {
840 822
         // This ensures that ParagonIE_Sodium_Core_BLAKE2b::$iv is initialized
841 823
         ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor();
842 824
 
@@ -864,8 +846,7 @@  discard block
 block discarded – undo
864 846
      * @throws SodiumException
865 847
      * @throws TypeError
866 848
      */
867
-    public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk)
868
-    {
849
+    public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) {
869 850
         return ParagonIE_Sodium_Compat::crypto_generichash(
870 851
             ParagonIE_Sodium_Compat::crypto_scalarmult($my_sk, $their_pk) .
871 852
             $client_pk .
@@ -885,8 +866,7 @@  discard block
 block discarded – undo
885 866
      * @throws SodiumException
886 867
      * @throws TypeError
887 868
      */
888
-    public static function scalarmult($sKey, $pKey)
889
-    {
869
+    public static function scalarmult($sKey, $pKey) {
890 870
         $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey);
891 871
         self::scalarmult_throw_if_zero($q);
892 872
         return $q;
@@ -902,8 +882,7 @@  discard block
 block discarded – undo
902 882
      * @throws SodiumException
903 883
      * @throws TypeError
904 884
      */
905
-    public static function scalarmult_base($secret)
906
-    {
885
+    public static function scalarmult_base($secret) {
907 886
         $q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret);
908 887
         self::scalarmult_throw_if_zero($q);
909 888
         return $q;
@@ -917,8 +896,7 @@  discard block
 block discarded – undo
917 896
      * @throws SodiumException
918 897
      * @throws TypeError
919 898
      */
920
-    protected static function scalarmult_throw_if_zero($q)
921
-    {
899
+    protected static function scalarmult_throw_if_zero($q) {
922 900
         $d = 0;
923 901
         for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) {
924 902
             $d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]);
@@ -942,8 +920,7 @@  discard block
 block discarded – undo
942 920
      * @throws SodiumException
943 921
      * @throws TypeError
944 922
      */
945
-    public static function secretbox($plaintext, $nonce, $key)
946
-    {
923
+    public static function secretbox($plaintext, $nonce, $key) {
947 924
         /** @var string $subkey */
948 925
         $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
949 926
 
@@ -1017,8 +994,7 @@  discard block
 block discarded – undo
1017 994
      * @throws SodiumException
1018 995
      * @throws TypeError
1019 996
      */
1020
-    public static function secretbox_open($ciphertext, $nonce, $key)
1021
-    {
997
+    public static function secretbox_open($ciphertext, $nonce, $key) {
1022 998
         /** @var string $mac */
1023 999
         $mac = ParagonIE_Sodium_Core_Util::substr(
1024 1000
             $ciphertext,
@@ -1090,8 +1066,7 @@  discard block
 block discarded – undo
1090 1066
      * @throws SodiumException
1091 1067
      * @throws TypeError
1092 1068
      */
1093
-    public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key)
1094
-    {
1069
+    public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) {
1095 1070
         /** @var string $subkey */
1096 1071
         $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
1097 1072
             ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16),
@@ -1169,8 +1144,7 @@  discard block
 block discarded – undo
1169 1144
      * @throws SodiumException
1170 1145
      * @throws TypeError
1171 1146
      */
1172
-    public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
1173
-    {
1147
+    public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) {
1174 1148
         /** @var string $mac */
1175 1149
         $mac = ParagonIE_Sodium_Core_Util::substr(
1176 1150
             $ciphertext,
@@ -1238,8 +1212,7 @@  discard block
 block discarded – undo
1238 1212
      * @throws Exception
1239 1213
      * @throws SodiumException
1240 1214
      */
1241
-    public static function secretstream_xchacha20poly1305_init_push($key)
1242
-    {
1215
+    public static function secretstream_xchacha20poly1305_init_push($key) {
1243 1216
         # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES);
1244 1217
         $out = random_bytes(24);
1245 1218
 
@@ -1268,8 +1241,7 @@  discard block
 block discarded – undo
1268 1241
      * @return string Returns a state.
1269 1242
      * @throws Exception
1270 1243
      */
1271
-    public static function secretstream_xchacha20poly1305_init_pull($key, $header)
1272
-    {
1244
+    public static function secretstream_xchacha20poly1305_init_pull($key, $header) {
1273 1245
         # crypto_core_hchacha20(state->k, in, k, NULL);
1274 1246
         $subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20(
1275 1247
             ParagonIE_Sodium_Core_Util::substr($header, 0, 16),
@@ -1295,8 +1267,7 @@  discard block
 block discarded – undo
1295 1267
      * @return string
1296 1268
      * @throws SodiumException
1297 1269
      */
1298
-    public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
1299
-    {
1270
+    public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0) {
1300 1271
         $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
1301 1272
         # crypto_onetimeauth_poly1305_state poly1305_state;
1302 1273
         # unsigned char                     block[64U];
@@ -1424,8 +1395,7 @@  discard block
 block discarded – undo
1424 1395
      * @return bool|array{0: string, 1: int}
1425 1396
      * @throws SodiumException
1426 1397
      */
1427
-    public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '')
1428
-    {
1398
+    public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '') {
1429 1399
         $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
1430 1400
 
1431 1401
         $cipherlen = ParagonIE_Sodium_Core_Util::strlen($cipher);
@@ -1547,8 +1517,7 @@  discard block
 block discarded – undo
1547 1517
      * @return void
1548 1518
      * @throws SodiumException
1549 1519
      */
1550
-    public static function secretstream_xchacha20poly1305_rekey(&$state)
1551
-    {
1520
+    public static function secretstream_xchacha20poly1305_rekey(&$state) {
1552 1521
         $st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state);
1553 1522
         # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES +
1554 1523
         # crypto_secretstream_xchacha20poly1305_INONCEBYTES];
@@ -1599,8 +1568,7 @@  discard block
 block discarded – undo
1599 1568
      * @throws SodiumException
1600 1569
      * @throws TypeError
1601 1570
      */
1602
-    public static function sign_detached($message, $sk)
1603
-    {
1571
+    public static function sign_detached($message, $sk) {
1604 1572
         return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk);
1605 1573
     }
1606 1574
 
@@ -1615,8 +1583,7 @@  discard block
 block discarded – undo
1615 1583
      * @throws SodiumException
1616 1584
      * @throws TypeError
1617 1585
      */
1618
-    public static function sign($message, $sk)
1619
-    {
1586
+    public static function sign($message, $sk) {
1620 1587
         return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk);
1621 1588
     }
1622 1589
 
@@ -1631,8 +1598,7 @@  discard block
 block discarded – undo
1631 1598
      * @throws SodiumException
1632 1599
      * @throws TypeError
1633 1600
      */
1634
-    public static function sign_open($signedMessage, $pk)
1635
-    {
1601
+    public static function sign_open($signedMessage, $pk) {
1636 1602
         return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk);
1637 1603
     }
1638 1604
 
@@ -1648,8 +1614,7 @@  discard block
 block discarded – undo
1648 1614
      * @throws SodiumException
1649 1615
      * @throws TypeError
1650 1616
      */
1651
-    public static function sign_verify_detached($signature, $message, $pk)
1652
-    {
1617
+    public static function sign_verify_detached($signature, $message, $pk) {
1653 1618
         return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk);
1654 1619
     }
1655 1620
 }
Please login to merge, or discard this patch.
vendor/paragonie/sodium_compat/src/File.php 3 patches
Indentation   +1532 added lines, -1532 removed lines patch added patch discarded remove patch
@@ -1,1560 +1,1560 @@
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 if (class_exists('ParagonIE_Sodium_File', false)) {
4
-    return;
4
+	return;
5 5
 }
6 6
 /**
7 7
  * Class ParagonIE_Sodium_File
8 8
  */
9 9
 class ParagonIE_Sodium_File extends ParagonIE_Sodium_Core_Util
10 10
 {
11
-    /* PHP's default buffer size is 8192 for fread()/fwrite(). */
12
-    const BUFFER_SIZE = 8192;
13
-
14
-    /**
15
-     * Box a file (rather than a string). Uses less memory than
16
-     * ParagonIE_Sodium_Compat::crypto_box(), but produces
17
-     * the same result.
18
-     *
19
-     * @param string $inputFile  Absolute path to a file on the filesystem
20
-     * @param string $outputFile Absolute path to a file on the filesystem
21
-     * @param string $nonce      Number to be used only once
22
-     * @param string $keyPair    ECDH secret key and ECDH public key concatenated
23
-     *
24
-     * @return bool
25
-     * @throws SodiumException
26
-     * @throws TypeError
27
-     */
28
-    public static function box($inputFile, $outputFile, $nonce, $keyPair)
29
-    {
30
-        /* Type checks: */
31
-        if (!is_string($inputFile)) {
32
-            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
33
-        }
34
-        if (!is_string($outputFile)) {
35
-            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
36
-        }
37
-        if (!is_string($nonce)) {
38
-            throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
39
-        }
40
-
41
-        /* Input validation: */
42
-        if (!is_string($keyPair)) {
43
-            throw new TypeError('Argument 4 must be a string, ' . gettype($keyPair) . ' given.');
44
-        }
45
-        if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) {
46
-            throw new TypeError('Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes');
47
-        }
48
-        if (self::strlen($keyPair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
49
-            throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
50
-        }
51
-
52
-        /** @var int $size */
53
-        $size = filesize($inputFile);
54
-        if (!is_int($size)) {
55
-            throw new SodiumException('Could not obtain the file size');
56
-        }
57
-
58
-        /** @var resource $ifp */
59
-        $ifp = fopen($inputFile, 'rb');
60
-        if (!is_resource($ifp)) {
61
-            throw new SodiumException('Could not open input file for reading');
62
-        }
63
-
64
-        /** @var resource $ofp */
65
-        $ofp = fopen($outputFile, 'wb');
66
-        if (!is_resource($ofp)) {
67
-            fclose($ifp);
68
-            throw new SodiumException('Could not open output file for writing');
69
-        }
70
-
71
-        $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $keyPair);
72
-        fclose($ifp);
73
-        fclose($ofp);
74
-        return $res;
75
-    }
76
-
77
-    /**
78
-     * Open a boxed file (rather than a string). Uses less memory than
79
-     * ParagonIE_Sodium_Compat::crypto_box_open(), but produces
80
-     * the same result.
81
-     *
82
-     * Warning: Does not protect against TOCTOU attacks. You should
83
-     * just load the file into memory and use crypto_box_open() if
84
-     * you are worried about those.
85
-     *
86
-     * @param string $inputFile
87
-     * @param string $outputFile
88
-     * @param string $nonce
89
-     * @param string $keypair
90
-     * @return bool
91
-     * @throws SodiumException
92
-     * @throws TypeError
93
-     */
94
-    public static function box_open($inputFile, $outputFile, $nonce, $keypair)
95
-    {
96
-        /* Type checks: */
97
-        if (!is_string($inputFile)) {
98
-            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
99
-        }
100
-        if (!is_string($outputFile)) {
101
-            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
102
-        }
103
-        if (!is_string($nonce)) {
104
-            throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
105
-        }
106
-        if (!is_string($keypair)) {
107
-            throw new TypeError('Argument 4 must be a string, ' . gettype($keypair) . ' given.');
108
-        }
109
-
110
-        /* Input validation: */
111
-        if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) {
112
-            throw new TypeError('Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes');
113
-        }
114
-        if (self::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
115
-            throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
116
-        }
117
-
118
-        /** @var int $size */
119
-        $size = filesize($inputFile);
120
-        if (!is_int($size)) {
121
-            throw new SodiumException('Could not obtain the file size');
122
-        }
123
-
124
-        /** @var resource $ifp */
125
-        $ifp = fopen($inputFile, 'rb');
126
-        if (!is_resource($ifp)) {
127
-            throw new SodiumException('Could not open input file for reading');
128
-        }
129
-
130
-        /** @var resource $ofp */
131
-        $ofp = fopen($outputFile, 'wb');
132
-        if (!is_resource($ofp)) {
133
-            fclose($ifp);
134
-            throw new SodiumException('Could not open output file for writing');
135
-        }
136
-
137
-        $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair);
138
-        fclose($ifp);
139
-        fclose($ofp);
140
-        try {
141
-            ParagonIE_Sodium_Compat::memzero($nonce);
142
-            ParagonIE_Sodium_Compat::memzero($ephKeypair);
143
-        } catch (SodiumException $ex) {
144
-            if (isset($ephKeypair)) {
145
-                unset($ephKeypair);
146
-            }
147
-        }
148
-        return $res;
149
-    }
150
-
151
-    /**
152
-     * Seal a file (rather than a string). Uses less memory than
153
-     * ParagonIE_Sodium_Compat::crypto_box_seal(), but produces
154
-     * the same result.
155
-     *
156
-     * @param string $inputFile  Absolute path to a file on the filesystem
157
-     * @param string $outputFile Absolute path to a file on the filesystem
158
-     * @param string $publicKey  ECDH public key
159
-     *
160
-     * @return bool
161
-     * @throws SodiumException
162
-     * @throws TypeError
163
-     */
164
-    public static function box_seal($inputFile, $outputFile, $publicKey)
165
-    {
166
-        /* Type checks: */
167
-        if (!is_string($inputFile)) {
168
-            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
169
-        }
170
-        if (!is_string($outputFile)) {
171
-            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
172
-        }
173
-        if (!is_string($publicKey)) {
174
-            throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.');
175
-        }
176
-
177
-        /* Input validation: */
178
-        if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
179
-            throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes');
180
-        }
181
-
182
-        /** @var int $size */
183
-        $size = filesize($inputFile);
184
-        if (!is_int($size)) {
185
-            throw new SodiumException('Could not obtain the file size');
186
-        }
187
-
188
-        /** @var resource $ifp */
189
-        $ifp = fopen($inputFile, 'rb');
190
-        if (!is_resource($ifp)) {
191
-            throw new SodiumException('Could not open input file for reading');
192
-        }
193
-
194
-        /** @var resource $ofp */
195
-        $ofp = fopen($outputFile, 'wb');
196
-        if (!is_resource($ofp)) {
197
-            fclose($ifp);
198
-            throw new SodiumException('Could not open output file for writing');
199
-        }
200
-
201
-        /** @var string $ephKeypair */
202
-        $ephKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair();
203
-
204
-        /** @var string $msgKeypair */
205
-        $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey(
206
-            ParagonIE_Sodium_Compat::crypto_box_secretkey($ephKeypair),
207
-            $publicKey
208
-        );
209
-
210
-        /** @var string $ephemeralPK */
211
-        $ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey($ephKeypair);
212
-
213
-        /** @var string $nonce */
214
-        $nonce = ParagonIE_Sodium_Compat::crypto_generichash(
215
-            $ephemeralPK . $publicKey,
216
-            '',
217
-            24
218
-        );
219
-
220
-        /** @var int $firstWrite */
221
-        $firstWrite = fwrite(
222
-            $ofp,
223
-            $ephemeralPK,
224
-            ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES
225
-        );
226
-        if (!is_int($firstWrite)) {
227
-            fclose($ifp);
228
-            fclose($ofp);
229
-            ParagonIE_Sodium_Compat::memzero($ephKeypair);
230
-            throw new SodiumException('Could not write to output file');
231
-        }
232
-        if ($firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
233
-            ParagonIE_Sodium_Compat::memzero($ephKeypair);
234
-            fclose($ifp);
235
-            fclose($ofp);
236
-            throw new SodiumException('Error writing public key to output file');
237
-        }
238
-
239
-        $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $msgKeypair);
240
-        fclose($ifp);
241
-        fclose($ofp);
242
-        try {
243
-            ParagonIE_Sodium_Compat::memzero($nonce);
244
-            ParagonIE_Sodium_Compat::memzero($ephKeypair);
245
-        } catch (SodiumException $ex) {
246
-            /** @psalm-suppress PossiblyUndefinedVariable */
247
-            unset($ephKeypair);
248
-        }
249
-        return $res;
250
-    }
251
-
252
-    /**
253
-     * Open a sealed file (rather than a string). Uses less memory than
254
-     * ParagonIE_Sodium_Compat::crypto_box_seal_open(), but produces
255
-     * the same result.
256
-     *
257
-     * Warning: Does not protect against TOCTOU attacks. You should
258
-     * just load the file into memory and use crypto_box_seal_open() if
259
-     * you are worried about those.
260
-     *
261
-     * @param string $inputFile
262
-     * @param string $outputFile
263
-     * @param string $ecdhKeypair
264
-     * @return bool
265
-     * @throws SodiumException
266
-     * @throws TypeError
267
-     */
268
-    public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair)
269
-    {
270
-        /* Type checks: */
271
-        if (!is_string($inputFile)) {
272
-            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
273
-        }
274
-        if (!is_string($outputFile)) {
275
-            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
276
-        }
277
-        if (!is_string($ecdhKeypair)) {
278
-            throw new TypeError('Argument 3 must be a string, ' . gettype($ecdhKeypair) . ' given.');
279
-        }
280
-
281
-        /* Input validation: */
282
-        if (self::strlen($ecdhKeypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
283
-            throw new TypeError('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
284
-        }
285
-
286
-        $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair);
287
-
288
-        /** @var int $size */
289
-        $size = filesize($inputFile);
290
-        if (!is_int($size)) {
291
-            throw new SodiumException('Could not obtain the file size');
292
-        }
293
-
294
-        /** @var resource $ifp */
295
-        $ifp = fopen($inputFile, 'rb');
296
-        if (!is_resource($ifp)) {
297
-            throw new SodiumException('Could not open input file for reading');
298
-        }
299
-
300
-        /** @var resource $ofp */
301
-        $ofp = fopen($outputFile, 'wb');
302
-        if (!is_resource($ofp)) {
303
-            fclose($ifp);
304
-            throw new SodiumException('Could not open output file for writing');
305
-        }
306
-
307
-        $ephemeralPK = fread($ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES);
308
-        if (!is_string($ephemeralPK)) {
309
-            throw new SodiumException('Could not read input file');
310
-        }
311
-        if (self::strlen($ephemeralPK) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
312
-            fclose($ifp);
313
-            fclose($ofp);
314
-            throw new SodiumException('Could not read public key from sealed file');
315
-        }
316
-
317
-        $nonce = ParagonIE_Sodium_Compat::crypto_generichash(
318
-            $ephemeralPK . $publicKey,
319
-            '',
320
-            24
321
-        );
322
-        $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey(
323
-            ParagonIE_Sodium_Compat::crypto_box_secretkey($ecdhKeypair),
324
-            $ephemeralPK
325
-        );
326
-
327
-        $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $msgKeypair);
328
-        fclose($ifp);
329
-        fclose($ofp);
330
-        try {
331
-            ParagonIE_Sodium_Compat::memzero($nonce);
332
-            ParagonIE_Sodium_Compat::memzero($ephKeypair);
333
-        } catch (SodiumException $ex) {
334
-            if (isset($ephKeypair)) {
335
-                unset($ephKeypair);
336
-            }
337
-        }
338
-        return $res;
339
-    }
340
-
341
-    /**
342
-     * Calculate the BLAKE2b hash of a file.
343
-     *
344
-     * @param string      $filePath     Absolute path to a file on the filesystem
345
-     * @param string|null $key          BLAKE2b key
346
-     * @param int         $outputLength Length of hash output
347
-     *
348
-     * @return string                   BLAKE2b hash
349
-     * @throws SodiumException
350
-     * @throws TypeError
351
-     * @psalm-suppress FailedTypeResolution
352
-     */
353
-    public static function generichash($filePath, $key = '', $outputLength = 32)
354
-    {
355
-        /* Type checks: */
356
-        if (!is_string($filePath)) {
357
-            throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.');
358
-        }
359
-        if (!is_string($key)) {
360
-            if (is_null($key)) {
361
-                $key = '';
362
-            } else {
363
-                throw new TypeError('Argument 2 must be a string, ' . gettype($key) . ' given.');
364
-            }
365
-        }
366
-        if (!is_int($outputLength)) {
367
-            if (!is_numeric($outputLength)) {
368
-                throw new TypeError('Argument 3 must be an integer, ' . gettype($outputLength) . ' given.');
369
-            }
370
-            $outputLength = (int) $outputLength;
371
-        }
372
-
373
-        /* Input validation: */
374
-        if (!empty($key)) {
375
-            if (self::strlen($key) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
376
-                throw new TypeError('Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes');
377
-            }
378
-            if (self::strlen($key) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
379
-                throw new TypeError('Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes');
380
-            }
381
-        }
382
-        if ($outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN) {
383
-            throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN');
384
-        }
385
-        if ($outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX) {
386
-            throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX');
387
-        }
388
-
389
-        /** @var int $size */
390
-        $size = filesize($filePath);
391
-        if (!is_int($size)) {
392
-            throw new SodiumException('Could not obtain the file size');
393
-        }
394
-
395
-        /** @var resource $fp */
396
-        $fp = fopen($filePath, 'rb');
397
-        if (!is_resource($fp)) {
398
-            throw new SodiumException('Could not open input file for reading');
399
-        }
400
-        $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outputLength);
401
-        while ($size > 0) {
402
-            $blockSize = $size > 64
403
-                ? 64
404
-                : $size;
405
-            $read = fread($fp, $blockSize);
406
-            if (!is_string($read)) {
407
-                throw new SodiumException('Could not read input file');
408
-            }
409
-            ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $read);
410
-            $size -= $blockSize;
411
-        }
412
-
413
-        fclose($fp);
414
-        return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength);
415
-    }
416
-
417
-    /**
418
-     * Encrypt a file (rather than a string). Uses less memory than
419
-     * ParagonIE_Sodium_Compat::crypto_secretbox(), but produces
420
-     * the same result.
421
-     *
422
-     * @param string $inputFile  Absolute path to a file on the filesystem
423
-     * @param string $outputFile Absolute path to a file on the filesystem
424
-     * @param string $nonce      Number to be used only once
425
-     * @param string $key        Encryption key
426
-     *
427
-     * @return bool
428
-     * @throws SodiumException
429
-     * @throws TypeError
430
-     */
431
-    public static function secretbox($inputFile, $outputFile, $nonce, $key)
432
-    {
433
-        /* Type checks: */
434
-        if (!is_string($inputFile)) {
435
-            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..');
436
-        }
437
-        if (!is_string($outputFile)) {
438
-            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
439
-        }
440
-        if (!is_string($nonce)) {
441
-            throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
442
-        }
443
-
444
-        /* Input validation: */
445
-        if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) {
446
-            throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes');
447
-        }
448
-        if (!is_string($key)) {
449
-            throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.');
450
-        }
451
-        if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) {
452
-            throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes');
453
-        }
454
-
455
-        /** @var int $size */
456
-        $size = filesize($inputFile);
457
-        if (!is_int($size)) {
458
-            throw new SodiumException('Could not obtain the file size');
459
-        }
460
-
461
-        /** @var resource $ifp */
462
-        $ifp = fopen($inputFile, 'rb');
463
-        if (!is_resource($ifp)) {
464
-            throw new SodiumException('Could not open input file for reading');
465
-        }
466
-
467
-        /** @var resource $ofp */
468
-        $ofp = fopen($outputFile, 'wb');
469
-        if (!is_resource($ofp)) {
470
-            fclose($ifp);
471
-            throw new SodiumException('Could not open output file for writing');
472
-        }
473
-
474
-        $res = self::secretbox_encrypt($ifp, $ofp, $size, $nonce, $key);
475
-        fclose($ifp);
476
-        fclose($ofp);
477
-        return $res;
478
-    }
479
-    /**
480
-     * Seal a file (rather than a string). Uses less memory than
481
-     * ParagonIE_Sodium_Compat::crypto_secretbox_open(), but produces
482
-     * the same result.
483
-     *
484
-     * Warning: Does not protect against TOCTOU attacks. You should
485
-     * just load the file into memory and use crypto_secretbox_open() if
486
-     * you are worried about those.
487
-     *
488
-     * @param string $inputFile
489
-     * @param string $outputFile
490
-     * @param string $nonce
491
-     * @param string $key
492
-     * @return bool
493
-     * @throws SodiumException
494
-     * @throws TypeError
495
-     */
496
-    public static function secretbox_open($inputFile, $outputFile, $nonce, $key)
497
-    {
498
-        /* Type checks: */
499
-        if (!is_string($inputFile)) {
500
-            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
501
-        }
502
-        if (!is_string($outputFile)) {
503
-            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
504
-        }
505
-        if (!is_string($nonce)) {
506
-            throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
507
-        }
508
-        if (!is_string($key)) {
509
-            throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.');
510
-        }
511
-
512
-        /* Input validation: */
513
-        if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) {
514
-            throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes');
515
-        }
516
-        if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) {
517
-            throw new TypeError('Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes');
518
-        }
519
-
520
-        /** @var int $size */
521
-        $size = filesize($inputFile);
522
-        if (!is_int($size)) {
523
-            throw new SodiumException('Could not obtain the file size');
524
-        }
525
-
526
-        /** @var resource $ifp */
527
-        $ifp = fopen($inputFile, 'rb');
528
-        if (!is_resource($ifp)) {
529
-            throw new SodiumException('Could not open input file for reading');
530
-        }
531
-
532
-        /** @var resource $ofp */
533
-        $ofp = fopen($outputFile, 'wb');
534
-        if (!is_resource($ofp)) {
535
-            fclose($ifp);
536
-            throw new SodiumException('Could not open output file for writing');
537
-        }
538
-
539
-        $res = self::secretbox_decrypt($ifp, $ofp, $size, $nonce, $key);
540
-        fclose($ifp);
541
-        fclose($ofp);
542
-        try {
543
-            ParagonIE_Sodium_Compat::memzero($key);
544
-        } catch (SodiumException $ex) {
545
-            /** @psalm-suppress PossiblyUndefinedVariable */
546
-            unset($key);
547
-        }
548
-        return $res;
549
-    }
550
-
551
-    /**
552
-     * Sign a file (rather than a string). Uses less memory than
553
-     * ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces
554
-     * the same result.
555
-     *
556
-     * @param string $filePath  Absolute path to a file on the filesystem
557
-     * @param string $secretKey Secret signing key
558
-     *
559
-     * @return string           Ed25519 signature
560
-     * @throws SodiumException
561
-     * @throws TypeError
562
-     */
563
-    public static function sign($filePath, $secretKey)
564
-    {
565
-        /* Type checks: */
566
-        if (!is_string($filePath)) {
567
-            throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.');
568
-        }
569
-        if (!is_string($secretKey)) {
570
-            throw new TypeError('Argument 2 must be a string, ' . gettype($secretKey) . ' given.');
571
-        }
572
-
573
-        /* Input validation: */
574
-        if (self::strlen($secretKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES) {
575
-            throw new TypeError('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes');
576
-        }
577
-        if (PHP_INT_SIZE === 4) {
578
-            return self::sign_core32($filePath, $secretKey);
579
-        }
580
-
581
-        /** @var int $size */
582
-        $size = filesize($filePath);
583
-        if (!is_int($size)) {
584
-            throw new SodiumException('Could not obtain the file size');
585
-        }
586
-
587
-        /** @var resource $fp */
588
-        $fp = fopen($filePath, 'rb');
589
-        if (!is_resource($fp)) {
590
-            throw new SodiumException('Could not open input file for reading');
591
-        }
592
-
593
-        /** @var string $az */
594
-        $az = hash('sha512', self::substr($secretKey, 0, 32), true);
595
-
596
-        $az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
597
-        $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
598
-
599
-        $hs = hash_init('sha512');
600
-        self::hash_update($hs, self::substr($az, 32, 32));
601
-        /** @var resource $hs */
602
-        $hs = self::updateHashWithFile($hs, $fp, $size);
603
-
604
-        /** @var string $nonceHash */
605
-        $nonceHash = hash_final($hs, true);
606
-
607
-        /** @var string $pk */
608
-        $pk = self::substr($secretKey, 32, 32);
609
-
610
-        /** @var string $nonce */
611
-        $nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
612
-
613
-        /** @var string $sig */
614
-        $sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes(
615
-            ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base($nonce)
616
-        );
617
-
618
-        $hs = hash_init('sha512');
619
-        self::hash_update($hs, self::substr($sig, 0, 32));
620
-        self::hash_update($hs, self::substr($pk, 0, 32));
621
-        /** @var resource $hs */
622
-        $hs = self::updateHashWithFile($hs, $fp, $size);
623
-
624
-        /** @var string $hramHash */
625
-        $hramHash = hash_final($hs, true);
626
-
627
-        /** @var string $hram */
628
-        $hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hramHash);
629
-
630
-        /** @var string $sigAfter */
631
-        $sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd($hram, $az, $nonce);
632
-
633
-        /** @var string $sig */
634
-        $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
635
-
636
-        try {
637
-            ParagonIE_Sodium_Compat::memzero($az);
638
-        } catch (SodiumException $ex) {
639
-            $az = null;
640
-        }
641
-        fclose($fp);
642
-        return $sig;
643
-    }
644
-
645
-    /**
646
-     * Verify a file (rather than a string). Uses less memory than
647
-     * ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but
648
-     * produces the same result.
649
-     *
650
-     * @param string $sig       Ed25519 signature
651
-     * @param string $filePath  Absolute path to a file on the filesystem
652
-     * @param string $publicKey Signing public key
653
-     *
654
-     * @return bool
655
-     * @throws SodiumException
656
-     * @throws TypeError
657
-     * @throws Exception
658
-     */
659
-    public static function verify($sig, $filePath, $publicKey)
660
-    {
661
-        /* Type checks: */
662
-        if (!is_string($sig)) {
663
-            throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.');
664
-        }
665
-        if (!is_string($filePath)) {
666
-            throw new TypeError('Argument 2 must be a string, ' . gettype($filePath) . ' given.');
667
-        }
668
-        if (!is_string($publicKey)) {
669
-            throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.');
670
-        }
671
-
672
-        /* Input validation: */
673
-        if (self::strlen($sig) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES) {
674
-            throw new TypeError('Argument 1 must be CRYPTO_SIGN_BYTES bytes');
675
-        }
676
-        if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES) {
677
-            throw new TypeError('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes');
678
-        }
679
-        if (self::strlen($sig) < 64) {
680
-            throw new SodiumException('Signature is too short');
681
-        }
682
-
683
-        if (PHP_INT_SIZE === 4) {
684
-            return self::verify_core32($sig, $filePath, $publicKey);
685
-        }
686
-
687
-        /* Security checks */
688
-        if (
689
-            (ParagonIE_Sodium_Core_Ed25519::chrToInt($sig[63]) & 240)
690
-                &&
691
-            ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))
692
-        ) {
693
-            throw new SodiumException('S < L - Invalid signature');
694
-        }
695
-        if (ParagonIE_Sodium_Core_Ed25519::small_order($sig)) {
696
-            throw new SodiumException('Signature is on too small of an order');
697
-        }
698
-        if ((self::chrToInt($sig[63]) & 224) !== 0) {
699
-            throw new SodiumException('Invalid signature');
700
-        }
701
-        $d = 0;
702
-        for ($i = 0; $i < 32; ++$i) {
703
-            $d |= self::chrToInt($publicKey[$i]);
704
-        }
705
-        if ($d === 0) {
706
-            throw new SodiumException('All zero public key');
707
-        }
708
-
709
-        /** @var int $size */
710
-        $size = filesize($filePath);
711
-        if (!is_int($size)) {
712
-            throw new SodiumException('Could not obtain the file size');
713
-        }
714
-
715
-        /** @var resource $fp */
716
-        $fp = fopen($filePath, 'rb');
717
-        if (!is_resource($fp)) {
718
-            throw new SodiumException('Could not open input file for reading');
719
-        }
720
-
721
-        /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
722
-        $orig = ParagonIE_Sodium_Compat::$fastMult;
723
-
724
-        // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification.
725
-        ParagonIE_Sodium_Compat::$fastMult = true;
726
-
727
-        /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */
728
-        $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime($publicKey);
729
-
730
-        $hs = hash_init('sha512');
731
-        self::hash_update($hs, self::substr($sig, 0, 32));
732
-        self::hash_update($hs, self::substr($publicKey, 0, 32));
733
-        /** @var resource $hs */
734
-        $hs = self::updateHashWithFile($hs, $fp, $size);
735
-        /** @var string $hDigest */
736
-        $hDigest = hash_final($hs, true);
737
-
738
-        /** @var string $h */
739
-        $h = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32);
740
-
741
-        /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */
742
-        $R = ParagonIE_Sodium_Core_Ed25519::ge_double_scalarmult_vartime(
743
-            $h,
744
-            $A,
745
-            self::substr($sig, 32)
746
-        );
747
-
748
-        /** @var string $rcheck */
749
-        $rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes($R);
750
-
751
-        // Close the file handle
752
-        fclose($fp);
753
-
754
-        // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
755
-        ParagonIE_Sodium_Compat::$fastMult = $orig;
756
-        return self::verify_32($rcheck, self::substr($sig, 0, 32));
757
-    }
758
-
759
-    /**
760
-     * @param resource $ifp
761
-     * @param resource $ofp
762
-     * @param int      $mlen
763
-     * @param string   $nonce
764
-     * @param string   $boxKeypair
765
-     * @return bool
766
-     * @throws SodiumException
767
-     * @throws TypeError
768
-     */
769
-    protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair)
770
-    {
771
-        if (PHP_INT_SIZE === 4) {
772
-            return self::secretbox_encrypt(
773
-                $ifp,
774
-                $ofp,
775
-                $mlen,
776
-                $nonce,
777
-                ParagonIE_Sodium_Crypto32::box_beforenm(
778
-                    ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair),
779
-                    ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair)
780
-                )
781
-            );
782
-        }
783
-        return self::secretbox_encrypt(
784
-            $ifp,
785
-            $ofp,
786
-            $mlen,
787
-            $nonce,
788
-            ParagonIE_Sodium_Crypto::box_beforenm(
789
-                ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair),
790
-                ParagonIE_Sodium_Crypto::box_publickey($boxKeypair)
791
-            )
792
-        );
793
-    }
794
-
795
-
796
-    /**
797
-     * @param resource $ifp
798
-     * @param resource $ofp
799
-     * @param int      $mlen
800
-     * @param string   $nonce
801
-     * @param string   $boxKeypair
802
-     * @return bool
803
-     * @throws SodiumException
804
-     * @throws TypeError
805
-     */
806
-    protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair)
807
-    {
808
-        if (PHP_INT_SIZE === 4) {
809
-            return self::secretbox_decrypt(
810
-                $ifp,
811
-                $ofp,
812
-                $mlen,
813
-                $nonce,
814
-                ParagonIE_Sodium_Crypto32::box_beforenm(
815
-                    ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair),
816
-                    ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair)
817
-                )
818
-            );
819
-        }
820
-        return self::secretbox_decrypt(
821
-            $ifp,
822
-            $ofp,
823
-            $mlen,
824
-            $nonce,
825
-            ParagonIE_Sodium_Crypto::box_beforenm(
826
-                ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair),
827
-                ParagonIE_Sodium_Crypto::box_publickey($boxKeypair)
828
-            )
829
-        );
830
-    }
831
-
832
-    /**
833
-     * Encrypt a file
834
-     *
835
-     * @param resource $ifp
836
-     * @param resource $ofp
837
-     * @param int $mlen
838
-     * @param string $nonce
839
-     * @param string $key
840
-     * @return bool
841
-     * @throws SodiumException
842
-     * @throws TypeError
843
-     */
844
-    protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key)
845
-    {
846
-        if (PHP_INT_SIZE === 4) {
847
-            return self::secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key);
848
-        }
849
-
850
-        $plaintext = fread($ifp, 32);
851
-        if (!is_string($plaintext)) {
852
-            throw new SodiumException('Could not read input file');
853
-        }
854
-        $first32 = self::ftell($ifp);
855
-
856
-        /** @var string $subkey */
857
-        $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
858
-
859
-        /** @var string $realNonce */
860
-        $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
861
-
862
-        /** @var string $block0 */
863
-        $block0 = str_repeat("\x00", 32);
864
-
865
-        /** @var int $mlen - Length of the plaintext message */
866
-        $mlen0 = $mlen;
867
-        if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) {
868
-            $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES;
869
-        }
870
-        $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0);
871
-
872
-        /** @var string $block0 */
873
-        $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor(
874
-            $block0,
875
-            $realNonce,
876
-            $subkey
877
-        );
878
-
879
-        $state = new ParagonIE_Sodium_Core_Poly1305_State(
880
-            ParagonIE_Sodium_Core_Util::substr(
881
-                $block0,
882
-                0,
883
-                ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES
884
-            )
885
-        );
886
-
887
-        // Pre-write 16 blank bytes for the Poly1305 tag
888
-        $start = self::ftell($ofp);
889
-        fwrite($ofp, str_repeat("\x00", 16));
890
-
891
-        /** @var string $c */
892
-        $cBlock = ParagonIE_Sodium_Core_Util::substr(
893
-            $block0,
894
-            ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES
895
-        );
896
-        $state->update($cBlock);
897
-        fwrite($ofp, $cBlock);
898
-        $mlen -= 32;
899
-
900
-        /** @var int $iter */
901
-        $iter = 1;
902
-
903
-        /** @var int $incr */
904
-        $incr = self::BUFFER_SIZE >> 6;
905
-
906
-        /*
11
+	/* PHP's default buffer size is 8192 for fread()/fwrite(). */
12
+	const BUFFER_SIZE = 8192;
13
+
14
+	/**
15
+	 * Box a file (rather than a string). Uses less memory than
16
+	 * ParagonIE_Sodium_Compat::crypto_box(), but produces
17
+	 * the same result.
18
+	 *
19
+	 * @param string $inputFile  Absolute path to a file on the filesystem
20
+	 * @param string $outputFile Absolute path to a file on the filesystem
21
+	 * @param string $nonce      Number to be used only once
22
+	 * @param string $keyPair    ECDH secret key and ECDH public key concatenated
23
+	 *
24
+	 * @return bool
25
+	 * @throws SodiumException
26
+	 * @throws TypeError
27
+	 */
28
+	public static function box($inputFile, $outputFile, $nonce, $keyPair)
29
+	{
30
+		/* Type checks: */
31
+		if (!is_string($inputFile)) {
32
+			throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
33
+		}
34
+		if (!is_string($outputFile)) {
35
+			throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
36
+		}
37
+		if (!is_string($nonce)) {
38
+			throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
39
+		}
40
+
41
+		/* Input validation: */
42
+		if (!is_string($keyPair)) {
43
+			throw new TypeError('Argument 4 must be a string, ' . gettype($keyPair) . ' given.');
44
+		}
45
+		if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) {
46
+			throw new TypeError('Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes');
47
+		}
48
+		if (self::strlen($keyPair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
49
+			throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
50
+		}
51
+
52
+		/** @var int $size */
53
+		$size = filesize($inputFile);
54
+		if (!is_int($size)) {
55
+			throw new SodiumException('Could not obtain the file size');
56
+		}
57
+
58
+		/** @var resource $ifp */
59
+		$ifp = fopen($inputFile, 'rb');
60
+		if (!is_resource($ifp)) {
61
+			throw new SodiumException('Could not open input file for reading');
62
+		}
63
+
64
+		/** @var resource $ofp */
65
+		$ofp = fopen($outputFile, 'wb');
66
+		if (!is_resource($ofp)) {
67
+			fclose($ifp);
68
+			throw new SodiumException('Could not open output file for writing');
69
+		}
70
+
71
+		$res = self::box_encrypt($ifp, $ofp, $size, $nonce, $keyPair);
72
+		fclose($ifp);
73
+		fclose($ofp);
74
+		return $res;
75
+	}
76
+
77
+	/**
78
+	 * Open a boxed file (rather than a string). Uses less memory than
79
+	 * ParagonIE_Sodium_Compat::crypto_box_open(), but produces
80
+	 * the same result.
81
+	 *
82
+	 * Warning: Does not protect against TOCTOU attacks. You should
83
+	 * just load the file into memory and use crypto_box_open() if
84
+	 * you are worried about those.
85
+	 *
86
+	 * @param string $inputFile
87
+	 * @param string $outputFile
88
+	 * @param string $nonce
89
+	 * @param string $keypair
90
+	 * @return bool
91
+	 * @throws SodiumException
92
+	 * @throws TypeError
93
+	 */
94
+	public static function box_open($inputFile, $outputFile, $nonce, $keypair)
95
+	{
96
+		/* Type checks: */
97
+		if (!is_string($inputFile)) {
98
+			throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
99
+		}
100
+		if (!is_string($outputFile)) {
101
+			throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
102
+		}
103
+		if (!is_string($nonce)) {
104
+			throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
105
+		}
106
+		if (!is_string($keypair)) {
107
+			throw new TypeError('Argument 4 must be a string, ' . gettype($keypair) . ' given.');
108
+		}
109
+
110
+		/* Input validation: */
111
+		if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) {
112
+			throw new TypeError('Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes');
113
+		}
114
+		if (self::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
115
+			throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
116
+		}
117
+
118
+		/** @var int $size */
119
+		$size = filesize($inputFile);
120
+		if (!is_int($size)) {
121
+			throw new SodiumException('Could not obtain the file size');
122
+		}
123
+
124
+		/** @var resource $ifp */
125
+		$ifp = fopen($inputFile, 'rb');
126
+		if (!is_resource($ifp)) {
127
+			throw new SodiumException('Could not open input file for reading');
128
+		}
129
+
130
+		/** @var resource $ofp */
131
+		$ofp = fopen($outputFile, 'wb');
132
+		if (!is_resource($ofp)) {
133
+			fclose($ifp);
134
+			throw new SodiumException('Could not open output file for writing');
135
+		}
136
+
137
+		$res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair);
138
+		fclose($ifp);
139
+		fclose($ofp);
140
+		try {
141
+			ParagonIE_Sodium_Compat::memzero($nonce);
142
+			ParagonIE_Sodium_Compat::memzero($ephKeypair);
143
+		} catch (SodiumException $ex) {
144
+			if (isset($ephKeypair)) {
145
+				unset($ephKeypair);
146
+			}
147
+		}
148
+		return $res;
149
+	}
150
+
151
+	/**
152
+	 * Seal a file (rather than a string). Uses less memory than
153
+	 * ParagonIE_Sodium_Compat::crypto_box_seal(), but produces
154
+	 * the same result.
155
+	 *
156
+	 * @param string $inputFile  Absolute path to a file on the filesystem
157
+	 * @param string $outputFile Absolute path to a file on the filesystem
158
+	 * @param string $publicKey  ECDH public key
159
+	 *
160
+	 * @return bool
161
+	 * @throws SodiumException
162
+	 * @throws TypeError
163
+	 */
164
+	public static function box_seal($inputFile, $outputFile, $publicKey)
165
+	{
166
+		/* Type checks: */
167
+		if (!is_string($inputFile)) {
168
+			throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
169
+		}
170
+		if (!is_string($outputFile)) {
171
+			throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
172
+		}
173
+		if (!is_string($publicKey)) {
174
+			throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.');
175
+		}
176
+
177
+		/* Input validation: */
178
+		if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
179
+			throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes');
180
+		}
181
+
182
+		/** @var int $size */
183
+		$size = filesize($inputFile);
184
+		if (!is_int($size)) {
185
+			throw new SodiumException('Could not obtain the file size');
186
+		}
187
+
188
+		/** @var resource $ifp */
189
+		$ifp = fopen($inputFile, 'rb');
190
+		if (!is_resource($ifp)) {
191
+			throw new SodiumException('Could not open input file for reading');
192
+		}
193
+
194
+		/** @var resource $ofp */
195
+		$ofp = fopen($outputFile, 'wb');
196
+		if (!is_resource($ofp)) {
197
+			fclose($ifp);
198
+			throw new SodiumException('Could not open output file for writing');
199
+		}
200
+
201
+		/** @var string $ephKeypair */
202
+		$ephKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair();
203
+
204
+		/** @var string $msgKeypair */
205
+		$msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey(
206
+			ParagonIE_Sodium_Compat::crypto_box_secretkey($ephKeypair),
207
+			$publicKey
208
+		);
209
+
210
+		/** @var string $ephemeralPK */
211
+		$ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey($ephKeypair);
212
+
213
+		/** @var string $nonce */
214
+		$nonce = ParagonIE_Sodium_Compat::crypto_generichash(
215
+			$ephemeralPK . $publicKey,
216
+			'',
217
+			24
218
+		);
219
+
220
+		/** @var int $firstWrite */
221
+		$firstWrite = fwrite(
222
+			$ofp,
223
+			$ephemeralPK,
224
+			ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES
225
+		);
226
+		if (!is_int($firstWrite)) {
227
+			fclose($ifp);
228
+			fclose($ofp);
229
+			ParagonIE_Sodium_Compat::memzero($ephKeypair);
230
+			throw new SodiumException('Could not write to output file');
231
+		}
232
+		if ($firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
233
+			ParagonIE_Sodium_Compat::memzero($ephKeypair);
234
+			fclose($ifp);
235
+			fclose($ofp);
236
+			throw new SodiumException('Error writing public key to output file');
237
+		}
238
+
239
+		$res = self::box_encrypt($ifp, $ofp, $size, $nonce, $msgKeypair);
240
+		fclose($ifp);
241
+		fclose($ofp);
242
+		try {
243
+			ParagonIE_Sodium_Compat::memzero($nonce);
244
+			ParagonIE_Sodium_Compat::memzero($ephKeypair);
245
+		} catch (SodiumException $ex) {
246
+			/** @psalm-suppress PossiblyUndefinedVariable */
247
+			unset($ephKeypair);
248
+		}
249
+		return $res;
250
+	}
251
+
252
+	/**
253
+	 * Open a sealed file (rather than a string). Uses less memory than
254
+	 * ParagonIE_Sodium_Compat::crypto_box_seal_open(), but produces
255
+	 * the same result.
256
+	 *
257
+	 * Warning: Does not protect against TOCTOU attacks. You should
258
+	 * just load the file into memory and use crypto_box_seal_open() if
259
+	 * you are worried about those.
260
+	 *
261
+	 * @param string $inputFile
262
+	 * @param string $outputFile
263
+	 * @param string $ecdhKeypair
264
+	 * @return bool
265
+	 * @throws SodiumException
266
+	 * @throws TypeError
267
+	 */
268
+	public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair)
269
+	{
270
+		/* Type checks: */
271
+		if (!is_string($inputFile)) {
272
+			throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
273
+		}
274
+		if (!is_string($outputFile)) {
275
+			throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
276
+		}
277
+		if (!is_string($ecdhKeypair)) {
278
+			throw new TypeError('Argument 3 must be a string, ' . gettype($ecdhKeypair) . ' given.');
279
+		}
280
+
281
+		/* Input validation: */
282
+		if (self::strlen($ecdhKeypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
283
+			throw new TypeError('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
284
+		}
285
+
286
+		$publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair);
287
+
288
+		/** @var int $size */
289
+		$size = filesize($inputFile);
290
+		if (!is_int($size)) {
291
+			throw new SodiumException('Could not obtain the file size');
292
+		}
293
+
294
+		/** @var resource $ifp */
295
+		$ifp = fopen($inputFile, 'rb');
296
+		if (!is_resource($ifp)) {
297
+			throw new SodiumException('Could not open input file for reading');
298
+		}
299
+
300
+		/** @var resource $ofp */
301
+		$ofp = fopen($outputFile, 'wb');
302
+		if (!is_resource($ofp)) {
303
+			fclose($ifp);
304
+			throw new SodiumException('Could not open output file for writing');
305
+		}
306
+
307
+		$ephemeralPK = fread($ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES);
308
+		if (!is_string($ephemeralPK)) {
309
+			throw new SodiumException('Could not read input file');
310
+		}
311
+		if (self::strlen($ephemeralPK) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
312
+			fclose($ifp);
313
+			fclose($ofp);
314
+			throw new SodiumException('Could not read public key from sealed file');
315
+		}
316
+
317
+		$nonce = ParagonIE_Sodium_Compat::crypto_generichash(
318
+			$ephemeralPK . $publicKey,
319
+			'',
320
+			24
321
+		);
322
+		$msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey(
323
+			ParagonIE_Sodium_Compat::crypto_box_secretkey($ecdhKeypair),
324
+			$ephemeralPK
325
+		);
326
+
327
+		$res = self::box_decrypt($ifp, $ofp, $size, $nonce, $msgKeypair);
328
+		fclose($ifp);
329
+		fclose($ofp);
330
+		try {
331
+			ParagonIE_Sodium_Compat::memzero($nonce);
332
+			ParagonIE_Sodium_Compat::memzero($ephKeypair);
333
+		} catch (SodiumException $ex) {
334
+			if (isset($ephKeypair)) {
335
+				unset($ephKeypair);
336
+			}
337
+		}
338
+		return $res;
339
+	}
340
+
341
+	/**
342
+	 * Calculate the BLAKE2b hash of a file.
343
+	 *
344
+	 * @param string      $filePath     Absolute path to a file on the filesystem
345
+	 * @param string|null $key          BLAKE2b key
346
+	 * @param int         $outputLength Length of hash output
347
+	 *
348
+	 * @return string                   BLAKE2b hash
349
+	 * @throws SodiumException
350
+	 * @throws TypeError
351
+	 * @psalm-suppress FailedTypeResolution
352
+	 */
353
+	public static function generichash($filePath, $key = '', $outputLength = 32)
354
+	{
355
+		/* Type checks: */
356
+		if (!is_string($filePath)) {
357
+			throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.');
358
+		}
359
+		if (!is_string($key)) {
360
+			if (is_null($key)) {
361
+				$key = '';
362
+			} else {
363
+				throw new TypeError('Argument 2 must be a string, ' . gettype($key) . ' given.');
364
+			}
365
+		}
366
+		if (!is_int($outputLength)) {
367
+			if (!is_numeric($outputLength)) {
368
+				throw new TypeError('Argument 3 must be an integer, ' . gettype($outputLength) . ' given.');
369
+			}
370
+			$outputLength = (int) $outputLength;
371
+		}
372
+
373
+		/* Input validation: */
374
+		if (!empty($key)) {
375
+			if (self::strlen($key) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
376
+				throw new TypeError('Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes');
377
+			}
378
+			if (self::strlen($key) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
379
+				throw new TypeError('Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes');
380
+			}
381
+		}
382
+		if ($outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN) {
383
+			throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN');
384
+		}
385
+		if ($outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX) {
386
+			throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX');
387
+		}
388
+
389
+		/** @var int $size */
390
+		$size = filesize($filePath);
391
+		if (!is_int($size)) {
392
+			throw new SodiumException('Could not obtain the file size');
393
+		}
394
+
395
+		/** @var resource $fp */
396
+		$fp = fopen($filePath, 'rb');
397
+		if (!is_resource($fp)) {
398
+			throw new SodiumException('Could not open input file for reading');
399
+		}
400
+		$ctx = ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outputLength);
401
+		while ($size > 0) {
402
+			$blockSize = $size > 64
403
+				? 64
404
+				: $size;
405
+			$read = fread($fp, $blockSize);
406
+			if (!is_string($read)) {
407
+				throw new SodiumException('Could not read input file');
408
+			}
409
+			ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $read);
410
+			$size -= $blockSize;
411
+		}
412
+
413
+		fclose($fp);
414
+		return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength);
415
+	}
416
+
417
+	/**
418
+	 * Encrypt a file (rather than a string). Uses less memory than
419
+	 * ParagonIE_Sodium_Compat::crypto_secretbox(), but produces
420
+	 * the same result.
421
+	 *
422
+	 * @param string $inputFile  Absolute path to a file on the filesystem
423
+	 * @param string $outputFile Absolute path to a file on the filesystem
424
+	 * @param string $nonce      Number to be used only once
425
+	 * @param string $key        Encryption key
426
+	 *
427
+	 * @return bool
428
+	 * @throws SodiumException
429
+	 * @throws TypeError
430
+	 */
431
+	public static function secretbox($inputFile, $outputFile, $nonce, $key)
432
+	{
433
+		/* Type checks: */
434
+		if (!is_string($inputFile)) {
435
+			throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..');
436
+		}
437
+		if (!is_string($outputFile)) {
438
+			throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
439
+		}
440
+		if (!is_string($nonce)) {
441
+			throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
442
+		}
443
+
444
+		/* Input validation: */
445
+		if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) {
446
+			throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes');
447
+		}
448
+		if (!is_string($key)) {
449
+			throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.');
450
+		}
451
+		if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) {
452
+			throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes');
453
+		}
454
+
455
+		/** @var int $size */
456
+		$size = filesize($inputFile);
457
+		if (!is_int($size)) {
458
+			throw new SodiumException('Could not obtain the file size');
459
+		}
460
+
461
+		/** @var resource $ifp */
462
+		$ifp = fopen($inputFile, 'rb');
463
+		if (!is_resource($ifp)) {
464
+			throw new SodiumException('Could not open input file for reading');
465
+		}
466
+
467
+		/** @var resource $ofp */
468
+		$ofp = fopen($outputFile, 'wb');
469
+		if (!is_resource($ofp)) {
470
+			fclose($ifp);
471
+			throw new SodiumException('Could not open output file for writing');
472
+		}
473
+
474
+		$res = self::secretbox_encrypt($ifp, $ofp, $size, $nonce, $key);
475
+		fclose($ifp);
476
+		fclose($ofp);
477
+		return $res;
478
+	}
479
+	/**
480
+	 * Seal a file (rather than a string). Uses less memory than
481
+	 * ParagonIE_Sodium_Compat::crypto_secretbox_open(), but produces
482
+	 * the same result.
483
+	 *
484
+	 * Warning: Does not protect against TOCTOU attacks. You should
485
+	 * just load the file into memory and use crypto_secretbox_open() if
486
+	 * you are worried about those.
487
+	 *
488
+	 * @param string $inputFile
489
+	 * @param string $outputFile
490
+	 * @param string $nonce
491
+	 * @param string $key
492
+	 * @return bool
493
+	 * @throws SodiumException
494
+	 * @throws TypeError
495
+	 */
496
+	public static function secretbox_open($inputFile, $outputFile, $nonce, $key)
497
+	{
498
+		/* Type checks: */
499
+		if (!is_string($inputFile)) {
500
+			throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
501
+		}
502
+		if (!is_string($outputFile)) {
503
+			throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
504
+		}
505
+		if (!is_string($nonce)) {
506
+			throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
507
+		}
508
+		if (!is_string($key)) {
509
+			throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.');
510
+		}
511
+
512
+		/* Input validation: */
513
+		if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) {
514
+			throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes');
515
+		}
516
+		if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) {
517
+			throw new TypeError('Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes');
518
+		}
519
+
520
+		/** @var int $size */
521
+		$size = filesize($inputFile);
522
+		if (!is_int($size)) {
523
+			throw new SodiumException('Could not obtain the file size');
524
+		}
525
+
526
+		/** @var resource $ifp */
527
+		$ifp = fopen($inputFile, 'rb');
528
+		if (!is_resource($ifp)) {
529
+			throw new SodiumException('Could not open input file for reading');
530
+		}
531
+
532
+		/** @var resource $ofp */
533
+		$ofp = fopen($outputFile, 'wb');
534
+		if (!is_resource($ofp)) {
535
+			fclose($ifp);
536
+			throw new SodiumException('Could not open output file for writing');
537
+		}
538
+
539
+		$res = self::secretbox_decrypt($ifp, $ofp, $size, $nonce, $key);
540
+		fclose($ifp);
541
+		fclose($ofp);
542
+		try {
543
+			ParagonIE_Sodium_Compat::memzero($key);
544
+		} catch (SodiumException $ex) {
545
+			/** @psalm-suppress PossiblyUndefinedVariable */
546
+			unset($key);
547
+		}
548
+		return $res;
549
+	}
550
+
551
+	/**
552
+	 * Sign a file (rather than a string). Uses less memory than
553
+	 * ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces
554
+	 * the same result.
555
+	 *
556
+	 * @param string $filePath  Absolute path to a file on the filesystem
557
+	 * @param string $secretKey Secret signing key
558
+	 *
559
+	 * @return string           Ed25519 signature
560
+	 * @throws SodiumException
561
+	 * @throws TypeError
562
+	 */
563
+	public static function sign($filePath, $secretKey)
564
+	{
565
+		/* Type checks: */
566
+		if (!is_string($filePath)) {
567
+			throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.');
568
+		}
569
+		if (!is_string($secretKey)) {
570
+			throw new TypeError('Argument 2 must be a string, ' . gettype($secretKey) . ' given.');
571
+		}
572
+
573
+		/* Input validation: */
574
+		if (self::strlen($secretKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES) {
575
+			throw new TypeError('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes');
576
+		}
577
+		if (PHP_INT_SIZE === 4) {
578
+			return self::sign_core32($filePath, $secretKey);
579
+		}
580
+
581
+		/** @var int $size */
582
+		$size = filesize($filePath);
583
+		if (!is_int($size)) {
584
+			throw new SodiumException('Could not obtain the file size');
585
+		}
586
+
587
+		/** @var resource $fp */
588
+		$fp = fopen($filePath, 'rb');
589
+		if (!is_resource($fp)) {
590
+			throw new SodiumException('Could not open input file for reading');
591
+		}
592
+
593
+		/** @var string $az */
594
+		$az = hash('sha512', self::substr($secretKey, 0, 32), true);
595
+
596
+		$az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
597
+		$az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
598
+
599
+		$hs = hash_init('sha512');
600
+		self::hash_update($hs, self::substr($az, 32, 32));
601
+		/** @var resource $hs */
602
+		$hs = self::updateHashWithFile($hs, $fp, $size);
603
+
604
+		/** @var string $nonceHash */
605
+		$nonceHash = hash_final($hs, true);
606
+
607
+		/** @var string $pk */
608
+		$pk = self::substr($secretKey, 32, 32);
609
+
610
+		/** @var string $nonce */
611
+		$nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
612
+
613
+		/** @var string $sig */
614
+		$sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes(
615
+			ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base($nonce)
616
+		);
617
+
618
+		$hs = hash_init('sha512');
619
+		self::hash_update($hs, self::substr($sig, 0, 32));
620
+		self::hash_update($hs, self::substr($pk, 0, 32));
621
+		/** @var resource $hs */
622
+		$hs = self::updateHashWithFile($hs, $fp, $size);
623
+
624
+		/** @var string $hramHash */
625
+		$hramHash = hash_final($hs, true);
626
+
627
+		/** @var string $hram */
628
+		$hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hramHash);
629
+
630
+		/** @var string $sigAfter */
631
+		$sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd($hram, $az, $nonce);
632
+
633
+		/** @var string $sig */
634
+		$sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
635
+
636
+		try {
637
+			ParagonIE_Sodium_Compat::memzero($az);
638
+		} catch (SodiumException $ex) {
639
+			$az = null;
640
+		}
641
+		fclose($fp);
642
+		return $sig;
643
+	}
644
+
645
+	/**
646
+	 * Verify a file (rather than a string). Uses less memory than
647
+	 * ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but
648
+	 * produces the same result.
649
+	 *
650
+	 * @param string $sig       Ed25519 signature
651
+	 * @param string $filePath  Absolute path to a file on the filesystem
652
+	 * @param string $publicKey Signing public key
653
+	 *
654
+	 * @return bool
655
+	 * @throws SodiumException
656
+	 * @throws TypeError
657
+	 * @throws Exception
658
+	 */
659
+	public static function verify($sig, $filePath, $publicKey)
660
+	{
661
+		/* Type checks: */
662
+		if (!is_string($sig)) {
663
+			throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.');
664
+		}
665
+		if (!is_string($filePath)) {
666
+			throw new TypeError('Argument 2 must be a string, ' . gettype($filePath) . ' given.');
667
+		}
668
+		if (!is_string($publicKey)) {
669
+			throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.');
670
+		}
671
+
672
+		/* Input validation: */
673
+		if (self::strlen($sig) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES) {
674
+			throw new TypeError('Argument 1 must be CRYPTO_SIGN_BYTES bytes');
675
+		}
676
+		if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES) {
677
+			throw new TypeError('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes');
678
+		}
679
+		if (self::strlen($sig) < 64) {
680
+			throw new SodiumException('Signature is too short');
681
+		}
682
+
683
+		if (PHP_INT_SIZE === 4) {
684
+			return self::verify_core32($sig, $filePath, $publicKey);
685
+		}
686
+
687
+		/* Security checks */
688
+		if (
689
+			(ParagonIE_Sodium_Core_Ed25519::chrToInt($sig[63]) & 240)
690
+				&&
691
+			ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))
692
+		) {
693
+			throw new SodiumException('S < L - Invalid signature');
694
+		}
695
+		if (ParagonIE_Sodium_Core_Ed25519::small_order($sig)) {
696
+			throw new SodiumException('Signature is on too small of an order');
697
+		}
698
+		if ((self::chrToInt($sig[63]) & 224) !== 0) {
699
+			throw new SodiumException('Invalid signature');
700
+		}
701
+		$d = 0;
702
+		for ($i = 0; $i < 32; ++$i) {
703
+			$d |= self::chrToInt($publicKey[$i]);
704
+		}
705
+		if ($d === 0) {
706
+			throw new SodiumException('All zero public key');
707
+		}
708
+
709
+		/** @var int $size */
710
+		$size = filesize($filePath);
711
+		if (!is_int($size)) {
712
+			throw new SodiumException('Could not obtain the file size');
713
+		}
714
+
715
+		/** @var resource $fp */
716
+		$fp = fopen($filePath, 'rb');
717
+		if (!is_resource($fp)) {
718
+			throw new SodiumException('Could not open input file for reading');
719
+		}
720
+
721
+		/** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
722
+		$orig = ParagonIE_Sodium_Compat::$fastMult;
723
+
724
+		// Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification.
725
+		ParagonIE_Sodium_Compat::$fastMult = true;
726
+
727
+		/** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */
728
+		$A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime($publicKey);
729
+
730
+		$hs = hash_init('sha512');
731
+		self::hash_update($hs, self::substr($sig, 0, 32));
732
+		self::hash_update($hs, self::substr($publicKey, 0, 32));
733
+		/** @var resource $hs */
734
+		$hs = self::updateHashWithFile($hs, $fp, $size);
735
+		/** @var string $hDigest */
736
+		$hDigest = hash_final($hs, true);
737
+
738
+		/** @var string $h */
739
+		$h = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32);
740
+
741
+		/** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */
742
+		$R = ParagonIE_Sodium_Core_Ed25519::ge_double_scalarmult_vartime(
743
+			$h,
744
+			$A,
745
+			self::substr($sig, 32)
746
+		);
747
+
748
+		/** @var string $rcheck */
749
+		$rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes($R);
750
+
751
+		// Close the file handle
752
+		fclose($fp);
753
+
754
+		// Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
755
+		ParagonIE_Sodium_Compat::$fastMult = $orig;
756
+		return self::verify_32($rcheck, self::substr($sig, 0, 32));
757
+	}
758
+
759
+	/**
760
+	 * @param resource $ifp
761
+	 * @param resource $ofp
762
+	 * @param int      $mlen
763
+	 * @param string   $nonce
764
+	 * @param string   $boxKeypair
765
+	 * @return bool
766
+	 * @throws SodiumException
767
+	 * @throws TypeError
768
+	 */
769
+	protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair)
770
+	{
771
+		if (PHP_INT_SIZE === 4) {
772
+			return self::secretbox_encrypt(
773
+				$ifp,
774
+				$ofp,
775
+				$mlen,
776
+				$nonce,
777
+				ParagonIE_Sodium_Crypto32::box_beforenm(
778
+					ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair),
779
+					ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair)
780
+				)
781
+			);
782
+		}
783
+		return self::secretbox_encrypt(
784
+			$ifp,
785
+			$ofp,
786
+			$mlen,
787
+			$nonce,
788
+			ParagonIE_Sodium_Crypto::box_beforenm(
789
+				ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair),
790
+				ParagonIE_Sodium_Crypto::box_publickey($boxKeypair)
791
+			)
792
+		);
793
+	}
794
+
795
+
796
+	/**
797
+	 * @param resource $ifp
798
+	 * @param resource $ofp
799
+	 * @param int      $mlen
800
+	 * @param string   $nonce
801
+	 * @param string   $boxKeypair
802
+	 * @return bool
803
+	 * @throws SodiumException
804
+	 * @throws TypeError
805
+	 */
806
+	protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair)
807
+	{
808
+		if (PHP_INT_SIZE === 4) {
809
+			return self::secretbox_decrypt(
810
+				$ifp,
811
+				$ofp,
812
+				$mlen,
813
+				$nonce,
814
+				ParagonIE_Sodium_Crypto32::box_beforenm(
815
+					ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair),
816
+					ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair)
817
+				)
818
+			);
819
+		}
820
+		return self::secretbox_decrypt(
821
+			$ifp,
822
+			$ofp,
823
+			$mlen,
824
+			$nonce,
825
+			ParagonIE_Sodium_Crypto::box_beforenm(
826
+				ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair),
827
+				ParagonIE_Sodium_Crypto::box_publickey($boxKeypair)
828
+			)
829
+		);
830
+	}
831
+
832
+	/**
833
+	 * Encrypt a file
834
+	 *
835
+	 * @param resource $ifp
836
+	 * @param resource $ofp
837
+	 * @param int $mlen
838
+	 * @param string $nonce
839
+	 * @param string $key
840
+	 * @return bool
841
+	 * @throws SodiumException
842
+	 * @throws TypeError
843
+	 */
844
+	protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key)
845
+	{
846
+		if (PHP_INT_SIZE === 4) {
847
+			return self::secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key);
848
+		}
849
+
850
+		$plaintext = fread($ifp, 32);
851
+		if (!is_string($plaintext)) {
852
+			throw new SodiumException('Could not read input file');
853
+		}
854
+		$first32 = self::ftell($ifp);
855
+
856
+		/** @var string $subkey */
857
+		$subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
858
+
859
+		/** @var string $realNonce */
860
+		$realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
861
+
862
+		/** @var string $block0 */
863
+		$block0 = str_repeat("\x00", 32);
864
+
865
+		/** @var int $mlen - Length of the plaintext message */
866
+		$mlen0 = $mlen;
867
+		if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) {
868
+			$mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES;
869
+		}
870
+		$block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0);
871
+
872
+		/** @var string $block0 */
873
+		$block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor(
874
+			$block0,
875
+			$realNonce,
876
+			$subkey
877
+		);
878
+
879
+		$state = new ParagonIE_Sodium_Core_Poly1305_State(
880
+			ParagonIE_Sodium_Core_Util::substr(
881
+				$block0,
882
+				0,
883
+				ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES
884
+			)
885
+		);
886
+
887
+		// Pre-write 16 blank bytes for the Poly1305 tag
888
+		$start = self::ftell($ofp);
889
+		fwrite($ofp, str_repeat("\x00", 16));
890
+
891
+		/** @var string $c */
892
+		$cBlock = ParagonIE_Sodium_Core_Util::substr(
893
+			$block0,
894
+			ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES
895
+		);
896
+		$state->update($cBlock);
897
+		fwrite($ofp, $cBlock);
898
+		$mlen -= 32;
899
+
900
+		/** @var int $iter */
901
+		$iter = 1;
902
+
903
+		/** @var int $incr */
904
+		$incr = self::BUFFER_SIZE >> 6;
905
+
906
+		/*
907 907
          * Set the cursor to the end of the first half-block. All future bytes will
908 908
          * generated from salsa20_xor_ic, starting from 1 (second block).
909 909
          */
910
-        fseek($ifp, $first32, SEEK_SET);
911
-
912
-        while ($mlen > 0) {
913
-            $blockSize = $mlen > self::BUFFER_SIZE
914
-                ? self::BUFFER_SIZE
915
-                : $mlen;
916
-            $plaintext = fread($ifp, $blockSize);
917
-            if (!is_string($plaintext)) {
918
-                throw new SodiumException('Could not read input file');
919
-            }
920
-            $cBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
921
-                $plaintext,
922
-                $realNonce,
923
-                $iter,
924
-                $subkey
925
-            );
926
-            fwrite($ofp, $cBlock, $blockSize);
927
-            $state->update($cBlock);
928
-
929
-            $mlen -= $blockSize;
930
-            $iter += $incr;
931
-        }
932
-        try {
933
-            ParagonIE_Sodium_Compat::memzero($block0);
934
-            ParagonIE_Sodium_Compat::memzero($subkey);
935
-        } catch (SodiumException $ex) {
936
-            $block0 = null;
937
-            $subkey = null;
938
-        }
939
-        $end = self::ftell($ofp);
940
-
941
-        /*
910
+		fseek($ifp, $first32, SEEK_SET);
911
+
912
+		while ($mlen > 0) {
913
+			$blockSize = $mlen > self::BUFFER_SIZE
914
+				? self::BUFFER_SIZE
915
+				: $mlen;
916
+			$plaintext = fread($ifp, $blockSize);
917
+			if (!is_string($plaintext)) {
918
+				throw new SodiumException('Could not read input file');
919
+			}
920
+			$cBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
921
+				$plaintext,
922
+				$realNonce,
923
+				$iter,
924
+				$subkey
925
+			);
926
+			fwrite($ofp, $cBlock, $blockSize);
927
+			$state->update($cBlock);
928
+
929
+			$mlen -= $blockSize;
930
+			$iter += $incr;
931
+		}
932
+		try {
933
+			ParagonIE_Sodium_Compat::memzero($block0);
934
+			ParagonIE_Sodium_Compat::memzero($subkey);
935
+		} catch (SodiumException $ex) {
936
+			$block0 = null;
937
+			$subkey = null;
938
+		}
939
+		$end = self::ftell($ofp);
940
+
941
+		/*
942 942
          * Write the Poly1305 authentication tag that provides integrity
943 943
          * over the ciphertext (encrypt-then-MAC)
944 944
          */
945
-        fseek($ofp, $start, SEEK_SET);
946
-        fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES);
947
-        fseek($ofp, $end, SEEK_SET);
948
-        unset($state);
949
-
950
-        return true;
951
-    }
952
-
953
-    /**
954
-     * Decrypt a file
955
-     *
956
-     * @param resource $ifp
957
-     * @param resource $ofp
958
-     * @param int $mlen
959
-     * @param string $nonce
960
-     * @param string $key
961
-     * @return bool
962
-     * @throws SodiumException
963
-     * @throws TypeError
964
-     */
965
-    protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key)
966
-    {
967
-        if (PHP_INT_SIZE === 4) {
968
-            return self::secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key);
969
-        }
970
-        $tag = fread($ifp, 16);
971
-        if (!is_string($tag)) {
972
-            throw new SodiumException('Could not read input file');
973
-        }
974
-
975
-        /** @var string $subkey */
976
-        $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
977
-
978
-        /** @var string $realNonce */
979
-        $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
980
-
981
-        /** @var string $block0 */
982
-        $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20(
983
-            64,
984
-            ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
985
-            $subkey
986
-        );
987
-
988
-        /* Verify the Poly1305 MAC -before- attempting to decrypt! */
989
-        $state = new ParagonIE_Sodium_Core_Poly1305_State(self::substr($block0, 0, 32));
990
-        if (!self::onetimeauth_verify($state, $ifp, $tag, $mlen)) {
991
-            throw new SodiumException('Invalid MAC');
992
-        }
993
-
994
-        /*
945
+		fseek($ofp, $start, SEEK_SET);
946
+		fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES);
947
+		fseek($ofp, $end, SEEK_SET);
948
+		unset($state);
949
+
950
+		return true;
951
+	}
952
+
953
+	/**
954
+	 * Decrypt a file
955
+	 *
956
+	 * @param resource $ifp
957
+	 * @param resource $ofp
958
+	 * @param int $mlen
959
+	 * @param string $nonce
960
+	 * @param string $key
961
+	 * @return bool
962
+	 * @throws SodiumException
963
+	 * @throws TypeError
964
+	 */
965
+	protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key)
966
+	{
967
+		if (PHP_INT_SIZE === 4) {
968
+			return self::secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key);
969
+		}
970
+		$tag = fread($ifp, 16);
971
+		if (!is_string($tag)) {
972
+			throw new SodiumException('Could not read input file');
973
+		}
974
+
975
+		/** @var string $subkey */
976
+		$subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
977
+
978
+		/** @var string $realNonce */
979
+		$realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
980
+
981
+		/** @var string $block0 */
982
+		$block0 = ParagonIE_Sodium_Core_Salsa20::salsa20(
983
+			64,
984
+			ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
985
+			$subkey
986
+		);
987
+
988
+		/* Verify the Poly1305 MAC -before- attempting to decrypt! */
989
+		$state = new ParagonIE_Sodium_Core_Poly1305_State(self::substr($block0, 0, 32));
990
+		if (!self::onetimeauth_verify($state, $ifp, $tag, $mlen)) {
991
+			throw new SodiumException('Invalid MAC');
992
+		}
993
+
994
+		/*
995 995
          * Set the cursor to the end of the first half-block. All future bytes will
996 996
          * generated from salsa20_xor_ic, starting from 1 (second block).
997 997
          */
998
-        $first32 = fread($ifp, 32);
999
-        if (!is_string($first32)) {
1000
-            throw new SodiumException('Could not read input file');
1001
-        }
1002
-        $first32len = self::strlen($first32);
1003
-        fwrite(
1004
-            $ofp,
1005
-            self::xorStrings(
1006
-                self::substr($block0, 32, $first32len),
1007
-                self::substr($first32, 0, $first32len)
1008
-            )
1009
-        );
1010
-        $mlen -= 32;
1011
-
1012
-        /** @var int $iter */
1013
-        $iter = 1;
1014
-
1015
-        /** @var int $incr */
1016
-        $incr = self::BUFFER_SIZE >> 6;
1017
-
1018
-        /* Decrypts ciphertext, writes to output file. */
1019
-        while ($mlen > 0) {
1020
-            $blockSize = $mlen > self::BUFFER_SIZE
1021
-                ? self::BUFFER_SIZE
1022
-                : $mlen;
1023
-            $ciphertext = fread($ifp, $blockSize);
1024
-            if (!is_string($ciphertext)) {
1025
-                throw new SodiumException('Could not read input file');
1026
-            }
1027
-            $pBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
1028
-                $ciphertext,
1029
-                $realNonce,
1030
-                $iter,
1031
-                $subkey
1032
-            );
1033
-            fwrite($ofp, $pBlock, $blockSize);
1034
-            $mlen -= $blockSize;
1035
-            $iter += $incr;
1036
-        }
1037
-        return true;
1038
-    }
1039
-
1040
-    /**
1041
-     * @param ParagonIE_Sodium_Core_Poly1305_State $state
1042
-     * @param resource $ifp
1043
-     * @param string $tag
1044
-     * @param int $mlen
1045
-     * @return bool
1046
-     * @throws SodiumException
1047
-     * @throws TypeError
1048
-     */
1049
-    protected static function onetimeauth_verify(
1050
-        ParagonIE_Sodium_Core_Poly1305_State $state,
1051
-        $ifp,
1052
-        $tag = '',
1053
-        $mlen = 0
1054
-    ) {
1055
-        /** @var int $pos */
1056
-        $pos = self::ftell($ifp);
1057
-
1058
-        /** @var int $iter */
1059
-        $iter = 1;
1060
-
1061
-        /** @var int $incr */
1062
-        $incr = self::BUFFER_SIZE >> 6;
1063
-
1064
-        while ($mlen > 0) {
1065
-            $blockSize = $mlen > self::BUFFER_SIZE
1066
-                ? self::BUFFER_SIZE
1067
-                : $mlen;
1068
-            $ciphertext = fread($ifp, $blockSize);
1069
-            if (!is_string($ciphertext)) {
1070
-                throw new SodiumException('Could not read input file');
1071
-            }
1072
-            $state->update($ciphertext);
1073
-            $mlen -= $blockSize;
1074
-            $iter += $incr;
1075
-        }
1076
-        $res = ParagonIE_Sodium_Core_Util::verify_16($tag, $state->finish());
1077
-
1078
-        fseek($ifp, $pos, SEEK_SET);
1079
-        return $res;
1080
-    }
1081
-
1082
-    /**
1083
-     * Update a hash context with the contents of a file, without
1084
-     * loading the entire file into memory.
1085
-     *
1086
-     * @param resource|HashContext $hash
1087
-     * @param resource $fp
1088
-     * @param int $size
1089
-     * @return resource|object Resource on PHP < 7.2, HashContext object on PHP >= 7.2
1090
-     * @throws SodiumException
1091
-     * @throws TypeError
1092
-     * @psalm-suppress PossiblyInvalidArgument
1093
-     *                 PHP 7.2 changes from a resource to an object,
1094
-     *                 which causes Psalm to complain about an error.
1095
-     * @psalm-suppress TypeCoercion
1096
-     *                 Ditto.
1097
-     */
1098
-    public static function updateHashWithFile($hash, $fp, $size = 0)
1099
-    {
1100
-        /* Type checks: */
1101
-        if (PHP_VERSION_ID < 70200) {
1102
-            if (!is_resource($hash)) {
1103
-                throw new TypeError('Argument 1 must be a resource, ' . gettype($hash) . ' given.');
1104
-            }
1105
-        } else {
1106
-            if (!is_object($hash)) {
1107
-                throw new TypeError('Argument 1 must be an object (PHP 7.2+), ' . gettype($hash) . ' given.');
1108
-            }
1109
-        }
1110
-
1111
-        if (!is_resource($fp)) {
1112
-            throw new TypeError('Argument 2 must be a resource, ' . gettype($fp) . ' given.');
1113
-        }
1114
-        if (!is_int($size)) {
1115
-            throw new TypeError('Argument 3 must be an integer, ' . gettype($size) . ' given.');
1116
-        }
1117
-
1118
-        /** @var int $originalPosition */
1119
-        $originalPosition = self::ftell($fp);
1120
-
1121
-        // Move file pointer to beginning of file
1122
-        fseek($fp, 0, SEEK_SET);
1123
-        for ($i = 0; $i < $size; $i += self::BUFFER_SIZE) {
1124
-            /** @var string|bool $message */
1125
-            $message = fread(
1126
-                $fp,
1127
-                ($size - $i) > self::BUFFER_SIZE
1128
-                    ? $size - $i
1129
-                    : self::BUFFER_SIZE
1130
-            );
1131
-            if (!is_string($message)) {
1132
-                throw new SodiumException('Unexpected error reading from file.');
1133
-            }
1134
-            /** @var string $message */
1135
-            /** @psalm-suppress InvalidArgument */
1136
-            self::hash_update($hash, $message);
1137
-        }
1138
-        // Reset file pointer's position
1139
-        fseek($fp, $originalPosition, SEEK_SET);
1140
-        return $hash;
1141
-    }
1142
-
1143
-    /**
1144
-     * Sign a file (rather than a string). Uses less memory than
1145
-     * ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces
1146
-     * the same result. (32-bit)
1147
-     *
1148
-     * @param string $filePath  Absolute path to a file on the filesystem
1149
-     * @param string $secretKey Secret signing key
1150
-     *
1151
-     * @return string           Ed25519 signature
1152
-     * @throws SodiumException
1153
-     * @throws TypeError
1154
-     */
1155
-    private static function sign_core32($filePath, $secretKey)
1156
-    {
1157
-        /** @var int|bool $size */
1158
-        $size = filesize($filePath);
1159
-        if (!is_int($size)) {
1160
-            throw new SodiumException('Could not obtain the file size');
1161
-        }
1162
-        /** @var int $size */
1163
-
1164
-        /** @var resource|bool $fp */
1165
-        $fp = fopen($filePath, 'rb');
1166
-        if (!is_resource($fp)) {
1167
-            throw new SodiumException('Could not open input file for reading');
1168
-        }
1169
-        /** @var resource $fp */
1170
-
1171
-        /** @var string $az */
1172
-        $az = hash('sha512', self::substr($secretKey, 0, 32), true);
1173
-
1174
-        $az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
1175
-        $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
1176
-
1177
-        $hs = hash_init('sha512');
1178
-        self::hash_update($hs, self::substr($az, 32, 32));
1179
-        /** @var resource $hs */
1180
-        $hs = self::updateHashWithFile($hs, $fp, $size);
1181
-
1182
-        /** @var string $nonceHash */
1183
-        $nonceHash = hash_final($hs, true);
1184
-
1185
-        /** @var string $pk */
1186
-        $pk = self::substr($secretKey, 32, 32);
1187
-
1188
-        /** @var string $nonce */
1189
-        $nonce = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
1190
-
1191
-        /** @var string $sig */
1192
-        $sig = ParagonIE_Sodium_Core32_Ed25519::ge_p3_tobytes(
1193
-            ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base($nonce)
1194
-        );
1195
-
1196
-        $hs = hash_init('sha512');
1197
-        self::hash_update($hs, self::substr($sig, 0, 32));
1198
-        self::hash_update($hs, self::substr($pk, 0, 32));
1199
-        /** @var resource $hs */
1200
-        $hs = self::updateHashWithFile($hs, $fp, $size);
1201
-
1202
-        /** @var string $hramHash */
1203
-        $hramHash = hash_final($hs, true);
1204
-
1205
-        /** @var string $hram */
1206
-        $hram = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hramHash);
1207
-
1208
-        /** @var string $sigAfter */
1209
-        $sigAfter = ParagonIE_Sodium_Core32_Ed25519::sc_muladd($hram, $az, $nonce);
1210
-
1211
-        /** @var string $sig */
1212
-        $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
1213
-
1214
-        try {
1215
-            ParagonIE_Sodium_Compat::memzero($az);
1216
-        } catch (SodiumException $ex) {
1217
-            $az = null;
1218
-        }
1219
-        fclose($fp);
1220
-        return $sig;
1221
-    }
1222
-
1223
-    /**
1224
-     *
1225
-     * Verify a file (rather than a string). Uses less memory than
1226
-     * ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but
1227
-     * produces the same result. (32-bit)
1228
-     *
1229
-     * @param string $sig       Ed25519 signature
1230
-     * @param string $filePath  Absolute path to a file on the filesystem
1231
-     * @param string $publicKey Signing public key
1232
-     *
1233
-     * @return bool
1234
-     * @throws SodiumException
1235
-     * @throws Exception
1236
-     */
1237
-    public static function verify_core32($sig, $filePath, $publicKey)
1238
-    {
1239
-        /* Security checks */
1240
-        if (ParagonIE_Sodium_Core32_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) {
1241
-            throw new SodiumException('S < L - Invalid signature');
1242
-        }
1243
-        if (ParagonIE_Sodium_Core32_Ed25519::small_order($sig)) {
1244
-            throw new SodiumException('Signature is on too small of an order');
1245
-        }
1246
-        if ((self::chrToInt($sig[63]) & 224) !== 0) {
1247
-            throw new SodiumException('Invalid signature');
1248
-        }
1249
-        $d = 0;
1250
-        for ($i = 0; $i < 32; ++$i) {
1251
-            $d |= self::chrToInt($publicKey[$i]);
1252
-        }
1253
-        if ($d === 0) {
1254
-            throw new SodiumException('All zero public key');
1255
-        }
1256
-
1257
-        /** @var int|bool $size */
1258
-        $size = filesize($filePath);
1259
-        if (!is_int($size)) {
1260
-            throw new SodiumException('Could not obtain the file size');
1261
-        }
1262
-        /** @var int $size */
1263
-
1264
-        /** @var resource|bool $fp */
1265
-        $fp = fopen($filePath, 'rb');
1266
-        if (!is_resource($fp)) {
1267
-            throw new SodiumException('Could not open input file for reading');
1268
-        }
1269
-        /** @var resource $fp */
1270
-
1271
-        /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
1272
-        $orig = ParagonIE_Sodium_Compat::$fastMult;
1273
-
1274
-        // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification.
1275
-        ParagonIE_Sodium_Compat::$fastMult = true;
1276
-
1277
-        /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */
1278
-        $A = ParagonIE_Sodium_Core32_Ed25519::ge_frombytes_negate_vartime($publicKey);
1279
-
1280
-        $hs = hash_init('sha512');
1281
-        self::hash_update($hs, self::substr($sig, 0, 32));
1282
-        self::hash_update($hs, self::substr($publicKey, 0, 32));
1283
-        /** @var resource $hs */
1284
-        $hs = self::updateHashWithFile($hs, $fp, $size);
1285
-        /** @var string $hDigest */
1286
-        $hDigest = hash_final($hs, true);
1287
-
1288
-        /** @var string $h */
1289
-        $h = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32);
1290
-
1291
-        /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */
1292
-        $R = ParagonIE_Sodium_Core32_Ed25519::ge_double_scalarmult_vartime(
1293
-            $h,
1294
-            $A,
1295
-            self::substr($sig, 32)
1296
-        );
1297
-
1298
-        /** @var string $rcheck */
1299
-        $rcheck = ParagonIE_Sodium_Core32_Ed25519::ge_tobytes($R);
1300
-
1301
-        // Close the file handle
1302
-        fclose($fp);
1303
-
1304
-        // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
1305
-        ParagonIE_Sodium_Compat::$fastMult = $orig;
1306
-        return self::verify_32($rcheck, self::substr($sig, 0, 32));
1307
-    }
1308
-
1309
-    /**
1310
-     * Encrypt a file (32-bit)
1311
-     *
1312
-     * @param resource $ifp
1313
-     * @param resource $ofp
1314
-     * @param int $mlen
1315
-     * @param string $nonce
1316
-     * @param string $key
1317
-     * @return bool
1318
-     * @throws SodiumException
1319
-     * @throws TypeError
1320
-     */
1321
-    protected static function secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key)
1322
-    {
1323
-        $plaintext = fread($ifp, 32);
1324
-        if (!is_string($plaintext)) {
1325
-            throw new SodiumException('Could not read input file');
1326
-        }
1327
-        $first32 = self::ftell($ifp);
1328
-
1329
-        /** @var string $subkey */
1330
-        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
1331
-
1332
-        /** @var string $realNonce */
1333
-        $realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
1334
-
1335
-        /** @var string $block0 */
1336
-        $block0 = str_repeat("\x00", 32);
1337
-
1338
-        /** @var int $mlen - Length of the plaintext message */
1339
-        $mlen0 = $mlen;
1340
-        if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) {
1341
-            $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES;
1342
-        }
1343
-        $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
1344
-
1345
-        /** @var string $block0 */
1346
-        $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor(
1347
-            $block0,
1348
-            $realNonce,
1349
-            $subkey
1350
-        );
1351
-
1352
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State(
1353
-            ParagonIE_Sodium_Core32_Util::substr(
1354
-                $block0,
1355
-                0,
1356
-                ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES
1357
-            )
1358
-        );
1359
-
1360
-        // Pre-write 16 blank bytes for the Poly1305 tag
1361
-        $start = self::ftell($ofp);
1362
-        fwrite($ofp, str_repeat("\x00", 16));
1363
-
1364
-        /** @var string $c */
1365
-        $cBlock = ParagonIE_Sodium_Core32_Util::substr(
1366
-            $block0,
1367
-            ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES
1368
-        );
1369
-        $state->update($cBlock);
1370
-        fwrite($ofp, $cBlock);
1371
-        $mlen -= 32;
1372
-
1373
-        /** @var int $iter */
1374
-        $iter = 1;
1375
-
1376
-        /** @var int $incr */
1377
-        $incr = self::BUFFER_SIZE >> 6;
1378
-
1379
-        /*
998
+		$first32 = fread($ifp, 32);
999
+		if (!is_string($first32)) {
1000
+			throw new SodiumException('Could not read input file');
1001
+		}
1002
+		$first32len = self::strlen($first32);
1003
+		fwrite(
1004
+			$ofp,
1005
+			self::xorStrings(
1006
+				self::substr($block0, 32, $first32len),
1007
+				self::substr($first32, 0, $first32len)
1008
+			)
1009
+		);
1010
+		$mlen -= 32;
1011
+
1012
+		/** @var int $iter */
1013
+		$iter = 1;
1014
+
1015
+		/** @var int $incr */
1016
+		$incr = self::BUFFER_SIZE >> 6;
1017
+
1018
+		/* Decrypts ciphertext, writes to output file. */
1019
+		while ($mlen > 0) {
1020
+			$blockSize = $mlen > self::BUFFER_SIZE
1021
+				? self::BUFFER_SIZE
1022
+				: $mlen;
1023
+			$ciphertext = fread($ifp, $blockSize);
1024
+			if (!is_string($ciphertext)) {
1025
+				throw new SodiumException('Could not read input file');
1026
+			}
1027
+			$pBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
1028
+				$ciphertext,
1029
+				$realNonce,
1030
+				$iter,
1031
+				$subkey
1032
+			);
1033
+			fwrite($ofp, $pBlock, $blockSize);
1034
+			$mlen -= $blockSize;
1035
+			$iter += $incr;
1036
+		}
1037
+		return true;
1038
+	}
1039
+
1040
+	/**
1041
+	 * @param ParagonIE_Sodium_Core_Poly1305_State $state
1042
+	 * @param resource $ifp
1043
+	 * @param string $tag
1044
+	 * @param int $mlen
1045
+	 * @return bool
1046
+	 * @throws SodiumException
1047
+	 * @throws TypeError
1048
+	 */
1049
+	protected static function onetimeauth_verify(
1050
+		ParagonIE_Sodium_Core_Poly1305_State $state,
1051
+		$ifp,
1052
+		$tag = '',
1053
+		$mlen = 0
1054
+	) {
1055
+		/** @var int $pos */
1056
+		$pos = self::ftell($ifp);
1057
+
1058
+		/** @var int $iter */
1059
+		$iter = 1;
1060
+
1061
+		/** @var int $incr */
1062
+		$incr = self::BUFFER_SIZE >> 6;
1063
+
1064
+		while ($mlen > 0) {
1065
+			$blockSize = $mlen > self::BUFFER_SIZE
1066
+				? self::BUFFER_SIZE
1067
+				: $mlen;
1068
+			$ciphertext = fread($ifp, $blockSize);
1069
+			if (!is_string($ciphertext)) {
1070
+				throw new SodiumException('Could not read input file');
1071
+			}
1072
+			$state->update($ciphertext);
1073
+			$mlen -= $blockSize;
1074
+			$iter += $incr;
1075
+		}
1076
+		$res = ParagonIE_Sodium_Core_Util::verify_16($tag, $state->finish());
1077
+
1078
+		fseek($ifp, $pos, SEEK_SET);
1079
+		return $res;
1080
+	}
1081
+
1082
+	/**
1083
+	 * Update a hash context with the contents of a file, without
1084
+	 * loading the entire file into memory.
1085
+	 *
1086
+	 * @param resource|HashContext $hash
1087
+	 * @param resource $fp
1088
+	 * @param int $size
1089
+	 * @return resource|object Resource on PHP < 7.2, HashContext object on PHP >= 7.2
1090
+	 * @throws SodiumException
1091
+	 * @throws TypeError
1092
+	 * @psalm-suppress PossiblyInvalidArgument
1093
+	 *                 PHP 7.2 changes from a resource to an object,
1094
+	 *                 which causes Psalm to complain about an error.
1095
+	 * @psalm-suppress TypeCoercion
1096
+	 *                 Ditto.
1097
+	 */
1098
+	public static function updateHashWithFile($hash, $fp, $size = 0)
1099
+	{
1100
+		/* Type checks: */
1101
+		if (PHP_VERSION_ID < 70200) {
1102
+			if (!is_resource($hash)) {
1103
+				throw new TypeError('Argument 1 must be a resource, ' . gettype($hash) . ' given.');
1104
+			}
1105
+		} else {
1106
+			if (!is_object($hash)) {
1107
+				throw new TypeError('Argument 1 must be an object (PHP 7.2+), ' . gettype($hash) . ' given.');
1108
+			}
1109
+		}
1110
+
1111
+		if (!is_resource($fp)) {
1112
+			throw new TypeError('Argument 2 must be a resource, ' . gettype($fp) . ' given.');
1113
+		}
1114
+		if (!is_int($size)) {
1115
+			throw new TypeError('Argument 3 must be an integer, ' . gettype($size) . ' given.');
1116
+		}
1117
+
1118
+		/** @var int $originalPosition */
1119
+		$originalPosition = self::ftell($fp);
1120
+
1121
+		// Move file pointer to beginning of file
1122
+		fseek($fp, 0, SEEK_SET);
1123
+		for ($i = 0; $i < $size; $i += self::BUFFER_SIZE) {
1124
+			/** @var string|bool $message */
1125
+			$message = fread(
1126
+				$fp,
1127
+				($size - $i) > self::BUFFER_SIZE
1128
+					? $size - $i
1129
+					: self::BUFFER_SIZE
1130
+			);
1131
+			if (!is_string($message)) {
1132
+				throw new SodiumException('Unexpected error reading from file.');
1133
+			}
1134
+			/** @var string $message */
1135
+			/** @psalm-suppress InvalidArgument */
1136
+			self::hash_update($hash, $message);
1137
+		}
1138
+		// Reset file pointer's position
1139
+		fseek($fp, $originalPosition, SEEK_SET);
1140
+		return $hash;
1141
+	}
1142
+
1143
+	/**
1144
+	 * Sign a file (rather than a string). Uses less memory than
1145
+	 * ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces
1146
+	 * the same result. (32-bit)
1147
+	 *
1148
+	 * @param string $filePath  Absolute path to a file on the filesystem
1149
+	 * @param string $secretKey Secret signing key
1150
+	 *
1151
+	 * @return string           Ed25519 signature
1152
+	 * @throws SodiumException
1153
+	 * @throws TypeError
1154
+	 */
1155
+	private static function sign_core32($filePath, $secretKey)
1156
+	{
1157
+		/** @var int|bool $size */
1158
+		$size = filesize($filePath);
1159
+		if (!is_int($size)) {
1160
+			throw new SodiumException('Could not obtain the file size');
1161
+		}
1162
+		/** @var int $size */
1163
+
1164
+		/** @var resource|bool $fp */
1165
+		$fp = fopen($filePath, 'rb');
1166
+		if (!is_resource($fp)) {
1167
+			throw new SodiumException('Could not open input file for reading');
1168
+		}
1169
+		/** @var resource $fp */
1170
+
1171
+		/** @var string $az */
1172
+		$az = hash('sha512', self::substr($secretKey, 0, 32), true);
1173
+
1174
+		$az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
1175
+		$az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
1176
+
1177
+		$hs = hash_init('sha512');
1178
+		self::hash_update($hs, self::substr($az, 32, 32));
1179
+		/** @var resource $hs */
1180
+		$hs = self::updateHashWithFile($hs, $fp, $size);
1181
+
1182
+		/** @var string $nonceHash */
1183
+		$nonceHash = hash_final($hs, true);
1184
+
1185
+		/** @var string $pk */
1186
+		$pk = self::substr($secretKey, 32, 32);
1187
+
1188
+		/** @var string $nonce */
1189
+		$nonce = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
1190
+
1191
+		/** @var string $sig */
1192
+		$sig = ParagonIE_Sodium_Core32_Ed25519::ge_p3_tobytes(
1193
+			ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base($nonce)
1194
+		);
1195
+
1196
+		$hs = hash_init('sha512');
1197
+		self::hash_update($hs, self::substr($sig, 0, 32));
1198
+		self::hash_update($hs, self::substr($pk, 0, 32));
1199
+		/** @var resource $hs */
1200
+		$hs = self::updateHashWithFile($hs, $fp, $size);
1201
+
1202
+		/** @var string $hramHash */
1203
+		$hramHash = hash_final($hs, true);
1204
+
1205
+		/** @var string $hram */
1206
+		$hram = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hramHash);
1207
+
1208
+		/** @var string $sigAfter */
1209
+		$sigAfter = ParagonIE_Sodium_Core32_Ed25519::sc_muladd($hram, $az, $nonce);
1210
+
1211
+		/** @var string $sig */
1212
+		$sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
1213
+
1214
+		try {
1215
+			ParagonIE_Sodium_Compat::memzero($az);
1216
+		} catch (SodiumException $ex) {
1217
+			$az = null;
1218
+		}
1219
+		fclose($fp);
1220
+		return $sig;
1221
+	}
1222
+
1223
+	/**
1224
+	 *
1225
+	 * Verify a file (rather than a string). Uses less memory than
1226
+	 * ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but
1227
+	 * produces the same result. (32-bit)
1228
+	 *
1229
+	 * @param string $sig       Ed25519 signature
1230
+	 * @param string $filePath  Absolute path to a file on the filesystem
1231
+	 * @param string $publicKey Signing public key
1232
+	 *
1233
+	 * @return bool
1234
+	 * @throws SodiumException
1235
+	 * @throws Exception
1236
+	 */
1237
+	public static function verify_core32($sig, $filePath, $publicKey)
1238
+	{
1239
+		/* Security checks */
1240
+		if (ParagonIE_Sodium_Core32_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) {
1241
+			throw new SodiumException('S < L - Invalid signature');
1242
+		}
1243
+		if (ParagonIE_Sodium_Core32_Ed25519::small_order($sig)) {
1244
+			throw new SodiumException('Signature is on too small of an order');
1245
+		}
1246
+		if ((self::chrToInt($sig[63]) & 224) !== 0) {
1247
+			throw new SodiumException('Invalid signature');
1248
+		}
1249
+		$d = 0;
1250
+		for ($i = 0; $i < 32; ++$i) {
1251
+			$d |= self::chrToInt($publicKey[$i]);
1252
+		}
1253
+		if ($d === 0) {
1254
+			throw new SodiumException('All zero public key');
1255
+		}
1256
+
1257
+		/** @var int|bool $size */
1258
+		$size = filesize($filePath);
1259
+		if (!is_int($size)) {
1260
+			throw new SodiumException('Could not obtain the file size');
1261
+		}
1262
+		/** @var int $size */
1263
+
1264
+		/** @var resource|bool $fp */
1265
+		$fp = fopen($filePath, 'rb');
1266
+		if (!is_resource($fp)) {
1267
+			throw new SodiumException('Could not open input file for reading');
1268
+		}
1269
+		/** @var resource $fp */
1270
+
1271
+		/** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
1272
+		$orig = ParagonIE_Sodium_Compat::$fastMult;
1273
+
1274
+		// Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification.
1275
+		ParagonIE_Sodium_Compat::$fastMult = true;
1276
+
1277
+		/** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */
1278
+		$A = ParagonIE_Sodium_Core32_Ed25519::ge_frombytes_negate_vartime($publicKey);
1279
+
1280
+		$hs = hash_init('sha512');
1281
+		self::hash_update($hs, self::substr($sig, 0, 32));
1282
+		self::hash_update($hs, self::substr($publicKey, 0, 32));
1283
+		/** @var resource $hs */
1284
+		$hs = self::updateHashWithFile($hs, $fp, $size);
1285
+		/** @var string $hDigest */
1286
+		$hDigest = hash_final($hs, true);
1287
+
1288
+		/** @var string $h */
1289
+		$h = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32);
1290
+
1291
+		/** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */
1292
+		$R = ParagonIE_Sodium_Core32_Ed25519::ge_double_scalarmult_vartime(
1293
+			$h,
1294
+			$A,
1295
+			self::substr($sig, 32)
1296
+		);
1297
+
1298
+		/** @var string $rcheck */
1299
+		$rcheck = ParagonIE_Sodium_Core32_Ed25519::ge_tobytes($R);
1300
+
1301
+		// Close the file handle
1302
+		fclose($fp);
1303
+
1304
+		// Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
1305
+		ParagonIE_Sodium_Compat::$fastMult = $orig;
1306
+		return self::verify_32($rcheck, self::substr($sig, 0, 32));
1307
+	}
1308
+
1309
+	/**
1310
+	 * Encrypt a file (32-bit)
1311
+	 *
1312
+	 * @param resource $ifp
1313
+	 * @param resource $ofp
1314
+	 * @param int $mlen
1315
+	 * @param string $nonce
1316
+	 * @param string $key
1317
+	 * @return bool
1318
+	 * @throws SodiumException
1319
+	 * @throws TypeError
1320
+	 */
1321
+	protected static function secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key)
1322
+	{
1323
+		$plaintext = fread($ifp, 32);
1324
+		if (!is_string($plaintext)) {
1325
+			throw new SodiumException('Could not read input file');
1326
+		}
1327
+		$first32 = self::ftell($ifp);
1328
+
1329
+		/** @var string $subkey */
1330
+		$subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
1331
+
1332
+		/** @var string $realNonce */
1333
+		$realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
1334
+
1335
+		/** @var string $block0 */
1336
+		$block0 = str_repeat("\x00", 32);
1337
+
1338
+		/** @var int $mlen - Length of the plaintext message */
1339
+		$mlen0 = $mlen;
1340
+		if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) {
1341
+			$mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES;
1342
+		}
1343
+		$block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
1344
+
1345
+		/** @var string $block0 */
1346
+		$block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor(
1347
+			$block0,
1348
+			$realNonce,
1349
+			$subkey
1350
+		);
1351
+
1352
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State(
1353
+			ParagonIE_Sodium_Core32_Util::substr(
1354
+				$block0,
1355
+				0,
1356
+				ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES
1357
+			)
1358
+		);
1359
+
1360
+		// Pre-write 16 blank bytes for the Poly1305 tag
1361
+		$start = self::ftell($ofp);
1362
+		fwrite($ofp, str_repeat("\x00", 16));
1363
+
1364
+		/** @var string $c */
1365
+		$cBlock = ParagonIE_Sodium_Core32_Util::substr(
1366
+			$block0,
1367
+			ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES
1368
+		);
1369
+		$state->update($cBlock);
1370
+		fwrite($ofp, $cBlock);
1371
+		$mlen -= 32;
1372
+
1373
+		/** @var int $iter */
1374
+		$iter = 1;
1375
+
1376
+		/** @var int $incr */
1377
+		$incr = self::BUFFER_SIZE >> 6;
1378
+
1379
+		/*
1380 1380
          * Set the cursor to the end of the first half-block. All future bytes will
1381 1381
          * generated from salsa20_xor_ic, starting from 1 (second block).
1382 1382
          */
1383
-        fseek($ifp, $first32, SEEK_SET);
1384
-
1385
-        while ($mlen > 0) {
1386
-            $blockSize = $mlen > self::BUFFER_SIZE
1387
-                ? self::BUFFER_SIZE
1388
-                : $mlen;
1389
-            $plaintext = fread($ifp, $blockSize);
1390
-            if (!is_string($plaintext)) {
1391
-                throw new SodiumException('Could not read input file');
1392
-            }
1393
-            $cBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
1394
-                $plaintext,
1395
-                $realNonce,
1396
-                $iter,
1397
-                $subkey
1398
-            );
1399
-            fwrite($ofp, $cBlock, $blockSize);
1400
-            $state->update($cBlock);
1401
-
1402
-            $mlen -= $blockSize;
1403
-            $iter += $incr;
1404
-        }
1405
-        try {
1406
-            ParagonIE_Sodium_Compat::memzero($block0);
1407
-            ParagonIE_Sodium_Compat::memzero($subkey);
1408
-        } catch (SodiumException $ex) {
1409
-            $block0 = null;
1410
-            $subkey = null;
1411
-        }
1412
-        $end = self::ftell($ofp);
1413
-
1414
-        /*
1383
+		fseek($ifp, $first32, SEEK_SET);
1384
+
1385
+		while ($mlen > 0) {
1386
+			$blockSize = $mlen > self::BUFFER_SIZE
1387
+				? self::BUFFER_SIZE
1388
+				: $mlen;
1389
+			$plaintext = fread($ifp, $blockSize);
1390
+			if (!is_string($plaintext)) {
1391
+				throw new SodiumException('Could not read input file');
1392
+			}
1393
+			$cBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
1394
+				$plaintext,
1395
+				$realNonce,
1396
+				$iter,
1397
+				$subkey
1398
+			);
1399
+			fwrite($ofp, $cBlock, $blockSize);
1400
+			$state->update($cBlock);
1401
+
1402
+			$mlen -= $blockSize;
1403
+			$iter += $incr;
1404
+		}
1405
+		try {
1406
+			ParagonIE_Sodium_Compat::memzero($block0);
1407
+			ParagonIE_Sodium_Compat::memzero($subkey);
1408
+		} catch (SodiumException $ex) {
1409
+			$block0 = null;
1410
+			$subkey = null;
1411
+		}
1412
+		$end = self::ftell($ofp);
1413
+
1414
+		/*
1415 1415
          * Write the Poly1305 authentication tag that provides integrity
1416 1416
          * over the ciphertext (encrypt-then-MAC)
1417 1417
          */
1418
-        fseek($ofp, $start, SEEK_SET);
1419
-        fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES);
1420
-        fseek($ofp, $end, SEEK_SET);
1421
-        unset($state);
1422
-
1423
-        return true;
1424
-    }
1425
-
1426
-    /**
1427
-     * Decrypt a file (32-bit)
1428
-     *
1429
-     * @param resource $ifp
1430
-     * @param resource $ofp
1431
-     * @param int $mlen
1432
-     * @param string $nonce
1433
-     * @param string $key
1434
-     * @return bool
1435
-     * @throws SodiumException
1436
-     * @throws TypeError
1437
-     */
1438
-    protected static function secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key)
1439
-    {
1440
-        $tag = fread($ifp, 16);
1441
-        if (!is_string($tag)) {
1442
-            throw new SodiumException('Could not read input file');
1443
-        }
1444
-
1445
-        /** @var string $subkey */
1446
-        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
1447
-
1448
-        /** @var string $realNonce */
1449
-        $realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
1450
-
1451
-        /** @var string $block0 */
1452
-        $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20(
1453
-            64,
1454
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1455
-            $subkey
1456
-        );
1457
-
1458
-        /* Verify the Poly1305 MAC -before- attempting to decrypt! */
1459
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State(self::substr($block0, 0, 32));
1460
-        if (!self::onetimeauth_verify_core32($state, $ifp, $tag, $mlen)) {
1461
-            throw new SodiumException('Invalid MAC');
1462
-        }
1463
-
1464
-        /*
1418
+		fseek($ofp, $start, SEEK_SET);
1419
+		fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES);
1420
+		fseek($ofp, $end, SEEK_SET);
1421
+		unset($state);
1422
+
1423
+		return true;
1424
+	}
1425
+
1426
+	/**
1427
+	 * Decrypt a file (32-bit)
1428
+	 *
1429
+	 * @param resource $ifp
1430
+	 * @param resource $ofp
1431
+	 * @param int $mlen
1432
+	 * @param string $nonce
1433
+	 * @param string $key
1434
+	 * @return bool
1435
+	 * @throws SodiumException
1436
+	 * @throws TypeError
1437
+	 */
1438
+	protected static function secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key)
1439
+	{
1440
+		$tag = fread($ifp, 16);
1441
+		if (!is_string($tag)) {
1442
+			throw new SodiumException('Could not read input file');
1443
+		}
1444
+
1445
+		/** @var string $subkey */
1446
+		$subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
1447
+
1448
+		/** @var string $realNonce */
1449
+		$realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
1450
+
1451
+		/** @var string $block0 */
1452
+		$block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20(
1453
+			64,
1454
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1455
+			$subkey
1456
+		);
1457
+
1458
+		/* Verify the Poly1305 MAC -before- attempting to decrypt! */
1459
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State(self::substr($block0, 0, 32));
1460
+		if (!self::onetimeauth_verify_core32($state, $ifp, $tag, $mlen)) {
1461
+			throw new SodiumException('Invalid MAC');
1462
+		}
1463
+
1464
+		/*
1465 1465
          * Set the cursor to the end of the first half-block. All future bytes will
1466 1466
          * generated from salsa20_xor_ic, starting from 1 (second block).
1467 1467
          */
1468
-        $first32 = fread($ifp, 32);
1469
-        if (!is_string($first32)) {
1470
-            throw new SodiumException('Could not read input file');
1471
-        }
1472
-        $first32len = self::strlen($first32);
1473
-        fwrite(
1474
-            $ofp,
1475
-            self::xorStrings(
1476
-                self::substr($block0, 32, $first32len),
1477
-                self::substr($first32, 0, $first32len)
1478
-            )
1479
-        );
1480
-        $mlen -= 32;
1481
-
1482
-        /** @var int $iter */
1483
-        $iter = 1;
1484
-
1485
-        /** @var int $incr */
1486
-        $incr = self::BUFFER_SIZE >> 6;
1487
-
1488
-        /* Decrypts ciphertext, writes to output file. */
1489
-        while ($mlen > 0) {
1490
-            $blockSize = $mlen > self::BUFFER_SIZE
1491
-                ? self::BUFFER_SIZE
1492
-                : $mlen;
1493
-            $ciphertext = fread($ifp, $blockSize);
1494
-            if (!is_string($ciphertext)) {
1495
-                throw new SodiumException('Could not read input file');
1496
-            }
1497
-            $pBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
1498
-                $ciphertext,
1499
-                $realNonce,
1500
-                $iter,
1501
-                $subkey
1502
-            );
1503
-            fwrite($ofp, $pBlock, $blockSize);
1504
-            $mlen -= $blockSize;
1505
-            $iter += $incr;
1506
-        }
1507
-        return true;
1508
-    }
1509
-
1510
-    /**
1511
-     * One-time message authentication for 32-bit systems
1512
-     *
1513
-     * @param ParagonIE_Sodium_Core32_Poly1305_State $state
1514
-     * @param resource $ifp
1515
-     * @param string $tag
1516
-     * @param int $mlen
1517
-     * @return bool
1518
-     * @throws SodiumException
1519
-     * @throws TypeError
1520
-     */
1521
-    protected static function onetimeauth_verify_core32(
1522
-        ParagonIE_Sodium_Core32_Poly1305_State $state,
1523
-        $ifp,
1524
-        $tag = '',
1525
-        $mlen = 0
1526
-    ) {
1527
-        /** @var int $pos */
1528
-        $pos = self::ftell($ifp);
1529
-
1530
-        while ($mlen > 0) {
1531
-            $blockSize = $mlen > self::BUFFER_SIZE
1532
-                ? self::BUFFER_SIZE
1533
-                : $mlen;
1534
-            $ciphertext = fread($ifp, $blockSize);
1535
-            if (!is_string($ciphertext)) {
1536
-                throw new SodiumException('Could not read input file');
1537
-            }
1538
-            $state->update($ciphertext);
1539
-            $mlen -= $blockSize;
1540
-        }
1541
-        $res = ParagonIE_Sodium_Core32_Util::verify_16($tag, $state->finish());
1542
-
1543
-        fseek($ifp, $pos, SEEK_SET);
1544
-        return $res;
1545
-    }
1546
-
1547
-    /**
1548
-     * @param resource $resource
1549
-     * @return int
1550
-     * @throws SodiumException
1551
-     */
1552
-    private static function ftell($resource)
1553
-    {
1554
-        $return = ftell($resource);
1555
-        if (!is_int($return)) {
1556
-            throw new SodiumException('ftell() returned false');
1557
-        }
1558
-        return (int) $return;
1559
-    }
1468
+		$first32 = fread($ifp, 32);
1469
+		if (!is_string($first32)) {
1470
+			throw new SodiumException('Could not read input file');
1471
+		}
1472
+		$first32len = self::strlen($first32);
1473
+		fwrite(
1474
+			$ofp,
1475
+			self::xorStrings(
1476
+				self::substr($block0, 32, $first32len),
1477
+				self::substr($first32, 0, $first32len)
1478
+			)
1479
+		);
1480
+		$mlen -= 32;
1481
+
1482
+		/** @var int $iter */
1483
+		$iter = 1;
1484
+
1485
+		/** @var int $incr */
1486
+		$incr = self::BUFFER_SIZE >> 6;
1487
+
1488
+		/* Decrypts ciphertext, writes to output file. */
1489
+		while ($mlen > 0) {
1490
+			$blockSize = $mlen > self::BUFFER_SIZE
1491
+				? self::BUFFER_SIZE
1492
+				: $mlen;
1493
+			$ciphertext = fread($ifp, $blockSize);
1494
+			if (!is_string($ciphertext)) {
1495
+				throw new SodiumException('Could not read input file');
1496
+			}
1497
+			$pBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
1498
+				$ciphertext,
1499
+				$realNonce,
1500
+				$iter,
1501
+				$subkey
1502
+			);
1503
+			fwrite($ofp, $pBlock, $blockSize);
1504
+			$mlen -= $blockSize;
1505
+			$iter += $incr;
1506
+		}
1507
+		return true;
1508
+	}
1509
+
1510
+	/**
1511
+	 * One-time message authentication for 32-bit systems
1512
+	 *
1513
+	 * @param ParagonIE_Sodium_Core32_Poly1305_State $state
1514
+	 * @param resource $ifp
1515
+	 * @param string $tag
1516
+	 * @param int $mlen
1517
+	 * @return bool
1518
+	 * @throws SodiumException
1519
+	 * @throws TypeError
1520
+	 */
1521
+	protected static function onetimeauth_verify_core32(
1522
+		ParagonIE_Sodium_Core32_Poly1305_State $state,
1523
+		$ifp,
1524
+		$tag = '',
1525
+		$mlen = 0
1526
+	) {
1527
+		/** @var int $pos */
1528
+		$pos = self::ftell($ifp);
1529
+
1530
+		while ($mlen > 0) {
1531
+			$blockSize = $mlen > self::BUFFER_SIZE
1532
+				? self::BUFFER_SIZE
1533
+				: $mlen;
1534
+			$ciphertext = fread($ifp, $blockSize);
1535
+			if (!is_string($ciphertext)) {
1536
+				throw new SodiumException('Could not read input file');
1537
+			}
1538
+			$state->update($ciphertext);
1539
+			$mlen -= $blockSize;
1540
+		}
1541
+		$res = ParagonIE_Sodium_Core32_Util::verify_16($tag, $state->finish());
1542
+
1543
+		fseek($ifp, $pos, SEEK_SET);
1544
+		return $res;
1545
+	}
1546
+
1547
+	/**
1548
+	 * @param resource $resource
1549
+	 * @return int
1550
+	 * @throws SodiumException
1551
+	 */
1552
+	private static function ftell($resource)
1553
+	{
1554
+		$return = ftell($resource);
1555
+		if (!is_int($return)) {
1556
+			throw new SodiumException('ftell() returned false');
1557
+		}
1558
+		return (int) $return;
1559
+	}
1560 1560
 }
Please login to merge, or discard this patch.
Spacing   +513 added lines, -513 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-if (class_exists('ParagonIE_Sodium_File', false)) {
3
+if ( class_exists( 'ParagonIE_Sodium_File', false ) ) {
4 4
     return;
5 5
 }
6 6
 /**
@@ -25,52 +25,52 @@  discard block
 block discarded – undo
25 25
      * @throws SodiumException
26 26
      * @throws TypeError
27 27
      */
28
-    public static function box($inputFile, $outputFile, $nonce, $keyPair)
28
+    public static function box( $inputFile, $outputFile, $nonce, $keyPair )
29 29
     {
30 30
         /* Type checks: */
31
-        if (!is_string($inputFile)) {
32
-            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
31
+        if ( ! is_string( $inputFile ) ) {
32
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' );
33 33
         }
34
-        if (!is_string($outputFile)) {
35
-            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
34
+        if ( ! is_string( $outputFile ) ) {
35
+            throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' );
36 36
         }
37
-        if (!is_string($nonce)) {
38
-            throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
37
+        if ( ! is_string( $nonce ) ) {
38
+            throw new TypeError( 'Argument 3 must be a string, ' . gettype( $nonce ) . ' given.' );
39 39
         }
40 40
 
41 41
         /* Input validation: */
42
-        if (!is_string($keyPair)) {
43
-            throw new TypeError('Argument 4 must be a string, ' . gettype($keyPair) . ' given.');
42
+        if ( ! is_string( $keyPair ) ) {
43
+            throw new TypeError( 'Argument 4 must be a string, ' . gettype( $keyPair ) . ' given.' );
44 44
         }
45
-        if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) {
46
-            throw new TypeError('Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes');
45
+        if ( self::strlen( $nonce ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES ) {
46
+            throw new TypeError( 'Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes' );
47 47
         }
48
-        if (self::strlen($keyPair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
49
-            throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
48
+        if ( self::strlen( $keyPair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) {
49
+            throw new TypeError( 'Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes' );
50 50
         }
51 51
 
52 52
         /** @var int $size */
53
-        $size = filesize($inputFile);
54
-        if (!is_int($size)) {
55
-            throw new SodiumException('Could not obtain the file size');
53
+        $size = filesize( $inputFile );
54
+        if ( ! is_int( $size ) ) {
55
+            throw new SodiumException( 'Could not obtain the file size' );
56 56
         }
57 57
 
58 58
         /** @var resource $ifp */
59
-        $ifp = fopen($inputFile, 'rb');
60
-        if (!is_resource($ifp)) {
61
-            throw new SodiumException('Could not open input file for reading');
59
+        $ifp = fopen( $inputFile, 'rb' );
60
+        if ( ! is_resource( $ifp ) ) {
61
+            throw new SodiumException( 'Could not open input file for reading' );
62 62
         }
63 63
 
64 64
         /** @var resource $ofp */
65
-        $ofp = fopen($outputFile, 'wb');
66
-        if (!is_resource($ofp)) {
67
-            fclose($ifp);
68
-            throw new SodiumException('Could not open output file for writing');
65
+        $ofp = fopen( $outputFile, 'wb' );
66
+        if ( ! is_resource( $ofp ) ) {
67
+            fclose( $ifp );
68
+            throw new SodiumException( 'Could not open output file for writing' );
69 69
         }
70 70
 
71
-        $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $keyPair);
72
-        fclose($ifp);
73
-        fclose($ofp);
71
+        $res = self::box_encrypt( $ifp, $ofp, $size, $nonce, $keyPair );
72
+        fclose( $ifp );
73
+        fclose( $ofp );
74 74
         return $res;
75 75
     }
76 76
 
@@ -91,58 +91,58 @@  discard block
 block discarded – undo
91 91
      * @throws SodiumException
92 92
      * @throws TypeError
93 93
      */
94
-    public static function box_open($inputFile, $outputFile, $nonce, $keypair)
94
+    public static function box_open( $inputFile, $outputFile, $nonce, $keypair )
95 95
     {
96 96
         /* Type checks: */
97
-        if (!is_string($inputFile)) {
98
-            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
97
+        if ( ! is_string( $inputFile ) ) {
98
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' );
99 99
         }
100
-        if (!is_string($outputFile)) {
101
-            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
100
+        if ( ! is_string( $outputFile ) ) {
101
+            throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' );
102 102
         }
103
-        if (!is_string($nonce)) {
104
-            throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
103
+        if ( ! is_string( $nonce ) ) {
104
+            throw new TypeError( 'Argument 3 must be a string, ' . gettype( $nonce ) . ' given.' );
105 105
         }
106
-        if (!is_string($keypair)) {
107
-            throw new TypeError('Argument 4 must be a string, ' . gettype($keypair) . ' given.');
106
+        if ( ! is_string( $keypair ) ) {
107
+            throw new TypeError( 'Argument 4 must be a string, ' . gettype( $keypair ) . ' given.' );
108 108
         }
109 109
 
110 110
         /* Input validation: */
111
-        if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) {
112
-            throw new TypeError('Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes');
111
+        if ( self::strlen( $nonce ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES ) {
112
+            throw new TypeError( 'Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes' );
113 113
         }
114
-        if (self::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
115
-            throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
114
+        if ( self::strlen( $keypair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) {
115
+            throw new TypeError( 'Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes' );
116 116
         }
117 117
 
118 118
         /** @var int $size */
119
-        $size = filesize($inputFile);
120
-        if (!is_int($size)) {
121
-            throw new SodiumException('Could not obtain the file size');
119
+        $size = filesize( $inputFile );
120
+        if ( ! is_int( $size ) ) {
121
+            throw new SodiumException( 'Could not obtain the file size' );
122 122
         }
123 123
 
124 124
         /** @var resource $ifp */
125
-        $ifp = fopen($inputFile, 'rb');
126
-        if (!is_resource($ifp)) {
127
-            throw new SodiumException('Could not open input file for reading');
125
+        $ifp = fopen( $inputFile, 'rb' );
126
+        if ( ! is_resource( $ifp ) ) {
127
+            throw new SodiumException( 'Could not open input file for reading' );
128 128
         }
129 129
 
130 130
         /** @var resource $ofp */
131
-        $ofp = fopen($outputFile, 'wb');
132
-        if (!is_resource($ofp)) {
133
-            fclose($ifp);
134
-            throw new SodiumException('Could not open output file for writing');
131
+        $ofp = fopen( $outputFile, 'wb' );
132
+        if ( ! is_resource( $ofp ) ) {
133
+            fclose( $ifp );
134
+            throw new SodiumException( 'Could not open output file for writing' );
135 135
         }
136 136
 
137
-        $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair);
138
-        fclose($ifp);
139
-        fclose($ofp);
137
+        $res = self::box_decrypt( $ifp, $ofp, $size, $nonce, $keypair );
138
+        fclose( $ifp );
139
+        fclose( $ofp );
140 140
         try {
141
-            ParagonIE_Sodium_Compat::memzero($nonce);
142
-            ParagonIE_Sodium_Compat::memzero($ephKeypair);
143
-        } catch (SodiumException $ex) {
144
-            if (isset($ephKeypair)) {
145
-                unset($ephKeypair);
141
+            ParagonIE_Sodium_Compat::memzero( $nonce );
142
+            ParagonIE_Sodium_Compat::memzero( $ephKeypair );
143
+        } catch ( SodiumException $ex ) {
144
+            if ( isset( $ephKeypair ) ) {
145
+                unset( $ephKeypair );
146 146
             }
147 147
         }
148 148
         return $res;
@@ -161,41 +161,41 @@  discard block
 block discarded – undo
161 161
      * @throws SodiumException
162 162
      * @throws TypeError
163 163
      */
164
-    public static function box_seal($inputFile, $outputFile, $publicKey)
164
+    public static function box_seal( $inputFile, $outputFile, $publicKey )
165 165
     {
166 166
         /* Type checks: */
167
-        if (!is_string($inputFile)) {
168
-            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
167
+        if ( ! is_string( $inputFile ) ) {
168
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' );
169 169
         }
170
-        if (!is_string($outputFile)) {
171
-            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
170
+        if ( ! is_string( $outputFile ) ) {
171
+            throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' );
172 172
         }
173
-        if (!is_string($publicKey)) {
174
-            throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.');
173
+        if ( ! is_string( $publicKey ) ) {
174
+            throw new TypeError( 'Argument 3 must be a string, ' . gettype( $publicKey ) . ' given.' );
175 175
         }
176 176
 
177 177
         /* Input validation: */
178
-        if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
179
-            throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes');
178
+        if ( self::strlen( $publicKey ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES ) {
179
+            throw new TypeError( 'Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes' );
180 180
         }
181 181
 
182 182
         /** @var int $size */
183
-        $size = filesize($inputFile);
184
-        if (!is_int($size)) {
185
-            throw new SodiumException('Could not obtain the file size');
183
+        $size = filesize( $inputFile );
184
+        if ( ! is_int( $size ) ) {
185
+            throw new SodiumException( 'Could not obtain the file size' );
186 186
         }
187 187
 
188 188
         /** @var resource $ifp */
189
-        $ifp = fopen($inputFile, 'rb');
190
-        if (!is_resource($ifp)) {
191
-            throw new SodiumException('Could not open input file for reading');
189
+        $ifp = fopen( $inputFile, 'rb' );
190
+        if ( ! is_resource( $ifp ) ) {
191
+            throw new SodiumException( 'Could not open input file for reading' );
192 192
         }
193 193
 
194 194
         /** @var resource $ofp */
195
-        $ofp = fopen($outputFile, 'wb');
196
-        if (!is_resource($ofp)) {
197
-            fclose($ifp);
198
-            throw new SodiumException('Could not open output file for writing');
195
+        $ofp = fopen( $outputFile, 'wb' );
196
+        if ( ! is_resource( $ofp ) ) {
197
+            fclose( $ifp );
198
+            throw new SodiumException( 'Could not open output file for writing' );
199 199
         }
200 200
 
201 201
         /** @var string $ephKeypair */
@@ -203,12 +203,12 @@  discard block
 block discarded – undo
203 203
 
204 204
         /** @var string $msgKeypair */
205 205
         $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey(
206
-            ParagonIE_Sodium_Compat::crypto_box_secretkey($ephKeypair),
206
+            ParagonIE_Sodium_Compat::crypto_box_secretkey( $ephKeypair ),
207 207
             $publicKey
208 208
         );
209 209
 
210 210
         /** @var string $ephemeralPK */
211
-        $ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey($ephKeypair);
211
+        $ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey( $ephKeypair );
212 212
 
213 213
         /** @var string $nonce */
214 214
         $nonce = ParagonIE_Sodium_Compat::crypto_generichash(
@@ -223,28 +223,28 @@  discard block
 block discarded – undo
223 223
             $ephemeralPK,
224 224
             ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES
225 225
         );
226
-        if (!is_int($firstWrite)) {
227
-            fclose($ifp);
228
-            fclose($ofp);
229
-            ParagonIE_Sodium_Compat::memzero($ephKeypair);
230
-            throw new SodiumException('Could not write to output file');
231
-        }
232
-        if ($firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
233
-            ParagonIE_Sodium_Compat::memzero($ephKeypair);
234
-            fclose($ifp);
235
-            fclose($ofp);
236
-            throw new SodiumException('Error writing public key to output file');
237
-        }
238
-
239
-        $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $msgKeypair);
240
-        fclose($ifp);
241
-        fclose($ofp);
226
+        if ( ! is_int( $firstWrite ) ) {
227
+            fclose( $ifp );
228
+            fclose( $ofp );
229
+            ParagonIE_Sodium_Compat::memzero( $ephKeypair );
230
+            throw new SodiumException( 'Could not write to output file' );
231
+        }
232
+        if ( $firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES ) {
233
+            ParagonIE_Sodium_Compat::memzero( $ephKeypair );
234
+            fclose( $ifp );
235
+            fclose( $ofp );
236
+            throw new SodiumException( 'Error writing public key to output file' );
237
+        }
238
+
239
+        $res = self::box_encrypt( $ifp, $ofp, $size, $nonce, $msgKeypair );
240
+        fclose( $ifp );
241
+        fclose( $ofp );
242 242
         try {
243
-            ParagonIE_Sodium_Compat::memzero($nonce);
244
-            ParagonIE_Sodium_Compat::memzero($ephKeypair);
245
-        } catch (SodiumException $ex) {
243
+            ParagonIE_Sodium_Compat::memzero( $nonce );
244
+            ParagonIE_Sodium_Compat::memzero( $ephKeypair );
245
+        } catch ( SodiumException $ex ) {
246 246
             /** @psalm-suppress PossiblyUndefinedVariable */
247
-            unset($ephKeypair);
247
+            unset( $ephKeypair );
248 248
         }
249 249
         return $res;
250 250
     }
@@ -265,53 +265,53 @@  discard block
 block discarded – undo
265 265
      * @throws SodiumException
266 266
      * @throws TypeError
267 267
      */
268
-    public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair)
268
+    public static function box_seal_open( $inputFile, $outputFile, $ecdhKeypair )
269 269
     {
270 270
         /* Type checks: */
271
-        if (!is_string($inputFile)) {
272
-            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
271
+        if ( ! is_string( $inputFile ) ) {
272
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' );
273 273
         }
274
-        if (!is_string($outputFile)) {
275
-            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
274
+        if ( ! is_string( $outputFile ) ) {
275
+            throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' );
276 276
         }
277
-        if (!is_string($ecdhKeypair)) {
278
-            throw new TypeError('Argument 3 must be a string, ' . gettype($ecdhKeypair) . ' given.');
277
+        if ( ! is_string( $ecdhKeypair ) ) {
278
+            throw new TypeError( 'Argument 3 must be a string, ' . gettype( $ecdhKeypair ) . ' given.' );
279 279
         }
280 280
 
281 281
         /* Input validation: */
282
-        if (self::strlen($ecdhKeypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
283
-            throw new TypeError('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
282
+        if ( self::strlen( $ecdhKeypair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) {
283
+            throw new TypeError( 'Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes' );
284 284
         }
285 285
 
286
-        $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair);
286
+        $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey( $ecdhKeypair );
287 287
 
288 288
         /** @var int $size */
289
-        $size = filesize($inputFile);
290
-        if (!is_int($size)) {
291
-            throw new SodiumException('Could not obtain the file size');
289
+        $size = filesize( $inputFile );
290
+        if ( ! is_int( $size ) ) {
291
+            throw new SodiumException( 'Could not obtain the file size' );
292 292
         }
293 293
 
294 294
         /** @var resource $ifp */
295
-        $ifp = fopen($inputFile, 'rb');
296
-        if (!is_resource($ifp)) {
297
-            throw new SodiumException('Could not open input file for reading');
295
+        $ifp = fopen( $inputFile, 'rb' );
296
+        if ( ! is_resource( $ifp ) ) {
297
+            throw new SodiumException( 'Could not open input file for reading' );
298 298
         }
299 299
 
300 300
         /** @var resource $ofp */
301
-        $ofp = fopen($outputFile, 'wb');
302
-        if (!is_resource($ofp)) {
303
-            fclose($ifp);
304
-            throw new SodiumException('Could not open output file for writing');
301
+        $ofp = fopen( $outputFile, 'wb' );
302
+        if ( ! is_resource( $ofp ) ) {
303
+            fclose( $ifp );
304
+            throw new SodiumException( 'Could not open output file for writing' );
305 305
         }
306 306
 
307
-        $ephemeralPK = fread($ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES);
308
-        if (!is_string($ephemeralPK)) {
309
-            throw new SodiumException('Could not read input file');
307
+        $ephemeralPK = fread( $ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES );
308
+        if ( ! is_string( $ephemeralPK ) ) {
309
+            throw new SodiumException( 'Could not read input file' );
310 310
         }
311
-        if (self::strlen($ephemeralPK) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
312
-            fclose($ifp);
313
-            fclose($ofp);
314
-            throw new SodiumException('Could not read public key from sealed file');
311
+        if ( self::strlen( $ephemeralPK ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES ) {
312
+            fclose( $ifp );
313
+            fclose( $ofp );
314
+            throw new SodiumException( 'Could not read public key from sealed file' );
315 315
         }
316 316
 
317 317
         $nonce = ParagonIE_Sodium_Compat::crypto_generichash(
@@ -320,19 +320,19 @@  discard block
 block discarded – undo
320 320
             24
321 321
         );
322 322
         $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey(
323
-            ParagonIE_Sodium_Compat::crypto_box_secretkey($ecdhKeypair),
323
+            ParagonIE_Sodium_Compat::crypto_box_secretkey( $ecdhKeypair ),
324 324
             $ephemeralPK
325 325
         );
326 326
 
327
-        $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $msgKeypair);
328
-        fclose($ifp);
329
-        fclose($ofp);
327
+        $res = self::box_decrypt( $ifp, $ofp, $size, $nonce, $msgKeypair );
328
+        fclose( $ifp );
329
+        fclose( $ofp );
330 330
         try {
331
-            ParagonIE_Sodium_Compat::memzero($nonce);
332
-            ParagonIE_Sodium_Compat::memzero($ephKeypair);
333
-        } catch (SodiumException $ex) {
334
-            if (isset($ephKeypair)) {
335
-                unset($ephKeypair);
331
+            ParagonIE_Sodium_Compat::memzero( $nonce );
332
+            ParagonIE_Sodium_Compat::memzero( $ephKeypair );
333
+        } catch ( SodiumException $ex ) {
334
+            if ( isset( $ephKeypair ) ) {
335
+                unset( $ephKeypair );
336 336
             }
337 337
         }
338 338
         return $res;
@@ -350,68 +350,68 @@  discard block
 block discarded – undo
350 350
      * @throws TypeError
351 351
      * @psalm-suppress FailedTypeResolution
352 352
      */
353
-    public static function generichash($filePath, $key = '', $outputLength = 32)
353
+    public static function generichash( $filePath, $key = '', $outputLength = 32 )
354 354
     {
355 355
         /* Type checks: */
356
-        if (!is_string($filePath)) {
357
-            throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.');
356
+        if ( ! is_string( $filePath ) ) {
357
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $filePath ) . ' given.' );
358 358
         }
359
-        if (!is_string($key)) {
360
-            if (is_null($key)) {
359
+        if ( ! is_string( $key ) ) {
360
+            if ( is_null( $key ) ) {
361 361
                 $key = '';
362 362
             } else {
363
-                throw new TypeError('Argument 2 must be a string, ' . gettype($key) . ' given.');
363
+                throw new TypeError( 'Argument 2 must be a string, ' . gettype( $key ) . ' given.' );
364 364
             }
365 365
         }
366
-        if (!is_int($outputLength)) {
367
-            if (!is_numeric($outputLength)) {
368
-                throw new TypeError('Argument 3 must be an integer, ' . gettype($outputLength) . ' given.');
366
+        if ( ! is_int( $outputLength ) ) {
367
+            if ( ! is_numeric( $outputLength ) ) {
368
+                throw new TypeError( 'Argument 3 must be an integer, ' . gettype( $outputLength ) . ' given.' );
369 369
             }
370
-            $outputLength = (int) $outputLength;
370
+            $outputLength = (int)$outputLength;
371 371
         }
372 372
 
373 373
         /* Input validation: */
374
-        if (!empty($key)) {
375
-            if (self::strlen($key) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
376
-                throw new TypeError('Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes');
374
+        if ( ! empty( $key ) ) {
375
+            if ( self::strlen( $key ) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN ) {
376
+                throw new TypeError( 'Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes' );
377 377
             }
378
-            if (self::strlen($key) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
379
-                throw new TypeError('Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes');
378
+            if ( self::strlen( $key ) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX ) {
379
+                throw new TypeError( 'Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes' );
380 380
             }
381 381
         }
382
-        if ($outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN) {
383
-            throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN');
382
+        if ( $outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN ) {
383
+            throw new SodiumException( 'Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN' );
384 384
         }
385
-        if ($outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX) {
386
-            throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX');
385
+        if ( $outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX ) {
386
+            throw new SodiumException( 'Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX' );
387 387
         }
388 388
 
389 389
         /** @var int $size */
390
-        $size = filesize($filePath);
391
-        if (!is_int($size)) {
392
-            throw new SodiumException('Could not obtain the file size');
390
+        $size = filesize( $filePath );
391
+        if ( ! is_int( $size ) ) {
392
+            throw new SodiumException( 'Could not obtain the file size' );
393 393
         }
394 394
 
395 395
         /** @var resource $fp */
396
-        $fp = fopen($filePath, 'rb');
397
-        if (!is_resource($fp)) {
398
-            throw new SodiumException('Could not open input file for reading');
396
+        $fp = fopen( $filePath, 'rb' );
397
+        if ( ! is_resource( $fp ) ) {
398
+            throw new SodiumException( 'Could not open input file for reading' );
399 399
         }
400
-        $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outputLength);
401
-        while ($size > 0) {
400
+        $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init( $key, $outputLength );
401
+        while ( $size > 0 ) {
402 402
             $blockSize = $size > 64
403 403
                 ? 64
404 404
                 : $size;
405
-            $read = fread($fp, $blockSize);
406
-            if (!is_string($read)) {
407
-                throw new SodiumException('Could not read input file');
405
+            $read = fread( $fp, $blockSize );
406
+            if ( ! is_string( $read ) ) {
407
+                throw new SodiumException( 'Could not read input file' );
408 408
             }
409
-            ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $read);
409
+            ParagonIE_Sodium_Compat::crypto_generichash_update( $ctx, $read );
410 410
             $size -= $blockSize;
411 411
         }
412 412
 
413
-        fclose($fp);
414
-        return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength);
413
+        fclose( $fp );
414
+        return ParagonIE_Sodium_Compat::crypto_generichash_final( $ctx, $outputLength );
415 415
     }
416 416
 
417 417
     /**
@@ -428,52 +428,52 @@  discard block
 block discarded – undo
428 428
      * @throws SodiumException
429 429
      * @throws TypeError
430 430
      */
431
-    public static function secretbox($inputFile, $outputFile, $nonce, $key)
431
+    public static function secretbox( $inputFile, $outputFile, $nonce, $key )
432 432
     {
433 433
         /* Type checks: */
434
-        if (!is_string($inputFile)) {
435
-            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..');
434
+        if ( ! is_string( $inputFile ) ) {
435
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given..' );
436 436
         }
437
-        if (!is_string($outputFile)) {
438
-            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
437
+        if ( ! is_string( $outputFile ) ) {
438
+            throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' );
439 439
         }
440
-        if (!is_string($nonce)) {
441
-            throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
440
+        if ( ! is_string( $nonce ) ) {
441
+            throw new TypeError( 'Argument 3 must be a string, ' . gettype( $nonce ) . ' given.' );
442 442
         }
443 443
 
444 444
         /* Input validation: */
445
-        if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) {
446
-            throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes');
445
+        if ( self::strlen( $nonce ) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES ) {
446
+            throw new TypeError( 'Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes' );
447 447
         }
448
-        if (!is_string($key)) {
449
-            throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.');
448
+        if ( ! is_string( $key ) ) {
449
+            throw new TypeError( 'Argument 4 must be a string, ' . gettype( $key ) . ' given.' );
450 450
         }
451
-        if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) {
452
-            throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes');
451
+        if ( self::strlen( $key ) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES ) {
452
+            throw new TypeError( 'Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes' );
453 453
         }
454 454
 
455 455
         /** @var int $size */
456
-        $size = filesize($inputFile);
457
-        if (!is_int($size)) {
458
-            throw new SodiumException('Could not obtain the file size');
456
+        $size = filesize( $inputFile );
457
+        if ( ! is_int( $size ) ) {
458
+            throw new SodiumException( 'Could not obtain the file size' );
459 459
         }
460 460
 
461 461
         /** @var resource $ifp */
462
-        $ifp = fopen($inputFile, 'rb');
463
-        if (!is_resource($ifp)) {
464
-            throw new SodiumException('Could not open input file for reading');
462
+        $ifp = fopen( $inputFile, 'rb' );
463
+        if ( ! is_resource( $ifp ) ) {
464
+            throw new SodiumException( 'Could not open input file for reading' );
465 465
         }
466 466
 
467 467
         /** @var resource $ofp */
468
-        $ofp = fopen($outputFile, 'wb');
469
-        if (!is_resource($ofp)) {
470
-            fclose($ifp);
471
-            throw new SodiumException('Could not open output file for writing');
468
+        $ofp = fopen( $outputFile, 'wb' );
469
+        if ( ! is_resource( $ofp ) ) {
470
+            fclose( $ifp );
471
+            throw new SodiumException( 'Could not open output file for writing' );
472 472
         }
473 473
 
474
-        $res = self::secretbox_encrypt($ifp, $ofp, $size, $nonce, $key);
475
-        fclose($ifp);
476
-        fclose($ofp);
474
+        $res = self::secretbox_encrypt( $ifp, $ofp, $size, $nonce, $key );
475
+        fclose( $ifp );
476
+        fclose( $ofp );
477 477
         return $res;
478 478
     }
479 479
     /**
@@ -493,57 +493,57 @@  discard block
 block discarded – undo
493 493
      * @throws SodiumException
494 494
      * @throws TypeError
495 495
      */
496
-    public static function secretbox_open($inputFile, $outputFile, $nonce, $key)
496
+    public static function secretbox_open( $inputFile, $outputFile, $nonce, $key )
497 497
     {
498 498
         /* Type checks: */
499
-        if (!is_string($inputFile)) {
500
-            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
499
+        if ( ! is_string( $inputFile ) ) {
500
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' );
501 501
         }
502
-        if (!is_string($outputFile)) {
503
-            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
502
+        if ( ! is_string( $outputFile ) ) {
503
+            throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' );
504 504
         }
505
-        if (!is_string($nonce)) {
506
-            throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
505
+        if ( ! is_string( $nonce ) ) {
506
+            throw new TypeError( 'Argument 3 must be a string, ' . gettype( $nonce ) . ' given.' );
507 507
         }
508
-        if (!is_string($key)) {
509
-            throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.');
508
+        if ( ! is_string( $key ) ) {
509
+            throw new TypeError( 'Argument 4 must be a string, ' . gettype( $key ) . ' given.' );
510 510
         }
511 511
 
512 512
         /* Input validation: */
513
-        if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) {
514
-            throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes');
513
+        if ( self::strlen( $nonce ) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES ) {
514
+            throw new TypeError( 'Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes' );
515 515
         }
516
-        if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) {
517
-            throw new TypeError('Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes');
516
+        if ( self::strlen( $key ) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES ) {
517
+            throw new TypeError( 'Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes' );
518 518
         }
519 519
 
520 520
         /** @var int $size */
521
-        $size = filesize($inputFile);
522
-        if (!is_int($size)) {
523
-            throw new SodiumException('Could not obtain the file size');
521
+        $size = filesize( $inputFile );
522
+        if ( ! is_int( $size ) ) {
523
+            throw new SodiumException( 'Could not obtain the file size' );
524 524
         }
525 525
 
526 526
         /** @var resource $ifp */
527
-        $ifp = fopen($inputFile, 'rb');
528
-        if (!is_resource($ifp)) {
529
-            throw new SodiumException('Could not open input file for reading');
527
+        $ifp = fopen( $inputFile, 'rb' );
528
+        if ( ! is_resource( $ifp ) ) {
529
+            throw new SodiumException( 'Could not open input file for reading' );
530 530
         }
531 531
 
532 532
         /** @var resource $ofp */
533
-        $ofp = fopen($outputFile, 'wb');
534
-        if (!is_resource($ofp)) {
535
-            fclose($ifp);
536
-            throw new SodiumException('Could not open output file for writing');
533
+        $ofp = fopen( $outputFile, 'wb' );
534
+        if ( ! is_resource( $ofp ) ) {
535
+            fclose( $ifp );
536
+            throw new SodiumException( 'Could not open output file for writing' );
537 537
         }
538 538
 
539
-        $res = self::secretbox_decrypt($ifp, $ofp, $size, $nonce, $key);
540
-        fclose($ifp);
541
-        fclose($ofp);
539
+        $res = self::secretbox_decrypt( $ifp, $ofp, $size, $nonce, $key );
540
+        fclose( $ifp );
541
+        fclose( $ofp );
542 542
         try {
543
-            ParagonIE_Sodium_Compat::memzero($key);
544
-        } catch (SodiumException $ex) {
543
+            ParagonIE_Sodium_Compat::memzero( $key );
544
+        } catch ( SodiumException $ex ) {
545 545
             /** @psalm-suppress PossiblyUndefinedVariable */
546
-            unset($key);
546
+            unset( $key );
547 547
         }
548 548
         return $res;
549 549
     }
@@ -560,85 +560,85 @@  discard block
 block discarded – undo
560 560
      * @throws SodiumException
561 561
      * @throws TypeError
562 562
      */
563
-    public static function sign($filePath, $secretKey)
563
+    public static function sign( $filePath, $secretKey )
564 564
     {
565 565
         /* Type checks: */
566
-        if (!is_string($filePath)) {
567
-            throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.');
566
+        if ( ! is_string( $filePath ) ) {
567
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $filePath ) . ' given.' );
568 568
         }
569
-        if (!is_string($secretKey)) {
570
-            throw new TypeError('Argument 2 must be a string, ' . gettype($secretKey) . ' given.');
569
+        if ( ! is_string( $secretKey ) ) {
570
+            throw new TypeError( 'Argument 2 must be a string, ' . gettype( $secretKey ) . ' given.' );
571 571
         }
572 572
 
573 573
         /* Input validation: */
574
-        if (self::strlen($secretKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES) {
575
-            throw new TypeError('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes');
574
+        if ( self::strlen( $secretKey ) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES ) {
575
+            throw new TypeError( 'Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes' );
576 576
         }
577
-        if (PHP_INT_SIZE === 4) {
578
-            return self::sign_core32($filePath, $secretKey);
577
+        if ( PHP_INT_SIZE === 4 ) {
578
+            return self::sign_core32( $filePath, $secretKey );
579 579
         }
580 580
 
581 581
         /** @var int $size */
582
-        $size = filesize($filePath);
583
-        if (!is_int($size)) {
584
-            throw new SodiumException('Could not obtain the file size');
582
+        $size = filesize( $filePath );
583
+        if ( ! is_int( $size ) ) {
584
+            throw new SodiumException( 'Could not obtain the file size' );
585 585
         }
586 586
 
587 587
         /** @var resource $fp */
588
-        $fp = fopen($filePath, 'rb');
589
-        if (!is_resource($fp)) {
590
-            throw new SodiumException('Could not open input file for reading');
588
+        $fp = fopen( $filePath, 'rb' );
589
+        if ( ! is_resource( $fp ) ) {
590
+            throw new SodiumException( 'Could not open input file for reading' );
591 591
         }
592 592
 
593 593
         /** @var string $az */
594
-        $az = hash('sha512', self::substr($secretKey, 0, 32), true);
594
+        $az = hash( 'sha512', self::substr( $secretKey, 0, 32 ), true );
595 595
 
596
-        $az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
597
-        $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
596
+        $az[ 0 ] = self::intToChr( self::chrToInt( $az[ 0 ] ) & 248 );
597
+        $az[ 31 ] = self::intToChr( ( self::chrToInt( $az[ 31 ] ) & 63 ) | 64 );
598 598
 
599
-        $hs = hash_init('sha512');
600
-        self::hash_update($hs, self::substr($az, 32, 32));
599
+        $hs = hash_init( 'sha512' );
600
+        self::hash_update( $hs, self::substr( $az, 32, 32 ) );
601 601
         /** @var resource $hs */
602
-        $hs = self::updateHashWithFile($hs, $fp, $size);
602
+        $hs = self::updateHashWithFile( $hs, $fp, $size );
603 603
 
604 604
         /** @var string $nonceHash */
605
-        $nonceHash = hash_final($hs, true);
605
+        $nonceHash = hash_final( $hs, true );
606 606
 
607 607
         /** @var string $pk */
608
-        $pk = self::substr($secretKey, 32, 32);
608
+        $pk = self::substr( $secretKey, 32, 32 );
609 609
 
610 610
         /** @var string $nonce */
611
-        $nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
611
+        $nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce( $nonceHash ) . self::substr( $nonceHash, 32 );
612 612
 
613 613
         /** @var string $sig */
614 614
         $sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes(
615
-            ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base($nonce)
615
+            ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base( $nonce )
616 616
         );
617 617
 
618
-        $hs = hash_init('sha512');
619
-        self::hash_update($hs, self::substr($sig, 0, 32));
620
-        self::hash_update($hs, self::substr($pk, 0, 32));
618
+        $hs = hash_init( 'sha512' );
619
+        self::hash_update( $hs, self::substr( $sig, 0, 32 ) );
620
+        self::hash_update( $hs, self::substr( $pk, 0, 32 ) );
621 621
         /** @var resource $hs */
622
-        $hs = self::updateHashWithFile($hs, $fp, $size);
622
+        $hs = self::updateHashWithFile( $hs, $fp, $size );
623 623
 
624 624
         /** @var string $hramHash */
625
-        $hramHash = hash_final($hs, true);
625
+        $hramHash = hash_final( $hs, true );
626 626
 
627 627
         /** @var string $hram */
628
-        $hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hramHash);
628
+        $hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce( $hramHash );
629 629
 
630 630
         /** @var string $sigAfter */
631
-        $sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd($hram, $az, $nonce);
631
+        $sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd( $hram, $az, $nonce );
632 632
 
633 633
         /** @var string $sig */
634
-        $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
634
+        $sig = self::substr( $sig, 0, 32 ) . self::substr( $sigAfter, 0, 32 );
635 635
 
636 636
         try {
637
-            ParagonIE_Sodium_Compat::memzero($az);
638
-        } catch (SodiumException $ex) {
637
+            ParagonIE_Sodium_Compat::memzero( $az );
638
+        } catch ( SodiumException $ex ) {
639 639
             $az = null;
640 640
         }
641
-        fclose($fp);
641
+        fclose( $fp );
642 642
         return $sig;
643 643
     }
644 644
 
@@ -656,66 +656,66 @@  discard block
 block discarded – undo
656 656
      * @throws TypeError
657 657
      * @throws Exception
658 658
      */
659
-    public static function verify($sig, $filePath, $publicKey)
659
+    public static function verify( $sig, $filePath, $publicKey )
660 660
     {
661 661
         /* Type checks: */
662
-        if (!is_string($sig)) {
663
-            throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.');
662
+        if ( ! is_string( $sig ) ) {
663
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $sig ) . ' given.' );
664 664
         }
665
-        if (!is_string($filePath)) {
666
-            throw new TypeError('Argument 2 must be a string, ' . gettype($filePath) . ' given.');
665
+        if ( ! is_string( $filePath ) ) {
666
+            throw new TypeError( 'Argument 2 must be a string, ' . gettype( $filePath ) . ' given.' );
667 667
         }
668
-        if (!is_string($publicKey)) {
669
-            throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.');
668
+        if ( ! is_string( $publicKey ) ) {
669
+            throw new TypeError( 'Argument 3 must be a string, ' . gettype( $publicKey ) . ' given.' );
670 670
         }
671 671
 
672 672
         /* Input validation: */
673
-        if (self::strlen($sig) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES) {
674
-            throw new TypeError('Argument 1 must be CRYPTO_SIGN_BYTES bytes');
673
+        if ( self::strlen( $sig ) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES ) {
674
+            throw new TypeError( 'Argument 1 must be CRYPTO_SIGN_BYTES bytes' );
675 675
         }
676
-        if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES) {
677
-            throw new TypeError('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes');
676
+        if ( self::strlen( $publicKey ) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES ) {
677
+            throw new TypeError( 'Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes' );
678 678
         }
679
-        if (self::strlen($sig) < 64) {
680
-            throw new SodiumException('Signature is too short');
679
+        if ( self::strlen( $sig ) < 64 ) {
680
+            throw new SodiumException( 'Signature is too short' );
681 681
         }
682 682
 
683
-        if (PHP_INT_SIZE === 4) {
684
-            return self::verify_core32($sig, $filePath, $publicKey);
683
+        if ( PHP_INT_SIZE === 4 ) {
684
+            return self::verify_core32( $sig, $filePath, $publicKey );
685 685
         }
686 686
 
687 687
         /* Security checks */
688 688
         if (
689
-            (ParagonIE_Sodium_Core_Ed25519::chrToInt($sig[63]) & 240)
689
+            ( ParagonIE_Sodium_Core_Ed25519::chrToInt( $sig[ 63 ] ) & 240 )
690 690
                 &&
691
-            ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))
691
+            ParagonIE_Sodium_Core_Ed25519::check_S_lt_L( self::substr( $sig, 32, 32 ) )
692 692
         ) {
693
-            throw new SodiumException('S < L - Invalid signature');
693
+            throw new SodiumException( 'S < L - Invalid signature' );
694 694
         }
695
-        if (ParagonIE_Sodium_Core_Ed25519::small_order($sig)) {
696
-            throw new SodiumException('Signature is on too small of an order');
695
+        if ( ParagonIE_Sodium_Core_Ed25519::small_order( $sig ) ) {
696
+            throw new SodiumException( 'Signature is on too small of an order' );
697 697
         }
698
-        if ((self::chrToInt($sig[63]) & 224) !== 0) {
699
-            throw new SodiumException('Invalid signature');
698
+        if ( ( self::chrToInt( $sig[ 63 ] ) & 224 ) !== 0 ) {
699
+            throw new SodiumException( 'Invalid signature' );
700 700
         }
701 701
         $d = 0;
702
-        for ($i = 0; $i < 32; ++$i) {
703
-            $d |= self::chrToInt($publicKey[$i]);
702
+        for ( $i = 0; $i < 32; ++$i ) {
703
+            $d |= self::chrToInt( $publicKey[ $i ] );
704 704
         }
705
-        if ($d === 0) {
706
-            throw new SodiumException('All zero public key');
705
+        if ( $d === 0 ) {
706
+            throw new SodiumException( 'All zero public key' );
707 707
         }
708 708
 
709 709
         /** @var int $size */
710
-        $size = filesize($filePath);
711
-        if (!is_int($size)) {
712
-            throw new SodiumException('Could not obtain the file size');
710
+        $size = filesize( $filePath );
711
+        if ( ! is_int( $size ) ) {
712
+            throw new SodiumException( 'Could not obtain the file size' );
713 713
         }
714 714
 
715 715
         /** @var resource $fp */
716
-        $fp = fopen($filePath, 'rb');
717
-        if (!is_resource($fp)) {
718
-            throw new SodiumException('Could not open input file for reading');
716
+        $fp = fopen( $filePath, 'rb' );
717
+        if ( ! is_resource( $fp ) ) {
718
+            throw new SodiumException( 'Could not open input file for reading' );
719 719
         }
720 720
 
721 721
         /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
@@ -725,35 +725,35 @@  discard block
 block discarded – undo
725 725
         ParagonIE_Sodium_Compat::$fastMult = true;
726 726
 
727 727
         /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */
728
-        $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime($publicKey);
728
+        $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime( $publicKey );
729 729
 
730
-        $hs = hash_init('sha512');
731
-        self::hash_update($hs, self::substr($sig, 0, 32));
732
-        self::hash_update($hs, self::substr($publicKey, 0, 32));
730
+        $hs = hash_init( 'sha512' );
731
+        self::hash_update( $hs, self::substr( $sig, 0, 32 ) );
732
+        self::hash_update( $hs, self::substr( $publicKey, 0, 32 ) );
733 733
         /** @var resource $hs */
734
-        $hs = self::updateHashWithFile($hs, $fp, $size);
734
+        $hs = self::updateHashWithFile( $hs, $fp, $size );
735 735
         /** @var string $hDigest */
736
-        $hDigest = hash_final($hs, true);
736
+        $hDigest = hash_final( $hs, true );
737 737
 
738 738
         /** @var string $h */
739
-        $h = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32);
739
+        $h = ParagonIE_Sodium_Core_Ed25519::sc_reduce( $hDigest ) . self::substr( $hDigest, 32 );
740 740
 
741 741
         /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */
742 742
         $R = ParagonIE_Sodium_Core_Ed25519::ge_double_scalarmult_vartime(
743 743
             $h,
744 744
             $A,
745
-            self::substr($sig, 32)
745
+            self::substr( $sig, 32 )
746 746
         );
747 747
 
748 748
         /** @var string $rcheck */
749
-        $rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes($R);
749
+        $rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes( $R );
750 750
 
751 751
         // Close the file handle
752
-        fclose($fp);
752
+        fclose( $fp );
753 753
 
754 754
         // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
755 755
         ParagonIE_Sodium_Compat::$fastMult = $orig;
756
-        return self::verify_32($rcheck, self::substr($sig, 0, 32));
756
+        return self::verify_32( $rcheck, self::substr( $sig, 0, 32 ) );
757 757
     }
758 758
 
759 759
     /**
@@ -766,17 +766,17 @@  discard block
 block discarded – undo
766 766
      * @throws SodiumException
767 767
      * @throws TypeError
768 768
      */
769
-    protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair)
769
+    protected static function box_encrypt( $ifp, $ofp, $mlen, $nonce, $boxKeypair )
770 770
     {
771
-        if (PHP_INT_SIZE === 4) {
771
+        if ( PHP_INT_SIZE === 4 ) {
772 772
             return self::secretbox_encrypt(
773 773
                 $ifp,
774 774
                 $ofp,
775 775
                 $mlen,
776 776
                 $nonce,
777 777
                 ParagonIE_Sodium_Crypto32::box_beforenm(
778
-                    ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair),
779
-                    ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair)
778
+                    ParagonIE_Sodium_Crypto32::box_secretkey( $boxKeypair ),
779
+                    ParagonIE_Sodium_Crypto32::box_publickey( $boxKeypair )
780 780
                 )
781 781
             );
782 782
         }
@@ -786,8 +786,8 @@  discard block
 block discarded – undo
786 786
             $mlen,
787 787
             $nonce,
788 788
             ParagonIE_Sodium_Crypto::box_beforenm(
789
-                ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair),
790
-                ParagonIE_Sodium_Crypto::box_publickey($boxKeypair)
789
+                ParagonIE_Sodium_Crypto::box_secretkey( $boxKeypair ),
790
+                ParagonIE_Sodium_Crypto::box_publickey( $boxKeypair )
791 791
             )
792 792
         );
793 793
     }
@@ -803,17 +803,17 @@  discard block
 block discarded – undo
803 803
      * @throws SodiumException
804 804
      * @throws TypeError
805 805
      */
806
-    protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair)
806
+    protected static function box_decrypt( $ifp, $ofp, $mlen, $nonce, $boxKeypair )
807 807
     {
808
-        if (PHP_INT_SIZE === 4) {
808
+        if ( PHP_INT_SIZE === 4 ) {
809 809
             return self::secretbox_decrypt(
810 810
                 $ifp,
811 811
                 $ofp,
812 812
                 $mlen,
813 813
                 $nonce,
814 814
                 ParagonIE_Sodium_Crypto32::box_beforenm(
815
-                    ParagonIE_Sodium_Crypto32::box_secretkey($boxKeypair),
816
-                    ParagonIE_Sodium_Crypto32::box_publickey($boxKeypair)
815
+                    ParagonIE_Sodium_Crypto32::box_secretkey( $boxKeypair ),
816
+                    ParagonIE_Sodium_Crypto32::box_publickey( $boxKeypair )
817 817
                 )
818 818
             );
819 819
         }
@@ -823,8 +823,8 @@  discard block
 block discarded – undo
823 823
             $mlen,
824 824
             $nonce,
825 825
             ParagonIE_Sodium_Crypto::box_beforenm(
826
-                ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair),
827
-                ParagonIE_Sodium_Crypto::box_publickey($boxKeypair)
826
+                ParagonIE_Sodium_Crypto::box_secretkey( $boxKeypair ),
827
+                ParagonIE_Sodium_Crypto::box_publickey( $boxKeypair )
828 828
             )
829 829
         );
830 830
     }
@@ -841,33 +841,33 @@  discard block
 block discarded – undo
841 841
      * @throws SodiumException
842 842
      * @throws TypeError
843 843
      */
844
-    protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key)
844
+    protected static function secretbox_encrypt( $ifp, $ofp, $mlen, $nonce, $key )
845 845
     {
846
-        if (PHP_INT_SIZE === 4) {
847
-            return self::secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key);
846
+        if ( PHP_INT_SIZE === 4 ) {
847
+            return self::secretbox_encrypt_core32( $ifp, $ofp, $mlen, $nonce, $key );
848 848
         }
849 849
 
850
-        $plaintext = fread($ifp, 32);
851
-        if (!is_string($plaintext)) {
852
-            throw new SodiumException('Could not read input file');
850
+        $plaintext = fread( $ifp, 32 );
851
+        if ( ! is_string( $plaintext ) ) {
852
+            throw new SodiumException( 'Could not read input file' );
853 853
         }
854
-        $first32 = self::ftell($ifp);
854
+        $first32 = self::ftell( $ifp );
855 855
 
856 856
         /** @var string $subkey */
857
-        $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
857
+        $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20( $nonce, $key );
858 858
 
859 859
         /** @var string $realNonce */
860
-        $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
860
+        $realNonce = ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 );
861 861
 
862 862
         /** @var string $block0 */
863
-        $block0 = str_repeat("\x00", 32);
863
+        $block0 = str_repeat( "\x00", 32 );
864 864
 
865 865
         /** @var int $mlen - Length of the plaintext message */
866 866
         $mlen0 = $mlen;
867
-        if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) {
867
+        if ( $mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES ) {
868 868
             $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES;
869 869
         }
870
-        $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0);
870
+        $block0 .= ParagonIE_Sodium_Core_Util::substr( $plaintext, 0, $mlen0 );
871 871
 
872 872
         /** @var string $block0 */
873 873
         $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor(
@@ -885,16 +885,16 @@  discard block
 block discarded – undo
885 885
         );
886 886
 
887 887
         // Pre-write 16 blank bytes for the Poly1305 tag
888
-        $start = self::ftell($ofp);
889
-        fwrite($ofp, str_repeat("\x00", 16));
888
+        $start = self::ftell( $ofp );
889
+        fwrite( $ofp, str_repeat( "\x00", 16 ) );
890 890
 
891 891
         /** @var string $c */
892 892
         $cBlock = ParagonIE_Sodium_Core_Util::substr(
893 893
             $block0,
894 894
             ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES
895 895
         );
896
-        $state->update($cBlock);
897
-        fwrite($ofp, $cBlock);
896
+        $state->update( $cBlock );
897
+        fwrite( $ofp, $cBlock );
898 898
         $mlen -= 32;
899 899
 
900 900
         /** @var int $iter */
@@ -907,15 +907,15 @@  discard block
 block discarded – undo
907 907
          * Set the cursor to the end of the first half-block. All future bytes will
908 908
          * generated from salsa20_xor_ic, starting from 1 (second block).
909 909
          */
910
-        fseek($ifp, $first32, SEEK_SET);
910
+        fseek( $ifp, $first32, SEEK_SET );
911 911
 
912
-        while ($mlen > 0) {
912
+        while ( $mlen > 0 ) {
913 913
             $blockSize = $mlen > self::BUFFER_SIZE
914 914
                 ? self::BUFFER_SIZE
915 915
                 : $mlen;
916
-            $plaintext = fread($ifp, $blockSize);
917
-            if (!is_string($plaintext)) {
918
-                throw new SodiumException('Could not read input file');
916
+            $plaintext = fread( $ifp, $blockSize );
917
+            if ( ! is_string( $plaintext ) ) {
918
+                throw new SodiumException( 'Could not read input file' );
919 919
             }
920 920
             $cBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
921 921
                 $plaintext,
@@ -923,29 +923,29 @@  discard block
 block discarded – undo
923 923
                 $iter,
924 924
                 $subkey
925 925
             );
926
-            fwrite($ofp, $cBlock, $blockSize);
927
-            $state->update($cBlock);
926
+            fwrite( $ofp, $cBlock, $blockSize );
927
+            $state->update( $cBlock );
928 928
 
929 929
             $mlen -= $blockSize;
930 930
             $iter += $incr;
931 931
         }
932 932
         try {
933
-            ParagonIE_Sodium_Compat::memzero($block0);
934
-            ParagonIE_Sodium_Compat::memzero($subkey);
935
-        } catch (SodiumException $ex) {
933
+            ParagonIE_Sodium_Compat::memzero( $block0 );
934
+            ParagonIE_Sodium_Compat::memzero( $subkey );
935
+        } catch ( SodiumException $ex ) {
936 936
             $block0 = null;
937 937
             $subkey = null;
938 938
         }
939
-        $end = self::ftell($ofp);
939
+        $end = self::ftell( $ofp );
940 940
 
941 941
         /*
942 942
          * Write the Poly1305 authentication tag that provides integrity
943 943
          * over the ciphertext (encrypt-then-MAC)
944 944
          */
945
-        fseek($ofp, $start, SEEK_SET);
946
-        fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES);
947
-        fseek($ofp, $end, SEEK_SET);
948
-        unset($state);
945
+        fseek( $ofp, $start, SEEK_SET );
946
+        fwrite( $ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES );
947
+        fseek( $ofp, $end, SEEK_SET );
948
+        unset( $state );
949 949
 
950 950
         return true;
951 951
     }
@@ -962,49 +962,49 @@  discard block
 block discarded – undo
962 962
      * @throws SodiumException
963 963
      * @throws TypeError
964 964
      */
965
-    protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key)
965
+    protected static function secretbox_decrypt( $ifp, $ofp, $mlen, $nonce, $key )
966 966
     {
967
-        if (PHP_INT_SIZE === 4) {
968
-            return self::secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key);
967
+        if ( PHP_INT_SIZE === 4 ) {
968
+            return self::secretbox_decrypt_core32( $ifp, $ofp, $mlen, $nonce, $key );
969 969
         }
970
-        $tag = fread($ifp, 16);
971
-        if (!is_string($tag)) {
972
-            throw new SodiumException('Could not read input file');
970
+        $tag = fread( $ifp, 16 );
971
+        if ( ! is_string( $tag ) ) {
972
+            throw new SodiumException( 'Could not read input file' );
973 973
         }
974 974
 
975 975
         /** @var string $subkey */
976
-        $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key);
976
+        $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20( $nonce, $key );
977 977
 
978 978
         /** @var string $realNonce */
979
-        $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8);
979
+        $realNonce = ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 );
980 980
 
981 981
         /** @var string $block0 */
982 982
         $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20(
983 983
             64,
984
-            ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8),
984
+            ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ),
985 985
             $subkey
986 986
         );
987 987
 
988 988
         /* Verify the Poly1305 MAC -before- attempting to decrypt! */
989
-        $state = new ParagonIE_Sodium_Core_Poly1305_State(self::substr($block0, 0, 32));
990
-        if (!self::onetimeauth_verify($state, $ifp, $tag, $mlen)) {
991
-            throw new SodiumException('Invalid MAC');
989
+        $state = new ParagonIE_Sodium_Core_Poly1305_State( self::substr( $block0, 0, 32 ) );
990
+        if ( ! self::onetimeauth_verify( $state, $ifp, $tag, $mlen ) ) {
991
+            throw new SodiumException( 'Invalid MAC' );
992 992
         }
993 993
 
994 994
         /*
995 995
          * Set the cursor to the end of the first half-block. All future bytes will
996 996
          * generated from salsa20_xor_ic, starting from 1 (second block).
997 997
          */
998
-        $first32 = fread($ifp, 32);
999
-        if (!is_string($first32)) {
1000
-            throw new SodiumException('Could not read input file');
998
+        $first32 = fread( $ifp, 32 );
999
+        if ( ! is_string( $first32 ) ) {
1000
+            throw new SodiumException( 'Could not read input file' );
1001 1001
         }
1002
-        $first32len = self::strlen($first32);
1002
+        $first32len = self::strlen( $first32 );
1003 1003
         fwrite(
1004 1004
             $ofp,
1005 1005
             self::xorStrings(
1006
-                self::substr($block0, 32, $first32len),
1007
-                self::substr($first32, 0, $first32len)
1006
+                self::substr( $block0, 32, $first32len ),
1007
+                self::substr( $first32, 0, $first32len )
1008 1008
             )
1009 1009
         );
1010 1010
         $mlen -= 32;
@@ -1016,13 +1016,13 @@  discard block
 block discarded – undo
1016 1016
         $incr = self::BUFFER_SIZE >> 6;
1017 1017
 
1018 1018
         /* Decrypts ciphertext, writes to output file. */
1019
-        while ($mlen > 0) {
1019
+        while ( $mlen > 0 ) {
1020 1020
             $blockSize = $mlen > self::BUFFER_SIZE
1021 1021
                 ? self::BUFFER_SIZE
1022 1022
                 : $mlen;
1023
-            $ciphertext = fread($ifp, $blockSize);
1024
-            if (!is_string($ciphertext)) {
1025
-                throw new SodiumException('Could not read input file');
1023
+            $ciphertext = fread( $ifp, $blockSize );
1024
+            if ( ! is_string( $ciphertext ) ) {
1025
+                throw new SodiumException( 'Could not read input file' );
1026 1026
             }
1027 1027
             $pBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic(
1028 1028
                 $ciphertext,
@@ -1030,7 +1030,7 @@  discard block
 block discarded – undo
1030 1030
                 $iter,
1031 1031
                 $subkey
1032 1032
             );
1033
-            fwrite($ofp, $pBlock, $blockSize);
1033
+            fwrite( $ofp, $pBlock, $blockSize );
1034 1034
             $mlen -= $blockSize;
1035 1035
             $iter += $incr;
1036 1036
         }
@@ -1053,7 +1053,7 @@  discard block
 block discarded – undo
1053 1053
         $mlen = 0
1054 1054
     ) {
1055 1055
         /** @var int $pos */
1056
-        $pos = self::ftell($ifp);
1056
+        $pos = self::ftell( $ifp );
1057 1057
 
1058 1058
         /** @var int $iter */
1059 1059
         $iter = 1;
@@ -1061,21 +1061,21 @@  discard block
 block discarded – undo
1061 1061
         /** @var int $incr */
1062 1062
         $incr = self::BUFFER_SIZE >> 6;
1063 1063
 
1064
-        while ($mlen > 0) {
1064
+        while ( $mlen > 0 ) {
1065 1065
             $blockSize = $mlen > self::BUFFER_SIZE
1066 1066
                 ? self::BUFFER_SIZE
1067 1067
                 : $mlen;
1068
-            $ciphertext = fread($ifp, $blockSize);
1069
-            if (!is_string($ciphertext)) {
1070
-                throw new SodiumException('Could not read input file');
1068
+            $ciphertext = fread( $ifp, $blockSize );
1069
+            if ( ! is_string( $ciphertext ) ) {
1070
+                throw new SodiumException( 'Could not read input file' );
1071 1071
             }
1072
-            $state->update($ciphertext);
1072
+            $state->update( $ciphertext );
1073 1073
             $mlen -= $blockSize;
1074 1074
             $iter += $incr;
1075 1075
         }
1076
-        $res = ParagonIE_Sodium_Core_Util::verify_16($tag, $state->finish());
1076
+        $res = ParagonIE_Sodium_Core_Util::verify_16( $tag, $state->finish() );
1077 1077
 
1078
-        fseek($ifp, $pos, SEEK_SET);
1078
+        fseek( $ifp, $pos, SEEK_SET );
1079 1079
         return $res;
1080 1080
     }
1081 1081
 
@@ -1095,48 +1095,48 @@  discard block
 block discarded – undo
1095 1095
      * @psalm-suppress TypeCoercion
1096 1096
      *                 Ditto.
1097 1097
      */
1098
-    public static function updateHashWithFile($hash, $fp, $size = 0)
1098
+    public static function updateHashWithFile( $hash, $fp, $size = 0 )
1099 1099
     {
1100 1100
         /* Type checks: */
1101
-        if (PHP_VERSION_ID < 70200) {
1102
-            if (!is_resource($hash)) {
1103
-                throw new TypeError('Argument 1 must be a resource, ' . gettype($hash) . ' given.');
1101
+        if ( PHP_VERSION_ID < 70200 ) {
1102
+            if ( ! is_resource( $hash ) ) {
1103
+                throw new TypeError( 'Argument 1 must be a resource, ' . gettype( $hash ) . ' given.' );
1104 1104
             }
1105 1105
         } else {
1106
-            if (!is_object($hash)) {
1107
-                throw new TypeError('Argument 1 must be an object (PHP 7.2+), ' . gettype($hash) . ' given.');
1106
+            if ( ! is_object( $hash ) ) {
1107
+                throw new TypeError( 'Argument 1 must be an object (PHP 7.2+), ' . gettype( $hash ) . ' given.' );
1108 1108
             }
1109 1109
         }
1110 1110
 
1111
-        if (!is_resource($fp)) {
1112
-            throw new TypeError('Argument 2 must be a resource, ' . gettype($fp) . ' given.');
1111
+        if ( ! is_resource( $fp ) ) {
1112
+            throw new TypeError( 'Argument 2 must be a resource, ' . gettype( $fp ) . ' given.' );
1113 1113
         }
1114
-        if (!is_int($size)) {
1115
-            throw new TypeError('Argument 3 must be an integer, ' . gettype($size) . ' given.');
1114
+        if ( ! is_int( $size ) ) {
1115
+            throw new TypeError( 'Argument 3 must be an integer, ' . gettype( $size ) . ' given.' );
1116 1116
         }
1117 1117
 
1118 1118
         /** @var int $originalPosition */
1119
-        $originalPosition = self::ftell($fp);
1119
+        $originalPosition = self::ftell( $fp );
1120 1120
 
1121 1121
         // Move file pointer to beginning of file
1122
-        fseek($fp, 0, SEEK_SET);
1123
-        for ($i = 0; $i < $size; $i += self::BUFFER_SIZE) {
1122
+        fseek( $fp, 0, SEEK_SET );
1123
+        for ( $i = 0; $i < $size; $i += self::BUFFER_SIZE ) {
1124 1124
             /** @var string|bool $message */
1125 1125
             $message = fread(
1126 1126
                 $fp,
1127
-                ($size - $i) > self::BUFFER_SIZE
1127
+                ( $size - $i ) > self::BUFFER_SIZE
1128 1128
                     ? $size - $i
1129 1129
                     : self::BUFFER_SIZE
1130 1130
             );
1131
-            if (!is_string($message)) {
1132
-                throw new SodiumException('Unexpected error reading from file.');
1131
+            if ( ! is_string( $message ) ) {
1132
+                throw new SodiumException( 'Unexpected error reading from file.' );
1133 1133
             }
1134 1134
             /** @var string $message */
1135 1135
             /** @psalm-suppress InvalidArgument */
1136
-            self::hash_update($hash, $message);
1136
+            self::hash_update( $hash, $message );
1137 1137
         }
1138 1138
         // Reset file pointer's position
1139
-        fseek($fp, $originalPosition, SEEK_SET);
1139
+        fseek( $fp, $originalPosition, SEEK_SET );
1140 1140
         return $hash;
1141 1141
     }
1142 1142
 
@@ -1152,71 +1152,71 @@  discard block
 block discarded – undo
1152 1152
      * @throws SodiumException
1153 1153
      * @throws TypeError
1154 1154
      */
1155
-    private static function sign_core32($filePath, $secretKey)
1155
+    private static function sign_core32( $filePath, $secretKey )
1156 1156
     {
1157 1157
         /** @var int|bool $size */
1158
-        $size = filesize($filePath);
1159
-        if (!is_int($size)) {
1160
-            throw new SodiumException('Could not obtain the file size');
1158
+        $size = filesize( $filePath );
1159
+        if ( ! is_int( $size ) ) {
1160
+            throw new SodiumException( 'Could not obtain the file size' );
1161 1161
         }
1162 1162
         /** @var int $size */
1163 1163
 
1164 1164
         /** @var resource|bool $fp */
1165
-        $fp = fopen($filePath, 'rb');
1166
-        if (!is_resource($fp)) {
1167
-            throw new SodiumException('Could not open input file for reading');
1165
+        $fp = fopen( $filePath, 'rb' );
1166
+        if ( ! is_resource( $fp ) ) {
1167
+            throw new SodiumException( 'Could not open input file for reading' );
1168 1168
         }
1169 1169
         /** @var resource $fp */
1170 1170
 
1171 1171
         /** @var string $az */
1172
-        $az = hash('sha512', self::substr($secretKey, 0, 32), true);
1172
+        $az = hash( 'sha512', self::substr( $secretKey, 0, 32 ), true );
1173 1173
 
1174
-        $az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
1175
-        $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
1174
+        $az[ 0 ] = self::intToChr( self::chrToInt( $az[ 0 ] ) & 248 );
1175
+        $az[ 31 ] = self::intToChr( ( self::chrToInt( $az[ 31 ] ) & 63 ) | 64 );
1176 1176
 
1177
-        $hs = hash_init('sha512');
1178
-        self::hash_update($hs, self::substr($az, 32, 32));
1177
+        $hs = hash_init( 'sha512' );
1178
+        self::hash_update( $hs, self::substr( $az, 32, 32 ) );
1179 1179
         /** @var resource $hs */
1180
-        $hs = self::updateHashWithFile($hs, $fp, $size);
1180
+        $hs = self::updateHashWithFile( $hs, $fp, $size );
1181 1181
 
1182 1182
         /** @var string $nonceHash */
1183
-        $nonceHash = hash_final($hs, true);
1183
+        $nonceHash = hash_final( $hs, true );
1184 1184
 
1185 1185
         /** @var string $pk */
1186
-        $pk = self::substr($secretKey, 32, 32);
1186
+        $pk = self::substr( $secretKey, 32, 32 );
1187 1187
 
1188 1188
         /** @var string $nonce */
1189
-        $nonce = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
1189
+        $nonce = ParagonIE_Sodium_Core32_Ed25519::sc_reduce( $nonceHash ) . self::substr( $nonceHash, 32 );
1190 1190
 
1191 1191
         /** @var string $sig */
1192 1192
         $sig = ParagonIE_Sodium_Core32_Ed25519::ge_p3_tobytes(
1193
-            ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base($nonce)
1193
+            ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base( $nonce )
1194 1194
         );
1195 1195
 
1196
-        $hs = hash_init('sha512');
1197
-        self::hash_update($hs, self::substr($sig, 0, 32));
1198
-        self::hash_update($hs, self::substr($pk, 0, 32));
1196
+        $hs = hash_init( 'sha512' );
1197
+        self::hash_update( $hs, self::substr( $sig, 0, 32 ) );
1198
+        self::hash_update( $hs, self::substr( $pk, 0, 32 ) );
1199 1199
         /** @var resource $hs */
1200
-        $hs = self::updateHashWithFile($hs, $fp, $size);
1200
+        $hs = self::updateHashWithFile( $hs, $fp, $size );
1201 1201
 
1202 1202
         /** @var string $hramHash */
1203
-        $hramHash = hash_final($hs, true);
1203
+        $hramHash = hash_final( $hs, true );
1204 1204
 
1205 1205
         /** @var string $hram */
1206
-        $hram = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hramHash);
1206
+        $hram = ParagonIE_Sodium_Core32_Ed25519::sc_reduce( $hramHash );
1207 1207
 
1208 1208
         /** @var string $sigAfter */
1209
-        $sigAfter = ParagonIE_Sodium_Core32_Ed25519::sc_muladd($hram, $az, $nonce);
1209
+        $sigAfter = ParagonIE_Sodium_Core32_Ed25519::sc_muladd( $hram, $az, $nonce );
1210 1210
 
1211 1211
         /** @var string $sig */
1212
-        $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
1212
+        $sig = self::substr( $sig, 0, 32 ) . self::substr( $sigAfter, 0, 32 );
1213 1213
 
1214 1214
         try {
1215
-            ParagonIE_Sodium_Compat::memzero($az);
1216
-        } catch (SodiumException $ex) {
1215
+            ParagonIE_Sodium_Compat::memzero( $az );
1216
+        } catch ( SodiumException $ex ) {
1217 1217
             $az = null;
1218 1218
         }
1219
-        fclose($fp);
1219
+        fclose( $fp );
1220 1220
         return $sig;
1221 1221
     }
1222 1222
 
@@ -1234,37 +1234,37 @@  discard block
 block discarded – undo
1234 1234
      * @throws SodiumException
1235 1235
      * @throws Exception
1236 1236
      */
1237
-    public static function verify_core32($sig, $filePath, $publicKey)
1237
+    public static function verify_core32( $sig, $filePath, $publicKey )
1238 1238
     {
1239 1239
         /* Security checks */
1240
-        if (ParagonIE_Sodium_Core32_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) {
1241
-            throw new SodiumException('S < L - Invalid signature');
1240
+        if ( ParagonIE_Sodium_Core32_Ed25519::check_S_lt_L( self::substr( $sig, 32, 32 ) ) ) {
1241
+            throw new SodiumException( 'S < L - Invalid signature' );
1242 1242
         }
1243
-        if (ParagonIE_Sodium_Core32_Ed25519::small_order($sig)) {
1244
-            throw new SodiumException('Signature is on too small of an order');
1243
+        if ( ParagonIE_Sodium_Core32_Ed25519::small_order( $sig ) ) {
1244
+            throw new SodiumException( 'Signature is on too small of an order' );
1245 1245
         }
1246
-        if ((self::chrToInt($sig[63]) & 224) !== 0) {
1247
-            throw new SodiumException('Invalid signature');
1246
+        if ( ( self::chrToInt( $sig[ 63 ] ) & 224 ) !== 0 ) {
1247
+            throw new SodiumException( 'Invalid signature' );
1248 1248
         }
1249 1249
         $d = 0;
1250
-        for ($i = 0; $i < 32; ++$i) {
1251
-            $d |= self::chrToInt($publicKey[$i]);
1250
+        for ( $i = 0; $i < 32; ++$i ) {
1251
+            $d |= self::chrToInt( $publicKey[ $i ] );
1252 1252
         }
1253
-        if ($d === 0) {
1254
-            throw new SodiumException('All zero public key');
1253
+        if ( $d === 0 ) {
1254
+            throw new SodiumException( 'All zero public key' );
1255 1255
         }
1256 1256
 
1257 1257
         /** @var int|bool $size */
1258
-        $size = filesize($filePath);
1259
-        if (!is_int($size)) {
1260
-            throw new SodiumException('Could not obtain the file size');
1258
+        $size = filesize( $filePath );
1259
+        if ( ! is_int( $size ) ) {
1260
+            throw new SodiumException( 'Could not obtain the file size' );
1261 1261
         }
1262 1262
         /** @var int $size */
1263 1263
 
1264 1264
         /** @var resource|bool $fp */
1265
-        $fp = fopen($filePath, 'rb');
1266
-        if (!is_resource($fp)) {
1267
-            throw new SodiumException('Could not open input file for reading');
1265
+        $fp = fopen( $filePath, 'rb' );
1266
+        if ( ! is_resource( $fp ) ) {
1267
+            throw new SodiumException( 'Could not open input file for reading' );
1268 1268
         }
1269 1269
         /** @var resource $fp */
1270 1270
 
@@ -1275,35 +1275,35 @@  discard block
 block discarded – undo
1275 1275
         ParagonIE_Sodium_Compat::$fastMult = true;
1276 1276
 
1277 1277
         /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */
1278
-        $A = ParagonIE_Sodium_Core32_Ed25519::ge_frombytes_negate_vartime($publicKey);
1278
+        $A = ParagonIE_Sodium_Core32_Ed25519::ge_frombytes_negate_vartime( $publicKey );
1279 1279
 
1280
-        $hs = hash_init('sha512');
1281
-        self::hash_update($hs, self::substr($sig, 0, 32));
1282
-        self::hash_update($hs, self::substr($publicKey, 0, 32));
1280
+        $hs = hash_init( 'sha512' );
1281
+        self::hash_update( $hs, self::substr( $sig, 0, 32 ) );
1282
+        self::hash_update( $hs, self::substr( $publicKey, 0, 32 ) );
1283 1283
         /** @var resource $hs */
1284
-        $hs = self::updateHashWithFile($hs, $fp, $size);
1284
+        $hs = self::updateHashWithFile( $hs, $fp, $size );
1285 1285
         /** @var string $hDigest */
1286
-        $hDigest = hash_final($hs, true);
1286
+        $hDigest = hash_final( $hs, true );
1287 1287
 
1288 1288
         /** @var string $h */
1289
-        $h = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32);
1289
+        $h = ParagonIE_Sodium_Core32_Ed25519::sc_reduce( $hDigest ) . self::substr( $hDigest, 32 );
1290 1290
 
1291 1291
         /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P2 $R */
1292 1292
         $R = ParagonIE_Sodium_Core32_Ed25519::ge_double_scalarmult_vartime(
1293 1293
             $h,
1294 1294
             $A,
1295
-            self::substr($sig, 32)
1295
+            self::substr( $sig, 32 )
1296 1296
         );
1297 1297
 
1298 1298
         /** @var string $rcheck */
1299
-        $rcheck = ParagonIE_Sodium_Core32_Ed25519::ge_tobytes($R);
1299
+        $rcheck = ParagonIE_Sodium_Core32_Ed25519::ge_tobytes( $R );
1300 1300
 
1301 1301
         // Close the file handle
1302
-        fclose($fp);
1302
+        fclose( $fp );
1303 1303
 
1304 1304
         // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
1305 1305
         ParagonIE_Sodium_Compat::$fastMult = $orig;
1306
-        return self::verify_32($rcheck, self::substr($sig, 0, 32));
1306
+        return self::verify_32( $rcheck, self::substr( $sig, 0, 32 ) );
1307 1307
     }
1308 1308
 
1309 1309
     /**
@@ -1318,29 +1318,29 @@  discard block
 block discarded – undo
1318 1318
      * @throws SodiumException
1319 1319
      * @throws TypeError
1320 1320
      */
1321
-    protected static function secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key)
1321
+    protected static function secretbox_encrypt_core32( $ifp, $ofp, $mlen, $nonce, $key )
1322 1322
     {
1323
-        $plaintext = fread($ifp, 32);
1324
-        if (!is_string($plaintext)) {
1325
-            throw new SodiumException('Could not read input file');
1323
+        $plaintext = fread( $ifp, 32 );
1324
+        if ( ! is_string( $plaintext ) ) {
1325
+            throw new SodiumException( 'Could not read input file' );
1326 1326
         }
1327
-        $first32 = self::ftell($ifp);
1327
+        $first32 = self::ftell( $ifp );
1328 1328
 
1329 1329
         /** @var string $subkey */
1330
-        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
1330
+        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20( $nonce, $key );
1331 1331
 
1332 1332
         /** @var string $realNonce */
1333
-        $realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
1333
+        $realNonce = ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 );
1334 1334
 
1335 1335
         /** @var string $block0 */
1336
-        $block0 = str_repeat("\x00", 32);
1336
+        $block0 = str_repeat( "\x00", 32 );
1337 1337
 
1338 1338
         /** @var int $mlen - Length of the plaintext message */
1339 1339
         $mlen0 = $mlen;
1340
-        if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) {
1340
+        if ( $mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES ) {
1341 1341
             $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES;
1342 1342
         }
1343
-        $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
1343
+        $block0 .= ParagonIE_Sodium_Core32_Util::substr( $plaintext, 0, $mlen0 );
1344 1344
 
1345 1345
         /** @var string $block0 */
1346 1346
         $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor(
@@ -1358,16 +1358,16 @@  discard block
 block discarded – undo
1358 1358
         );
1359 1359
 
1360 1360
         // Pre-write 16 blank bytes for the Poly1305 tag
1361
-        $start = self::ftell($ofp);
1362
-        fwrite($ofp, str_repeat("\x00", 16));
1361
+        $start = self::ftell( $ofp );
1362
+        fwrite( $ofp, str_repeat( "\x00", 16 ) );
1363 1363
 
1364 1364
         /** @var string $c */
1365 1365
         $cBlock = ParagonIE_Sodium_Core32_Util::substr(
1366 1366
             $block0,
1367 1367
             ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES
1368 1368
         );
1369
-        $state->update($cBlock);
1370
-        fwrite($ofp, $cBlock);
1369
+        $state->update( $cBlock );
1370
+        fwrite( $ofp, $cBlock );
1371 1371
         $mlen -= 32;
1372 1372
 
1373 1373
         /** @var int $iter */
@@ -1380,15 +1380,15 @@  discard block
 block discarded – undo
1380 1380
          * Set the cursor to the end of the first half-block. All future bytes will
1381 1381
          * generated from salsa20_xor_ic, starting from 1 (second block).
1382 1382
          */
1383
-        fseek($ifp, $first32, SEEK_SET);
1383
+        fseek( $ifp, $first32, SEEK_SET );
1384 1384
 
1385
-        while ($mlen > 0) {
1385
+        while ( $mlen > 0 ) {
1386 1386
             $blockSize = $mlen > self::BUFFER_SIZE
1387 1387
                 ? self::BUFFER_SIZE
1388 1388
                 : $mlen;
1389
-            $plaintext = fread($ifp, $blockSize);
1390
-            if (!is_string($plaintext)) {
1391
-                throw new SodiumException('Could not read input file');
1389
+            $plaintext = fread( $ifp, $blockSize );
1390
+            if ( ! is_string( $plaintext ) ) {
1391
+                throw new SodiumException( 'Could not read input file' );
1392 1392
             }
1393 1393
             $cBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
1394 1394
                 $plaintext,
@@ -1396,29 +1396,29 @@  discard block
 block discarded – undo
1396 1396
                 $iter,
1397 1397
                 $subkey
1398 1398
             );
1399
-            fwrite($ofp, $cBlock, $blockSize);
1400
-            $state->update($cBlock);
1399
+            fwrite( $ofp, $cBlock, $blockSize );
1400
+            $state->update( $cBlock );
1401 1401
 
1402 1402
             $mlen -= $blockSize;
1403 1403
             $iter += $incr;
1404 1404
         }
1405 1405
         try {
1406
-            ParagonIE_Sodium_Compat::memzero($block0);
1407
-            ParagonIE_Sodium_Compat::memzero($subkey);
1408
-        } catch (SodiumException $ex) {
1406
+            ParagonIE_Sodium_Compat::memzero( $block0 );
1407
+            ParagonIE_Sodium_Compat::memzero( $subkey );
1408
+        } catch ( SodiumException $ex ) {
1409 1409
             $block0 = null;
1410 1410
             $subkey = null;
1411 1411
         }
1412
-        $end = self::ftell($ofp);
1412
+        $end = self::ftell( $ofp );
1413 1413
 
1414 1414
         /*
1415 1415
          * Write the Poly1305 authentication tag that provides integrity
1416 1416
          * over the ciphertext (encrypt-then-MAC)
1417 1417
          */
1418
-        fseek($ofp, $start, SEEK_SET);
1419
-        fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES);
1420
-        fseek($ofp, $end, SEEK_SET);
1421
-        unset($state);
1418
+        fseek( $ofp, $start, SEEK_SET );
1419
+        fwrite( $ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES );
1420
+        fseek( $ofp, $end, SEEK_SET );
1421
+        unset( $state );
1422 1422
 
1423 1423
         return true;
1424 1424
     }
@@ -1435,46 +1435,46 @@  discard block
 block discarded – undo
1435 1435
      * @throws SodiumException
1436 1436
      * @throws TypeError
1437 1437
      */
1438
-    protected static function secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key)
1438
+    protected static function secretbox_decrypt_core32( $ifp, $ofp, $mlen, $nonce, $key )
1439 1439
     {
1440
-        $tag = fread($ifp, 16);
1441
-        if (!is_string($tag)) {
1442
-            throw new SodiumException('Could not read input file');
1440
+        $tag = fread( $ifp, 16 );
1441
+        if ( ! is_string( $tag ) ) {
1442
+            throw new SodiumException( 'Could not read input file' );
1443 1443
         }
1444 1444
 
1445 1445
         /** @var string $subkey */
1446
-        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
1446
+        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20( $nonce, $key );
1447 1447
 
1448 1448
         /** @var string $realNonce */
1449
-        $realNonce = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
1449
+        $realNonce = ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 );
1450 1450
 
1451 1451
         /** @var string $block0 */
1452 1452
         $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20(
1453 1453
             64,
1454
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1454
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ),
1455 1455
             $subkey
1456 1456
         );
1457 1457
 
1458 1458
         /* Verify the Poly1305 MAC -before- attempting to decrypt! */
1459
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State(self::substr($block0, 0, 32));
1460
-        if (!self::onetimeauth_verify_core32($state, $ifp, $tag, $mlen)) {
1461
-            throw new SodiumException('Invalid MAC');
1459
+        $state = new ParagonIE_Sodium_Core32_Poly1305_State( self::substr( $block0, 0, 32 ) );
1460
+        if ( ! self::onetimeauth_verify_core32( $state, $ifp, $tag, $mlen ) ) {
1461
+            throw new SodiumException( 'Invalid MAC' );
1462 1462
         }
1463 1463
 
1464 1464
         /*
1465 1465
          * Set the cursor to the end of the first half-block. All future bytes will
1466 1466
          * generated from salsa20_xor_ic, starting from 1 (second block).
1467 1467
          */
1468
-        $first32 = fread($ifp, 32);
1469
-        if (!is_string($first32)) {
1470
-            throw new SodiumException('Could not read input file');
1468
+        $first32 = fread( $ifp, 32 );
1469
+        if ( ! is_string( $first32 ) ) {
1470
+            throw new SodiumException( 'Could not read input file' );
1471 1471
         }
1472
-        $first32len = self::strlen($first32);
1472
+        $first32len = self::strlen( $first32 );
1473 1473
         fwrite(
1474 1474
             $ofp,
1475 1475
             self::xorStrings(
1476
-                self::substr($block0, 32, $first32len),
1477
-                self::substr($first32, 0, $first32len)
1476
+                self::substr( $block0, 32, $first32len ),
1477
+                self::substr( $first32, 0, $first32len )
1478 1478
             )
1479 1479
         );
1480 1480
         $mlen -= 32;
@@ -1486,13 +1486,13 @@  discard block
 block discarded – undo
1486 1486
         $incr = self::BUFFER_SIZE >> 6;
1487 1487
 
1488 1488
         /* Decrypts ciphertext, writes to output file. */
1489
-        while ($mlen > 0) {
1489
+        while ( $mlen > 0 ) {
1490 1490
             $blockSize = $mlen > self::BUFFER_SIZE
1491 1491
                 ? self::BUFFER_SIZE
1492 1492
                 : $mlen;
1493
-            $ciphertext = fread($ifp, $blockSize);
1494
-            if (!is_string($ciphertext)) {
1495
-                throw new SodiumException('Could not read input file');
1493
+            $ciphertext = fread( $ifp, $blockSize );
1494
+            if ( ! is_string( $ciphertext ) ) {
1495
+                throw new SodiumException( 'Could not read input file' );
1496 1496
             }
1497 1497
             $pBlock = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
1498 1498
                 $ciphertext,
@@ -1500,7 +1500,7 @@  discard block
 block discarded – undo
1500 1500
                 $iter,
1501 1501
                 $subkey
1502 1502
             );
1503
-            fwrite($ofp, $pBlock, $blockSize);
1503
+            fwrite( $ofp, $pBlock, $blockSize );
1504 1504
             $mlen -= $blockSize;
1505 1505
             $iter += $incr;
1506 1506
         }
@@ -1525,22 +1525,22 @@  discard block
 block discarded – undo
1525 1525
         $mlen = 0
1526 1526
     ) {
1527 1527
         /** @var int $pos */
1528
-        $pos = self::ftell($ifp);
1528
+        $pos = self::ftell( $ifp );
1529 1529
 
1530
-        while ($mlen > 0) {
1530
+        while ( $mlen > 0 ) {
1531 1531
             $blockSize = $mlen > self::BUFFER_SIZE
1532 1532
                 ? self::BUFFER_SIZE
1533 1533
                 : $mlen;
1534
-            $ciphertext = fread($ifp, $blockSize);
1535
-            if (!is_string($ciphertext)) {
1536
-                throw new SodiumException('Could not read input file');
1534
+            $ciphertext = fread( $ifp, $blockSize );
1535
+            if ( ! is_string( $ciphertext ) ) {
1536
+                throw new SodiumException( 'Could not read input file' );
1537 1537
             }
1538
-            $state->update($ciphertext);
1538
+            $state->update( $ciphertext );
1539 1539
             $mlen -= $blockSize;
1540 1540
         }
1541
-        $res = ParagonIE_Sodium_Core32_Util::verify_16($tag, $state->finish());
1541
+        $res = ParagonIE_Sodium_Core32_Util::verify_16( $tag, $state->finish() );
1542 1542
 
1543
-        fseek($ifp, $pos, SEEK_SET);
1543
+        fseek( $ifp, $pos, SEEK_SET );
1544 1544
         return $res;
1545 1545
     }
1546 1546
 
@@ -1549,12 +1549,12 @@  discard block
 block discarded – undo
1549 1549
      * @return int
1550 1550
      * @throws SodiumException
1551 1551
      */
1552
-    private static function ftell($resource)
1552
+    private static function ftell( $resource )
1553 1553
     {
1554
-        $return = ftell($resource);
1555
-        if (!is_int($return)) {
1556
-            throw new SodiumException('ftell() returned false');
1554
+        $return = ftell( $resource );
1555
+        if ( ! is_int( $return ) ) {
1556
+            throw new SodiumException( 'ftell() returned false' );
1557 1557
         }
1558
-        return (int) $return;
1558
+        return (int)$return;
1559 1559
     }
1560 1560
 }
Please login to merge, or discard this patch.
Braces   +20 added lines, -40 removed lines patch added patch discarded remove patch
@@ -6,8 +6,7 @@  discard block
 block discarded – undo
6 6
 /**
7 7
  * Class ParagonIE_Sodium_File
8 8
  */
9
-class ParagonIE_Sodium_File extends ParagonIE_Sodium_Core_Util
10
-{
9
+class ParagonIE_Sodium_File extends ParagonIE_Sodium_Core_Util {
11 10
     /* PHP's default buffer size is 8192 for fread()/fwrite(). */
12 11
     const BUFFER_SIZE = 8192;
13 12
 
@@ -25,8 +24,7 @@  discard block
 block discarded – undo
25 24
      * @throws SodiumException
26 25
      * @throws TypeError
27 26
      */
28
-    public static function box($inputFile, $outputFile, $nonce, $keyPair)
29
-    {
27
+    public static function box($inputFile, $outputFile, $nonce, $keyPair) {
30 28
         /* Type checks: */
31 29
         if (!is_string($inputFile)) {
32 30
             throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
@@ -91,8 +89,7 @@  discard block
 block discarded – undo
91 89
      * @throws SodiumException
92 90
      * @throws TypeError
93 91
      */
94
-    public static function box_open($inputFile, $outputFile, $nonce, $keypair)
95
-    {
92
+    public static function box_open($inputFile, $outputFile, $nonce, $keypair) {
96 93
         /* Type checks: */
97 94
         if (!is_string($inputFile)) {
98 95
             throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
@@ -161,8 +158,7 @@  discard block
 block discarded – undo
161 158
      * @throws SodiumException
162 159
      * @throws TypeError
163 160
      */
164
-    public static function box_seal($inputFile, $outputFile, $publicKey)
165
-    {
161
+    public static function box_seal($inputFile, $outputFile, $publicKey) {
166 162
         /* Type checks: */
167 163
         if (!is_string($inputFile)) {
168 164
             throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
@@ -265,8 +261,7 @@  discard block
 block discarded – undo
265 261
      * @throws SodiumException
266 262
      * @throws TypeError
267 263
      */
268
-    public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair)
269
-    {
264
+    public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair) {
270 265
         /* Type checks: */
271 266
         if (!is_string($inputFile)) {
272 267
             throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
@@ -350,8 +345,7 @@  discard block
 block discarded – undo
350 345
      * @throws TypeError
351 346
      * @psalm-suppress FailedTypeResolution
352 347
      */
353
-    public static function generichash($filePath, $key = '', $outputLength = 32)
354
-    {
348
+    public static function generichash($filePath, $key = '', $outputLength = 32) {
355 349
         /* Type checks: */
356 350
         if (!is_string($filePath)) {
357 351
             throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.');
@@ -428,8 +422,7 @@  discard block
 block discarded – undo
428 422
      * @throws SodiumException
429 423
      * @throws TypeError
430 424
      */
431
-    public static function secretbox($inputFile, $outputFile, $nonce, $key)
432
-    {
425
+    public static function secretbox($inputFile, $outputFile, $nonce, $key) {
433 426
         /* Type checks: */
434 427
         if (!is_string($inputFile)) {
435 428
             throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..');
@@ -493,8 +486,7 @@  discard block
 block discarded – undo
493 486
      * @throws SodiumException
494 487
      * @throws TypeError
495 488
      */
496
-    public static function secretbox_open($inputFile, $outputFile, $nonce, $key)
497
-    {
489
+    public static function secretbox_open($inputFile, $outputFile, $nonce, $key) {
498 490
         /* Type checks: */
499 491
         if (!is_string($inputFile)) {
500 492
             throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
@@ -560,8 +552,7 @@  discard block
 block discarded – undo
560 552
      * @throws SodiumException
561 553
      * @throws TypeError
562 554
      */
563
-    public static function sign($filePath, $secretKey)
564
-    {
555
+    public static function sign($filePath, $secretKey) {
565 556
         /* Type checks: */
566 557
         if (!is_string($filePath)) {
567 558
             throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.');
@@ -656,8 +647,7 @@  discard block
 block discarded – undo
656 647
      * @throws TypeError
657 648
      * @throws Exception
658 649
      */
659
-    public static function verify($sig, $filePath, $publicKey)
660
-    {
650
+    public static function verify($sig, $filePath, $publicKey) {
661 651
         /* Type checks: */
662 652
         if (!is_string($sig)) {
663 653
             throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.');
@@ -766,8 +756,7 @@  discard block
 block discarded – undo
766 756
      * @throws SodiumException
767 757
      * @throws TypeError
768 758
      */
769
-    protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair)
770
-    {
759
+    protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) {
771 760
         if (PHP_INT_SIZE === 4) {
772 761
             return self::secretbox_encrypt(
773 762
                 $ifp,
@@ -803,8 +792,7 @@  discard block
 block discarded – undo
803 792
      * @throws SodiumException
804 793
      * @throws TypeError
805 794
      */
806
-    protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair)
807
-    {
795
+    protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) {
808 796
         if (PHP_INT_SIZE === 4) {
809 797
             return self::secretbox_decrypt(
810 798
                 $ifp,
@@ -841,8 +829,7 @@  discard block
 block discarded – undo
841 829
      * @throws SodiumException
842 830
      * @throws TypeError
843 831
      */
844
-    protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key)
845
-    {
832
+    protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key) {
846 833
         if (PHP_INT_SIZE === 4) {
847 834
             return self::secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key);
848 835
         }
@@ -962,8 +949,7 @@  discard block
 block discarded – undo
962 949
      * @throws SodiumException
963 950
      * @throws TypeError
964 951
      */
965
-    protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key)
966
-    {
952
+    protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key) {
967 953
         if (PHP_INT_SIZE === 4) {
968 954
             return self::secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key);
969 955
         }
@@ -1095,8 +1081,7 @@  discard block
 block discarded – undo
1095 1081
      * @psalm-suppress TypeCoercion
1096 1082
      *                 Ditto.
1097 1083
      */
1098
-    public static function updateHashWithFile($hash, $fp, $size = 0)
1099
-    {
1084
+    public static function updateHashWithFile($hash, $fp, $size = 0) {
1100 1085
         /* Type checks: */
1101 1086
         if (PHP_VERSION_ID < 70200) {
1102 1087
             if (!is_resource($hash)) {
@@ -1152,8 +1137,7 @@  discard block
 block discarded – undo
1152 1137
      * @throws SodiumException
1153 1138
      * @throws TypeError
1154 1139
      */
1155
-    private static function sign_core32($filePath, $secretKey)
1156
-    {
1140
+    private static function sign_core32($filePath, $secretKey) {
1157 1141
         /** @var int|bool $size */
1158 1142
         $size = filesize($filePath);
1159 1143
         if (!is_int($size)) {
@@ -1234,8 +1218,7 @@  discard block
 block discarded – undo
1234 1218
      * @throws SodiumException
1235 1219
      * @throws Exception
1236 1220
      */
1237
-    public static function verify_core32($sig, $filePath, $publicKey)
1238
-    {
1221
+    public static function verify_core32($sig, $filePath, $publicKey) {
1239 1222
         /* Security checks */
1240 1223
         if (ParagonIE_Sodium_Core32_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) {
1241 1224
             throw new SodiumException('S < L - Invalid signature');
@@ -1318,8 +1301,7 @@  discard block
 block discarded – undo
1318 1301
      * @throws SodiumException
1319 1302
      * @throws TypeError
1320 1303
      */
1321
-    protected static function secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key)
1322
-    {
1304
+    protected static function secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key) {
1323 1305
         $plaintext = fread($ifp, 32);
1324 1306
         if (!is_string($plaintext)) {
1325 1307
             throw new SodiumException('Could not read input file');
@@ -1435,8 +1417,7 @@  discard block
 block discarded – undo
1435 1417
      * @throws SodiumException
1436 1418
      * @throws TypeError
1437 1419
      */
1438
-    protected static function secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key)
1439
-    {
1420
+    protected static function secretbox_decrypt_core32($ifp, $ofp, $mlen, $nonce, $key) {
1440 1421
         $tag = fread($ifp, 16);
1441 1422
         if (!is_string($tag)) {
1442 1423
             throw new SodiumException('Could not read input file');
@@ -1549,8 +1530,7 @@  discard block
 block discarded – undo
1549 1530
      * @return int
1550 1531
      * @throws SodiumException
1551 1532
      */
1552
-    private static function ftell($resource)
1553
-    {
1533
+    private static function ftell($resource) {
1554 1534
         $return = ftell($resource);
1555 1535
         if (!is_int($return)) {
1556 1536
             throw new SodiumException('ftell() returned false');
Please login to merge, or discard this patch.
vendor/paragonie/sodium_compat/src/PHP52/SplFixedArray.php 3 patches
Indentation   +174 added lines, -174 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 if (class_exists('SplFixedArray')) {
4
-    return;
4
+	return;
5 5
 }
6 6
 
7 7
 /**
@@ -13,177 +13,177 @@  discard block
 block discarded – undo
13 13
  */
14 14
 class SplFixedArray implements Iterator, ArrayAccess, Countable
15 15
 {
16
-    /** @var array<int, mixed> */
17
-    private $internalArray = array();
18
-
19
-    /** @var int $size */
20
-    private $size = 0;
21
-
22
-    /**
23
-     * SplFixedArray constructor.
24
-     * @param int $size
25
-     */
26
-    public function __construct($size = 0)
27
-    {
28
-        $this->size = $size;
29
-        $this->internalArray = array();
30
-    }
31
-
32
-    /**
33
-     * @return int
34
-     */
35
-    public function count()
36
-    {
37
-        return count($this->internalArray);
38
-    }
39
-
40
-    /**
41
-     * @return array
42
-     */
43
-    public function toArray()
44
-    {
45
-        ksort($this->internalArray);
46
-        return (array) $this->internalArray;
47
-    }
48
-
49
-    /**
50
-     * @param array $array
51
-     * @param bool $save_indexes
52
-     * @return SplFixedArray
53
-     * @psalm-suppress MixedAssignment
54
-     */
55
-    public static function fromArray(array $array, $save_indexes = true)
56
-    {
57
-        $self = new SplFixedArray(count($array));
58
-        if($save_indexes) {
59
-            foreach($array as $key => $value) {
60
-                $self[(int) $key] = $value;
61
-            }
62
-        } else {
63
-            $i = 0;
64
-            foreach (array_values($array) as $value) {
65
-                $self[$i] = $value;
66
-                $i++;
67
-            }
68
-        }
69
-        return $self;
70
-    }
71
-
72
-    /**
73
-     * @return int
74
-     */
75
-    public function getSize()
76
-    {
77
-        return $this->size;
78
-    }
79
-
80
-    /**
81
-     * @param int $size
82
-     * @return bool
83
-     */
84
-    public function setSize($size)
85
-    {
86
-        $this->size = $size;
87
-        return true;
88
-    }
89
-
90
-    /**
91
-     * @param string|int $index
92
-     * @return bool
93
-     */
94
-    public function offsetExists($index)
95
-    {
96
-        return array_key_exists((int) $index, $this->internalArray);
97
-    }
98
-
99
-    /**
100
-     * @param string|int $index
101
-     * @return mixed
102
-     */
103
-    public function offsetGet($index)
104
-    {
105
-        /** @psalm-suppress MixedReturnStatement */
106
-        return $this->internalArray[(int) $index];
107
-    }
108
-
109
-    /**
110
-     * @param string|int $index
111
-     * @param mixed $newval
112
-     * @psalm-suppress MixedAssignment
113
-     */
114
-    public function offsetSet($index, $newval)
115
-    {
116
-        $this->internalArray[(int) $index] = $newval;
117
-    }
118
-
119
-    /**
120
-     * @param string|int $index
121
-     */
122
-    public function offsetUnset($index)
123
-    {
124
-        unset($this->internalArray[(int) $index]);
125
-    }
126
-
127
-    /**
128
-     * Rewind iterator back to the start
129
-     * @link https://php.net/manual/en/splfixedarray.rewind.php
130
-     * @return void
131
-     * @since 5.3.0
132
-     */
133
-    public function rewind()
134
-    {
135
-        reset($this->internalArray);
136
-    }
137
-
138
-    /**
139
-     * Return current array entry
140
-     * @link https://php.net/manual/en/splfixedarray.current.php
141
-     * @return mixed The current element value.
142
-     * @since 5.3.0
143
-     */
144
-    public function current()
145
-    {
146
-        /** @psalm-suppress MixedReturnStatement */
147
-        return current($this->internalArray);
148
-    }
149
-
150
-    /**
151
-     * Return current array index
152
-     * @return int The current array index.
153
-     */
154
-    public function key()
155
-    {
156
-        return key($this->internalArray);
157
-    }
158
-
159
-    /**
160
-     * @return void
161
-     */
162
-    public function next()
163
-    {
164
-        next($this->internalArray);
165
-    }
166
-
167
-    /**
168
-     * Check whether the array contains more elements
169
-     * @link https://php.net/manual/en/splfixedarray.valid.php
170
-     * @return bool true if the array contains any more elements, false otherwise.
171
-     */
172
-    public function valid()
173
-    {
174
-        if (empty($this->internalArray)) {
175
-            return false;
176
-        }
177
-        $result = next($this->internalArray) !== false;
178
-        prev($this->internalArray);
179
-        return $result;
180
-    }
181
-
182
-    /**
183
-     * Do nothing.
184
-     */
185
-    public function __wakeup()
186
-    {
187
-        // NOP
188
-    }
16
+	/** @var array<int, mixed> */
17
+	private $internalArray = array();
18
+
19
+	/** @var int $size */
20
+	private $size = 0;
21
+
22
+	/**
23
+	 * SplFixedArray constructor.
24
+	 * @param int $size
25
+	 */
26
+	public function __construct($size = 0)
27
+	{
28
+		$this->size = $size;
29
+		$this->internalArray = array();
30
+	}
31
+
32
+	/**
33
+	 * @return int
34
+	 */
35
+	public function count()
36
+	{
37
+		return count($this->internalArray);
38
+	}
39
+
40
+	/**
41
+	 * @return array
42
+	 */
43
+	public function toArray()
44
+	{
45
+		ksort($this->internalArray);
46
+		return (array) $this->internalArray;
47
+	}
48
+
49
+	/**
50
+	 * @param array $array
51
+	 * @param bool $save_indexes
52
+	 * @return SplFixedArray
53
+	 * @psalm-suppress MixedAssignment
54
+	 */
55
+	public static function fromArray(array $array, $save_indexes = true)
56
+	{
57
+		$self = new SplFixedArray(count($array));
58
+		if($save_indexes) {
59
+			foreach($array as $key => $value) {
60
+				$self[(int) $key] = $value;
61
+			}
62
+		} else {
63
+			$i = 0;
64
+			foreach (array_values($array) as $value) {
65
+				$self[$i] = $value;
66
+				$i++;
67
+			}
68
+		}
69
+		return $self;
70
+	}
71
+
72
+	/**
73
+	 * @return int
74
+	 */
75
+	public function getSize()
76
+	{
77
+		return $this->size;
78
+	}
79
+
80
+	/**
81
+	 * @param int $size
82
+	 * @return bool
83
+	 */
84
+	public function setSize($size)
85
+	{
86
+		$this->size = $size;
87
+		return true;
88
+	}
89
+
90
+	/**
91
+	 * @param string|int $index
92
+	 * @return bool
93
+	 */
94
+	public function offsetExists($index)
95
+	{
96
+		return array_key_exists((int) $index, $this->internalArray);
97
+	}
98
+
99
+	/**
100
+	 * @param string|int $index
101
+	 * @return mixed
102
+	 */
103
+	public function offsetGet($index)
104
+	{
105
+		/** @psalm-suppress MixedReturnStatement */
106
+		return $this->internalArray[(int) $index];
107
+	}
108
+
109
+	/**
110
+	 * @param string|int $index
111
+	 * @param mixed $newval
112
+	 * @psalm-suppress MixedAssignment
113
+	 */
114
+	public function offsetSet($index, $newval)
115
+	{
116
+		$this->internalArray[(int) $index] = $newval;
117
+	}
118
+
119
+	/**
120
+	 * @param string|int $index
121
+	 */
122
+	public function offsetUnset($index)
123
+	{
124
+		unset($this->internalArray[(int) $index]);
125
+	}
126
+
127
+	/**
128
+	 * Rewind iterator back to the start
129
+	 * @link https://php.net/manual/en/splfixedarray.rewind.php
130
+	 * @return void
131
+	 * @since 5.3.0
132
+	 */
133
+	public function rewind()
134
+	{
135
+		reset($this->internalArray);
136
+	}
137
+
138
+	/**
139
+	 * Return current array entry
140
+	 * @link https://php.net/manual/en/splfixedarray.current.php
141
+	 * @return mixed The current element value.
142
+	 * @since 5.3.0
143
+	 */
144
+	public function current()
145
+	{
146
+		/** @psalm-suppress MixedReturnStatement */
147
+		return current($this->internalArray);
148
+	}
149
+
150
+	/**
151
+	 * Return current array index
152
+	 * @return int The current array index.
153
+	 */
154
+	public function key()
155
+	{
156
+		return key($this->internalArray);
157
+	}
158
+
159
+	/**
160
+	 * @return void
161
+	 */
162
+	public function next()
163
+	{
164
+		next($this->internalArray);
165
+	}
166
+
167
+	/**
168
+	 * Check whether the array contains more elements
169
+	 * @link https://php.net/manual/en/splfixedarray.valid.php
170
+	 * @return bool true if the array contains any more elements, false otherwise.
171
+	 */
172
+	public function valid()
173
+	{
174
+		if (empty($this->internalArray)) {
175
+			return false;
176
+		}
177
+		$result = next($this->internalArray) !== false;
178
+		prev($this->internalArray);
179
+		return $result;
180
+	}
181
+
182
+	/**
183
+	 * Do nothing.
184
+	 */
185
+	public function __wakeup()
186
+	{
187
+		// NOP
188
+	}
189 189
 }
190 190
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-if (class_exists('SplFixedArray')) {
3
+if ( class_exists( 'SplFixedArray' ) ) {
4 4
     return;
5 5
 }
6 6
 
@@ -23,7 +23,7 @@  discard block
 block discarded – undo
23 23
      * SplFixedArray constructor.
24 24
      * @param int $size
25 25
      */
26
-    public function __construct($size = 0)
26
+    public function __construct( $size = 0 )
27 27
     {
28 28
         $this->size = $size;
29 29
         $this->internalArray = array();
@@ -34,7 +34,7 @@  discard block
 block discarded – undo
34 34
      */
35 35
     public function count()
36 36
     {
37
-        return count($this->internalArray);
37
+        return count( $this->internalArray );
38 38
     }
39 39
 
40 40
     /**
@@ -42,8 +42,8 @@  discard block
 block discarded – undo
42 42
      */
43 43
     public function toArray()
44 44
     {
45
-        ksort($this->internalArray);
46
-        return (array) $this->internalArray;
45
+        ksort( $this->internalArray );
46
+        return (array)$this->internalArray;
47 47
     }
48 48
 
49 49
     /**
@@ -52,17 +52,17 @@  discard block
 block discarded – undo
52 52
      * @return SplFixedArray
53 53
      * @psalm-suppress MixedAssignment
54 54
      */
55
-    public static function fromArray(array $array, $save_indexes = true)
55
+    public static function fromArray( array $array, $save_indexes = true )
56 56
     {
57
-        $self = new SplFixedArray(count($array));
58
-        if($save_indexes) {
59
-            foreach($array as $key => $value) {
60
-                $self[(int) $key] = $value;
57
+        $self = new SplFixedArray( count( $array ) );
58
+        if ( $save_indexes ) {
59
+            foreach ( $array as $key => $value ) {
60
+                $self[ (int)$key ] = $value;
61 61
             }
62 62
         } else {
63 63
             $i = 0;
64
-            foreach (array_values($array) as $value) {
65
-                $self[$i] = $value;
64
+            foreach ( array_values( $array ) as $value ) {
65
+                $self[ $i ] = $value;
66 66
                 $i++;
67 67
             }
68 68
         }
@@ -81,7 +81,7 @@  discard block
 block discarded – undo
81 81
      * @param int $size
82 82
      * @return bool
83 83
      */
84
-    public function setSize($size)
84
+    public function setSize( $size )
85 85
     {
86 86
         $this->size = $size;
87 87
         return true;
@@ -91,19 +91,19 @@  discard block
 block discarded – undo
91 91
      * @param string|int $index
92 92
      * @return bool
93 93
      */
94
-    public function offsetExists($index)
94
+    public function offsetExists( $index )
95 95
     {
96
-        return array_key_exists((int) $index, $this->internalArray);
96
+        return array_key_exists( (int)$index, $this->internalArray );
97 97
     }
98 98
 
99 99
     /**
100 100
      * @param string|int $index
101 101
      * @return mixed
102 102
      */
103
-    public function offsetGet($index)
103
+    public function offsetGet( $index )
104 104
     {
105 105
         /** @psalm-suppress MixedReturnStatement */
106
-        return $this->internalArray[(int) $index];
106
+        return $this->internalArray[ (int)$index ];
107 107
     }
108 108
 
109 109
     /**
@@ -111,17 +111,17 @@  discard block
 block discarded – undo
111 111
      * @param mixed $newval
112 112
      * @psalm-suppress MixedAssignment
113 113
      */
114
-    public function offsetSet($index, $newval)
114
+    public function offsetSet( $index, $newval )
115 115
     {
116
-        $this->internalArray[(int) $index] = $newval;
116
+        $this->internalArray[ (int)$index ] = $newval;
117 117
     }
118 118
 
119 119
     /**
120 120
      * @param string|int $index
121 121
      */
122
-    public function offsetUnset($index)
122
+    public function offsetUnset( $index )
123 123
     {
124
-        unset($this->internalArray[(int) $index]);
124
+        unset( $this->internalArray[ (int)$index ] );
125 125
     }
126 126
 
127 127
     /**
@@ -132,7 +132,7 @@  discard block
 block discarded – undo
132 132
      */
133 133
     public function rewind()
134 134
     {
135
-        reset($this->internalArray);
135
+        reset( $this->internalArray );
136 136
     }
137 137
 
138 138
     /**
@@ -144,7 +144,7 @@  discard block
 block discarded – undo
144 144
     public function current()
145 145
     {
146 146
         /** @psalm-suppress MixedReturnStatement */
147
-        return current($this->internalArray);
147
+        return current( $this->internalArray );
148 148
     }
149 149
 
150 150
     /**
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
      */
154 154
     public function key()
155 155
     {
156
-        return key($this->internalArray);
156
+        return key( $this->internalArray );
157 157
     }
158 158
 
159 159
     /**
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
      */
162 162
     public function next()
163 163
     {
164
-        next($this->internalArray);
164
+        next( $this->internalArray );
165 165
     }
166 166
 
167 167
     /**
@@ -171,11 +171,11 @@  discard block
 block discarded – undo
171 171
      */
172 172
     public function valid()
173 173
     {
174
-        if (empty($this->internalArray)) {
174
+        if ( empty( $this->internalArray ) ) {
175 175
             return false;
176 176
         }
177
-        $result = next($this->internalArray) !== false;
178
-        prev($this->internalArray);
177
+        $result = next( $this->internalArray ) !== false;
178
+        prev( $this->internalArray );
179 179
         return $result;
180 180
     }
181 181
 
Please login to merge, or discard this patch.
Braces   +16 added lines, -32 removed lines patch added patch discarded remove patch
@@ -23,8 +23,7 @@  discard block
 block discarded – undo
23 23
      * SplFixedArray constructor.
24 24
      * @param int $size
25 25
      */
26
-    public function __construct($size = 0)
27
-    {
26
+    public function __construct($size = 0) {
28 27
         $this->size = $size;
29 28
         $this->internalArray = array();
30 29
     }
@@ -32,16 +31,14 @@  discard block
 block discarded – undo
32 31
     /**
33 32
      * @return int
34 33
      */
35
-    public function count()
36
-    {
34
+    public function count() {
37 35
         return count($this->internalArray);
38 36
     }
39 37
 
40 38
     /**
41 39
      * @return array
42 40
      */
43
-    public function toArray()
44
-    {
41
+    public function toArray() {
45 42
         ksort($this->internalArray);
46 43
         return (array) $this->internalArray;
47 44
     }
@@ -52,8 +49,7 @@  discard block
 block discarded – undo
52 49
      * @return SplFixedArray
53 50
      * @psalm-suppress MixedAssignment
54 51
      */
55
-    public static function fromArray(array $array, $save_indexes = true)
56
-    {
52
+    public static function fromArray(array $array, $save_indexes = true) {
57 53
         $self = new SplFixedArray(count($array));
58 54
         if($save_indexes) {
59 55
             foreach($array as $key => $value) {
@@ -72,8 +68,7 @@  discard block
 block discarded – undo
72 68
     /**
73 69
      * @return int
74 70
      */
75
-    public function getSize()
76
-    {
71
+    public function getSize() {
77 72
         return $this->size;
78 73
     }
79 74
 
@@ -81,8 +76,7 @@  discard block
 block discarded – undo
81 76
      * @param int $size
82 77
      * @return bool
83 78
      */
84
-    public function setSize($size)
85
-    {
79
+    public function setSize($size) {
86 80
         $this->size = $size;
87 81
         return true;
88 82
     }
@@ -91,8 +85,7 @@  discard block
 block discarded – undo
91 85
      * @param string|int $index
92 86
      * @return bool
93 87
      */
94
-    public function offsetExists($index)
95
-    {
88
+    public function offsetExists($index) {
96 89
         return array_key_exists((int) $index, $this->internalArray);
97 90
     }
98 91
 
@@ -100,8 +93,7 @@  discard block
 block discarded – undo
100 93
      * @param string|int $index
101 94
      * @return mixed
102 95
      */
103
-    public function offsetGet($index)
104
-    {
96
+    public function offsetGet($index) {
105 97
         /** @psalm-suppress MixedReturnStatement */
106 98
         return $this->internalArray[(int) $index];
107 99
     }
@@ -111,16 +103,14 @@  discard block
 block discarded – undo
111 103
      * @param mixed $newval
112 104
      * @psalm-suppress MixedAssignment
113 105
      */
114
-    public function offsetSet($index, $newval)
115
-    {
106
+    public function offsetSet($index, $newval) {
116 107
         $this->internalArray[(int) $index] = $newval;
117 108
     }
118 109
 
119 110
     /**
120 111
      * @param string|int $index
121 112
      */
122
-    public function offsetUnset($index)
123
-    {
113
+    public function offsetUnset($index) {
124 114
         unset($this->internalArray[(int) $index]);
125 115
     }
126 116
 
@@ -130,8 +120,7 @@  discard block
 block discarded – undo
130 120
      * @return void
131 121
      * @since 5.3.0
132 122
      */
133
-    public function rewind()
134
-    {
123
+    public function rewind() {
135 124
         reset($this->internalArray);
136 125
     }
137 126
 
@@ -141,8 +130,7 @@  discard block
 block discarded – undo
141 130
      * @return mixed The current element value.
142 131
      * @since 5.3.0
143 132
      */
144
-    public function current()
145
-    {
133
+    public function current() {
146 134
         /** @psalm-suppress MixedReturnStatement */
147 135
         return current($this->internalArray);
148 136
     }
@@ -151,16 +139,14 @@  discard block
 block discarded – undo
151 139
      * Return current array index
152 140
      * @return int The current array index.
153 141
      */
154
-    public function key()
155
-    {
142
+    public function key() {
156 143
         return key($this->internalArray);
157 144
     }
158 145
 
159 146
     /**
160 147
      * @return void
161 148
      */
162
-    public function next()
163
-    {
149
+    public function next() {
164 150
         next($this->internalArray);
165 151
     }
166 152
 
@@ -169,8 +155,7 @@  discard block
 block discarded – undo
169 155
      * @link https://php.net/manual/en/splfixedarray.valid.php
170 156
      * @return bool true if the array contains any more elements, false otherwise.
171 157
      */
172
-    public function valid()
173
-    {
158
+    public function valid() {
174 159
         if (empty($this->internalArray)) {
175 160
             return false;
176 161
         }
@@ -182,8 +167,7 @@  discard block
 block discarded – undo
182 167
     /**
183 168
      * Do nothing.
184 169
      */
185
-    public function __wakeup()
186
-    {
170
+    public function __wakeup() {
187 171
         // NOP
188 172
     }
189 173
 }
190 174
\ No newline at end of file
Please login to merge, or discard this patch.
vendor/paragonie/sodium_compat/src/Compat.php 3 patches
Indentation   +3914 added lines, -3914 removed lines patch added patch discarded remove patch
@@ -22,3926 +22,3926 @@
 block discarded – undo
22 22
  */
23 23
 
24 24
 if (class_exists('ParagonIE_Sodium_Compat', false)) {
25
-    return;
25
+	return;
26 26
 }
27 27
 
28 28
 class ParagonIE_Sodium_Compat
29 29
 {
30
-    /**
31
-     * This parameter prevents the use of the PECL extension.
32
-     * It should only be used for unit testing.
33
-     *
34
-     * @var bool
35
-     */
36
-    public static $disableFallbackForUnitTests = false;
37
-
38
-    /**
39
-     * Use fast multiplication rather than our constant-time multiplication
40
-     * implementation. Can be enabled at runtime. Only enable this if you
41
-     * are absolutely certain that there is no timing leak on your platform.
42
-     *
43
-     * @var bool
44
-     */
45
-    public static $fastMult = false;
46
-
47
-    const LIBRARY_MAJOR_VERSION = 9;
48
-    const LIBRARY_MINOR_VERSION = 1;
49
-    const LIBRARY_VERSION_MAJOR = 9;
50
-    const LIBRARY_VERSION_MINOR = 1;
51
-    const VERSION_STRING = 'polyfill-1.0.8';
52
-
53
-    // From libsodium
54
-    const BASE64_VARIANT_ORIGINAL = 1;
55
-    const BASE64_VARIANT_ORIGINAL_NO_PADDING = 3;
56
-    const BASE64_VARIANT_URLSAFE = 5;
57
-    const BASE64_VARIANT_URLSAFE_NO_PADDING = 7;
58
-    const CRYPTO_AEAD_AES256GCM_KEYBYTES = 32;
59
-    const CRYPTO_AEAD_AES256GCM_NSECBYTES = 0;
60
-    const CRYPTO_AEAD_AES256GCM_NPUBBYTES = 12;
61
-    const CRYPTO_AEAD_AES256GCM_ABYTES = 16;
62
-    const CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES = 32;
63
-    const CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES = 0;
64
-    const CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES = 8;
65
-    const CRYPTO_AEAD_CHACHA20POLY1305_ABYTES = 16;
66
-    const CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES = 32;
67
-    const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES = 0;
68
-    const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES = 12;
69
-    const CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES = 16;
70
-    const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES = 32;
71
-    const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NSECBYTES = 0;
72
-    const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES = 24;
73
-    const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES = 16;
74
-    const CRYPTO_AUTH_BYTES = 32;
75
-    const CRYPTO_AUTH_KEYBYTES = 32;
76
-    const CRYPTO_BOX_SEALBYTES = 16;
77
-    const CRYPTO_BOX_SECRETKEYBYTES = 32;
78
-    const CRYPTO_BOX_PUBLICKEYBYTES = 32;
79
-    const CRYPTO_BOX_KEYPAIRBYTES = 64;
80
-    const CRYPTO_BOX_MACBYTES = 16;
81
-    const CRYPTO_BOX_NONCEBYTES = 24;
82
-    const CRYPTO_BOX_SEEDBYTES = 32;
83
-    const CRYPTO_CORE_RISTRETTO255_BYTES = 32;
84
-    const CRYPTO_CORE_RISTRETTO255_SCALARBYTES = 32;
85
-    const CRYPTO_CORE_RISTRETTO255_HASHBYTES = 64;
86
-    const CRYPTO_CORE_RISTRETTO255_NONREDUCEDSCALARBYTES = 64;
87
-    const CRYPTO_KDF_BYTES_MIN = 16;
88
-    const CRYPTO_KDF_BYTES_MAX = 64;
89
-    const CRYPTO_KDF_CONTEXTBYTES = 8;
90
-    const CRYPTO_KDF_KEYBYTES = 32;
91
-    const CRYPTO_KX_BYTES = 32;
92
-    const CRYPTO_KX_PRIMITIVE = 'x25519blake2b';
93
-    const CRYPTO_KX_SEEDBYTES = 32;
94
-    const CRYPTO_KX_KEYPAIRBYTES = 64;
95
-    const CRYPTO_KX_PUBLICKEYBYTES = 32;
96
-    const CRYPTO_KX_SECRETKEYBYTES = 32;
97
-    const CRYPTO_KX_SESSIONKEYBYTES = 32;
98
-    const CRYPTO_GENERICHASH_BYTES = 32;
99
-    const CRYPTO_GENERICHASH_BYTES_MIN = 16;
100
-    const CRYPTO_GENERICHASH_BYTES_MAX = 64;
101
-    const CRYPTO_GENERICHASH_KEYBYTES = 32;
102
-    const CRYPTO_GENERICHASH_KEYBYTES_MIN = 16;
103
-    const CRYPTO_GENERICHASH_KEYBYTES_MAX = 64;
104
-    const CRYPTO_PWHASH_SALTBYTES = 16;
105
-    const CRYPTO_PWHASH_STRPREFIX = '$argon2id$';
106
-    const CRYPTO_PWHASH_ALG_ARGON2I13 = 1;
107
-    const CRYPTO_PWHASH_ALG_ARGON2ID13 = 2;
108
-    const CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE = 33554432;
109
-    const CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE = 4;
110
-    const CRYPTO_PWHASH_MEMLIMIT_MODERATE = 134217728;
111
-    const CRYPTO_PWHASH_OPSLIMIT_MODERATE = 6;
112
-    const CRYPTO_PWHASH_MEMLIMIT_SENSITIVE = 536870912;
113
-    const CRYPTO_PWHASH_OPSLIMIT_SENSITIVE = 8;
114
-    const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES = 32;
115
-    const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX = '$7$';
116
-    const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE = 534288;
117
-    const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE = 16777216;
118
-    const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_SENSITIVE = 33554432;
119
-    const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_SENSITIVE = 1073741824;
120
-    const CRYPTO_SCALARMULT_BYTES = 32;
121
-    const CRYPTO_SCALARMULT_SCALARBYTES = 32;
122
-    const CRYPTO_SCALARMULT_RISTRETTO255_BYTES = 32;
123
-    const CRYPTO_SCALARMULT_RISTRETTO255_SCALARBYTES = 32;
124
-    const CRYPTO_SHORTHASH_BYTES = 8;
125
-    const CRYPTO_SHORTHASH_KEYBYTES = 16;
126
-    const CRYPTO_SECRETBOX_KEYBYTES = 32;
127
-    const CRYPTO_SECRETBOX_MACBYTES = 16;
128
-    const CRYPTO_SECRETBOX_NONCEBYTES = 24;
129
-    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES = 17;
130
-    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES = 24;
131
-    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES = 32;
132
-    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH = 0;
133
-    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PULL = 1;
134
-    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY = 2;
135
-    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL = 3;
136
-    const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX = 0x3fffffff80;
137
-    const CRYPTO_SIGN_BYTES = 64;
138
-    const CRYPTO_SIGN_SEEDBYTES = 32;
139
-    const CRYPTO_SIGN_PUBLICKEYBYTES = 32;
140
-    const CRYPTO_SIGN_SECRETKEYBYTES = 64;
141
-    const CRYPTO_SIGN_KEYPAIRBYTES = 96;
142
-    const CRYPTO_STREAM_KEYBYTES = 32;
143
-    const CRYPTO_STREAM_NONCEBYTES = 24;
144
-    const CRYPTO_STREAM_XCHACHA20_KEYBYTES = 32;
145
-    const CRYPTO_STREAM_XCHACHA20_NONCEBYTES = 24;
146
-
147
-    /**
148
-     * Add two numbers (little-endian unsigned), storing the value in the first
149
-     * parameter.
150
-     *
151
-     * This mutates $val.
152
-     *
153
-     * @param string $val
154
-     * @param string $addv
155
-     * @return void
156
-     * @throws SodiumException
157
-     */
158
-    public static function add(&$val, $addv)
159
-    {
160
-        $val_len = ParagonIE_Sodium_Core_Util::strlen($val);
161
-        $addv_len = ParagonIE_Sodium_Core_Util::strlen($addv);
162
-        if ($val_len !== $addv_len) {
163
-            throw new SodiumException('values must have the same length');
164
-        }
165
-        $A = ParagonIE_Sodium_Core_Util::stringToIntArray($val);
166
-        $B = ParagonIE_Sodium_Core_Util::stringToIntArray($addv);
167
-
168
-        $c = 0;
169
-        for ($i = 0; $i < $val_len; $i++) {
170
-            $c += ($A[$i] + $B[$i]);
171
-            $A[$i] = ($c & 0xff);
172
-            $c >>= 8;
173
-        }
174
-        $val = ParagonIE_Sodium_Core_Util::intArrayToString($A);
175
-    }
176
-
177
-    /**
178
-     * @param string $encoded
179
-     * @param int $variant
180
-     * @param string $ignore
181
-     * @return string
182
-     * @throws SodiumException
183
-     */
184
-    public static function base642bin($encoded, $variant, $ignore = '')
185
-    {
186
-        /* Type checks: */
187
-        ParagonIE_Sodium_Core_Util::declareScalarType($encoded, 'string', 1);
188
-
189
-        /** @var string $encoded */
190
-        $encoded = (string) $encoded;
191
-        if (ParagonIE_Sodium_Core_Util::strlen($encoded) === 0) {
192
-            return '';
193
-        }
194
-
195
-        // Just strip before decoding
196
-        if (!empty($ignore)) {
197
-            $encoded = str_replace($ignore, '', $encoded);
198
-        }
199
-
200
-        try {
201
-            switch ($variant) {
202
-                case self::BASE64_VARIANT_ORIGINAL:
203
-                    return ParagonIE_Sodium_Core_Base64_Original::decode($encoded, true);
204
-                case self::BASE64_VARIANT_ORIGINAL_NO_PADDING:
205
-                    return ParagonIE_Sodium_Core_Base64_Original::decode($encoded, false);
206
-                case self::BASE64_VARIANT_URLSAFE:
207
-                    return ParagonIE_Sodium_Core_Base64_UrlSafe::decode($encoded, true);
208
-                case self::BASE64_VARIANT_URLSAFE_NO_PADDING:
209
-                    return ParagonIE_Sodium_Core_Base64_UrlSafe::decode($encoded, false);
210
-                default:
211
-                    throw new SodiumException('invalid base64 variant identifier');
212
-            }
213
-        } catch (Exception $ex) {
214
-            if ($ex instanceof SodiumException) {
215
-                throw $ex;
216
-            }
217
-            throw new SodiumException('invalid base64 string');
218
-        }
219
-    }
220
-
221
-    /**
222
-     * @param string $decoded
223
-     * @param int $variant
224
-     * @return string
225
-     * @throws SodiumException
226
-     */
227
-    public static function bin2base64($decoded, $variant)
228
-    {
229
-        /* Type checks: */
230
-        ParagonIE_Sodium_Core_Util::declareScalarType($decoded, 'string', 1);
231
-        /** @var string $decoded */
232
-        $decoded = (string) $decoded;
233
-        if (ParagonIE_Sodium_Core_Util::strlen($decoded) === 0) {
234
-            return '';
235
-        }
236
-
237
-        switch ($variant) {
238
-            case self::BASE64_VARIANT_ORIGINAL:
239
-                return ParagonIE_Sodium_Core_Base64_Original::encode($decoded);
240
-            case self::BASE64_VARIANT_ORIGINAL_NO_PADDING:
241
-                return ParagonIE_Sodium_Core_Base64_Original::encodeUnpadded($decoded);
242
-            case self::BASE64_VARIANT_URLSAFE:
243
-                return ParagonIE_Sodium_Core_Base64_UrlSafe::encode($decoded);
244
-            case self::BASE64_VARIANT_URLSAFE_NO_PADDING:
245
-                return ParagonIE_Sodium_Core_Base64_UrlSafe::encodeUnpadded($decoded);
246
-            default:
247
-                throw new SodiumException('invalid base64 variant identifier');
248
-        }
249
-    }
250
-
251
-    /**
252
-     * Cache-timing-safe implementation of bin2hex().
253
-     *
254
-     * @param string $string A string (probably raw binary)
255
-     * @return string        A hexadecimal-encoded string
256
-     * @throws SodiumException
257
-     * @throws TypeError
258
-     * @psalm-suppress MixedArgument
259
-     */
260
-    public static function bin2hex($string)
261
-    {
262
-        /* Type checks: */
263
-        ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1);
264
-
265
-        if (self::useNewSodiumAPI()) {
266
-            return (string) sodium_bin2hex($string);
267
-        }
268
-        if (self::use_fallback('bin2hex')) {
269
-            return (string) call_user_func('\\Sodium\\bin2hex', $string);
270
-        }
271
-        return ParagonIE_Sodium_Core_Util::bin2hex($string);
272
-    }
273
-
274
-    /**
275
-     * Compare two strings, in constant-time.
276
-     * Compared to memcmp(), compare() is more useful for sorting.
277
-     *
278
-     * @param string $left  The left operand; must be a string
279
-     * @param string $right The right operand; must be a string
280
-     * @return int          If < 0 if the left operand is less than the right
281
-     *                      If = 0 if both strings are equal
282
-     *                      If > 0 if the right operand is less than the left
283
-     * @throws SodiumException
284
-     * @throws TypeError
285
-     * @psalm-suppress MixedArgument
286
-     */
287
-    public static function compare($left, $right)
288
-    {
289
-        /* Type checks: */
290
-        ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1);
291
-        ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2);
292
-
293
-        if (self::useNewSodiumAPI()) {
294
-            return (int) sodium_compare($left, $right);
295
-        }
296
-        if (self::use_fallback('compare')) {
297
-            return (int) call_user_func('\\Sodium\\compare', $left, $right);
298
-        }
299
-        return ParagonIE_Sodium_Core_Util::compare($left, $right);
300
-    }
301
-
302
-    /**
303
-     * Is AES-256-GCM even available to use?
304
-     *
305
-     * @return bool
306
-     * @psalm-suppress UndefinedFunction
307
-     * @psalm-suppress MixedInferredReturnType
308
-     * @psalm-suppress MixedReturnStatement
309
-     */
310
-    public static function crypto_aead_aes256gcm_is_available()
311
-    {
312
-        if (self::useNewSodiumAPI()) {
313
-            return sodium_crypto_aead_aes256gcm_is_available();
314
-        }
315
-        if (self::use_fallback('crypto_aead_aes256gcm_is_available')) {
316
-            return call_user_func('\\Sodium\\crypto_aead_aes256gcm_is_available');
317
-        }
318
-        if (PHP_VERSION_ID < 70100) {
319
-            // OpenSSL doesn't support AEAD before 7.1.0
320
-            return false;
321
-        }
322
-        if (!is_callable('openssl_encrypt') || !is_callable('openssl_decrypt')) {
323
-            // OpenSSL isn't installed
324
-            return false;
325
-        }
326
-        return (bool) in_array('aes-256-gcm', openssl_get_cipher_methods());
327
-    }
328
-
329
-    /**
330
-     * Authenticated Encryption with Associated Data: Decryption
331
-     *
332
-     * Algorithm:
333
-     *     AES-256-GCM
334
-     *
335
-     * This mode uses a 64-bit random nonce with a 64-bit counter.
336
-     * IETF mode uses a 96-bit random nonce with a 32-bit counter.
337
-     *
338
-     * @param string $ciphertext Encrypted message (with Poly1305 MAC appended)
339
-     * @param string $assocData  Authenticated Associated Data (unencrypted)
340
-     * @param string $nonce      Number to be used only Once; must be 8 bytes
341
-     * @param string $key        Encryption key
342
-     *
343
-     * @return string|bool       The original plaintext message
344
-     * @throws SodiumException
345
-     * @throws TypeError
346
-     * @psalm-suppress MixedArgument
347
-     * @psalm-suppress MixedInferredReturnType
348
-     * @psalm-suppress MixedReturnStatement
349
-     */
350
-    public static function crypto_aead_aes256gcm_decrypt(
351
-        $ciphertext = '',
352
-        $assocData = '',
353
-        $nonce = '',
354
-        $key = ''
355
-    ) {
356
-        if (!self::crypto_aead_aes256gcm_is_available()) {
357
-            throw new SodiumException('AES-256-GCM is not available');
358
-        }
359
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
360
-        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
361
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
362
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
363
-
364
-        /* Input validation: */
365
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AES256GCM_NPUBBYTES) {
366
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_AES256GCM_NPUBBYTES long');
367
-        }
368
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AES256GCM_KEYBYTES) {
369
-            throw new SodiumException('Key must be CRYPTO_AEAD_AES256GCM_KEYBYTES long');
370
-        }
371
-        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_AES256GCM_ABYTES) {
372
-            throw new SodiumException('Message must be at least CRYPTO_AEAD_AES256GCM_ABYTES long');
373
-        }
374
-        if (!is_callable('openssl_decrypt')) {
375
-            throw new SodiumException('The OpenSSL extension is not installed, or openssl_decrypt() is not available');
376
-        }
377
-
378
-        /** @var string $ctext */
379
-        $ctext = ParagonIE_Sodium_Core_Util::substr($ciphertext, 0, -self::CRYPTO_AEAD_AES256GCM_ABYTES);
380
-        /** @var string $authTag */
381
-        $authTag = ParagonIE_Sodium_Core_Util::substr($ciphertext, -self::CRYPTO_AEAD_AES256GCM_ABYTES, 16);
382
-        return openssl_decrypt(
383
-            $ctext,
384
-            'aes-256-gcm',
385
-            $key,
386
-            OPENSSL_RAW_DATA,
387
-            $nonce,
388
-            $authTag,
389
-            $assocData
390
-        );
391
-    }
392
-
393
-    /**
394
-     * Authenticated Encryption with Associated Data: Encryption
395
-     *
396
-     * Algorithm:
397
-     *     AES-256-GCM
398
-     *
399
-     * @param string $plaintext Message to be encrypted
400
-     * @param string $assocData Authenticated Associated Data (unencrypted)
401
-     * @param string $nonce     Number to be used only Once; must be 8 bytes
402
-     * @param string $key       Encryption key
403
-     *
404
-     * @return string           Ciphertext with a 16-byte GCM message
405
-     *                          authentication code appended
406
-     * @throws SodiumException
407
-     * @throws TypeError
408
-     * @psalm-suppress MixedArgument
409
-     */
410
-    public static function crypto_aead_aes256gcm_encrypt(
411
-        $plaintext = '',
412
-        $assocData = '',
413
-        $nonce = '',
414
-        $key = ''
415
-    ) {
416
-        if (!self::crypto_aead_aes256gcm_is_available()) {
417
-            throw new SodiumException('AES-256-GCM is not available');
418
-        }
419
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
420
-        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
421
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
422
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
423
-
424
-        /* Input validation: */
425
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AES256GCM_NPUBBYTES) {
426
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_AES256GCM_NPUBBYTES long');
427
-        }
428
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AES256GCM_KEYBYTES) {
429
-            throw new SodiumException('Key must be CRYPTO_AEAD_AES256GCM_KEYBYTES long');
430
-        }
431
-
432
-        if (!is_callable('openssl_encrypt')) {
433
-            throw new SodiumException('The OpenSSL extension is not installed, or openssl_encrypt() is not available');
434
-        }
435
-
436
-        $authTag = '';
437
-        $ciphertext = openssl_encrypt(
438
-            $plaintext,
439
-            'aes-256-gcm',
440
-            $key,
441
-            OPENSSL_RAW_DATA,
442
-            $nonce,
443
-            $authTag,
444
-            $assocData
445
-        );
446
-        return $ciphertext . $authTag;
447
-    }
448
-
449
-    /**
450
-     * Return a secure random key for use with the AES-256-GCM
451
-     * symmetric AEAD interface.
452
-     *
453
-     * @return string
454
-     * @throws Exception
455
-     * @throws Error
456
-     */
457
-    public static function crypto_aead_aes256gcm_keygen()
458
-    {
459
-        return random_bytes(self::CRYPTO_AEAD_AES256GCM_KEYBYTES);
460
-    }
461
-
462
-    /**
463
-     * Authenticated Encryption with Associated Data: Decryption
464
-     *
465
-     * Algorithm:
466
-     *     ChaCha20-Poly1305
467
-     *
468
-     * This mode uses a 64-bit random nonce with a 64-bit counter.
469
-     * IETF mode uses a 96-bit random nonce with a 32-bit counter.
470
-     *
471
-     * @param string $ciphertext Encrypted message (with Poly1305 MAC appended)
472
-     * @param string $assocData  Authenticated Associated Data (unencrypted)
473
-     * @param string $nonce      Number to be used only Once; must be 8 bytes
474
-     * @param string $key        Encryption key
475
-     *
476
-     * @return string            The original plaintext message
477
-     * @throws SodiumException
478
-     * @throws TypeError
479
-     * @psalm-suppress MixedArgument
480
-     * @psalm-suppress MixedInferredReturnType
481
-     * @psalm-suppress MixedReturnStatement
482
-     */
483
-    public static function crypto_aead_chacha20poly1305_decrypt(
484
-        $ciphertext = '',
485
-        $assocData = '',
486
-        $nonce = '',
487
-        $key = ''
488
-    ) {
489
-        /* Type checks: */
490
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
491
-        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
492
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
493
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
494
-
495
-        /* Input validation: */
496
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES) {
497
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES long');
498
-        }
499
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
500
-            throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
501
-        }
502
-        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES) {
503
-            throw new SodiumException('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_ABYTES long');
504
-        }
505
-
506
-        if (self::useNewSodiumAPI()) {
507
-            /**
508
-             * @psalm-suppress InvalidReturnStatement
509
-             * @psalm-suppress FalsableReturnStatement
510
-             */
511
-            return sodium_crypto_aead_chacha20poly1305_decrypt(
512
-                $ciphertext,
513
-                $assocData,
514
-                $nonce,
515
-                $key
516
-            );
517
-        }
518
-        if (self::use_fallback('crypto_aead_chacha20poly1305_decrypt')) {
519
-            return call_user_func(
520
-                '\\Sodium\\crypto_aead_chacha20poly1305_decrypt',
521
-                $ciphertext,
522
-                $assocData,
523
-                $nonce,
524
-                $key
525
-            );
526
-        }
527
-        if (PHP_INT_SIZE === 4) {
528
-            return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_decrypt(
529
-                $ciphertext,
530
-                $assocData,
531
-                $nonce,
532
-                $key
533
-            );
534
-        }
535
-        return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_decrypt(
536
-            $ciphertext,
537
-            $assocData,
538
-            $nonce,
539
-            $key
540
-        );
541
-    }
542
-
543
-    /**
544
-     * Authenticated Encryption with Associated Data
545
-     *
546
-     * Algorithm:
547
-     *     ChaCha20-Poly1305
548
-     *
549
-     * This mode uses a 64-bit random nonce with a 64-bit counter.
550
-     * IETF mode uses a 96-bit random nonce with a 32-bit counter.
551
-     *
552
-     * @param string $plaintext Message to be encrypted
553
-     * @param string $assocData Authenticated Associated Data (unencrypted)
554
-     * @param string $nonce     Number to be used only Once; must be 8 bytes
555
-     * @param string $key       Encryption key
556
-     *
557
-     * @return string           Ciphertext with a 16-byte Poly1305 message
558
-     *                          authentication code appended
559
-     * @throws SodiumException
560
-     * @throws TypeError
561
-     * @psalm-suppress MixedArgument
562
-     */
563
-    public static function crypto_aead_chacha20poly1305_encrypt(
564
-        $plaintext = '',
565
-        $assocData = '',
566
-        $nonce = '',
567
-        $key = ''
568
-    ) {
569
-        /* Type checks: */
570
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
571
-        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
572
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
573
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
574
-
575
-        /* Input validation: */
576
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES) {
577
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES long');
578
-        }
579
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
580
-            throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
581
-        }
582
-
583
-        if (self::useNewSodiumAPI()) {
584
-            return (string) sodium_crypto_aead_chacha20poly1305_encrypt(
585
-                $plaintext,
586
-                $assocData,
587
-                $nonce,
588
-                $key
589
-            );
590
-        }
591
-        if (self::use_fallback('crypto_aead_chacha20poly1305_encrypt')) {
592
-            return (string) call_user_func(
593
-                '\\Sodium\\crypto_aead_chacha20poly1305_encrypt',
594
-                $plaintext,
595
-                $assocData,
596
-                $nonce,
597
-                $key
598
-            );
599
-        }
600
-        if (PHP_INT_SIZE === 4) {
601
-            return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_encrypt(
602
-                $plaintext,
603
-                $assocData,
604
-                $nonce,
605
-                $key
606
-            );
607
-        }
608
-        return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_encrypt(
609
-            $plaintext,
610
-            $assocData,
611
-            $nonce,
612
-            $key
613
-        );
614
-    }
615
-
616
-    /**
617
-     * Authenticated Encryption with Associated Data: Decryption
618
-     *
619
-     * Algorithm:
620
-     *     ChaCha20-Poly1305
621
-     *
622
-     * IETF mode uses a 96-bit random nonce with a 32-bit counter.
623
-     * Regular mode uses a 64-bit random nonce with a 64-bit counter.
624
-     *
625
-     * @param string $ciphertext Encrypted message (with Poly1305 MAC appended)
626
-     * @param string $assocData  Authenticated Associated Data (unencrypted)
627
-     * @param string $nonce      Number to be used only Once; must be 12 bytes
628
-     * @param string $key        Encryption key
629
-     *
630
-     * @return string            The original plaintext message
631
-     * @throws SodiumException
632
-     * @throws TypeError
633
-     * @psalm-suppress MixedArgument
634
-     * @psalm-suppress MixedInferredReturnType
635
-     * @psalm-suppress MixedReturnStatement
636
-     */
637
-    public static function crypto_aead_chacha20poly1305_ietf_decrypt(
638
-        $ciphertext = '',
639
-        $assocData = '',
640
-        $nonce = '',
641
-        $key = ''
642
-    ) {
643
-        /* Type checks: */
644
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
645
-        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
646
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
647
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
648
-
649
-        /* Input validation: */
650
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES) {
651
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long');
652
-        }
653
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
654
-            throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
655
-        }
656
-        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES) {
657
-            throw new SodiumException('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_ABYTES long');
658
-        }
659
-
660
-        if (self::useNewSodiumAPI()) {
661
-            /**
662
-             * @psalm-suppress InvalidReturnStatement
663
-             * @psalm-suppress FalsableReturnStatement
664
-             */
665
-            return sodium_crypto_aead_chacha20poly1305_ietf_decrypt(
666
-                $ciphertext,
667
-                $assocData,
668
-                $nonce,
669
-                $key
670
-            );
671
-        }
672
-        if (self::use_fallback('crypto_aead_chacha20poly1305_ietf_decrypt')) {
673
-            return call_user_func(
674
-                '\\Sodium\\crypto_aead_chacha20poly1305_ietf_decrypt',
675
-                $ciphertext,
676
-                $assocData,
677
-                $nonce,
678
-                $key
679
-            );
680
-        }
681
-        if (PHP_INT_SIZE === 4) {
682
-            return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_ietf_decrypt(
683
-                $ciphertext,
684
-                $assocData,
685
-                $nonce,
686
-                $key
687
-            );
688
-        }
689
-        return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_ietf_decrypt(
690
-            $ciphertext,
691
-            $assocData,
692
-            $nonce,
693
-            $key
694
-        );
695
-    }
696
-
697
-    /**
698
-     * Return a secure random key for use with the ChaCha20-Poly1305
699
-     * symmetric AEAD interface.
700
-     *
701
-     * @return string
702
-     * @throws Exception
703
-     * @throws Error
704
-     */
705
-    public static function crypto_aead_chacha20poly1305_keygen()
706
-    {
707
-        return random_bytes(self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES);
708
-    }
709
-
710
-    /**
711
-     * Authenticated Encryption with Associated Data
712
-     *
713
-     * Algorithm:
714
-     *     ChaCha20-Poly1305
715
-     *
716
-     * IETF mode uses a 96-bit random nonce with a 32-bit counter.
717
-     * Regular mode uses a 64-bit random nonce with a 64-bit counter.
718
-     *
719
-     * @param string $plaintext Message to be encrypted
720
-     * @param string $assocData Authenticated Associated Data (unencrypted)
721
-     * @param string $nonce Number to be used only Once; must be 8 bytes
722
-     * @param string $key Encryption key
723
-     *
724
-     * @return string           Ciphertext with a 16-byte Poly1305 message
725
-     *                          authentication code appended
726
-     * @throws SodiumException
727
-     * @throws TypeError
728
-     * @psalm-suppress MixedArgument
729
-     */
730
-    public static function crypto_aead_chacha20poly1305_ietf_encrypt(
731
-        $plaintext = '',
732
-        $assocData = '',
733
-        $nonce = '',
734
-        $key = ''
735
-    ) {
736
-        /* Type checks: */
737
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
738
-        if (!is_null($assocData)) {
739
-            ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
740
-        }
741
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
742
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
743
-
744
-        /* Input validation: */
745
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES) {
746
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long');
747
-        }
748
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
749
-            throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
750
-        }
751
-
752
-        if (self::useNewSodiumAPI()) {
753
-            return (string) sodium_crypto_aead_chacha20poly1305_ietf_encrypt(
754
-                $plaintext,
755
-                $assocData,
756
-                $nonce,
757
-                $key
758
-            );
759
-        }
760
-        if (self::use_fallback('crypto_aead_chacha20poly1305_ietf_encrypt')) {
761
-            return (string) call_user_func(
762
-                '\\Sodium\\crypto_aead_chacha20poly1305_ietf_encrypt',
763
-                $plaintext,
764
-                $assocData,
765
-                $nonce,
766
-                $key
767
-            );
768
-        }
769
-        if (PHP_INT_SIZE === 4) {
770
-            return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_ietf_encrypt(
771
-                $plaintext,
772
-                $assocData,
773
-                $nonce,
774
-                $key
775
-            );
776
-        }
777
-        return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_ietf_encrypt(
778
-            $plaintext,
779
-            $assocData,
780
-            $nonce,
781
-            $key
782
-        );
783
-    }
784
-
785
-    /**
786
-     * Return a secure random key for use with the ChaCha20-Poly1305
787
-     * symmetric AEAD interface. (IETF version)
788
-     *
789
-     * @return string
790
-     * @throws Exception
791
-     * @throws Error
792
-     */
793
-    public static function crypto_aead_chacha20poly1305_ietf_keygen()
794
-    {
795
-        return random_bytes(self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES);
796
-    }
797
-
798
-    /**
799
-     * Authenticated Encryption with Associated Data: Decryption
800
-     *
801
-     * Algorithm:
802
-     *     XChaCha20-Poly1305
803
-     *
804
-     * This mode uses a 64-bit random nonce with a 64-bit counter.
805
-     * IETF mode uses a 96-bit random nonce with a 32-bit counter.
806
-     *
807
-     * @param string $ciphertext   Encrypted message (with Poly1305 MAC appended)
808
-     * @param string $assocData    Authenticated Associated Data (unencrypted)
809
-     * @param string $nonce        Number to be used only Once; must be 8 bytes
810
-     * @param string $key          Encryption key
811
-     * @param bool   $dontFallback Don't fallback to ext/sodium
812
-     *
813
-     * @return string|bool         The original plaintext message
814
-     * @throws SodiumException
815
-     * @throws TypeError
816
-     * @psalm-suppress MixedArgument
817
-     */
818
-    public static function crypto_aead_xchacha20poly1305_ietf_decrypt(
819
-        $ciphertext = '',
820
-        $assocData = '',
821
-        $nonce = '',
822
-        $key = '',
823
-        $dontFallback = false
824
-    ) {
825
-        /* Type checks: */
826
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
827
-        if (!is_null($assocData)) {
828
-            ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
829
-        } else {
830
-            $assocData = '';
831
-        }
832
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
833
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
834
-
835
-        /* Input validation: */
836
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES) {
837
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES long');
838
-        }
839
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES) {
840
-            throw new SodiumException('Key must be CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES long');
841
-        }
842
-        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES) {
843
-            throw new SodiumException('Message must be at least CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES long');
844
-        }
845
-        if (self::useNewSodiumAPI() && !$dontFallback) {
846
-            if (is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_decrypt')) {
847
-                return sodium_crypto_aead_xchacha20poly1305_ietf_decrypt(
848
-                    $ciphertext,
849
-                    $assocData,
850
-                    $nonce,
851
-                    $key
852
-                );
853
-            }
854
-        }
855
-
856
-        if (PHP_INT_SIZE === 4) {
857
-            return ParagonIE_Sodium_Crypto32::aead_xchacha20poly1305_ietf_decrypt(
858
-                $ciphertext,
859
-                $assocData,
860
-                $nonce,
861
-                $key
862
-            );
863
-        }
864
-        return ParagonIE_Sodium_Crypto::aead_xchacha20poly1305_ietf_decrypt(
865
-            $ciphertext,
866
-            $assocData,
867
-            $nonce,
868
-            $key
869
-        );
870
-    }
871
-
872
-    /**
873
-     * Authenticated Encryption with Associated Data
874
-     *
875
-     * Algorithm:
876
-     *     XChaCha20-Poly1305
877
-     *
878
-     * This mode uses a 64-bit random nonce with a 64-bit counter.
879
-     * IETF mode uses a 96-bit random nonce with a 32-bit counter.
880
-     *
881
-     * @param string $plaintext    Message to be encrypted
882
-     * @param string $assocData    Authenticated Associated Data (unencrypted)
883
-     * @param string $nonce        Number to be used only Once; must be 8 bytes
884
-     * @param string $key          Encryption key
885
-     * @param bool   $dontFallback Don't fallback to ext/sodium
886
-     *
887
-     * @return string           Ciphertext with a 16-byte Poly1305 message
888
-     *                          authentication code appended
889
-     * @throws SodiumException
890
-     * @throws TypeError
891
-     * @psalm-suppress MixedArgument
892
-     */
893
-    public static function crypto_aead_xchacha20poly1305_ietf_encrypt(
894
-        $plaintext = '',
895
-        $assocData = '',
896
-        $nonce = '',
897
-        $key = '',
898
-        $dontFallback = false
899
-    ) {
900
-        /* Type checks: */
901
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
902
-        if (!is_null($assocData)) {
903
-            ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
904
-        } else {
905
-            $assocData = '';
906
-        }
907
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
908
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
909
-
910
-        /* Input validation: */
911
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES) {
912
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_XCHACHA20POLY1305_NPUBBYTES long');
913
-        }
914
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES) {
915
-            throw new SodiumException('Key must be CRYPTO_AEAD_XCHACHA20POLY1305_KEYBYTES long');
916
-        }
917
-        if (self::useNewSodiumAPI() && !$dontFallback) {
918
-            if (is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')) {
919
-                return sodium_crypto_aead_xchacha20poly1305_ietf_encrypt(
920
-                    $plaintext,
921
-                    $assocData,
922
-                    $nonce,
923
-                    $key
924
-                );
925
-            }
926
-        }
927
-
928
-        if (PHP_INT_SIZE === 4) {
929
-            return ParagonIE_Sodium_Crypto32::aead_xchacha20poly1305_ietf_encrypt(
930
-                $plaintext,
931
-                $assocData,
932
-                $nonce,
933
-                $key
934
-            );
935
-        }
936
-        return ParagonIE_Sodium_Crypto::aead_xchacha20poly1305_ietf_encrypt(
937
-            $plaintext,
938
-            $assocData,
939
-            $nonce,
940
-            $key
941
-        );
942
-    }
943
-
944
-    /**
945
-     * Return a secure random key for use with the XChaCha20-Poly1305
946
-     * symmetric AEAD interface.
947
-     *
948
-     * @return string
949
-     * @throws Exception
950
-     * @throws Error
951
-     */
952
-    public static function crypto_aead_xchacha20poly1305_ietf_keygen()
953
-    {
954
-        return random_bytes(self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES);
955
-    }
956
-
957
-    /**
958
-     * Authenticate a message. Uses symmetric-key cryptography.
959
-     *
960
-     * Algorithm:
961
-     *     HMAC-SHA512-256. Which is HMAC-SHA-512 truncated to 256 bits.
962
-     *     Not to be confused with HMAC-SHA-512/256 which would use the
963
-     *     SHA-512/256 hash function (uses different initial parameters
964
-     *     but still truncates to 256 bits to sidestep length-extension
965
-     *     attacks).
966
-     *
967
-     * @param string $message Message to be authenticated
968
-     * @param string $key Symmetric authentication key
969
-     * @return string         Message authentication code
970
-     * @throws SodiumException
971
-     * @throws TypeError
972
-     * @psalm-suppress MixedArgument
973
-     */
974
-    public static function crypto_auth($message, $key)
975
-    {
976
-        /* Type checks: */
977
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
978
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2);
979
-
980
-        /* Input validation: */
981
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AUTH_KEYBYTES) {
982
-            throw new SodiumException('Argument 2 must be CRYPTO_AUTH_KEYBYTES long.');
983
-        }
984
-
985
-        if (self::useNewSodiumAPI()) {
986
-            return (string) sodium_crypto_auth($message, $key);
987
-        }
988
-        if (self::use_fallback('crypto_auth')) {
989
-            return (string) call_user_func('\\Sodium\\crypto_auth', $message, $key);
990
-        }
991
-        if (PHP_INT_SIZE === 4) {
992
-            return ParagonIE_Sodium_Crypto32::auth($message, $key);
993
-        }
994
-        return ParagonIE_Sodium_Crypto::auth($message, $key);
995
-    }
996
-
997
-    /**
998
-     * @return string
999
-     * @throws Exception
1000
-     * @throws Error
1001
-     */
1002
-    public static function crypto_auth_keygen()
1003
-    {
1004
-        return random_bytes(self::CRYPTO_AUTH_KEYBYTES);
1005
-    }
1006
-
1007
-    /**
1008
-     * Verify the MAC of a message previously authenticated with crypto_auth.
1009
-     *
1010
-     * @param string $mac Message authentication code
1011
-     * @param string $message Message whose authenticity you are attempting to
1012
-     *                        verify (with a given MAC and key)
1013
-     * @param string $key Symmetric authentication key
1014
-     * @return bool           TRUE if authenticated, FALSE otherwise
1015
-     * @throws SodiumException
1016
-     * @throws TypeError
1017
-     * @psalm-suppress MixedArgument
1018
-     */
1019
-    public static function crypto_auth_verify($mac, $message, $key)
1020
-    {
1021
-        /* Type checks: */
1022
-        ParagonIE_Sodium_Core_Util::declareScalarType($mac, 'string', 1);
1023
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2);
1024
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
1025
-
1026
-        /* Input validation: */
1027
-        if (ParagonIE_Sodium_Core_Util::strlen($mac) !== self::CRYPTO_AUTH_BYTES) {
1028
-            throw new SodiumException('Argument 1 must be CRYPTO_AUTH_BYTES long.');
1029
-        }
1030
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AUTH_KEYBYTES) {
1031
-            throw new SodiumException('Argument 3 must be CRYPTO_AUTH_KEYBYTES long.');
1032
-        }
1033
-
1034
-        if (self::useNewSodiumAPI()) {
1035
-            return (bool) sodium_crypto_auth_verify($mac, $message, $key);
1036
-        }
1037
-        if (self::use_fallback('crypto_auth_verify')) {
1038
-            return (bool) call_user_func('\\Sodium\\crypto_auth_verify', $mac, $message, $key);
1039
-        }
1040
-        if (PHP_INT_SIZE === 4) {
1041
-            return ParagonIE_Sodium_Crypto32::auth_verify($mac, $message, $key);
1042
-        }
1043
-        return ParagonIE_Sodium_Crypto::auth_verify($mac, $message, $key);
1044
-    }
1045
-
1046
-    /**
1047
-     * Authenticated asymmetric-key encryption. Both the sender and recipient
1048
-     * may decrypt messages.
1049
-     *
1050
-     * Algorithm: X25519-XSalsa20-Poly1305.
1051
-     *     X25519: Elliptic-Curve Diffie Hellman over Curve25519.
1052
-     *     XSalsa20: Extended-nonce variant of salsa20.
1053
-     *     Poyl1305: Polynomial MAC for one-time message authentication.
1054
-     *
1055
-     * @param string $plaintext The message to be encrypted
1056
-     * @param string $nonce A Number to only be used Once; must be 24 bytes
1057
-     * @param string $keypair Your secret key and your recipient's public key
1058
-     * @return string           Ciphertext with 16-byte Poly1305 MAC
1059
-     * @throws SodiumException
1060
-     * @throws TypeError
1061
-     * @psalm-suppress MixedArgument
1062
-     */
1063
-    public static function crypto_box($plaintext, $nonce, $keypair)
1064
-    {
1065
-        /* Type checks: */
1066
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
1067
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
1068
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 3);
1069
-
1070
-        /* Input validation: */
1071
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_BOX_NONCEBYTES) {
1072
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_NONCEBYTES long.');
1073
-        }
1074
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1075
-            throw new SodiumException('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1076
-        }
1077
-
1078
-        if (self::useNewSodiumAPI()) {
1079
-            return (string) sodium_crypto_box($plaintext, $nonce, $keypair);
1080
-        }
1081
-        if (self::use_fallback('crypto_box')) {
1082
-            return (string) call_user_func('\\Sodium\\crypto_box', $plaintext, $nonce, $keypair);
1083
-        }
1084
-        if (PHP_INT_SIZE === 4) {
1085
-            return ParagonIE_Sodium_Crypto32::box($plaintext, $nonce, $keypair);
1086
-        }
1087
-        return ParagonIE_Sodium_Crypto::box($plaintext, $nonce, $keypair);
1088
-    }
1089
-
1090
-    /**
1091
-     * Anonymous public-key encryption. Only the recipient may decrypt messages.
1092
-     *
1093
-     * Algorithm: X25519-XSalsa20-Poly1305, as with crypto_box.
1094
-     *     The sender's X25519 keypair is ephemeral.
1095
-     *     Nonce is generated from the BLAKE2b hash of both public keys.
1096
-     *
1097
-     * This provides ciphertext integrity.
1098
-     *
1099
-     * @param string $plaintext Message to be sealed
1100
-     * @param string $publicKey Your recipient's public key
1101
-     * @return string           Sealed message that only your recipient can
1102
-     *                          decrypt
1103
-     * @throws SodiumException
1104
-     * @throws TypeError
1105
-     * @psalm-suppress MixedArgument
1106
-     */
1107
-    public static function crypto_box_seal($plaintext, $publicKey)
1108
-    {
1109
-        /* Type checks: */
1110
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
1111
-        ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
1112
-
1113
-        /* Input validation: */
1114
-        if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1115
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1116
-        }
1117
-
1118
-        if (self::useNewSodiumAPI()) {
1119
-            return (string) sodium_crypto_box_seal($plaintext, $publicKey);
1120
-        }
1121
-        if (self::use_fallback('crypto_box_seal')) {
1122
-            return (string) call_user_func('\\Sodium\\crypto_box_seal', $plaintext, $publicKey);
1123
-        }
1124
-        if (PHP_INT_SIZE === 4) {
1125
-            return ParagonIE_Sodium_Crypto32::box_seal($plaintext, $publicKey);
1126
-        }
1127
-        return ParagonIE_Sodium_Crypto::box_seal($plaintext, $publicKey);
1128
-    }
1129
-
1130
-    /**
1131
-     * Opens a message encrypted with crypto_box_seal(). Requires
1132
-     * the recipient's keypair (sk || pk) to decrypt successfully.
1133
-     *
1134
-     * This validates ciphertext integrity.
1135
-     *
1136
-     * @param string $ciphertext Sealed message to be opened
1137
-     * @param string $keypair    Your crypto_box keypair
1138
-     * @return string            The original plaintext message
1139
-     * @throws SodiumException
1140
-     * @throws TypeError
1141
-     * @psalm-suppress MixedArgument
1142
-     * @psalm-suppress MixedInferredReturnType
1143
-     * @psalm-suppress MixedReturnStatement
1144
-     */
1145
-    public static function crypto_box_seal_open($ciphertext, $keypair)
1146
-    {
1147
-        /* Type checks: */
1148
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
1149
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 2);
1150
-
1151
-        /* Input validation: */
1152
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1153
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1154
-        }
1155
-
1156
-        if (self::useNewSodiumAPI()) {
1157
-            /**
1158
-             * @psalm-suppress InvalidReturnStatement
1159
-             * @psalm-suppress FalsableReturnStatement
1160
-             */
1161
-            return sodium_crypto_box_seal_open($ciphertext, $keypair);
1162
-        }
1163
-        if (self::use_fallback('crypto_box_seal_open')) {
1164
-            return call_user_func('\\Sodium\\crypto_box_seal_open', $ciphertext, $keypair);
1165
-        }
1166
-        if (PHP_INT_SIZE === 4) {
1167
-            return ParagonIE_Sodium_Crypto32::box_seal_open($ciphertext, $keypair);
1168
-        }
1169
-        return ParagonIE_Sodium_Crypto::box_seal_open($ciphertext, $keypair);
1170
-    }
1171
-
1172
-    /**
1173
-     * Generate a new random X25519 keypair.
1174
-     *
1175
-     * @return string A 64-byte string; the first 32 are your secret key, while
1176
-     *                the last 32 are your public key. crypto_box_secretkey()
1177
-     *                and crypto_box_publickey() exist to separate them so you
1178
-     *                don't accidentally get them mixed up!
1179
-     * @throws SodiumException
1180
-     * @throws TypeError
1181
-     * @psalm-suppress MixedArgument
1182
-     */
1183
-    public static function crypto_box_keypair()
1184
-    {
1185
-        if (self::useNewSodiumAPI()) {
1186
-            return (string) sodium_crypto_box_keypair();
1187
-        }
1188
-        if (self::use_fallback('crypto_box_keypair')) {
1189
-            return (string) call_user_func('\\Sodium\\crypto_box_keypair');
1190
-        }
1191
-        if (PHP_INT_SIZE === 4) {
1192
-            return ParagonIE_Sodium_Crypto32::box_keypair();
1193
-        }
1194
-        return ParagonIE_Sodium_Crypto::box_keypair();
1195
-    }
1196
-
1197
-    /**
1198
-     * Combine two keys into a keypair for use in library methods that expect
1199
-     * a keypair. This doesn't necessarily have to be the same person's keys.
1200
-     *
1201
-     * @param string $secretKey Secret key
1202
-     * @param string $publicKey Public key
1203
-     * @return string    Keypair
1204
-     * @throws SodiumException
1205
-     * @throws TypeError
1206
-     * @psalm-suppress MixedArgument
1207
-     */
1208
-    public static function crypto_box_keypair_from_secretkey_and_publickey($secretKey, $publicKey)
1209
-    {
1210
-        /* Type checks: */
1211
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
1212
-        ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
1213
-
1214
-        /* Input validation: */
1215
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
1216
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
1217
-        }
1218
-        if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1219
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1220
-        }
1221
-
1222
-        if (self::useNewSodiumAPI()) {
1223
-            return (string) sodium_crypto_box_keypair_from_secretkey_and_publickey($secretKey, $publicKey);
1224
-        }
1225
-        if (self::use_fallback('crypto_box_keypair_from_secretkey_and_publickey')) {
1226
-            return (string) call_user_func('\\Sodium\\crypto_box_keypair_from_secretkey_and_publickey', $secretKey, $publicKey);
1227
-        }
1228
-        if (PHP_INT_SIZE === 4) {
1229
-            return ParagonIE_Sodium_Crypto32::box_keypair_from_secretkey_and_publickey($secretKey, $publicKey);
1230
-        }
1231
-        return ParagonIE_Sodium_Crypto::box_keypair_from_secretkey_and_publickey($secretKey, $publicKey);
1232
-    }
1233
-
1234
-    /**
1235
-     * Decrypt a message previously encrypted with crypto_box().
1236
-     *
1237
-     * @param string $ciphertext Encrypted message
1238
-     * @param string $nonce      Number to only be used Once; must be 24 bytes
1239
-     * @param string $keypair    Your secret key and the sender's public key
1240
-     * @return string            The original plaintext message
1241
-     * @throws SodiumException
1242
-     * @throws TypeError
1243
-     * @psalm-suppress MixedArgument
1244
-     * @psalm-suppress MixedInferredReturnType
1245
-     * @psalm-suppress MixedReturnStatement
1246
-     */
1247
-    public static function crypto_box_open($ciphertext, $nonce, $keypair)
1248
-    {
1249
-        /* Type checks: */
1250
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
1251
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
1252
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 3);
1253
-
1254
-        /* Input validation: */
1255
-        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_BOX_MACBYTES) {
1256
-            throw new SodiumException('Argument 1 must be at least CRYPTO_BOX_MACBYTES long.');
1257
-        }
1258
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_BOX_NONCEBYTES) {
1259
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_NONCEBYTES long.');
1260
-        }
1261
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1262
-            throw new SodiumException('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1263
-        }
1264
-
1265
-        if (self::useNewSodiumAPI()) {
1266
-            /**
1267
-             * @psalm-suppress InvalidReturnStatement
1268
-             * @psalm-suppress FalsableReturnStatement
1269
-             */
1270
-            return sodium_crypto_box_open($ciphertext, $nonce, $keypair);
1271
-        }
1272
-        if (self::use_fallback('crypto_box_open')) {
1273
-            return call_user_func('\\Sodium\\crypto_box_open', $ciphertext, $nonce, $keypair);
1274
-        }
1275
-        if (PHP_INT_SIZE === 4) {
1276
-            return ParagonIE_Sodium_Crypto32::box_open($ciphertext, $nonce, $keypair);
1277
-        }
1278
-        return ParagonIE_Sodium_Crypto::box_open($ciphertext, $nonce, $keypair);
1279
-    }
1280
-
1281
-    /**
1282
-     * Extract the public key from a crypto_box keypair.
1283
-     *
1284
-     * @param string $keypair Keypair containing secret and public key
1285
-     * @return string         Your crypto_box public key
1286
-     * @throws SodiumException
1287
-     * @throws TypeError
1288
-     * @psalm-suppress MixedArgument
1289
-     */
1290
-    public static function crypto_box_publickey($keypair)
1291
-    {
1292
-        /* Type checks: */
1293
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1294
-
1295
-        /* Input validation: */
1296
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1297
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1298
-        }
1299
-
1300
-        if (self::useNewSodiumAPI()) {
1301
-            return (string) sodium_crypto_box_publickey($keypair);
1302
-        }
1303
-        if (self::use_fallback('crypto_box_publickey')) {
1304
-            return (string) call_user_func('\\Sodium\\crypto_box_publickey', $keypair);
1305
-        }
1306
-        if (PHP_INT_SIZE === 4) {
1307
-            return ParagonIE_Sodium_Crypto32::box_publickey($keypair);
1308
-        }
1309
-        return ParagonIE_Sodium_Crypto::box_publickey($keypair);
1310
-    }
1311
-
1312
-    /**
1313
-     * Calculate the X25519 public key from a given X25519 secret key.
1314
-     *
1315
-     * @param string $secretKey Any X25519 secret key
1316
-     * @return string           The corresponding X25519 public key
1317
-     * @throws SodiumException
1318
-     * @throws TypeError
1319
-     * @psalm-suppress MixedArgument
1320
-     */
1321
-    public static function crypto_box_publickey_from_secretkey($secretKey)
1322
-    {
1323
-        /* Type checks: */
1324
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
1325
-
1326
-        /* Input validation: */
1327
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
1328
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
1329
-        }
1330
-
1331
-        if (self::useNewSodiumAPI()) {
1332
-            return (string) sodium_crypto_box_publickey_from_secretkey($secretKey);
1333
-        }
1334
-        if (self::use_fallback('crypto_box_publickey_from_secretkey')) {
1335
-            return (string) call_user_func('\\Sodium\\crypto_box_publickey_from_secretkey', $secretKey);
1336
-        }
1337
-        if (PHP_INT_SIZE === 4) {
1338
-            return ParagonIE_Sodium_Crypto32::box_publickey_from_secretkey($secretKey);
1339
-        }
1340
-        return ParagonIE_Sodium_Crypto::box_publickey_from_secretkey($secretKey);
1341
-    }
1342
-
1343
-    /**
1344
-     * Extract the secret key from a crypto_box keypair.
1345
-     *
1346
-     * @param string $keypair
1347
-     * @return string         Your crypto_box secret key
1348
-     * @throws SodiumException
1349
-     * @throws TypeError
1350
-     * @psalm-suppress MixedArgument
1351
-     */
1352
-    public static function crypto_box_secretkey($keypair)
1353
-    {
1354
-        /* Type checks: */
1355
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1356
-
1357
-        /* Input validation: */
1358
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1359
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1360
-        }
1361
-
1362
-        if (self::useNewSodiumAPI()) {
1363
-            return (string) sodium_crypto_box_secretkey($keypair);
1364
-        }
1365
-        if (self::use_fallback('crypto_box_secretkey')) {
1366
-            return (string) call_user_func('\\Sodium\\crypto_box_secretkey', $keypair);
1367
-        }
1368
-        if (PHP_INT_SIZE === 4) {
1369
-            return ParagonIE_Sodium_Crypto32::box_secretkey($keypair);
1370
-        }
1371
-        return ParagonIE_Sodium_Crypto::box_secretkey($keypair);
1372
-    }
1373
-
1374
-    /**
1375
-     * Generate an X25519 keypair from a seed.
1376
-     *
1377
-     * @param string $seed
1378
-     * @return string
1379
-     * @throws SodiumException
1380
-     * @throws TypeError
1381
-     * @psalm-suppress MixedArgument
1382
-     * @psalm-suppress UndefinedFunction
1383
-     */
1384
-    public static function crypto_box_seed_keypair($seed)
1385
-    {
1386
-        /* Type checks: */
1387
-        ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1);
1388
-
1389
-        if (self::useNewSodiumAPI()) {
1390
-            return (string) sodium_crypto_box_seed_keypair($seed);
1391
-        }
1392
-        if (self::use_fallback('crypto_box_seed_keypair')) {
1393
-            return (string) call_user_func('\\Sodium\\crypto_box_seed_keypair', $seed);
1394
-        }
1395
-        if (PHP_INT_SIZE === 4) {
1396
-            return ParagonIE_Sodium_Crypto32::box_seed_keypair($seed);
1397
-        }
1398
-        return ParagonIE_Sodium_Crypto::box_seed_keypair($seed);
1399
-    }
1400
-
1401
-    /**
1402
-     * Calculates a BLAKE2b hash, with an optional key.
1403
-     *
1404
-     * @param string      $message The message to be hashed
1405
-     * @param string|null $key     If specified, must be a string between 16
1406
-     *                             and 64 bytes long
1407
-     * @param int         $length  Output length in bytes; must be between 16
1408
-     *                             and 64 (default = 32)
1409
-     * @return string              Raw binary
1410
-     * @throws SodiumException
1411
-     * @throws TypeError
1412
-     * @psalm-suppress MixedArgument
1413
-     */
1414
-    public static function crypto_generichash($message, $key = '', $length = self::CRYPTO_GENERICHASH_BYTES)
1415
-    {
1416
-        /* Type checks: */
1417
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
1418
-        if (is_null($key)) {
1419
-            $key = '';
1420
-        }
1421
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2);
1422
-        ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 3);
1423
-
1424
-        /* Input validation: */
1425
-        if (!empty($key)) {
1426
-            if (ParagonIE_Sodium_Core_Util::strlen($key) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
1427
-                throw new SodiumException('Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.');
1428
-            }
1429
-            if (ParagonIE_Sodium_Core_Util::strlen($key) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
1430
-                throw new SodiumException('Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.');
1431
-            }
1432
-        }
1433
-
1434
-        if (self::useNewSodiumAPI()) {
1435
-            return (string) sodium_crypto_generichash($message, $key, $length);
1436
-        }
1437
-        if (self::use_fallback('crypto_generichash')) {
1438
-            return (string) call_user_func('\\Sodium\\crypto_generichash', $message, $key, $length);
1439
-        }
1440
-        if (PHP_INT_SIZE === 4) {
1441
-            return ParagonIE_Sodium_Crypto32::generichash($message, $key, $length);
1442
-        }
1443
-        return ParagonIE_Sodium_Crypto::generichash($message, $key, $length);
1444
-    }
1445
-
1446
-    /**
1447
-     * Get the final BLAKE2b hash output for a given context.
1448
-     *
1449
-     * @param string $ctx BLAKE2 hashing context. Generated by crypto_generichash_init().
1450
-     * @param int $length Hash output size.
1451
-     * @return string     Final BLAKE2b hash.
1452
-     * @throws SodiumException
1453
-     * @throws TypeError
1454
-     * @psalm-suppress MixedArgument
1455
-     * @psalm-suppress ReferenceConstraintViolation
1456
-     * @psalm-suppress ConflictingReferenceConstraint
1457
-     */
1458
-    public static function crypto_generichash_final(&$ctx, $length = self::CRYPTO_GENERICHASH_BYTES)
1459
-    {
1460
-        /* Type checks: */
1461
-        ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1);
1462
-        ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2);
1463
-
1464
-        if (self::useNewSodiumAPI()) {
1465
-            return sodium_crypto_generichash_final($ctx, $length);
1466
-        }
1467
-        if (self::use_fallback('crypto_generichash_final')) {
1468
-            $func = '\\Sodium\\crypto_generichash_final';
1469
-            return (string) $func($ctx, $length);
1470
-        }
1471
-        if ($length < 1) {
1472
-            try {
1473
-                self::memzero($ctx);
1474
-            } catch (SodiumException $ex) {
1475
-                unset($ctx);
1476
-            }
1477
-            return '';
1478
-        }
1479
-        if (PHP_INT_SIZE === 4) {
1480
-            $result = ParagonIE_Sodium_Crypto32::generichash_final($ctx, $length);
1481
-        } else {
1482
-            $result = ParagonIE_Sodium_Crypto::generichash_final($ctx, $length);
1483
-        }
1484
-        try {
1485
-            self::memzero($ctx);
1486
-        } catch (SodiumException $ex) {
1487
-            unset($ctx);
1488
-        }
1489
-        return $result;
1490
-    }
1491
-
1492
-    /**
1493
-     * Initialize a BLAKE2b hashing context, for use in a streaming interface.
1494
-     *
1495
-     * @param string|null $key If specified must be a string between 16 and 64 bytes
1496
-     * @param int $length      The size of the desired hash output
1497
-     * @return string          A BLAKE2 hashing context, encoded as a string
1498
-     *                         (To be 100% compatible with ext/libsodium)
1499
-     * @throws SodiumException
1500
-     * @throws TypeError
1501
-     * @psalm-suppress MixedArgument
1502
-     */
1503
-    public static function crypto_generichash_init($key = '', $length = self::CRYPTO_GENERICHASH_BYTES)
1504
-    {
1505
-        /* Type checks: */
1506
-        if (is_null($key)) {
1507
-            $key = '';
1508
-        }
1509
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 1);
1510
-        ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2);
1511
-
1512
-        /* Input validation: */
1513
-        if (!empty($key)) {
1514
-            if (ParagonIE_Sodium_Core_Util::strlen($key) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
1515
-                throw new SodiumException('Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.');
1516
-            }
1517
-            if (ParagonIE_Sodium_Core_Util::strlen($key) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
1518
-                throw new SodiumException('Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.');
1519
-            }
1520
-        }
1521
-
1522
-        if (self::useNewSodiumAPI()) {
1523
-            return sodium_crypto_generichash_init($key, $length);
1524
-        }
1525
-        if (self::use_fallback('crypto_generichash_init')) {
1526
-            return (string) call_user_func('\\Sodium\\crypto_generichash_init', $key, $length);
1527
-        }
1528
-        if (PHP_INT_SIZE === 4) {
1529
-            return ParagonIE_Sodium_Crypto32::generichash_init($key, $length);
1530
-        }
1531
-        return ParagonIE_Sodium_Crypto::generichash_init($key, $length);
1532
-    }
1533
-
1534
-    /**
1535
-     * Initialize a BLAKE2b hashing context, for use in a streaming interface.
1536
-     *
1537
-     * @param string|null $key If specified must be a string between 16 and 64 bytes
1538
-     * @param int $length      The size of the desired hash output
1539
-     * @param string $salt     Salt (up to 16 bytes)
1540
-     * @param string $personal Personalization string (up to 16 bytes)
1541
-     * @return string          A BLAKE2 hashing context, encoded as a string
1542
-     *                         (To be 100% compatible with ext/libsodium)
1543
-     * @throws SodiumException
1544
-     * @throws TypeError
1545
-     * @psalm-suppress MixedArgument
1546
-     */
1547
-    public static function crypto_generichash_init_salt_personal(
1548
-        $key = '',
1549
-        $length = self::CRYPTO_GENERICHASH_BYTES,
1550
-        $salt = '',
1551
-        $personal = ''
1552
-    ) {
1553
-        /* Type checks: */
1554
-        if (is_null($key)) {
1555
-            $key = '';
1556
-        }
1557
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 1);
1558
-        ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2);
1559
-        ParagonIE_Sodium_Core_Util::declareScalarType($salt, 'string', 3);
1560
-        ParagonIE_Sodium_Core_Util::declareScalarType($personal, 'string', 4);
1561
-        $salt = str_pad($salt, 16, "\0", STR_PAD_RIGHT);
1562
-        $personal = str_pad($personal, 16, "\0", STR_PAD_RIGHT);
1563
-
1564
-        /* Input validation: */
1565
-        if (!empty($key)) {
1566
-            /*
30
+	/**
31
+	 * This parameter prevents the use of the PECL extension.
32
+	 * It should only be used for unit testing.
33
+	 *
34
+	 * @var bool
35
+	 */
36
+	public static $disableFallbackForUnitTests = false;
37
+
38
+	/**
39
+	 * Use fast multiplication rather than our constant-time multiplication
40
+	 * implementation. Can be enabled at runtime. Only enable this if you
41
+	 * are absolutely certain that there is no timing leak on your platform.
42
+	 *
43
+	 * @var bool
44
+	 */
45
+	public static $fastMult = false;
46
+
47
+	const LIBRARY_MAJOR_VERSION = 9;
48
+	const LIBRARY_MINOR_VERSION = 1;
49
+	const LIBRARY_VERSION_MAJOR = 9;
50
+	const LIBRARY_VERSION_MINOR = 1;
51
+	const VERSION_STRING = 'polyfill-1.0.8';
52
+
53
+	// From libsodium
54
+	const BASE64_VARIANT_ORIGINAL = 1;
55
+	const BASE64_VARIANT_ORIGINAL_NO_PADDING = 3;
56
+	const BASE64_VARIANT_URLSAFE = 5;
57
+	const BASE64_VARIANT_URLSAFE_NO_PADDING = 7;
58
+	const CRYPTO_AEAD_AES256GCM_KEYBYTES = 32;
59
+	const CRYPTO_AEAD_AES256GCM_NSECBYTES = 0;
60
+	const CRYPTO_AEAD_AES256GCM_NPUBBYTES = 12;
61
+	const CRYPTO_AEAD_AES256GCM_ABYTES = 16;
62
+	const CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES = 32;
63
+	const CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES = 0;
64
+	const CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES = 8;
65
+	const CRYPTO_AEAD_CHACHA20POLY1305_ABYTES = 16;
66
+	const CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES = 32;
67
+	const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES = 0;
68
+	const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES = 12;
69
+	const CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES = 16;
70
+	const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES = 32;
71
+	const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NSECBYTES = 0;
72
+	const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES = 24;
73
+	const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES = 16;
74
+	const CRYPTO_AUTH_BYTES = 32;
75
+	const CRYPTO_AUTH_KEYBYTES = 32;
76
+	const CRYPTO_BOX_SEALBYTES = 16;
77
+	const CRYPTO_BOX_SECRETKEYBYTES = 32;
78
+	const CRYPTO_BOX_PUBLICKEYBYTES = 32;
79
+	const CRYPTO_BOX_KEYPAIRBYTES = 64;
80
+	const CRYPTO_BOX_MACBYTES = 16;
81
+	const CRYPTO_BOX_NONCEBYTES = 24;
82
+	const CRYPTO_BOX_SEEDBYTES = 32;
83
+	const CRYPTO_CORE_RISTRETTO255_BYTES = 32;
84
+	const CRYPTO_CORE_RISTRETTO255_SCALARBYTES = 32;
85
+	const CRYPTO_CORE_RISTRETTO255_HASHBYTES = 64;
86
+	const CRYPTO_CORE_RISTRETTO255_NONREDUCEDSCALARBYTES = 64;
87
+	const CRYPTO_KDF_BYTES_MIN = 16;
88
+	const CRYPTO_KDF_BYTES_MAX = 64;
89
+	const CRYPTO_KDF_CONTEXTBYTES = 8;
90
+	const CRYPTO_KDF_KEYBYTES = 32;
91
+	const CRYPTO_KX_BYTES = 32;
92
+	const CRYPTO_KX_PRIMITIVE = 'x25519blake2b';
93
+	const CRYPTO_KX_SEEDBYTES = 32;
94
+	const CRYPTO_KX_KEYPAIRBYTES = 64;
95
+	const CRYPTO_KX_PUBLICKEYBYTES = 32;
96
+	const CRYPTO_KX_SECRETKEYBYTES = 32;
97
+	const CRYPTO_KX_SESSIONKEYBYTES = 32;
98
+	const CRYPTO_GENERICHASH_BYTES = 32;
99
+	const CRYPTO_GENERICHASH_BYTES_MIN = 16;
100
+	const CRYPTO_GENERICHASH_BYTES_MAX = 64;
101
+	const CRYPTO_GENERICHASH_KEYBYTES = 32;
102
+	const CRYPTO_GENERICHASH_KEYBYTES_MIN = 16;
103
+	const CRYPTO_GENERICHASH_KEYBYTES_MAX = 64;
104
+	const CRYPTO_PWHASH_SALTBYTES = 16;
105
+	const CRYPTO_PWHASH_STRPREFIX = '$argon2id$';
106
+	const CRYPTO_PWHASH_ALG_ARGON2I13 = 1;
107
+	const CRYPTO_PWHASH_ALG_ARGON2ID13 = 2;
108
+	const CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE = 33554432;
109
+	const CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE = 4;
110
+	const CRYPTO_PWHASH_MEMLIMIT_MODERATE = 134217728;
111
+	const CRYPTO_PWHASH_OPSLIMIT_MODERATE = 6;
112
+	const CRYPTO_PWHASH_MEMLIMIT_SENSITIVE = 536870912;
113
+	const CRYPTO_PWHASH_OPSLIMIT_SENSITIVE = 8;
114
+	const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES = 32;
115
+	const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX = '$7$';
116
+	const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE = 534288;
117
+	const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE = 16777216;
118
+	const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_SENSITIVE = 33554432;
119
+	const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_SENSITIVE = 1073741824;
120
+	const CRYPTO_SCALARMULT_BYTES = 32;
121
+	const CRYPTO_SCALARMULT_SCALARBYTES = 32;
122
+	const CRYPTO_SCALARMULT_RISTRETTO255_BYTES = 32;
123
+	const CRYPTO_SCALARMULT_RISTRETTO255_SCALARBYTES = 32;
124
+	const CRYPTO_SHORTHASH_BYTES = 8;
125
+	const CRYPTO_SHORTHASH_KEYBYTES = 16;
126
+	const CRYPTO_SECRETBOX_KEYBYTES = 32;
127
+	const CRYPTO_SECRETBOX_MACBYTES = 16;
128
+	const CRYPTO_SECRETBOX_NONCEBYTES = 24;
129
+	const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES = 17;
130
+	const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES = 24;
131
+	const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES = 32;
132
+	const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH = 0;
133
+	const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PULL = 1;
134
+	const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY = 2;
135
+	const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL = 3;
136
+	const CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX = 0x3fffffff80;
137
+	const CRYPTO_SIGN_BYTES = 64;
138
+	const CRYPTO_SIGN_SEEDBYTES = 32;
139
+	const CRYPTO_SIGN_PUBLICKEYBYTES = 32;
140
+	const CRYPTO_SIGN_SECRETKEYBYTES = 64;
141
+	const CRYPTO_SIGN_KEYPAIRBYTES = 96;
142
+	const CRYPTO_STREAM_KEYBYTES = 32;
143
+	const CRYPTO_STREAM_NONCEBYTES = 24;
144
+	const CRYPTO_STREAM_XCHACHA20_KEYBYTES = 32;
145
+	const CRYPTO_STREAM_XCHACHA20_NONCEBYTES = 24;
146
+
147
+	/**
148
+	 * Add two numbers (little-endian unsigned), storing the value in the first
149
+	 * parameter.
150
+	 *
151
+	 * This mutates $val.
152
+	 *
153
+	 * @param string $val
154
+	 * @param string $addv
155
+	 * @return void
156
+	 * @throws SodiumException
157
+	 */
158
+	public static function add(&$val, $addv)
159
+	{
160
+		$val_len = ParagonIE_Sodium_Core_Util::strlen($val);
161
+		$addv_len = ParagonIE_Sodium_Core_Util::strlen($addv);
162
+		if ($val_len !== $addv_len) {
163
+			throw new SodiumException('values must have the same length');
164
+		}
165
+		$A = ParagonIE_Sodium_Core_Util::stringToIntArray($val);
166
+		$B = ParagonIE_Sodium_Core_Util::stringToIntArray($addv);
167
+
168
+		$c = 0;
169
+		for ($i = 0; $i < $val_len; $i++) {
170
+			$c += ($A[$i] + $B[$i]);
171
+			$A[$i] = ($c & 0xff);
172
+			$c >>= 8;
173
+		}
174
+		$val = ParagonIE_Sodium_Core_Util::intArrayToString($A);
175
+	}
176
+
177
+	/**
178
+	 * @param string $encoded
179
+	 * @param int $variant
180
+	 * @param string $ignore
181
+	 * @return string
182
+	 * @throws SodiumException
183
+	 */
184
+	public static function base642bin($encoded, $variant, $ignore = '')
185
+	{
186
+		/* Type checks: */
187
+		ParagonIE_Sodium_Core_Util::declareScalarType($encoded, 'string', 1);
188
+
189
+		/** @var string $encoded */
190
+		$encoded = (string) $encoded;
191
+		if (ParagonIE_Sodium_Core_Util::strlen($encoded) === 0) {
192
+			return '';
193
+		}
194
+
195
+		// Just strip before decoding
196
+		if (!empty($ignore)) {
197
+			$encoded = str_replace($ignore, '', $encoded);
198
+		}
199
+
200
+		try {
201
+			switch ($variant) {
202
+				case self::BASE64_VARIANT_ORIGINAL:
203
+					return ParagonIE_Sodium_Core_Base64_Original::decode($encoded, true);
204
+				case self::BASE64_VARIANT_ORIGINAL_NO_PADDING:
205
+					return ParagonIE_Sodium_Core_Base64_Original::decode($encoded, false);
206
+				case self::BASE64_VARIANT_URLSAFE:
207
+					return ParagonIE_Sodium_Core_Base64_UrlSafe::decode($encoded, true);
208
+				case self::BASE64_VARIANT_URLSAFE_NO_PADDING:
209
+					return ParagonIE_Sodium_Core_Base64_UrlSafe::decode($encoded, false);
210
+				default:
211
+					throw new SodiumException('invalid base64 variant identifier');
212
+			}
213
+		} catch (Exception $ex) {
214
+			if ($ex instanceof SodiumException) {
215
+				throw $ex;
216
+			}
217
+			throw new SodiumException('invalid base64 string');
218
+		}
219
+	}
220
+
221
+	/**
222
+	 * @param string $decoded
223
+	 * @param int $variant
224
+	 * @return string
225
+	 * @throws SodiumException
226
+	 */
227
+	public static function bin2base64($decoded, $variant)
228
+	{
229
+		/* Type checks: */
230
+		ParagonIE_Sodium_Core_Util::declareScalarType($decoded, 'string', 1);
231
+		/** @var string $decoded */
232
+		$decoded = (string) $decoded;
233
+		if (ParagonIE_Sodium_Core_Util::strlen($decoded) === 0) {
234
+			return '';
235
+		}
236
+
237
+		switch ($variant) {
238
+			case self::BASE64_VARIANT_ORIGINAL:
239
+				return ParagonIE_Sodium_Core_Base64_Original::encode($decoded);
240
+			case self::BASE64_VARIANT_ORIGINAL_NO_PADDING:
241
+				return ParagonIE_Sodium_Core_Base64_Original::encodeUnpadded($decoded);
242
+			case self::BASE64_VARIANT_URLSAFE:
243
+				return ParagonIE_Sodium_Core_Base64_UrlSafe::encode($decoded);
244
+			case self::BASE64_VARIANT_URLSAFE_NO_PADDING:
245
+				return ParagonIE_Sodium_Core_Base64_UrlSafe::encodeUnpadded($decoded);
246
+			default:
247
+				throw new SodiumException('invalid base64 variant identifier');
248
+		}
249
+	}
250
+
251
+	/**
252
+	 * Cache-timing-safe implementation of bin2hex().
253
+	 *
254
+	 * @param string $string A string (probably raw binary)
255
+	 * @return string        A hexadecimal-encoded string
256
+	 * @throws SodiumException
257
+	 * @throws TypeError
258
+	 * @psalm-suppress MixedArgument
259
+	 */
260
+	public static function bin2hex($string)
261
+	{
262
+		/* Type checks: */
263
+		ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1);
264
+
265
+		if (self::useNewSodiumAPI()) {
266
+			return (string) sodium_bin2hex($string);
267
+		}
268
+		if (self::use_fallback('bin2hex')) {
269
+			return (string) call_user_func('\\Sodium\\bin2hex', $string);
270
+		}
271
+		return ParagonIE_Sodium_Core_Util::bin2hex($string);
272
+	}
273
+
274
+	/**
275
+	 * Compare two strings, in constant-time.
276
+	 * Compared to memcmp(), compare() is more useful for sorting.
277
+	 *
278
+	 * @param string $left  The left operand; must be a string
279
+	 * @param string $right The right operand; must be a string
280
+	 * @return int          If < 0 if the left operand is less than the right
281
+	 *                      If = 0 if both strings are equal
282
+	 *                      If > 0 if the right operand is less than the left
283
+	 * @throws SodiumException
284
+	 * @throws TypeError
285
+	 * @psalm-suppress MixedArgument
286
+	 */
287
+	public static function compare($left, $right)
288
+	{
289
+		/* Type checks: */
290
+		ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1);
291
+		ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2);
292
+
293
+		if (self::useNewSodiumAPI()) {
294
+			return (int) sodium_compare($left, $right);
295
+		}
296
+		if (self::use_fallback('compare')) {
297
+			return (int) call_user_func('\\Sodium\\compare', $left, $right);
298
+		}
299
+		return ParagonIE_Sodium_Core_Util::compare($left, $right);
300
+	}
301
+
302
+	/**
303
+	 * Is AES-256-GCM even available to use?
304
+	 *
305
+	 * @return bool
306
+	 * @psalm-suppress UndefinedFunction
307
+	 * @psalm-suppress MixedInferredReturnType
308
+	 * @psalm-suppress MixedReturnStatement
309
+	 */
310
+	public static function crypto_aead_aes256gcm_is_available()
311
+	{
312
+		if (self::useNewSodiumAPI()) {
313
+			return sodium_crypto_aead_aes256gcm_is_available();
314
+		}
315
+		if (self::use_fallback('crypto_aead_aes256gcm_is_available')) {
316
+			return call_user_func('\\Sodium\\crypto_aead_aes256gcm_is_available');
317
+		}
318
+		if (PHP_VERSION_ID < 70100) {
319
+			// OpenSSL doesn't support AEAD before 7.1.0
320
+			return false;
321
+		}
322
+		if (!is_callable('openssl_encrypt') || !is_callable('openssl_decrypt')) {
323
+			// OpenSSL isn't installed
324
+			return false;
325
+		}
326
+		return (bool) in_array('aes-256-gcm', openssl_get_cipher_methods());
327
+	}
328
+
329
+	/**
330
+	 * Authenticated Encryption with Associated Data: Decryption
331
+	 *
332
+	 * Algorithm:
333
+	 *     AES-256-GCM
334
+	 *
335
+	 * This mode uses a 64-bit random nonce with a 64-bit counter.
336
+	 * IETF mode uses a 96-bit random nonce with a 32-bit counter.
337
+	 *
338
+	 * @param string $ciphertext Encrypted message (with Poly1305 MAC appended)
339
+	 * @param string $assocData  Authenticated Associated Data (unencrypted)
340
+	 * @param string $nonce      Number to be used only Once; must be 8 bytes
341
+	 * @param string $key        Encryption key
342
+	 *
343
+	 * @return string|bool       The original plaintext message
344
+	 * @throws SodiumException
345
+	 * @throws TypeError
346
+	 * @psalm-suppress MixedArgument
347
+	 * @psalm-suppress MixedInferredReturnType
348
+	 * @psalm-suppress MixedReturnStatement
349
+	 */
350
+	public static function crypto_aead_aes256gcm_decrypt(
351
+		$ciphertext = '',
352
+		$assocData = '',
353
+		$nonce = '',
354
+		$key = ''
355
+	) {
356
+		if (!self::crypto_aead_aes256gcm_is_available()) {
357
+			throw new SodiumException('AES-256-GCM is not available');
358
+		}
359
+		ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
360
+		ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
361
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
362
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
363
+
364
+		/* Input validation: */
365
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AES256GCM_NPUBBYTES) {
366
+			throw new SodiumException('Nonce must be CRYPTO_AEAD_AES256GCM_NPUBBYTES long');
367
+		}
368
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AES256GCM_KEYBYTES) {
369
+			throw new SodiumException('Key must be CRYPTO_AEAD_AES256GCM_KEYBYTES long');
370
+		}
371
+		if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_AES256GCM_ABYTES) {
372
+			throw new SodiumException('Message must be at least CRYPTO_AEAD_AES256GCM_ABYTES long');
373
+		}
374
+		if (!is_callable('openssl_decrypt')) {
375
+			throw new SodiumException('The OpenSSL extension is not installed, or openssl_decrypt() is not available');
376
+		}
377
+
378
+		/** @var string $ctext */
379
+		$ctext = ParagonIE_Sodium_Core_Util::substr($ciphertext, 0, -self::CRYPTO_AEAD_AES256GCM_ABYTES);
380
+		/** @var string $authTag */
381
+		$authTag = ParagonIE_Sodium_Core_Util::substr($ciphertext, -self::CRYPTO_AEAD_AES256GCM_ABYTES, 16);
382
+		return openssl_decrypt(
383
+			$ctext,
384
+			'aes-256-gcm',
385
+			$key,
386
+			OPENSSL_RAW_DATA,
387
+			$nonce,
388
+			$authTag,
389
+			$assocData
390
+		);
391
+	}
392
+
393
+	/**
394
+	 * Authenticated Encryption with Associated Data: Encryption
395
+	 *
396
+	 * Algorithm:
397
+	 *     AES-256-GCM
398
+	 *
399
+	 * @param string $plaintext Message to be encrypted
400
+	 * @param string $assocData Authenticated Associated Data (unencrypted)
401
+	 * @param string $nonce     Number to be used only Once; must be 8 bytes
402
+	 * @param string $key       Encryption key
403
+	 *
404
+	 * @return string           Ciphertext with a 16-byte GCM message
405
+	 *                          authentication code appended
406
+	 * @throws SodiumException
407
+	 * @throws TypeError
408
+	 * @psalm-suppress MixedArgument
409
+	 */
410
+	public static function crypto_aead_aes256gcm_encrypt(
411
+		$plaintext = '',
412
+		$assocData = '',
413
+		$nonce = '',
414
+		$key = ''
415
+	) {
416
+		if (!self::crypto_aead_aes256gcm_is_available()) {
417
+			throw new SodiumException('AES-256-GCM is not available');
418
+		}
419
+		ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
420
+		ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
421
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
422
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
423
+
424
+		/* Input validation: */
425
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AES256GCM_NPUBBYTES) {
426
+			throw new SodiumException('Nonce must be CRYPTO_AEAD_AES256GCM_NPUBBYTES long');
427
+		}
428
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AES256GCM_KEYBYTES) {
429
+			throw new SodiumException('Key must be CRYPTO_AEAD_AES256GCM_KEYBYTES long');
430
+		}
431
+
432
+		if (!is_callable('openssl_encrypt')) {
433
+			throw new SodiumException('The OpenSSL extension is not installed, or openssl_encrypt() is not available');
434
+		}
435
+
436
+		$authTag = '';
437
+		$ciphertext = openssl_encrypt(
438
+			$plaintext,
439
+			'aes-256-gcm',
440
+			$key,
441
+			OPENSSL_RAW_DATA,
442
+			$nonce,
443
+			$authTag,
444
+			$assocData
445
+		);
446
+		return $ciphertext . $authTag;
447
+	}
448
+
449
+	/**
450
+	 * Return a secure random key for use with the AES-256-GCM
451
+	 * symmetric AEAD interface.
452
+	 *
453
+	 * @return string
454
+	 * @throws Exception
455
+	 * @throws Error
456
+	 */
457
+	public static function crypto_aead_aes256gcm_keygen()
458
+	{
459
+		return random_bytes(self::CRYPTO_AEAD_AES256GCM_KEYBYTES);
460
+	}
461
+
462
+	/**
463
+	 * Authenticated Encryption with Associated Data: Decryption
464
+	 *
465
+	 * Algorithm:
466
+	 *     ChaCha20-Poly1305
467
+	 *
468
+	 * This mode uses a 64-bit random nonce with a 64-bit counter.
469
+	 * IETF mode uses a 96-bit random nonce with a 32-bit counter.
470
+	 *
471
+	 * @param string $ciphertext Encrypted message (with Poly1305 MAC appended)
472
+	 * @param string $assocData  Authenticated Associated Data (unencrypted)
473
+	 * @param string $nonce      Number to be used only Once; must be 8 bytes
474
+	 * @param string $key        Encryption key
475
+	 *
476
+	 * @return string            The original plaintext message
477
+	 * @throws SodiumException
478
+	 * @throws TypeError
479
+	 * @psalm-suppress MixedArgument
480
+	 * @psalm-suppress MixedInferredReturnType
481
+	 * @psalm-suppress MixedReturnStatement
482
+	 */
483
+	public static function crypto_aead_chacha20poly1305_decrypt(
484
+		$ciphertext = '',
485
+		$assocData = '',
486
+		$nonce = '',
487
+		$key = ''
488
+	) {
489
+		/* Type checks: */
490
+		ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
491
+		ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
492
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
493
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
494
+
495
+		/* Input validation: */
496
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES) {
497
+			throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES long');
498
+		}
499
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
500
+			throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
501
+		}
502
+		if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES) {
503
+			throw new SodiumException('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_ABYTES long');
504
+		}
505
+
506
+		if (self::useNewSodiumAPI()) {
507
+			/**
508
+			 * @psalm-suppress InvalidReturnStatement
509
+			 * @psalm-suppress FalsableReturnStatement
510
+			 */
511
+			return sodium_crypto_aead_chacha20poly1305_decrypt(
512
+				$ciphertext,
513
+				$assocData,
514
+				$nonce,
515
+				$key
516
+			);
517
+		}
518
+		if (self::use_fallback('crypto_aead_chacha20poly1305_decrypt')) {
519
+			return call_user_func(
520
+				'\\Sodium\\crypto_aead_chacha20poly1305_decrypt',
521
+				$ciphertext,
522
+				$assocData,
523
+				$nonce,
524
+				$key
525
+			);
526
+		}
527
+		if (PHP_INT_SIZE === 4) {
528
+			return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_decrypt(
529
+				$ciphertext,
530
+				$assocData,
531
+				$nonce,
532
+				$key
533
+			);
534
+		}
535
+		return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_decrypt(
536
+			$ciphertext,
537
+			$assocData,
538
+			$nonce,
539
+			$key
540
+		);
541
+	}
542
+
543
+	/**
544
+	 * Authenticated Encryption with Associated Data
545
+	 *
546
+	 * Algorithm:
547
+	 *     ChaCha20-Poly1305
548
+	 *
549
+	 * This mode uses a 64-bit random nonce with a 64-bit counter.
550
+	 * IETF mode uses a 96-bit random nonce with a 32-bit counter.
551
+	 *
552
+	 * @param string $plaintext Message to be encrypted
553
+	 * @param string $assocData Authenticated Associated Data (unencrypted)
554
+	 * @param string $nonce     Number to be used only Once; must be 8 bytes
555
+	 * @param string $key       Encryption key
556
+	 *
557
+	 * @return string           Ciphertext with a 16-byte Poly1305 message
558
+	 *                          authentication code appended
559
+	 * @throws SodiumException
560
+	 * @throws TypeError
561
+	 * @psalm-suppress MixedArgument
562
+	 */
563
+	public static function crypto_aead_chacha20poly1305_encrypt(
564
+		$plaintext = '',
565
+		$assocData = '',
566
+		$nonce = '',
567
+		$key = ''
568
+	) {
569
+		/* Type checks: */
570
+		ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
571
+		ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
572
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
573
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
574
+
575
+		/* Input validation: */
576
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES) {
577
+			throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES long');
578
+		}
579
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
580
+			throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
581
+		}
582
+
583
+		if (self::useNewSodiumAPI()) {
584
+			return (string) sodium_crypto_aead_chacha20poly1305_encrypt(
585
+				$plaintext,
586
+				$assocData,
587
+				$nonce,
588
+				$key
589
+			);
590
+		}
591
+		if (self::use_fallback('crypto_aead_chacha20poly1305_encrypt')) {
592
+			return (string) call_user_func(
593
+				'\\Sodium\\crypto_aead_chacha20poly1305_encrypt',
594
+				$plaintext,
595
+				$assocData,
596
+				$nonce,
597
+				$key
598
+			);
599
+		}
600
+		if (PHP_INT_SIZE === 4) {
601
+			return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_encrypt(
602
+				$plaintext,
603
+				$assocData,
604
+				$nonce,
605
+				$key
606
+			);
607
+		}
608
+		return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_encrypt(
609
+			$plaintext,
610
+			$assocData,
611
+			$nonce,
612
+			$key
613
+		);
614
+	}
615
+
616
+	/**
617
+	 * Authenticated Encryption with Associated Data: Decryption
618
+	 *
619
+	 * Algorithm:
620
+	 *     ChaCha20-Poly1305
621
+	 *
622
+	 * IETF mode uses a 96-bit random nonce with a 32-bit counter.
623
+	 * Regular mode uses a 64-bit random nonce with a 64-bit counter.
624
+	 *
625
+	 * @param string $ciphertext Encrypted message (with Poly1305 MAC appended)
626
+	 * @param string $assocData  Authenticated Associated Data (unencrypted)
627
+	 * @param string $nonce      Number to be used only Once; must be 12 bytes
628
+	 * @param string $key        Encryption key
629
+	 *
630
+	 * @return string            The original plaintext message
631
+	 * @throws SodiumException
632
+	 * @throws TypeError
633
+	 * @psalm-suppress MixedArgument
634
+	 * @psalm-suppress MixedInferredReturnType
635
+	 * @psalm-suppress MixedReturnStatement
636
+	 */
637
+	public static function crypto_aead_chacha20poly1305_ietf_decrypt(
638
+		$ciphertext = '',
639
+		$assocData = '',
640
+		$nonce = '',
641
+		$key = ''
642
+	) {
643
+		/* Type checks: */
644
+		ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
645
+		ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
646
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
647
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
648
+
649
+		/* Input validation: */
650
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES) {
651
+			throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long');
652
+		}
653
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
654
+			throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
655
+		}
656
+		if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES) {
657
+			throw new SodiumException('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_ABYTES long');
658
+		}
659
+
660
+		if (self::useNewSodiumAPI()) {
661
+			/**
662
+			 * @psalm-suppress InvalidReturnStatement
663
+			 * @psalm-suppress FalsableReturnStatement
664
+			 */
665
+			return sodium_crypto_aead_chacha20poly1305_ietf_decrypt(
666
+				$ciphertext,
667
+				$assocData,
668
+				$nonce,
669
+				$key
670
+			);
671
+		}
672
+		if (self::use_fallback('crypto_aead_chacha20poly1305_ietf_decrypt')) {
673
+			return call_user_func(
674
+				'\\Sodium\\crypto_aead_chacha20poly1305_ietf_decrypt',
675
+				$ciphertext,
676
+				$assocData,
677
+				$nonce,
678
+				$key
679
+			);
680
+		}
681
+		if (PHP_INT_SIZE === 4) {
682
+			return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_ietf_decrypt(
683
+				$ciphertext,
684
+				$assocData,
685
+				$nonce,
686
+				$key
687
+			);
688
+		}
689
+		return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_ietf_decrypt(
690
+			$ciphertext,
691
+			$assocData,
692
+			$nonce,
693
+			$key
694
+		);
695
+	}
696
+
697
+	/**
698
+	 * Return a secure random key for use with the ChaCha20-Poly1305
699
+	 * symmetric AEAD interface.
700
+	 *
701
+	 * @return string
702
+	 * @throws Exception
703
+	 * @throws Error
704
+	 */
705
+	public static function crypto_aead_chacha20poly1305_keygen()
706
+	{
707
+		return random_bytes(self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES);
708
+	}
709
+
710
+	/**
711
+	 * Authenticated Encryption with Associated Data
712
+	 *
713
+	 * Algorithm:
714
+	 *     ChaCha20-Poly1305
715
+	 *
716
+	 * IETF mode uses a 96-bit random nonce with a 32-bit counter.
717
+	 * Regular mode uses a 64-bit random nonce with a 64-bit counter.
718
+	 *
719
+	 * @param string $plaintext Message to be encrypted
720
+	 * @param string $assocData Authenticated Associated Data (unencrypted)
721
+	 * @param string $nonce Number to be used only Once; must be 8 bytes
722
+	 * @param string $key Encryption key
723
+	 *
724
+	 * @return string           Ciphertext with a 16-byte Poly1305 message
725
+	 *                          authentication code appended
726
+	 * @throws SodiumException
727
+	 * @throws TypeError
728
+	 * @psalm-suppress MixedArgument
729
+	 */
730
+	public static function crypto_aead_chacha20poly1305_ietf_encrypt(
731
+		$plaintext = '',
732
+		$assocData = '',
733
+		$nonce = '',
734
+		$key = ''
735
+	) {
736
+		/* Type checks: */
737
+		ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
738
+		if (!is_null($assocData)) {
739
+			ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
740
+		}
741
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
742
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
743
+
744
+		/* Input validation: */
745
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES) {
746
+			throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long');
747
+		}
748
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
749
+			throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
750
+		}
751
+
752
+		if (self::useNewSodiumAPI()) {
753
+			return (string) sodium_crypto_aead_chacha20poly1305_ietf_encrypt(
754
+				$plaintext,
755
+				$assocData,
756
+				$nonce,
757
+				$key
758
+			);
759
+		}
760
+		if (self::use_fallback('crypto_aead_chacha20poly1305_ietf_encrypt')) {
761
+			return (string) call_user_func(
762
+				'\\Sodium\\crypto_aead_chacha20poly1305_ietf_encrypt',
763
+				$plaintext,
764
+				$assocData,
765
+				$nonce,
766
+				$key
767
+			);
768
+		}
769
+		if (PHP_INT_SIZE === 4) {
770
+			return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_ietf_encrypt(
771
+				$plaintext,
772
+				$assocData,
773
+				$nonce,
774
+				$key
775
+			);
776
+		}
777
+		return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_ietf_encrypt(
778
+			$plaintext,
779
+			$assocData,
780
+			$nonce,
781
+			$key
782
+		);
783
+	}
784
+
785
+	/**
786
+	 * Return a secure random key for use with the ChaCha20-Poly1305
787
+	 * symmetric AEAD interface. (IETF version)
788
+	 *
789
+	 * @return string
790
+	 * @throws Exception
791
+	 * @throws Error
792
+	 */
793
+	public static function crypto_aead_chacha20poly1305_ietf_keygen()
794
+	{
795
+		return random_bytes(self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES);
796
+	}
797
+
798
+	/**
799
+	 * Authenticated Encryption with Associated Data: Decryption
800
+	 *
801
+	 * Algorithm:
802
+	 *     XChaCha20-Poly1305
803
+	 *
804
+	 * This mode uses a 64-bit random nonce with a 64-bit counter.
805
+	 * IETF mode uses a 96-bit random nonce with a 32-bit counter.
806
+	 *
807
+	 * @param string $ciphertext   Encrypted message (with Poly1305 MAC appended)
808
+	 * @param string $assocData    Authenticated Associated Data (unencrypted)
809
+	 * @param string $nonce        Number to be used only Once; must be 8 bytes
810
+	 * @param string $key          Encryption key
811
+	 * @param bool   $dontFallback Don't fallback to ext/sodium
812
+	 *
813
+	 * @return string|bool         The original plaintext message
814
+	 * @throws SodiumException
815
+	 * @throws TypeError
816
+	 * @psalm-suppress MixedArgument
817
+	 */
818
+	public static function crypto_aead_xchacha20poly1305_ietf_decrypt(
819
+		$ciphertext = '',
820
+		$assocData = '',
821
+		$nonce = '',
822
+		$key = '',
823
+		$dontFallback = false
824
+	) {
825
+		/* Type checks: */
826
+		ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
827
+		if (!is_null($assocData)) {
828
+			ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
829
+		} else {
830
+			$assocData = '';
831
+		}
832
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
833
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
834
+
835
+		/* Input validation: */
836
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES) {
837
+			throw new SodiumException('Nonce must be CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES long');
838
+		}
839
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES) {
840
+			throw new SodiumException('Key must be CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES long');
841
+		}
842
+		if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES) {
843
+			throw new SodiumException('Message must be at least CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES long');
844
+		}
845
+		if (self::useNewSodiumAPI() && !$dontFallback) {
846
+			if (is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_decrypt')) {
847
+				return sodium_crypto_aead_xchacha20poly1305_ietf_decrypt(
848
+					$ciphertext,
849
+					$assocData,
850
+					$nonce,
851
+					$key
852
+				);
853
+			}
854
+		}
855
+
856
+		if (PHP_INT_SIZE === 4) {
857
+			return ParagonIE_Sodium_Crypto32::aead_xchacha20poly1305_ietf_decrypt(
858
+				$ciphertext,
859
+				$assocData,
860
+				$nonce,
861
+				$key
862
+			);
863
+		}
864
+		return ParagonIE_Sodium_Crypto::aead_xchacha20poly1305_ietf_decrypt(
865
+			$ciphertext,
866
+			$assocData,
867
+			$nonce,
868
+			$key
869
+		);
870
+	}
871
+
872
+	/**
873
+	 * Authenticated Encryption with Associated Data
874
+	 *
875
+	 * Algorithm:
876
+	 *     XChaCha20-Poly1305
877
+	 *
878
+	 * This mode uses a 64-bit random nonce with a 64-bit counter.
879
+	 * IETF mode uses a 96-bit random nonce with a 32-bit counter.
880
+	 *
881
+	 * @param string $plaintext    Message to be encrypted
882
+	 * @param string $assocData    Authenticated Associated Data (unencrypted)
883
+	 * @param string $nonce        Number to be used only Once; must be 8 bytes
884
+	 * @param string $key          Encryption key
885
+	 * @param bool   $dontFallback Don't fallback to ext/sodium
886
+	 *
887
+	 * @return string           Ciphertext with a 16-byte Poly1305 message
888
+	 *                          authentication code appended
889
+	 * @throws SodiumException
890
+	 * @throws TypeError
891
+	 * @psalm-suppress MixedArgument
892
+	 */
893
+	public static function crypto_aead_xchacha20poly1305_ietf_encrypt(
894
+		$plaintext = '',
895
+		$assocData = '',
896
+		$nonce = '',
897
+		$key = '',
898
+		$dontFallback = false
899
+	) {
900
+		/* Type checks: */
901
+		ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
902
+		if (!is_null($assocData)) {
903
+			ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
904
+		} else {
905
+			$assocData = '';
906
+		}
907
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
908
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
909
+
910
+		/* Input validation: */
911
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES) {
912
+			throw new SodiumException('Nonce must be CRYPTO_AEAD_XCHACHA20POLY1305_NPUBBYTES long');
913
+		}
914
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES) {
915
+			throw new SodiumException('Key must be CRYPTO_AEAD_XCHACHA20POLY1305_KEYBYTES long');
916
+		}
917
+		if (self::useNewSodiumAPI() && !$dontFallback) {
918
+			if (is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')) {
919
+				return sodium_crypto_aead_xchacha20poly1305_ietf_encrypt(
920
+					$plaintext,
921
+					$assocData,
922
+					$nonce,
923
+					$key
924
+				);
925
+			}
926
+		}
927
+
928
+		if (PHP_INT_SIZE === 4) {
929
+			return ParagonIE_Sodium_Crypto32::aead_xchacha20poly1305_ietf_encrypt(
930
+				$plaintext,
931
+				$assocData,
932
+				$nonce,
933
+				$key
934
+			);
935
+		}
936
+		return ParagonIE_Sodium_Crypto::aead_xchacha20poly1305_ietf_encrypt(
937
+			$plaintext,
938
+			$assocData,
939
+			$nonce,
940
+			$key
941
+		);
942
+	}
943
+
944
+	/**
945
+	 * Return a secure random key for use with the XChaCha20-Poly1305
946
+	 * symmetric AEAD interface.
947
+	 *
948
+	 * @return string
949
+	 * @throws Exception
950
+	 * @throws Error
951
+	 */
952
+	public static function crypto_aead_xchacha20poly1305_ietf_keygen()
953
+	{
954
+		return random_bytes(self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES);
955
+	}
956
+
957
+	/**
958
+	 * Authenticate a message. Uses symmetric-key cryptography.
959
+	 *
960
+	 * Algorithm:
961
+	 *     HMAC-SHA512-256. Which is HMAC-SHA-512 truncated to 256 bits.
962
+	 *     Not to be confused with HMAC-SHA-512/256 which would use the
963
+	 *     SHA-512/256 hash function (uses different initial parameters
964
+	 *     but still truncates to 256 bits to sidestep length-extension
965
+	 *     attacks).
966
+	 *
967
+	 * @param string $message Message to be authenticated
968
+	 * @param string $key Symmetric authentication key
969
+	 * @return string         Message authentication code
970
+	 * @throws SodiumException
971
+	 * @throws TypeError
972
+	 * @psalm-suppress MixedArgument
973
+	 */
974
+	public static function crypto_auth($message, $key)
975
+	{
976
+		/* Type checks: */
977
+		ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
978
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2);
979
+
980
+		/* Input validation: */
981
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AUTH_KEYBYTES) {
982
+			throw new SodiumException('Argument 2 must be CRYPTO_AUTH_KEYBYTES long.');
983
+		}
984
+
985
+		if (self::useNewSodiumAPI()) {
986
+			return (string) sodium_crypto_auth($message, $key);
987
+		}
988
+		if (self::use_fallback('crypto_auth')) {
989
+			return (string) call_user_func('\\Sodium\\crypto_auth', $message, $key);
990
+		}
991
+		if (PHP_INT_SIZE === 4) {
992
+			return ParagonIE_Sodium_Crypto32::auth($message, $key);
993
+		}
994
+		return ParagonIE_Sodium_Crypto::auth($message, $key);
995
+	}
996
+
997
+	/**
998
+	 * @return string
999
+	 * @throws Exception
1000
+	 * @throws Error
1001
+	 */
1002
+	public static function crypto_auth_keygen()
1003
+	{
1004
+		return random_bytes(self::CRYPTO_AUTH_KEYBYTES);
1005
+	}
1006
+
1007
+	/**
1008
+	 * Verify the MAC of a message previously authenticated with crypto_auth.
1009
+	 *
1010
+	 * @param string $mac Message authentication code
1011
+	 * @param string $message Message whose authenticity you are attempting to
1012
+	 *                        verify (with a given MAC and key)
1013
+	 * @param string $key Symmetric authentication key
1014
+	 * @return bool           TRUE if authenticated, FALSE otherwise
1015
+	 * @throws SodiumException
1016
+	 * @throws TypeError
1017
+	 * @psalm-suppress MixedArgument
1018
+	 */
1019
+	public static function crypto_auth_verify($mac, $message, $key)
1020
+	{
1021
+		/* Type checks: */
1022
+		ParagonIE_Sodium_Core_Util::declareScalarType($mac, 'string', 1);
1023
+		ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2);
1024
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
1025
+
1026
+		/* Input validation: */
1027
+		if (ParagonIE_Sodium_Core_Util::strlen($mac) !== self::CRYPTO_AUTH_BYTES) {
1028
+			throw new SodiumException('Argument 1 must be CRYPTO_AUTH_BYTES long.');
1029
+		}
1030
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AUTH_KEYBYTES) {
1031
+			throw new SodiumException('Argument 3 must be CRYPTO_AUTH_KEYBYTES long.');
1032
+		}
1033
+
1034
+		if (self::useNewSodiumAPI()) {
1035
+			return (bool) sodium_crypto_auth_verify($mac, $message, $key);
1036
+		}
1037
+		if (self::use_fallback('crypto_auth_verify')) {
1038
+			return (bool) call_user_func('\\Sodium\\crypto_auth_verify', $mac, $message, $key);
1039
+		}
1040
+		if (PHP_INT_SIZE === 4) {
1041
+			return ParagonIE_Sodium_Crypto32::auth_verify($mac, $message, $key);
1042
+		}
1043
+		return ParagonIE_Sodium_Crypto::auth_verify($mac, $message, $key);
1044
+	}
1045
+
1046
+	/**
1047
+	 * Authenticated asymmetric-key encryption. Both the sender and recipient
1048
+	 * may decrypt messages.
1049
+	 *
1050
+	 * Algorithm: X25519-XSalsa20-Poly1305.
1051
+	 *     X25519: Elliptic-Curve Diffie Hellman over Curve25519.
1052
+	 *     XSalsa20: Extended-nonce variant of salsa20.
1053
+	 *     Poyl1305: Polynomial MAC for one-time message authentication.
1054
+	 *
1055
+	 * @param string $plaintext The message to be encrypted
1056
+	 * @param string $nonce A Number to only be used Once; must be 24 bytes
1057
+	 * @param string $keypair Your secret key and your recipient's public key
1058
+	 * @return string           Ciphertext with 16-byte Poly1305 MAC
1059
+	 * @throws SodiumException
1060
+	 * @throws TypeError
1061
+	 * @psalm-suppress MixedArgument
1062
+	 */
1063
+	public static function crypto_box($plaintext, $nonce, $keypair)
1064
+	{
1065
+		/* Type checks: */
1066
+		ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
1067
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
1068
+		ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 3);
1069
+
1070
+		/* Input validation: */
1071
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_BOX_NONCEBYTES) {
1072
+			throw new SodiumException('Argument 2 must be CRYPTO_BOX_NONCEBYTES long.');
1073
+		}
1074
+		if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1075
+			throw new SodiumException('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1076
+		}
1077
+
1078
+		if (self::useNewSodiumAPI()) {
1079
+			return (string) sodium_crypto_box($plaintext, $nonce, $keypair);
1080
+		}
1081
+		if (self::use_fallback('crypto_box')) {
1082
+			return (string) call_user_func('\\Sodium\\crypto_box', $plaintext, $nonce, $keypair);
1083
+		}
1084
+		if (PHP_INT_SIZE === 4) {
1085
+			return ParagonIE_Sodium_Crypto32::box($plaintext, $nonce, $keypair);
1086
+		}
1087
+		return ParagonIE_Sodium_Crypto::box($plaintext, $nonce, $keypair);
1088
+	}
1089
+
1090
+	/**
1091
+	 * Anonymous public-key encryption. Only the recipient may decrypt messages.
1092
+	 *
1093
+	 * Algorithm: X25519-XSalsa20-Poly1305, as with crypto_box.
1094
+	 *     The sender's X25519 keypair is ephemeral.
1095
+	 *     Nonce is generated from the BLAKE2b hash of both public keys.
1096
+	 *
1097
+	 * This provides ciphertext integrity.
1098
+	 *
1099
+	 * @param string $plaintext Message to be sealed
1100
+	 * @param string $publicKey Your recipient's public key
1101
+	 * @return string           Sealed message that only your recipient can
1102
+	 *                          decrypt
1103
+	 * @throws SodiumException
1104
+	 * @throws TypeError
1105
+	 * @psalm-suppress MixedArgument
1106
+	 */
1107
+	public static function crypto_box_seal($plaintext, $publicKey)
1108
+	{
1109
+		/* Type checks: */
1110
+		ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
1111
+		ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
1112
+
1113
+		/* Input validation: */
1114
+		if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1115
+			throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1116
+		}
1117
+
1118
+		if (self::useNewSodiumAPI()) {
1119
+			return (string) sodium_crypto_box_seal($plaintext, $publicKey);
1120
+		}
1121
+		if (self::use_fallback('crypto_box_seal')) {
1122
+			return (string) call_user_func('\\Sodium\\crypto_box_seal', $plaintext, $publicKey);
1123
+		}
1124
+		if (PHP_INT_SIZE === 4) {
1125
+			return ParagonIE_Sodium_Crypto32::box_seal($plaintext, $publicKey);
1126
+		}
1127
+		return ParagonIE_Sodium_Crypto::box_seal($plaintext, $publicKey);
1128
+	}
1129
+
1130
+	/**
1131
+	 * Opens a message encrypted with crypto_box_seal(). Requires
1132
+	 * the recipient's keypair (sk || pk) to decrypt successfully.
1133
+	 *
1134
+	 * This validates ciphertext integrity.
1135
+	 *
1136
+	 * @param string $ciphertext Sealed message to be opened
1137
+	 * @param string $keypair    Your crypto_box keypair
1138
+	 * @return string            The original plaintext message
1139
+	 * @throws SodiumException
1140
+	 * @throws TypeError
1141
+	 * @psalm-suppress MixedArgument
1142
+	 * @psalm-suppress MixedInferredReturnType
1143
+	 * @psalm-suppress MixedReturnStatement
1144
+	 */
1145
+	public static function crypto_box_seal_open($ciphertext, $keypair)
1146
+	{
1147
+		/* Type checks: */
1148
+		ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
1149
+		ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 2);
1150
+
1151
+		/* Input validation: */
1152
+		if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1153
+			throw new SodiumException('Argument 2 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1154
+		}
1155
+
1156
+		if (self::useNewSodiumAPI()) {
1157
+			/**
1158
+			 * @psalm-suppress InvalidReturnStatement
1159
+			 * @psalm-suppress FalsableReturnStatement
1160
+			 */
1161
+			return sodium_crypto_box_seal_open($ciphertext, $keypair);
1162
+		}
1163
+		if (self::use_fallback('crypto_box_seal_open')) {
1164
+			return call_user_func('\\Sodium\\crypto_box_seal_open', $ciphertext, $keypair);
1165
+		}
1166
+		if (PHP_INT_SIZE === 4) {
1167
+			return ParagonIE_Sodium_Crypto32::box_seal_open($ciphertext, $keypair);
1168
+		}
1169
+		return ParagonIE_Sodium_Crypto::box_seal_open($ciphertext, $keypair);
1170
+	}
1171
+
1172
+	/**
1173
+	 * Generate a new random X25519 keypair.
1174
+	 *
1175
+	 * @return string A 64-byte string; the first 32 are your secret key, while
1176
+	 *                the last 32 are your public key. crypto_box_secretkey()
1177
+	 *                and crypto_box_publickey() exist to separate them so you
1178
+	 *                don't accidentally get them mixed up!
1179
+	 * @throws SodiumException
1180
+	 * @throws TypeError
1181
+	 * @psalm-suppress MixedArgument
1182
+	 */
1183
+	public static function crypto_box_keypair()
1184
+	{
1185
+		if (self::useNewSodiumAPI()) {
1186
+			return (string) sodium_crypto_box_keypair();
1187
+		}
1188
+		if (self::use_fallback('crypto_box_keypair')) {
1189
+			return (string) call_user_func('\\Sodium\\crypto_box_keypair');
1190
+		}
1191
+		if (PHP_INT_SIZE === 4) {
1192
+			return ParagonIE_Sodium_Crypto32::box_keypair();
1193
+		}
1194
+		return ParagonIE_Sodium_Crypto::box_keypair();
1195
+	}
1196
+
1197
+	/**
1198
+	 * Combine two keys into a keypair for use in library methods that expect
1199
+	 * a keypair. This doesn't necessarily have to be the same person's keys.
1200
+	 *
1201
+	 * @param string $secretKey Secret key
1202
+	 * @param string $publicKey Public key
1203
+	 * @return string    Keypair
1204
+	 * @throws SodiumException
1205
+	 * @throws TypeError
1206
+	 * @psalm-suppress MixedArgument
1207
+	 */
1208
+	public static function crypto_box_keypair_from_secretkey_and_publickey($secretKey, $publicKey)
1209
+	{
1210
+		/* Type checks: */
1211
+		ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
1212
+		ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
1213
+
1214
+		/* Input validation: */
1215
+		if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
1216
+			throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
1217
+		}
1218
+		if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1219
+			throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1220
+		}
1221
+
1222
+		if (self::useNewSodiumAPI()) {
1223
+			return (string) sodium_crypto_box_keypair_from_secretkey_and_publickey($secretKey, $publicKey);
1224
+		}
1225
+		if (self::use_fallback('crypto_box_keypair_from_secretkey_and_publickey')) {
1226
+			return (string) call_user_func('\\Sodium\\crypto_box_keypair_from_secretkey_and_publickey', $secretKey, $publicKey);
1227
+		}
1228
+		if (PHP_INT_SIZE === 4) {
1229
+			return ParagonIE_Sodium_Crypto32::box_keypair_from_secretkey_and_publickey($secretKey, $publicKey);
1230
+		}
1231
+		return ParagonIE_Sodium_Crypto::box_keypair_from_secretkey_and_publickey($secretKey, $publicKey);
1232
+	}
1233
+
1234
+	/**
1235
+	 * Decrypt a message previously encrypted with crypto_box().
1236
+	 *
1237
+	 * @param string $ciphertext Encrypted message
1238
+	 * @param string $nonce      Number to only be used Once; must be 24 bytes
1239
+	 * @param string $keypair    Your secret key and the sender's public key
1240
+	 * @return string            The original plaintext message
1241
+	 * @throws SodiumException
1242
+	 * @throws TypeError
1243
+	 * @psalm-suppress MixedArgument
1244
+	 * @psalm-suppress MixedInferredReturnType
1245
+	 * @psalm-suppress MixedReturnStatement
1246
+	 */
1247
+	public static function crypto_box_open($ciphertext, $nonce, $keypair)
1248
+	{
1249
+		/* Type checks: */
1250
+		ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
1251
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
1252
+		ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 3);
1253
+
1254
+		/* Input validation: */
1255
+		if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_BOX_MACBYTES) {
1256
+			throw new SodiumException('Argument 1 must be at least CRYPTO_BOX_MACBYTES long.');
1257
+		}
1258
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_BOX_NONCEBYTES) {
1259
+			throw new SodiumException('Argument 2 must be CRYPTO_BOX_NONCEBYTES long.');
1260
+		}
1261
+		if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1262
+			throw new SodiumException('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1263
+		}
1264
+
1265
+		if (self::useNewSodiumAPI()) {
1266
+			/**
1267
+			 * @psalm-suppress InvalidReturnStatement
1268
+			 * @psalm-suppress FalsableReturnStatement
1269
+			 */
1270
+			return sodium_crypto_box_open($ciphertext, $nonce, $keypair);
1271
+		}
1272
+		if (self::use_fallback('crypto_box_open')) {
1273
+			return call_user_func('\\Sodium\\crypto_box_open', $ciphertext, $nonce, $keypair);
1274
+		}
1275
+		if (PHP_INT_SIZE === 4) {
1276
+			return ParagonIE_Sodium_Crypto32::box_open($ciphertext, $nonce, $keypair);
1277
+		}
1278
+		return ParagonIE_Sodium_Crypto::box_open($ciphertext, $nonce, $keypair);
1279
+	}
1280
+
1281
+	/**
1282
+	 * Extract the public key from a crypto_box keypair.
1283
+	 *
1284
+	 * @param string $keypair Keypair containing secret and public key
1285
+	 * @return string         Your crypto_box public key
1286
+	 * @throws SodiumException
1287
+	 * @throws TypeError
1288
+	 * @psalm-suppress MixedArgument
1289
+	 */
1290
+	public static function crypto_box_publickey($keypair)
1291
+	{
1292
+		/* Type checks: */
1293
+		ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1294
+
1295
+		/* Input validation: */
1296
+		if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1297
+			throw new SodiumException('Argument 1 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1298
+		}
1299
+
1300
+		if (self::useNewSodiumAPI()) {
1301
+			return (string) sodium_crypto_box_publickey($keypair);
1302
+		}
1303
+		if (self::use_fallback('crypto_box_publickey')) {
1304
+			return (string) call_user_func('\\Sodium\\crypto_box_publickey', $keypair);
1305
+		}
1306
+		if (PHP_INT_SIZE === 4) {
1307
+			return ParagonIE_Sodium_Crypto32::box_publickey($keypair);
1308
+		}
1309
+		return ParagonIE_Sodium_Crypto::box_publickey($keypair);
1310
+	}
1311
+
1312
+	/**
1313
+	 * Calculate the X25519 public key from a given X25519 secret key.
1314
+	 *
1315
+	 * @param string $secretKey Any X25519 secret key
1316
+	 * @return string           The corresponding X25519 public key
1317
+	 * @throws SodiumException
1318
+	 * @throws TypeError
1319
+	 * @psalm-suppress MixedArgument
1320
+	 */
1321
+	public static function crypto_box_publickey_from_secretkey($secretKey)
1322
+	{
1323
+		/* Type checks: */
1324
+		ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
1325
+
1326
+		/* Input validation: */
1327
+		if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
1328
+			throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
1329
+		}
1330
+
1331
+		if (self::useNewSodiumAPI()) {
1332
+			return (string) sodium_crypto_box_publickey_from_secretkey($secretKey);
1333
+		}
1334
+		if (self::use_fallback('crypto_box_publickey_from_secretkey')) {
1335
+			return (string) call_user_func('\\Sodium\\crypto_box_publickey_from_secretkey', $secretKey);
1336
+		}
1337
+		if (PHP_INT_SIZE === 4) {
1338
+			return ParagonIE_Sodium_Crypto32::box_publickey_from_secretkey($secretKey);
1339
+		}
1340
+		return ParagonIE_Sodium_Crypto::box_publickey_from_secretkey($secretKey);
1341
+	}
1342
+
1343
+	/**
1344
+	 * Extract the secret key from a crypto_box keypair.
1345
+	 *
1346
+	 * @param string $keypair
1347
+	 * @return string         Your crypto_box secret key
1348
+	 * @throws SodiumException
1349
+	 * @throws TypeError
1350
+	 * @psalm-suppress MixedArgument
1351
+	 */
1352
+	public static function crypto_box_secretkey($keypair)
1353
+	{
1354
+		/* Type checks: */
1355
+		ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1356
+
1357
+		/* Input validation: */
1358
+		if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1359
+			throw new SodiumException('Argument 1 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1360
+		}
1361
+
1362
+		if (self::useNewSodiumAPI()) {
1363
+			return (string) sodium_crypto_box_secretkey($keypair);
1364
+		}
1365
+		if (self::use_fallback('crypto_box_secretkey')) {
1366
+			return (string) call_user_func('\\Sodium\\crypto_box_secretkey', $keypair);
1367
+		}
1368
+		if (PHP_INT_SIZE === 4) {
1369
+			return ParagonIE_Sodium_Crypto32::box_secretkey($keypair);
1370
+		}
1371
+		return ParagonIE_Sodium_Crypto::box_secretkey($keypair);
1372
+	}
1373
+
1374
+	/**
1375
+	 * Generate an X25519 keypair from a seed.
1376
+	 *
1377
+	 * @param string $seed
1378
+	 * @return string
1379
+	 * @throws SodiumException
1380
+	 * @throws TypeError
1381
+	 * @psalm-suppress MixedArgument
1382
+	 * @psalm-suppress UndefinedFunction
1383
+	 */
1384
+	public static function crypto_box_seed_keypair($seed)
1385
+	{
1386
+		/* Type checks: */
1387
+		ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1);
1388
+
1389
+		if (self::useNewSodiumAPI()) {
1390
+			return (string) sodium_crypto_box_seed_keypair($seed);
1391
+		}
1392
+		if (self::use_fallback('crypto_box_seed_keypair')) {
1393
+			return (string) call_user_func('\\Sodium\\crypto_box_seed_keypair', $seed);
1394
+		}
1395
+		if (PHP_INT_SIZE === 4) {
1396
+			return ParagonIE_Sodium_Crypto32::box_seed_keypair($seed);
1397
+		}
1398
+		return ParagonIE_Sodium_Crypto::box_seed_keypair($seed);
1399
+	}
1400
+
1401
+	/**
1402
+	 * Calculates a BLAKE2b hash, with an optional key.
1403
+	 *
1404
+	 * @param string      $message The message to be hashed
1405
+	 * @param string|null $key     If specified, must be a string between 16
1406
+	 *                             and 64 bytes long
1407
+	 * @param int         $length  Output length in bytes; must be between 16
1408
+	 *                             and 64 (default = 32)
1409
+	 * @return string              Raw binary
1410
+	 * @throws SodiumException
1411
+	 * @throws TypeError
1412
+	 * @psalm-suppress MixedArgument
1413
+	 */
1414
+	public static function crypto_generichash($message, $key = '', $length = self::CRYPTO_GENERICHASH_BYTES)
1415
+	{
1416
+		/* Type checks: */
1417
+		ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
1418
+		if (is_null($key)) {
1419
+			$key = '';
1420
+		}
1421
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2);
1422
+		ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 3);
1423
+
1424
+		/* Input validation: */
1425
+		if (!empty($key)) {
1426
+			if (ParagonIE_Sodium_Core_Util::strlen($key) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
1427
+				throw new SodiumException('Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.');
1428
+			}
1429
+			if (ParagonIE_Sodium_Core_Util::strlen($key) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
1430
+				throw new SodiumException('Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.');
1431
+			}
1432
+		}
1433
+
1434
+		if (self::useNewSodiumAPI()) {
1435
+			return (string) sodium_crypto_generichash($message, $key, $length);
1436
+		}
1437
+		if (self::use_fallback('crypto_generichash')) {
1438
+			return (string) call_user_func('\\Sodium\\crypto_generichash', $message, $key, $length);
1439
+		}
1440
+		if (PHP_INT_SIZE === 4) {
1441
+			return ParagonIE_Sodium_Crypto32::generichash($message, $key, $length);
1442
+		}
1443
+		return ParagonIE_Sodium_Crypto::generichash($message, $key, $length);
1444
+	}
1445
+
1446
+	/**
1447
+	 * Get the final BLAKE2b hash output for a given context.
1448
+	 *
1449
+	 * @param string $ctx BLAKE2 hashing context. Generated by crypto_generichash_init().
1450
+	 * @param int $length Hash output size.
1451
+	 * @return string     Final BLAKE2b hash.
1452
+	 * @throws SodiumException
1453
+	 * @throws TypeError
1454
+	 * @psalm-suppress MixedArgument
1455
+	 * @psalm-suppress ReferenceConstraintViolation
1456
+	 * @psalm-suppress ConflictingReferenceConstraint
1457
+	 */
1458
+	public static function crypto_generichash_final(&$ctx, $length = self::CRYPTO_GENERICHASH_BYTES)
1459
+	{
1460
+		/* Type checks: */
1461
+		ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1);
1462
+		ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2);
1463
+
1464
+		if (self::useNewSodiumAPI()) {
1465
+			return sodium_crypto_generichash_final($ctx, $length);
1466
+		}
1467
+		if (self::use_fallback('crypto_generichash_final')) {
1468
+			$func = '\\Sodium\\crypto_generichash_final';
1469
+			return (string) $func($ctx, $length);
1470
+		}
1471
+		if ($length < 1) {
1472
+			try {
1473
+				self::memzero($ctx);
1474
+			} catch (SodiumException $ex) {
1475
+				unset($ctx);
1476
+			}
1477
+			return '';
1478
+		}
1479
+		if (PHP_INT_SIZE === 4) {
1480
+			$result = ParagonIE_Sodium_Crypto32::generichash_final($ctx, $length);
1481
+		} else {
1482
+			$result = ParagonIE_Sodium_Crypto::generichash_final($ctx, $length);
1483
+		}
1484
+		try {
1485
+			self::memzero($ctx);
1486
+		} catch (SodiumException $ex) {
1487
+			unset($ctx);
1488
+		}
1489
+		return $result;
1490
+	}
1491
+
1492
+	/**
1493
+	 * Initialize a BLAKE2b hashing context, for use in a streaming interface.
1494
+	 *
1495
+	 * @param string|null $key If specified must be a string between 16 and 64 bytes
1496
+	 * @param int $length      The size of the desired hash output
1497
+	 * @return string          A BLAKE2 hashing context, encoded as a string
1498
+	 *                         (To be 100% compatible with ext/libsodium)
1499
+	 * @throws SodiumException
1500
+	 * @throws TypeError
1501
+	 * @psalm-suppress MixedArgument
1502
+	 */
1503
+	public static function crypto_generichash_init($key = '', $length = self::CRYPTO_GENERICHASH_BYTES)
1504
+	{
1505
+		/* Type checks: */
1506
+		if (is_null($key)) {
1507
+			$key = '';
1508
+		}
1509
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 1);
1510
+		ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2);
1511
+
1512
+		/* Input validation: */
1513
+		if (!empty($key)) {
1514
+			if (ParagonIE_Sodium_Core_Util::strlen($key) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
1515
+				throw new SodiumException('Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.');
1516
+			}
1517
+			if (ParagonIE_Sodium_Core_Util::strlen($key) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
1518
+				throw new SodiumException('Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.');
1519
+			}
1520
+		}
1521
+
1522
+		if (self::useNewSodiumAPI()) {
1523
+			return sodium_crypto_generichash_init($key, $length);
1524
+		}
1525
+		if (self::use_fallback('crypto_generichash_init')) {
1526
+			return (string) call_user_func('\\Sodium\\crypto_generichash_init', $key, $length);
1527
+		}
1528
+		if (PHP_INT_SIZE === 4) {
1529
+			return ParagonIE_Sodium_Crypto32::generichash_init($key, $length);
1530
+		}
1531
+		return ParagonIE_Sodium_Crypto::generichash_init($key, $length);
1532
+	}
1533
+
1534
+	/**
1535
+	 * Initialize a BLAKE2b hashing context, for use in a streaming interface.
1536
+	 *
1537
+	 * @param string|null $key If specified must be a string between 16 and 64 bytes
1538
+	 * @param int $length      The size of the desired hash output
1539
+	 * @param string $salt     Salt (up to 16 bytes)
1540
+	 * @param string $personal Personalization string (up to 16 bytes)
1541
+	 * @return string          A BLAKE2 hashing context, encoded as a string
1542
+	 *                         (To be 100% compatible with ext/libsodium)
1543
+	 * @throws SodiumException
1544
+	 * @throws TypeError
1545
+	 * @psalm-suppress MixedArgument
1546
+	 */
1547
+	public static function crypto_generichash_init_salt_personal(
1548
+		$key = '',
1549
+		$length = self::CRYPTO_GENERICHASH_BYTES,
1550
+		$salt = '',
1551
+		$personal = ''
1552
+	) {
1553
+		/* Type checks: */
1554
+		if (is_null($key)) {
1555
+			$key = '';
1556
+		}
1557
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 1);
1558
+		ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2);
1559
+		ParagonIE_Sodium_Core_Util::declareScalarType($salt, 'string', 3);
1560
+		ParagonIE_Sodium_Core_Util::declareScalarType($personal, 'string', 4);
1561
+		$salt = str_pad($salt, 16, "\0", STR_PAD_RIGHT);
1562
+		$personal = str_pad($personal, 16, "\0", STR_PAD_RIGHT);
1563
+
1564
+		/* Input validation: */
1565
+		if (!empty($key)) {
1566
+			/*
1567 1567
             if (ParagonIE_Sodium_Core_Util::strlen($key) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
1568 1568
                 throw new SodiumException('Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.');
1569 1569
             }
1570 1570
             */
1571
-            if (ParagonIE_Sodium_Core_Util::strlen($key) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
1572
-                throw new SodiumException('Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.');
1573
-            }
1574
-        }
1575
-        if (PHP_INT_SIZE === 4) {
1576
-            return ParagonIE_Sodium_Crypto32::generichash_init_salt_personal($key, $length, $salt, $personal);
1577
-        }
1578
-        return ParagonIE_Sodium_Crypto::generichash_init_salt_personal($key, $length, $salt, $personal);
1579
-    }
1580
-
1581
-    /**
1582
-     * Update a BLAKE2b hashing context with additional data.
1583
-     *
1584
-     * @param string $ctx    BLAKE2 hashing context. Generated by crypto_generichash_init().
1585
-     *                       $ctx is passed by reference and gets updated in-place.
1586
-     * @param-out string $ctx
1587
-     * @param string $message The message to append to the existing hash state.
1588
-     * @return void
1589
-     * @throws SodiumException
1590
-     * @throws TypeError
1591
-     * @psalm-suppress MixedArgument
1592
-     * @psalm-suppress ReferenceConstraintViolation
1593
-     */
1594
-    public static function crypto_generichash_update(&$ctx, $message)
1595
-    {
1596
-        /* Type checks: */
1597
-        ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1);
1598
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2);
1599
-
1600
-        if (self::useNewSodiumAPI()) {
1601
-            sodium_crypto_generichash_update($ctx, $message);
1602
-            return;
1603
-        }
1604
-        if (self::use_fallback('crypto_generichash_update')) {
1605
-            $func = '\\Sodium\\crypto_generichash_update';
1606
-            $func($ctx, $message);
1607
-            return;
1608
-        }
1609
-        if (PHP_INT_SIZE === 4) {
1610
-            $ctx = ParagonIE_Sodium_Crypto32::generichash_update($ctx, $message);
1611
-        } else {
1612
-            $ctx = ParagonIE_Sodium_Crypto::generichash_update($ctx, $message);
1613
-        }
1614
-    }
1615
-
1616
-    /**
1617
-     * @return string
1618
-     * @throws Exception
1619
-     * @throws Error
1620
-     */
1621
-    public static function crypto_generichash_keygen()
1622
-    {
1623
-        return random_bytes(self::CRYPTO_GENERICHASH_KEYBYTES);
1624
-    }
1625
-
1626
-    /**
1627
-     * @param int $subkey_len
1628
-     * @param int $subkey_id
1629
-     * @param string $context
1630
-     * @param string $key
1631
-     * @return string
1632
-     * @throws SodiumException
1633
-     */
1634
-    public static function crypto_kdf_derive_from_key(
1635
-        $subkey_len,
1636
-        $subkey_id,
1637
-        $context,
1638
-        $key
1639
-    ) {
1640
-        ParagonIE_Sodium_Core_Util::declareScalarType($subkey_len, 'int', 1);
1641
-        ParagonIE_Sodium_Core_Util::declareScalarType($subkey_id, 'int', 2);
1642
-        ParagonIE_Sodium_Core_Util::declareScalarType($context, 'string', 3);
1643
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
1644
-        $subkey_id = (int) $subkey_id;
1645
-        $subkey_len = (int) $subkey_len;
1646
-        $context = (string) $context;
1647
-        $key = (string) $key;
1648
-
1649
-        if ($subkey_len < self::CRYPTO_KDF_BYTES_MIN) {
1650
-            throw new SodiumException('subkey cannot be smaller than SODIUM_CRYPTO_KDF_BYTES_MIN');
1651
-        }
1652
-        if ($subkey_len > self::CRYPTO_KDF_BYTES_MAX) {
1653
-            throw new SodiumException('subkey cannot be larger than SODIUM_CRYPTO_KDF_BYTES_MAX');
1654
-        }
1655
-        if ($subkey_id < 0) {
1656
-            throw new SodiumException('subkey_id cannot be negative');
1657
-        }
1658
-        if (ParagonIE_Sodium_Core_Util::strlen($context) !== self::CRYPTO_KDF_CONTEXTBYTES) {
1659
-            throw new SodiumException('context should be SODIUM_CRYPTO_KDF_CONTEXTBYTES bytes');
1660
-        }
1661
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_KDF_KEYBYTES) {
1662
-            throw new SodiumException('key should be SODIUM_CRYPTO_KDF_KEYBYTES bytes');
1663
-        }
1664
-
1665
-        $salt = ParagonIE_Sodium_Core_Util::store64_le($subkey_id);
1666
-        $state = self::crypto_generichash_init_salt_personal(
1667
-            $key,
1668
-            $subkey_len,
1669
-            $salt,
1670
-            $context
1671
-        );
1672
-        return self::crypto_generichash_final($state, $subkey_len);
1673
-    }
1674
-
1675
-    /**
1676
-     * @return string
1677
-     * @throws Exception
1678
-     * @throws Error
1679
-     */
1680
-    public static function crypto_kdf_keygen()
1681
-    {
1682
-        return random_bytes(self::CRYPTO_KDF_KEYBYTES);
1683
-    }
1684
-
1685
-    /**
1686
-     * Perform a key exchange, between a designated client and a server.
1687
-     *
1688
-     * Typically, you would designate one machine to be the client and the
1689
-     * other to be the server. The first two keys are what you'd expect for
1690
-     * scalarmult() below, but the latter two public keys don't swap places.
1691
-     *
1692
-     * | ALICE                          | BOB                                 |
1693
-     * | Client                         | Server                              |
1694
-     * |--------------------------------|-------------------------------------|
1695
-     * | shared = crypto_kx(            | shared = crypto_kx(                 |
1696
-     * |     alice_sk,                  |     bob_sk,                         | <- contextual
1697
-     * |     bob_pk,                    |     alice_pk,                       | <- contextual
1698
-     * |     alice_pk,                  |     alice_pk,                       | <----- static
1699
-     * |     bob_pk                     |     bob_pk                          | <----- static
1700
-     * | )                              | )                                   |
1701
-     *
1702
-     * They are used along with the scalarmult product to generate a 256-bit
1703
-     * BLAKE2b hash unique to the client and server keys.
1704
-     *
1705
-     * @param string $my_secret
1706
-     * @param string $their_public
1707
-     * @param string $client_public
1708
-     * @param string $server_public
1709
-     * @param bool $dontFallback
1710
-     * @return string
1711
-     * @throws SodiumException
1712
-     * @throws TypeError
1713
-     * @psalm-suppress MixedArgument
1714
-     */
1715
-    public static function crypto_kx($my_secret, $their_public, $client_public, $server_public, $dontFallback = false)
1716
-    {
1717
-        /* Type checks: */
1718
-        ParagonIE_Sodium_Core_Util::declareScalarType($my_secret, 'string', 1);
1719
-        ParagonIE_Sodium_Core_Util::declareScalarType($their_public, 'string', 2);
1720
-        ParagonIE_Sodium_Core_Util::declareScalarType($client_public, 'string', 3);
1721
-        ParagonIE_Sodium_Core_Util::declareScalarType($server_public, 'string', 4);
1722
-
1723
-        /* Input validation: */
1724
-        if (ParagonIE_Sodium_Core_Util::strlen($my_secret) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
1725
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
1726
-        }
1727
-        if (ParagonIE_Sodium_Core_Util::strlen($their_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1728
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1729
-        }
1730
-        if (ParagonIE_Sodium_Core_Util::strlen($client_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1731
-            throw new SodiumException('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1732
-        }
1733
-        if (ParagonIE_Sodium_Core_Util::strlen($server_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1734
-            throw new SodiumException('Argument 4 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1735
-        }
1736
-
1737
-        if (self::useNewSodiumAPI() && !$dontFallback) {
1738
-            if (is_callable('sodium_crypto_kx')) {
1739
-                return (string) sodium_crypto_kx(
1740
-                    $my_secret,
1741
-                    $their_public,
1742
-                    $client_public,
1743
-                    $server_public
1744
-                );
1745
-            }
1746
-        }
1747
-        if (self::use_fallback('crypto_kx')) {
1748
-            return (string) call_user_func(
1749
-                '\\Sodium\\crypto_kx',
1750
-                $my_secret,
1751
-                $their_public,
1752
-                $client_public,
1753
-                $server_public
1754
-            );
1755
-        }
1756
-        if (PHP_INT_SIZE === 4) {
1757
-            return ParagonIE_Sodium_Crypto32::keyExchange(
1758
-                $my_secret,
1759
-                $their_public,
1760
-                $client_public,
1761
-                $server_public
1762
-            );
1763
-        }
1764
-        return ParagonIE_Sodium_Crypto::keyExchange(
1765
-            $my_secret,
1766
-            $their_public,
1767
-            $client_public,
1768
-            $server_public
1769
-        );
1770
-    }
1771
-
1772
-    /**
1773
-     * @param string $seed
1774
-     * @return string
1775
-     * @throws SodiumException
1776
-     */
1777
-    public static function crypto_kx_seed_keypair($seed)
1778
-    {
1779
-        ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1);
1780
-
1781
-        $seed = (string) $seed;
1782
-
1783
-        if (ParagonIE_Sodium_Core_Util::strlen($seed) !== self::CRYPTO_KX_SEEDBYTES) {
1784
-            throw new SodiumException('seed must be SODIUM_CRYPTO_KX_SEEDBYTES bytes');
1785
-        }
1786
-
1787
-        $sk = self::crypto_generichash($seed, '', self::CRYPTO_KX_SECRETKEYBYTES);
1788
-        $pk = self::crypto_scalarmult_base($sk);
1789
-        return $sk . $pk;
1790
-    }
1791
-
1792
-    /**
1793
-     * @return string
1794
-     * @throws Exception
1795
-     */
1796
-    public static function crypto_kx_keypair()
1797
-    {
1798
-        $sk = self::randombytes_buf(self::CRYPTO_KX_SECRETKEYBYTES);
1799
-        $pk = self::crypto_scalarmult_base($sk);
1800
-        return $sk . $pk;
1801
-    }
1802
-
1803
-    /**
1804
-     * @param string $keypair
1805
-     * @param string $serverPublicKey
1806
-     * @return array{0: string, 1: string}
1807
-     * @throws SodiumException
1808
-     */
1809
-    public static function crypto_kx_client_session_keys($keypair, $serverPublicKey)
1810
-    {
1811
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1812
-        ParagonIE_Sodium_Core_Util::declareScalarType($serverPublicKey, 'string', 2);
1813
-
1814
-        $keypair = (string) $keypair;
1815
-        $serverPublicKey = (string) $serverPublicKey;
1816
-
1817
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_KX_KEYPAIRBYTES) {
1818
-            throw new SodiumException('keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes');
1819
-        }
1820
-        if (ParagonIE_Sodium_Core_Util::strlen($serverPublicKey) !== self::CRYPTO_KX_PUBLICKEYBYTES) {
1821
-            throw new SodiumException('public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes');
1822
-        }
1823
-
1824
-        $sk = self::crypto_kx_secretkey($keypair);
1825
-        $pk = self::crypto_kx_publickey($keypair);
1826
-        $h = self::crypto_generichash_init(null, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
1827
-        self::crypto_generichash_update($h, self::crypto_scalarmult($sk, $serverPublicKey));
1828
-        self::crypto_generichash_update($h, $pk);
1829
-        self::crypto_generichash_update($h, $serverPublicKey);
1830
-        $sessionKeys = self::crypto_generichash_final($h, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
1831
-        return array(
1832
-            ParagonIE_Sodium_Core_Util::substr(
1833
-                $sessionKeys,
1834
-                0,
1835
-                self::CRYPTO_KX_SESSIONKEYBYTES
1836
-            ),
1837
-            ParagonIE_Sodium_Core_Util::substr(
1838
-                $sessionKeys,
1839
-                self::CRYPTO_KX_SESSIONKEYBYTES,
1840
-                self::CRYPTO_KX_SESSIONKEYBYTES
1841
-            )
1842
-        );
1843
-    }
1844
-
1845
-    /**
1846
-     * @param string $keypair
1847
-     * @param string $clientPublicKey
1848
-     * @return array{0: string, 1: string}
1849
-     * @throws SodiumException
1850
-     */
1851
-    public static function crypto_kx_server_session_keys($keypair, $clientPublicKey)
1852
-    {
1853
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1854
-        ParagonIE_Sodium_Core_Util::declareScalarType($clientPublicKey, 'string', 2);
1855
-
1856
-        $keypair = (string) $keypair;
1857
-        $clientPublicKey = (string) $clientPublicKey;
1858
-
1859
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_KX_KEYPAIRBYTES) {
1860
-            throw new SodiumException('keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes');
1861
-        }
1862
-        if (ParagonIE_Sodium_Core_Util::strlen($clientPublicKey) !== self::CRYPTO_KX_PUBLICKEYBYTES) {
1863
-            throw new SodiumException('public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes');
1864
-        }
1865
-
1866
-        $sk = self::crypto_kx_secretkey($keypair);
1867
-        $pk = self::crypto_kx_publickey($keypair);
1868
-        $h = self::crypto_generichash_init(null, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
1869
-        self::crypto_generichash_update($h, self::crypto_scalarmult($sk, $clientPublicKey));
1870
-        self::crypto_generichash_update($h, $clientPublicKey);
1871
-        self::crypto_generichash_update($h, $pk);
1872
-        $sessionKeys = self::crypto_generichash_final($h, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
1873
-        return array(
1874
-            ParagonIE_Sodium_Core_Util::substr(
1875
-                $sessionKeys,
1876
-                self::CRYPTO_KX_SESSIONKEYBYTES,
1877
-                self::CRYPTO_KX_SESSIONKEYBYTES
1878
-            ),
1879
-            ParagonIE_Sodium_Core_Util::substr(
1880
-                $sessionKeys,
1881
-                0,
1882
-                self::CRYPTO_KX_SESSIONKEYBYTES
1883
-            )
1884
-        );
1885
-    }
1886
-
1887
-    /**
1888
-     * @param string $kp
1889
-     * @return string
1890
-     * @throws SodiumException
1891
-     */
1892
-    public static function crypto_kx_secretkey($kp)
1893
-    {
1894
-        return ParagonIE_Sodium_Core_Util::substr(
1895
-            $kp,
1896
-            0,
1897
-            self::CRYPTO_KX_SECRETKEYBYTES
1898
-        );
1899
-    }
1900
-
1901
-    /**
1902
-     * @param string $kp
1903
-     * @return string
1904
-     * @throws SodiumException
1905
-     */
1906
-    public static function crypto_kx_publickey($kp)
1907
-    {
1908
-        return ParagonIE_Sodium_Core_Util::substr(
1909
-            $kp,
1910
-            self::CRYPTO_KX_SECRETKEYBYTES,
1911
-            self::CRYPTO_KX_PUBLICKEYBYTES
1912
-        );
1913
-    }
1914
-
1915
-    /**
1916
-     * @param int $outlen
1917
-     * @param string $passwd
1918
-     * @param string $salt
1919
-     * @param int $opslimit
1920
-     * @param int $memlimit
1921
-     * @param int|null $alg
1922
-     * @return string
1923
-     * @throws SodiumException
1924
-     * @throws TypeError
1925
-     * @psalm-suppress MixedArgument
1926
-     */
1927
-    public static function crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit, $alg = null)
1928
-    {
1929
-        ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1);
1930
-        ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2);
1931
-        ParagonIE_Sodium_Core_Util::declareScalarType($salt,  'string', 3);
1932
-        ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 4);
1933
-        ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 5);
1934
-
1935
-        if (self::useNewSodiumAPI()) {
1936
-            if (!is_null($alg)) {
1937
-                ParagonIE_Sodium_Core_Util::declareScalarType($alg, 'int', 6);
1938
-                return sodium_crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit, $alg);
1939
-            }
1940
-            return sodium_crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit);
1941
-        }
1942
-        if (self::use_fallback('crypto_pwhash')) {
1943
-            return (string) call_user_func('\\Sodium\\crypto_pwhash', $outlen, $passwd, $salt, $opslimit, $memlimit);
1944
-        }
1945
-        // This is the best we can do.
1946
-        throw new SodiumException(
1947
-            'This is not implemented, as it is not possible to implement Argon2i with acceptable performance in pure-PHP'
1948
-        );
1949
-    }
1950
-
1951
-    /**
1952
-     * !Exclusive to sodium_compat!
1953
-     *
1954
-     * This returns TRUE if the native crypto_pwhash API is available by libsodium.
1955
-     * This returns FALSE if only sodium_compat is available.
1956
-     *
1957
-     * @return bool
1958
-     */
1959
-    public static function crypto_pwhash_is_available()
1960
-    {
1961
-        if (self::useNewSodiumAPI()) {
1962
-            return true;
1963
-        }
1964
-        if (self::use_fallback('crypto_pwhash')) {
1965
-            return true;
1966
-        }
1967
-        return false;
1968
-    }
1969
-
1970
-    /**
1971
-     * @param string $passwd
1972
-     * @param int $opslimit
1973
-     * @param int $memlimit
1974
-     * @return string
1975
-     * @throws SodiumException
1976
-     * @throws TypeError
1977
-     * @psalm-suppress MixedArgument
1978
-     */
1979
-    public static function crypto_pwhash_str($passwd, $opslimit, $memlimit)
1980
-    {
1981
-        ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
1982
-        ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2);
1983
-        ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);
1984
-
1985
-        if (self::useNewSodiumAPI()) {
1986
-            return sodium_crypto_pwhash_str($passwd, $opslimit, $memlimit);
1987
-        }
1988
-        if (self::use_fallback('crypto_pwhash_str')) {
1989
-            return (string) call_user_func('\\Sodium\\crypto_pwhash_str', $passwd, $opslimit, $memlimit);
1990
-        }
1991
-        // This is the best we can do.
1992
-        throw new SodiumException(
1993
-            'This is not implemented, as it is not possible to implement Argon2i with acceptable performance in pure-PHP'
1994
-        );
1995
-    }
1996
-
1997
-    /**
1998
-     * Do we need to rehash this password?
1999
-     *
2000
-     * @param string $hash
2001
-     * @param int $opslimit
2002
-     * @param int $memlimit
2003
-     * @return bool
2004
-     * @throws SodiumException
2005
-     */
2006
-    public static function crypto_pwhash_str_needs_rehash($hash, $opslimit, $memlimit)
2007
-    {
2008
-        ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 1);
2009
-        ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2);
2010
-        ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);
2011
-
2012
-        // Just grab the first 4 pieces.
2013
-        $pieces = explode('$', (string) $hash);
2014
-        $prefix = implode('$', array_slice($pieces, 0, 4));
2015
-
2016
-        // Rebuild the expected header.
2017
-        /** @var int $ops */
2018
-        $ops = (int) $opslimit;
2019
-        /** @var int $mem */
2020
-        $mem = (int) $memlimit >> 10;
2021
-        $encoded = self::CRYPTO_PWHASH_STRPREFIX . 'v=19$m=' . $mem . ',t=' . $ops . ',p=1';
2022
-
2023
-        // Do they match? If so, we don't need to rehash, so return false.
2024
-        return !ParagonIE_Sodium_Core_Util::hashEquals($encoded, $prefix);
2025
-    }
2026
-
2027
-    /**
2028
-     * @param string $passwd
2029
-     * @param string $hash
2030
-     * @return bool
2031
-     * @throws SodiumException
2032
-     * @throws TypeError
2033
-     * @psalm-suppress MixedArgument
2034
-     */
2035
-    public static function crypto_pwhash_str_verify($passwd, $hash)
2036
-    {
2037
-        ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
2038
-        ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2);
2039
-
2040
-        if (self::useNewSodiumAPI()) {
2041
-            return (bool) sodium_crypto_pwhash_str_verify($passwd, $hash);
2042
-        }
2043
-        if (self::use_fallback('crypto_pwhash_str_verify')) {
2044
-            return (bool) call_user_func('\\Sodium\\crypto_pwhash_str_verify', $passwd, $hash);
2045
-        }
2046
-        // This is the best we can do.
2047
-        throw new SodiumException(
2048
-            'This is not implemented, as it is not possible to implement Argon2i with acceptable performance in pure-PHP'
2049
-        );
2050
-    }
2051
-
2052
-    /**
2053
-     * @param int $outlen
2054
-     * @param string $passwd
2055
-     * @param string $salt
2056
-     * @param int $opslimit
2057
-     * @param int $memlimit
2058
-     * @return string
2059
-     * @throws SodiumException
2060
-     * @throws TypeError
2061
-     */
2062
-    public static function crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit)
2063
-    {
2064
-        ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1);
2065
-        ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2);
2066
-        ParagonIE_Sodium_Core_Util::declareScalarType($salt,  'string', 3);
2067
-        ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 4);
2068
-        ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 5);
2069
-
2070
-        if (self::useNewSodiumAPI()) {
2071
-            return (string) sodium_crypto_pwhash_scryptsalsa208sha256(
2072
-                (int) $outlen,
2073
-                (string) $passwd,
2074
-                (string) $salt,
2075
-                (int) $opslimit,
2076
-                (int) $memlimit
2077
-            );
2078
-        }
2079
-        if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256')) {
2080
-            return (string) call_user_func(
2081
-                '\\Sodium\\crypto_pwhash_scryptsalsa208sha256',
2082
-                (int) $outlen,
2083
-                (string) $passwd,
2084
-                (string) $salt,
2085
-                (int) $opslimit,
2086
-                (int) $memlimit
2087
-            );
2088
-        }
2089
-        // This is the best we can do.
2090
-        throw new SodiumException(
2091
-            'This is not implemented, as it is not possible to implement Scrypt with acceptable performance in pure-PHP'
2092
-        );
2093
-    }
2094
-
2095
-    /**
2096
-     * !Exclusive to sodium_compat!
2097
-     *
2098
-     * This returns TRUE if the native crypto_pwhash API is available by libsodium.
2099
-     * This returns FALSE if only sodium_compat is available.
2100
-     *
2101
-     * @return bool
2102
-     */
2103
-    public static function crypto_pwhash_scryptsalsa208sha256_is_available()
2104
-    {
2105
-        if (self::useNewSodiumAPI()) {
2106
-            return true;
2107
-        }
2108
-        if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256')) {
2109
-            return true;
2110
-        }
2111
-        return false;
2112
-    }
2113
-
2114
-    /**
2115
-     * @param string $passwd
2116
-     * @param int $opslimit
2117
-     * @param int $memlimit
2118
-     * @return string
2119
-     * @throws SodiumException
2120
-     * @throws TypeError
2121
-     */
2122
-    public static function crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit)
2123
-    {
2124
-        ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
2125
-        ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2);
2126
-        ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);
2127
-
2128
-        if (self::useNewSodiumAPI()) {
2129
-            return (string) sodium_crypto_pwhash_scryptsalsa208sha256_str(
2130
-                (string) $passwd,
2131
-                (int) $opslimit,
2132
-                (int) $memlimit
2133
-            );
2134
-        }
2135
-        if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256_str')) {
2136
-            return (string) call_user_func(
2137
-                '\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str',
2138
-                (string) $passwd,
2139
-                (int) $opslimit,
2140
-                (int) $memlimit
2141
-            );
2142
-        }
2143
-        // This is the best we can do.
2144
-        throw new SodiumException(
2145
-            'This is not implemented, as it is not possible to implement Scrypt with acceptable performance in pure-PHP'
2146
-        );
2147
-    }
2148
-
2149
-    /**
2150
-     * @param string $passwd
2151
-     * @param string $hash
2152
-     * @return bool
2153
-     * @throws SodiumException
2154
-     * @throws TypeError
2155
-     */
2156
-    public static function crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash)
2157
-    {
2158
-        ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
2159
-        ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2);
2160
-
2161
-        if (self::useNewSodiumAPI()) {
2162
-            return (bool) sodium_crypto_pwhash_scryptsalsa208sha256_str_verify(
2163
-                (string) $passwd,
2164
-                (string) $hash
2165
-            );
2166
-        }
2167
-        if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256_str_verify')) {
2168
-            return (bool) call_user_func(
2169
-                '\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str_verify',
2170
-                (string) $passwd,
2171
-                (string) $hash
2172
-            );
2173
-        }
2174
-        // This is the best we can do.
2175
-        throw new SodiumException(
2176
-            'This is not implemented, as it is not possible to implement Scrypt with acceptable performance in pure-PHP'
2177
-        );
2178
-    }
2179
-
2180
-    /**
2181
-     * Calculate the shared secret between your secret key and your
2182
-     * recipient's public key.
2183
-     *
2184
-     * Algorithm: X25519 (ECDH over Curve25519)
2185
-     *
2186
-     * @param string $secretKey
2187
-     * @param string $publicKey
2188
-     * @return string
2189
-     * @throws SodiumException
2190
-     * @throws TypeError
2191
-     * @psalm-suppress MixedArgument
2192
-     */
2193
-    public static function crypto_scalarmult($secretKey, $publicKey)
2194
-    {
2195
-        /* Type checks: */
2196
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
2197
-        ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
2198
-
2199
-        /* Input validation: */
2200
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
2201
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
2202
-        }
2203
-        if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
2204
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
2205
-        }
2206
-
2207
-        if (self::useNewSodiumAPI()) {
2208
-            return sodium_crypto_scalarmult($secretKey, $publicKey);
2209
-        }
2210
-        if (self::use_fallback('crypto_scalarmult')) {
2211
-            return (string) call_user_func('\\Sodium\\crypto_scalarmult', $secretKey, $publicKey);
2212
-        }
2213
-
2214
-        /* Output validation: Forbid all-zero keys */
2215
-        if (ParagonIE_Sodium_Core_Util::hashEquals($secretKey, str_repeat("\0", self::CRYPTO_BOX_SECRETKEYBYTES))) {
2216
-            throw new SodiumException('Zero secret key is not allowed');
2217
-        }
2218
-        if (ParagonIE_Sodium_Core_Util::hashEquals($publicKey, str_repeat("\0", self::CRYPTO_BOX_PUBLICKEYBYTES))) {
2219
-            throw new SodiumException('Zero public key is not allowed');
2220
-        }
2221
-        if (PHP_INT_SIZE === 4) {
2222
-            return ParagonIE_Sodium_Crypto32::scalarmult($secretKey, $publicKey);
2223
-        }
2224
-        return ParagonIE_Sodium_Crypto::scalarmult($secretKey, $publicKey);
2225
-    }
2226
-
2227
-    /**
2228
-     * Calculate an X25519 public key from an X25519 secret key.
2229
-     *
2230
-     * @param string $secretKey
2231
-     * @return string
2232
-     * @throws SodiumException
2233
-     * @throws TypeError
2234
-     * @psalm-suppress TooFewArguments
2235
-     * @psalm-suppress MixedArgument
2236
-     */
2237
-    public static function crypto_scalarmult_base($secretKey)
2238
-    {
2239
-        /* Type checks: */
2240
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
2241
-
2242
-        /* Input validation: */
2243
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
2244
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
2245
-        }
2246
-
2247
-        if (self::useNewSodiumAPI()) {
2248
-            return sodium_crypto_scalarmult_base($secretKey);
2249
-        }
2250
-        if (self::use_fallback('crypto_scalarmult_base')) {
2251
-            return (string) call_user_func('\\Sodium\\crypto_scalarmult_base', $secretKey);
2252
-        }
2253
-        if (ParagonIE_Sodium_Core_Util::hashEquals($secretKey, str_repeat("\0", self::CRYPTO_BOX_SECRETKEYBYTES))) {
2254
-            throw new SodiumException('Zero secret key is not allowed');
2255
-        }
2256
-        if (PHP_INT_SIZE === 4) {
2257
-            return ParagonIE_Sodium_Crypto32::scalarmult_base($secretKey);
2258
-        }
2259
-        return ParagonIE_Sodium_Crypto::scalarmult_base($secretKey);
2260
-    }
2261
-
2262
-    /**
2263
-     * Authenticated symmetric-key encryption.
2264
-     *
2265
-     * Algorithm: XSalsa20-Poly1305
2266
-     *
2267
-     * @param string $plaintext The message you're encrypting
2268
-     * @param string $nonce A Number to be used Once; must be 24 bytes
2269
-     * @param string $key Symmetric encryption key
2270
-     * @return string           Ciphertext with Poly1305 MAC
2271
-     * @throws SodiumException
2272
-     * @throws TypeError
2273
-     * @psalm-suppress MixedArgument
2274
-     */
2275
-    public static function crypto_secretbox($plaintext, $nonce, $key)
2276
-    {
2277
-        /* Type checks: */
2278
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
2279
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2280
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2281
-
2282
-        /* Input validation: */
2283
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) {
2284
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2285
-        }
2286
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) {
2287
-            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
2288
-        }
2289
-
2290
-        if (self::useNewSodiumAPI()) {
2291
-            return sodium_crypto_secretbox($plaintext, $nonce, $key);
2292
-        }
2293
-        if (self::use_fallback('crypto_secretbox')) {
2294
-            return (string) call_user_func('\\Sodium\\crypto_secretbox', $plaintext, $nonce, $key);
2295
-        }
2296
-        if (PHP_INT_SIZE === 4) {
2297
-            return ParagonIE_Sodium_Crypto32::secretbox($plaintext, $nonce, $key);
2298
-        }
2299
-        return ParagonIE_Sodium_Crypto::secretbox($plaintext, $nonce, $key);
2300
-    }
2301
-
2302
-    /**
2303
-     * Decrypts a message previously encrypted with crypto_secretbox().
2304
-     *
2305
-     * @param string $ciphertext Ciphertext with Poly1305 MAC
2306
-     * @param string $nonce      A Number to be used Once; must be 24 bytes
2307
-     * @param string $key        Symmetric encryption key
2308
-     * @return string            Original plaintext message
2309
-     * @throws SodiumException
2310
-     * @throws TypeError
2311
-     * @psalm-suppress MixedArgument
2312
-     * @psalm-suppress MixedInferredReturnType
2313
-     * @psalm-suppress MixedReturnStatement
2314
-     */
2315
-    public static function crypto_secretbox_open($ciphertext, $nonce, $key)
2316
-    {
2317
-        /* Type checks: */
2318
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
2319
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2320
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2321
-
2322
-        /* Input validation: */
2323
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) {
2324
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2325
-        }
2326
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) {
2327
-            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
2328
-        }
2329
-
2330
-        if (self::useNewSodiumAPI()) {
2331
-            /**
2332
-             * @psalm-suppress InvalidReturnStatement
2333
-             * @psalm-suppress FalsableReturnStatement
2334
-             */
2335
-            return sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
2336
-        }
2337
-        if (self::use_fallback('crypto_secretbox_open')) {
2338
-            return call_user_func('\\Sodium\\crypto_secretbox_open', $ciphertext, $nonce, $key);
2339
-        }
2340
-        if (PHP_INT_SIZE === 4) {
2341
-            return ParagonIE_Sodium_Crypto32::secretbox_open($ciphertext, $nonce, $key);
2342
-        }
2343
-        return ParagonIE_Sodium_Crypto::secretbox_open($ciphertext, $nonce, $key);
2344
-    }
2345
-
2346
-    /**
2347
-     * Return a secure random key for use with crypto_secretbox
2348
-     *
2349
-     * @return string
2350
-     * @throws Exception
2351
-     * @throws Error
2352
-     */
2353
-    public static function crypto_secretbox_keygen()
2354
-    {
2355
-        return random_bytes(self::CRYPTO_SECRETBOX_KEYBYTES);
2356
-    }
2357
-
2358
-    /**
2359
-     * Authenticated symmetric-key encryption.
2360
-     *
2361
-     * Algorithm: XChaCha20-Poly1305
2362
-     *
2363
-     * @param string $plaintext The message you're encrypting
2364
-     * @param string $nonce     A Number to be used Once; must be 24 bytes
2365
-     * @param string $key       Symmetric encryption key
2366
-     * @return string           Ciphertext with Poly1305 MAC
2367
-     * @throws SodiumException
2368
-     * @throws TypeError
2369
-     * @psalm-suppress MixedArgument
2370
-     */
2371
-    public static function crypto_secretbox_xchacha20poly1305($plaintext, $nonce, $key)
2372
-    {
2373
-        /* Type checks: */
2374
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
2375
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2376
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2377
-
2378
-        /* Input validation: */
2379
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) {
2380
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2381
-        }
2382
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) {
2383
-            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
2384
-        }
2385
-        if (PHP_INT_SIZE === 4) {
2386
-            return ParagonIE_Sodium_Crypto32::secretbox_xchacha20poly1305($plaintext, $nonce, $key);
2387
-        }
2388
-        return ParagonIE_Sodium_Crypto::secretbox_xchacha20poly1305($plaintext, $nonce, $key);
2389
-    }
2390
-    /**
2391
-     * Decrypts a message previously encrypted with crypto_secretbox_xchacha20poly1305().
2392
-     *
2393
-     * @param string $ciphertext Ciphertext with Poly1305 MAC
2394
-     * @param string $nonce      A Number to be used Once; must be 24 bytes
2395
-     * @param string $key        Symmetric encryption key
2396
-     * @return string            Original plaintext message
2397
-     * @throws SodiumException
2398
-     * @throws TypeError
2399
-     * @psalm-suppress MixedArgument
2400
-     */
2401
-    public static function crypto_secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
2402
-    {
2403
-        /* Type checks: */
2404
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
2405
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2406
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2407
-
2408
-        /* Input validation: */
2409
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) {
2410
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2411
-        }
2412
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) {
2413
-            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
2414
-        }
2415
-
2416
-        if (PHP_INT_SIZE === 4) {
2417
-            return ParagonIE_Sodium_Crypto32::secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key);
2418
-        }
2419
-        return ParagonIE_Sodium_Crypto::secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key);
2420
-    }
2421
-
2422
-    /**
2423
-     * @param string $key
2424
-     * @return array<int, string> Returns a state and a header.
2425
-     * @throws Exception
2426
-     * @throws SodiumException
2427
-     */
2428
-    public static function crypto_secretstream_xchacha20poly1305_init_push($key)
2429
-    {
2430
-        if (PHP_INT_SIZE === 4) {
2431
-            return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_push($key);
2432
-        }
2433
-        return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_push($key);
2434
-    }
2435
-
2436
-    /**
2437
-     * @param string $header
2438
-     * @param string $key
2439
-     * @return string Returns a state.
2440
-     * @throws Exception
2441
-     */
2442
-    public static function crypto_secretstream_xchacha20poly1305_init_pull($header, $key)
2443
-    {
2444
-        if (ParagonIE_Sodium_Core_Util::strlen($header) < self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES) {
2445
-            throw new SodiumException(
2446
-                'header size should be SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES bytes'
2447
-            );
2448
-        }
2449
-        if (PHP_INT_SIZE === 4) {
2450
-            return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_pull($key, $header);
2451
-        }
2452
-        return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_pull($key, $header);
2453
-    }
2454
-
2455
-    /**
2456
-     * @param string $state
2457
-     * @param string $msg
2458
-     * @param string $aad
2459
-     * @param int $tag
2460
-     * @return string
2461
-     * @throws SodiumException
2462
-     */
2463
-    public static function crypto_secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
2464
-    {
2465
-        if (PHP_INT_SIZE === 4) {
2466
-            return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_push(
2467
-                $state,
2468
-                $msg,
2469
-                $aad,
2470
-                $tag
2471
-            );
2472
-        }
2473
-        return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_push(
2474
-            $state,
2475
-            $msg,
2476
-            $aad,
2477
-            $tag
2478
-        );
2479
-    }
2480
-
2481
-    /**
2482
-     * @param string $state
2483
-     * @param string $msg
2484
-     * @param string $aad
2485
-     * @return bool|array{0: string, 1: int}
2486
-     * @throws SodiumException
2487
-     */
2488
-    public static function crypto_secretstream_xchacha20poly1305_pull(&$state, $msg, $aad = '')
2489
-    {
2490
-        if (PHP_INT_SIZE === 4) {
2491
-            return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_pull(
2492
-                $state,
2493
-                $msg,
2494
-                $aad
2495
-            );
2496
-        }
2497
-        return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_pull(
2498
-            $state,
2499
-            $msg,
2500
-            $aad
2501
-        );
2502
-    }
2503
-
2504
-    /**
2505
-     * @return string
2506
-     * @throws Exception
2507
-     */
2508
-    public static function crypto_secretstream_xchacha20poly1305_keygen()
2509
-    {
2510
-        return random_bytes(self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES);
2511
-    }
2512
-
2513
-    /**
2514
-     * @param string $state
2515
-     * @return void
2516
-     * @throws SodiumException
2517
-     */
2518
-    public static function crypto_secretstream_xchacha20poly1305_rekey(&$state)
2519
-    {
2520
-        if (PHP_INT_SIZE === 4) {
2521
-            ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_rekey($state);
2522
-        } else {
2523
-            ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_rekey($state);
2524
-        }
2525
-    }
2526
-
2527
-    /**
2528
-     * Calculates a SipHash-2-4 hash of a message for a given key.
2529
-     *
2530
-     * @param string $message Input message
2531
-     * @param string $key SipHash-2-4 key
2532
-     * @return string         Hash
2533
-     * @throws SodiumException
2534
-     * @throws TypeError
2535
-     * @psalm-suppress MixedArgument
2536
-     * @psalm-suppress MixedInferredReturnType
2537
-     * @psalm-suppress MixedReturnStatement
2538
-     */
2539
-    public static function crypto_shorthash($message, $key)
2540
-    {
2541
-        /* Type checks: */
2542
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
2543
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2);
2544
-
2545
-        /* Input validation: */
2546
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SHORTHASH_KEYBYTES) {
2547
-            throw new SodiumException('Argument 2 must be CRYPTO_SHORTHASH_KEYBYTES long.');
2548
-        }
2549
-
2550
-        if (self::useNewSodiumAPI()) {
2551
-            return sodium_crypto_shorthash($message, $key);
2552
-        }
2553
-        if (self::use_fallback('crypto_shorthash')) {
2554
-            return (string) call_user_func('\\Sodium\\crypto_shorthash', $message, $key);
2555
-        }
2556
-        if (PHP_INT_SIZE === 4) {
2557
-            return ParagonIE_Sodium_Core32_SipHash::sipHash24($message, $key);
2558
-        }
2559
-        return ParagonIE_Sodium_Core_SipHash::sipHash24($message, $key);
2560
-    }
2561
-
2562
-    /**
2563
-     * Return a secure random key for use with crypto_shorthash
2564
-     *
2565
-     * @return string
2566
-     * @throws Exception
2567
-     * @throws Error
2568
-     */
2569
-    public static function crypto_shorthash_keygen()
2570
-    {
2571
-        return random_bytes(self::CRYPTO_SHORTHASH_KEYBYTES);
2572
-    }
2573
-
2574
-    /**
2575
-     * Returns a signed message. You probably want crypto_sign_detached()
2576
-     * instead, which only returns the signature.
2577
-     *
2578
-     * Algorithm: Ed25519 (EdDSA over Curve25519)
2579
-     *
2580
-     * @param string $message Message to be signed.
2581
-     * @param string $secretKey Secret signing key.
2582
-     * @return string           Signed message (signature is prefixed).
2583
-     * @throws SodiumException
2584
-     * @throws TypeError
2585
-     * @psalm-suppress MixedArgument
2586
-     * @psalm-suppress MixedInferredReturnType
2587
-     * @psalm-suppress MixedReturnStatement
2588
-     */
2589
-    public static function crypto_sign($message, $secretKey)
2590
-    {
2591
-        /* Type checks: */
2592
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
2593
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2);
2594
-
2595
-        /* Input validation: */
2596
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
2597
-            throw new SodiumException('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long.');
2598
-        }
2599
-
2600
-        if (self::useNewSodiumAPI()) {
2601
-            return sodium_crypto_sign($message, $secretKey);
2602
-        }
2603
-        if (self::use_fallback('crypto_sign')) {
2604
-            return (string) call_user_func('\\Sodium\\crypto_sign', $message, $secretKey);
2605
-        }
2606
-        if (PHP_INT_SIZE === 4) {
2607
-            return ParagonIE_Sodium_Crypto32::sign($message, $secretKey);
2608
-        }
2609
-        return ParagonIE_Sodium_Crypto::sign($message, $secretKey);
2610
-    }
2611
-
2612
-    /**
2613
-     * Validates a signed message then returns the message.
2614
-     *
2615
-     * @param string $signedMessage A signed message
2616
-     * @param string $publicKey A public key
2617
-     * @return string               The original message (if the signature is
2618
-     *                              valid for this public key)
2619
-     * @throws SodiumException
2620
-     * @throws TypeError
2621
-     * @psalm-suppress MixedArgument
2622
-     * @psalm-suppress MixedInferredReturnType
2623
-     * @psalm-suppress MixedReturnStatement
2624
-     */
2625
-    public static function crypto_sign_open($signedMessage, $publicKey)
2626
-    {
2627
-        /* Type checks: */
2628
-        ParagonIE_Sodium_Core_Util::declareScalarType($signedMessage, 'string', 1);
2629
-        ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
2630
-
2631
-        /* Input validation: */
2632
-        if (ParagonIE_Sodium_Core_Util::strlen($signedMessage) < self::CRYPTO_SIGN_BYTES) {
2633
-            throw new SodiumException('Argument 1 must be at least CRYPTO_SIGN_BYTES long.');
2634
-        }
2635
-        if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) {
2636
-            throw new SodiumException('Argument 2 must be CRYPTO_SIGN_PUBLICKEYBYTES long.');
2637
-        }
2638
-
2639
-        if (self::useNewSodiumAPI()) {
2640
-            /**
2641
-             * @psalm-suppress InvalidReturnStatement
2642
-             * @psalm-suppress FalsableReturnStatement
2643
-             */
2644
-            return sodium_crypto_sign_open($signedMessage, $publicKey);
2645
-        }
2646
-        if (self::use_fallback('crypto_sign_open')) {
2647
-            return call_user_func('\\Sodium\\crypto_sign_open', $signedMessage, $publicKey);
2648
-        }
2649
-        if (PHP_INT_SIZE === 4) {
2650
-            return ParagonIE_Sodium_Crypto32::sign_open($signedMessage, $publicKey);
2651
-        }
2652
-        return ParagonIE_Sodium_Crypto::sign_open($signedMessage, $publicKey);
2653
-    }
2654
-
2655
-    /**
2656
-     * Generate a new random Ed25519 keypair.
2657
-     *
2658
-     * @return string
2659
-     * @throws SodiumException
2660
-     * @throws TypeError
2661
-     */
2662
-    public static function crypto_sign_keypair()
2663
-    {
2664
-        if (self::useNewSodiumAPI()) {
2665
-            return sodium_crypto_sign_keypair();
2666
-        }
2667
-        if (self::use_fallback('crypto_sign_keypair')) {
2668
-            return (string) call_user_func('\\Sodium\\crypto_sign_keypair');
2669
-        }
2670
-        if (PHP_INT_SIZE === 4) {
2671
-            return ParagonIE_Sodium_Core32_Ed25519::keypair();
2672
-        }
2673
-        return ParagonIE_Sodium_Core_Ed25519::keypair();
2674
-    }
2675
-
2676
-    /**
2677
-     * @param string $sk
2678
-     * @param string $pk
2679
-     * @return string
2680
-     * @throws SodiumException
2681
-     */
2682
-    public static function crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk)
2683
-    {
2684
-        ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1);
2685
-        ParagonIE_Sodium_Core_Util::declareScalarType($pk, 'string', 1);
2686
-        $sk = (string) $sk;
2687
-        $pk = (string) $pk;
2688
-
2689
-        if (ParagonIE_Sodium_Core_Util::strlen($sk) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
2690
-            throw new SodiumException('secretkey should be SODIUM_CRYPTO_SIGN_SECRETKEYBYTES bytes');
2691
-        }
2692
-        if (ParagonIE_Sodium_Core_Util::strlen($pk) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) {
2693
-            throw new SodiumException('publickey should be SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES bytes');
2694
-        }
2695
-
2696
-        if (self::useNewSodiumAPI()) {
2697
-            return sodium_crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk);
2698
-        }
2699
-        return $sk . $pk;
2700
-    }
2701
-
2702
-    /**
2703
-     * Generate an Ed25519 keypair from a seed.
2704
-     *
2705
-     * @param string $seed Input seed
2706
-     * @return string      Keypair
2707
-     * @throws SodiumException
2708
-     * @throws TypeError
2709
-     * @psalm-suppress MixedArgument
2710
-     */
2711
-    public static function crypto_sign_seed_keypair($seed)
2712
-    {
2713
-        ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1);
2714
-
2715
-        if (self::useNewSodiumAPI()) {
2716
-            return sodium_crypto_sign_seed_keypair($seed);
2717
-        }
2718
-        if (self::use_fallback('crypto_sign_keypair')) {
2719
-            return (string) call_user_func('\\Sodium\\crypto_sign_seed_keypair', $seed);
2720
-        }
2721
-        $publicKey = '';
2722
-        $secretKey = '';
2723
-        if (PHP_INT_SIZE === 4) {
2724
-            ParagonIE_Sodium_Core32_Ed25519::seed_keypair($publicKey, $secretKey, $seed);
2725
-        } else {
2726
-            ParagonIE_Sodium_Core_Ed25519::seed_keypair($publicKey, $secretKey, $seed);
2727
-        }
2728
-        return $secretKey . $publicKey;
2729
-    }
2730
-
2731
-    /**
2732
-     * Extract an Ed25519 public key from an Ed25519 keypair.
2733
-     *
2734
-     * @param string $keypair Keypair
2735
-     * @return string         Public key
2736
-     * @throws SodiumException
2737
-     * @throws TypeError
2738
-     * @psalm-suppress MixedArgument
2739
-     */
2740
-    public static function crypto_sign_publickey($keypair)
2741
-    {
2742
-        /* Type checks: */
2743
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
2744
-
2745
-        /* Input validation: */
2746
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_SIGN_KEYPAIRBYTES) {
2747
-            throw new SodiumException('Argument 1 must be CRYPTO_SIGN_KEYPAIRBYTES long.');
2748
-        }
2749
-
2750
-        if (self::useNewSodiumAPI()) {
2751
-            return sodium_crypto_sign_publickey($keypair);
2752
-        }
2753
-        if (self::use_fallback('crypto_sign_publickey')) {
2754
-            return (string) call_user_func('\\Sodium\\crypto_sign_publickey', $keypair);
2755
-        }
2756
-        if (PHP_INT_SIZE === 4) {
2757
-            return ParagonIE_Sodium_Core32_Ed25519::publickey($keypair);
2758
-        }
2759
-        return ParagonIE_Sodium_Core_Ed25519::publickey($keypair);
2760
-    }
2761
-
2762
-    /**
2763
-     * Calculate an Ed25519 public key from an Ed25519 secret key.
2764
-     *
2765
-     * @param string $secretKey Your Ed25519 secret key
2766
-     * @return string           The corresponding Ed25519 public key
2767
-     * @throws SodiumException
2768
-     * @throws TypeError
2769
-     * @psalm-suppress MixedArgument
2770
-     */
2771
-    public static function crypto_sign_publickey_from_secretkey($secretKey)
2772
-    {
2773
-        /* Type checks: */
2774
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
2775
-
2776
-        /* Input validation: */
2777
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
2778
-            throw new SodiumException('Argument 1 must be CRYPTO_SIGN_SECRETKEYBYTES long.');
2779
-        }
2780
-
2781
-        if (self::useNewSodiumAPI()) {
2782
-            return sodium_crypto_sign_publickey_from_secretkey($secretKey);
2783
-        }
2784
-        if (self::use_fallback('crypto_sign_publickey_from_secretkey')) {
2785
-            return (string) call_user_func('\\Sodium\\crypto_sign_publickey_from_secretkey', $secretKey);
2786
-        }
2787
-        if (PHP_INT_SIZE === 4) {
2788
-            return ParagonIE_Sodium_Core32_Ed25519::publickey_from_secretkey($secretKey);
2789
-        }
2790
-        return ParagonIE_Sodium_Core_Ed25519::publickey_from_secretkey($secretKey);
2791
-    }
2792
-
2793
-    /**
2794
-     * Extract an Ed25519 secret key from an Ed25519 keypair.
2795
-     *
2796
-     * @param string $keypair Keypair
2797
-     * @return string         Secret key
2798
-     * @throws SodiumException
2799
-     * @throws TypeError
2800
-     * @psalm-suppress MixedArgument
2801
-     */
2802
-    public static function crypto_sign_secretkey($keypair)
2803
-    {
2804
-        /* Type checks: */
2805
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
2806
-
2807
-        /* Input validation: */
2808
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_SIGN_KEYPAIRBYTES) {
2809
-            throw new SodiumException('Argument 1 must be CRYPTO_SIGN_KEYPAIRBYTES long.');
2810
-        }
2811
-
2812
-        if (self::useNewSodiumAPI()) {
2813
-            return sodium_crypto_sign_secretkey($keypair);
2814
-        }
2815
-        if (self::use_fallback('crypto_sign_secretkey')) {
2816
-            return (string) call_user_func('\\Sodium\\crypto_sign_secretkey', $keypair);
2817
-        }
2818
-        if (PHP_INT_SIZE === 4) {
2819
-            return ParagonIE_Sodium_Core32_Ed25519::secretkey($keypair);
2820
-        }
2821
-        return ParagonIE_Sodium_Core_Ed25519::secretkey($keypair);
2822
-    }
2823
-
2824
-    /**
2825
-     * Calculate the Ed25519 signature of a message and return ONLY the signature.
2826
-     *
2827
-     * Algorithm: Ed25519 (EdDSA over Curve25519)
2828
-     *
2829
-     * @param string $message Message to be signed
2830
-     * @param string $secretKey Secret signing key
2831
-     * @return string           Digital signature
2832
-     * @throws SodiumException
2833
-     * @throws TypeError
2834
-     * @psalm-suppress MixedArgument
2835
-     */
2836
-    public static function crypto_sign_detached($message, $secretKey)
2837
-    {
2838
-        /* Type checks: */
2839
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
2840
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2);
2841
-
2842
-        /* Input validation: */
2843
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
2844
-            throw new SodiumException('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long.');
2845
-        }
2846
-
2847
-        if (self::useNewSodiumAPI()) {
2848
-            return sodium_crypto_sign_detached($message, $secretKey);
2849
-        }
2850
-        if (self::use_fallback('crypto_sign_detached')) {
2851
-            return (string) call_user_func('\\Sodium\\crypto_sign_detached', $message, $secretKey);
2852
-        }
2853
-        if (PHP_INT_SIZE === 4) {
2854
-            return ParagonIE_Sodium_Crypto32::sign_detached($message, $secretKey);
2855
-        }
2856
-        return ParagonIE_Sodium_Crypto::sign_detached($message, $secretKey);
2857
-    }
2858
-
2859
-    /**
2860
-     * Verify the Ed25519 signature of a message.
2861
-     *
2862
-     * @param string $signature Digital sginature
2863
-     * @param string $message Message to be verified
2864
-     * @param string $publicKey Public key
2865
-     * @return bool             TRUE if this signature is good for this public key;
2866
-     *                          FALSE otherwise
2867
-     * @throws SodiumException
2868
-     * @throws TypeError
2869
-     * @psalm-suppress MixedArgument
2870
-     */
2871
-    public static function crypto_sign_verify_detached($signature, $message, $publicKey)
2872
-    {
2873
-        /* Type checks: */
2874
-        ParagonIE_Sodium_Core_Util::declareScalarType($signature, 'string', 1);
2875
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2);
2876
-        ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 3);
2877
-
2878
-        /* Input validation: */
2879
-        if (ParagonIE_Sodium_Core_Util::strlen($signature) !== self::CRYPTO_SIGN_BYTES) {
2880
-            throw new SodiumException('Argument 1 must be CRYPTO_SIGN_BYTES long.');
2881
-        }
2882
-        if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) {
2883
-            throw new SodiumException('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES long.');
2884
-        }
2885
-
2886
-        if (self::useNewSodiumAPI()) {
2887
-            return sodium_crypto_sign_verify_detached($signature, $message, $publicKey);
2888
-        }
2889
-        if (self::use_fallback('crypto_sign_verify_detached')) {
2890
-            return (bool) call_user_func(
2891
-                '\\Sodium\\crypto_sign_verify_detached',
2892
-                $signature,
2893
-                $message,
2894
-                $publicKey
2895
-            );
2896
-        }
2897
-        if (PHP_INT_SIZE === 4) {
2898
-            return ParagonIE_Sodium_Crypto32::sign_verify_detached($signature, $message, $publicKey);
2899
-        }
2900
-        return ParagonIE_Sodium_Crypto::sign_verify_detached($signature, $message, $publicKey);
2901
-    }
2902
-
2903
-    /**
2904
-     * Convert an Ed25519 public key to a Curve25519 public key
2905
-     *
2906
-     * @param string $pk
2907
-     * @return string
2908
-     * @throws SodiumException
2909
-     * @throws TypeError
2910
-     * @psalm-suppress MixedArgument
2911
-     */
2912
-    public static function crypto_sign_ed25519_pk_to_curve25519($pk)
2913
-    {
2914
-        /* Type checks: */
2915
-        ParagonIE_Sodium_Core_Util::declareScalarType($pk, 'string', 1);
2916
-
2917
-        /* Input validation: */
2918
-        if (ParagonIE_Sodium_Core_Util::strlen($pk) < self::CRYPTO_SIGN_PUBLICKEYBYTES) {
2919
-            throw new SodiumException('Argument 1 must be at least CRYPTO_SIGN_PUBLICKEYBYTES long.');
2920
-        }
2921
-        if (self::useNewSodiumAPI()) {
2922
-            if (is_callable('crypto_sign_ed25519_pk_to_curve25519')) {
2923
-                return (string) sodium_crypto_sign_ed25519_pk_to_curve25519($pk);
2924
-            }
2925
-        }
2926
-        if (self::use_fallback('crypto_sign_ed25519_pk_to_curve25519')) {
2927
-            return (string) call_user_func('\\Sodium\\crypto_sign_ed25519_pk_to_curve25519', $pk);
2928
-        }
2929
-        if (PHP_INT_SIZE === 4) {
2930
-            return ParagonIE_Sodium_Core32_Ed25519::pk_to_curve25519($pk);
2931
-        }
2932
-        return ParagonIE_Sodium_Core_Ed25519::pk_to_curve25519($pk);
2933
-    }
2934
-
2935
-    /**
2936
-     * Convert an Ed25519 secret key to a Curve25519 secret key
2937
-     *
2938
-     * @param string $sk
2939
-     * @return string
2940
-     * @throws SodiumException
2941
-     * @throws TypeError
2942
-     * @psalm-suppress MixedArgument
2943
-     */
2944
-    public static function crypto_sign_ed25519_sk_to_curve25519($sk)
2945
-    {
2946
-        /* Type checks: */
2947
-        ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1);
2948
-
2949
-        /* Input validation: */
2950
-        if (ParagonIE_Sodium_Core_Util::strlen($sk) < self::CRYPTO_SIGN_SEEDBYTES) {
2951
-            throw new SodiumException('Argument 1 must be at least CRYPTO_SIGN_SEEDBYTES long.');
2952
-        }
2953
-        if (self::useNewSodiumAPI()) {
2954
-            if (is_callable('crypto_sign_ed25519_sk_to_curve25519')) {
2955
-                return sodium_crypto_sign_ed25519_sk_to_curve25519($sk);
2956
-            }
2957
-        }
2958
-        if (self::use_fallback('crypto_sign_ed25519_sk_to_curve25519')) {
2959
-            return (string) call_user_func('\\Sodium\\crypto_sign_ed25519_sk_to_curve25519', $sk);
2960
-        }
2961
-
2962
-        $h = hash('sha512', ParagonIE_Sodium_Core_Util::substr($sk, 0, 32), true);
2963
-        $h[0] = ParagonIE_Sodium_Core_Util::intToChr(
2964
-            ParagonIE_Sodium_Core_Util::chrToInt($h[0]) & 248
2965
-        );
2966
-        $h[31] = ParagonIE_Sodium_Core_Util::intToChr(
2967
-            (ParagonIE_Sodium_Core_Util::chrToInt($h[31]) & 127) | 64
2968
-        );
2969
-        return ParagonIE_Sodium_Core_Util::substr($h, 0, 32);
2970
-    }
2971
-
2972
-    /**
2973
-     * Expand a key and nonce into a keystream of pseudorandom bytes.
2974
-     *
2975
-     * @param int $len Number of bytes desired
2976
-     * @param string $nonce Number to be used Once; must be 24 bytes
2977
-     * @param string $key XSalsa20 key
2978
-     * @return string       Pseudorandom stream that can be XORed with messages
2979
-     *                      to provide encryption (but not authentication; see
2980
-     *                      Poly1305 or crypto_auth() for that, which is not
2981
-     *                      optional for security)
2982
-     * @throws SodiumException
2983
-     * @throws TypeError
2984
-     * @psalm-suppress MixedArgument
2985
-     */
2986
-    public static function crypto_stream($len, $nonce, $key)
2987
-    {
2988
-        /* Type checks: */
2989
-        ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1);
2990
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2991
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2992
-
2993
-        /* Input validation: */
2994
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_NONCEBYTES) {
2995
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2996
-        }
2997
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_KEYBYTES) {
2998
-            throw new SodiumException('Argument 3 must be CRYPTO_STREAM_KEYBYTES long.');
2999
-        }
3000
-
3001
-        if (self::useNewSodiumAPI()) {
3002
-            return sodium_crypto_stream($len, $nonce, $key);
3003
-        }
3004
-        if (self::use_fallback('crypto_stream')) {
3005
-            return (string) call_user_func('\\Sodium\\crypto_stream', $len, $nonce, $key);
3006
-        }
3007
-        if (PHP_INT_SIZE === 4) {
3008
-            return ParagonIE_Sodium_Core32_XSalsa20::xsalsa20($len, $nonce, $key);
3009
-        }
3010
-        return ParagonIE_Sodium_Core_XSalsa20::xsalsa20($len, $nonce, $key);
3011
-    }
3012
-
3013
-    /**
3014
-     * DANGER! UNAUTHENTICATED ENCRYPTION!
3015
-     *
3016
-     * Unless you are following expert advice, do not use this feature.
3017
-     *
3018
-     * Algorithm: XSalsa20
3019
-     *
3020
-     * This DOES NOT provide ciphertext integrity.
3021
-     *
3022
-     * @param string $message Plaintext message
3023
-     * @param string $nonce Number to be used Once; must be 24 bytes
3024
-     * @param string $key Encryption key
3025
-     * @return string         Encrypted text which is vulnerable to chosen-
3026
-     *                        ciphertext attacks unless you implement some
3027
-     *                        other mitigation to the ciphertext (i.e.
3028
-     *                        Encrypt then MAC)
3029
-     * @throws SodiumException
3030
-     * @throws TypeError
3031
-     * @psalm-suppress MixedArgument
3032
-     */
3033
-    public static function crypto_stream_xor($message, $nonce, $key)
3034
-    {
3035
-        /* Type checks: */
3036
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
3037
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
3038
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
3039
-
3040
-        /* Input validation: */
3041
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_NONCEBYTES) {
3042
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
3043
-        }
3044
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_KEYBYTES) {
3045
-            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
3046
-        }
3047
-
3048
-        if (self::useNewSodiumAPI()) {
3049
-            return sodium_crypto_stream_xor($message, $nonce, $key);
3050
-        }
3051
-        if (self::use_fallback('crypto_stream_xor')) {
3052
-            return (string) call_user_func('\\Sodium\\crypto_stream_xor', $message, $nonce, $key);
3053
-        }
3054
-        if (PHP_INT_SIZE === 4) {
3055
-            return ParagonIE_Sodium_Core32_XSalsa20::xsalsa20_xor($message, $nonce, $key);
3056
-        }
3057
-        return ParagonIE_Sodium_Core_XSalsa20::xsalsa20_xor($message, $nonce, $key);
3058
-    }
3059
-
3060
-    /**
3061
-     * Return a secure random key for use with crypto_stream
3062
-     *
3063
-     * @return string
3064
-     * @throws Exception
3065
-     * @throws Error
3066
-     */
3067
-    public static function crypto_stream_keygen()
3068
-    {
3069
-        return random_bytes(self::CRYPTO_STREAM_KEYBYTES);
3070
-    }
3071
-
3072
-
3073
-    /**
3074
-     * Expand a key and nonce into a keystream of pseudorandom bytes.
3075
-     *
3076
-     * @param int $len Number of bytes desired
3077
-     * @param string $nonce Number to be used Once; must be 24 bytes
3078
-     * @param string $key XChaCha20 key
3079
-     * @param bool $dontFallback
3080
-     * @return string       Pseudorandom stream that can be XORed with messages
3081
-     *                      to provide encryption (but not authentication; see
3082
-     *                      Poly1305 or crypto_auth() for that, which is not
3083
-     *                      optional for security)
3084
-     * @throws SodiumException
3085
-     * @throws TypeError
3086
-     * @psalm-suppress MixedArgument
3087
-     */
3088
-    public static function crypto_stream_xchacha20($len, $nonce, $key, $dontFallback = false)
3089
-    {
3090
-        /* Type checks: */
3091
-        ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1);
3092
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
3093
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
3094
-
3095
-        /* Input validation: */
3096
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_XCHACHA20_NONCEBYTES) {
3097
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_XCHACHA20_NONCEBYTES long.');
3098
-        }
3099
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_XCHACHA20_KEYBYTES) {
3100
-            throw new SodiumException('Argument 3 must be CRYPTO_STREAM_XCHACHA20_KEYBYTES long.');
3101
-        }
3102
-
3103
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3104
-            return sodium_crypto_stream_xchacha20($len, $nonce, $key);
3105
-        }
3106
-        if (PHP_INT_SIZE === 4) {
3107
-            return ParagonIE_Sodium_Core32_XChaCha20::stream($len, $nonce, $key);
3108
-        }
3109
-        return ParagonIE_Sodium_Core_XChaCha20::stream($len, $nonce, $key);
3110
-    }
3111
-
3112
-    /**
3113
-     * DANGER! UNAUTHENTICATED ENCRYPTION!
3114
-     *
3115
-     * Unless you are following expert advice, do not use this feature.
3116
-     *
3117
-     * Algorithm: XChaCha20
3118
-     *
3119
-     * This DOES NOT provide ciphertext integrity.
3120
-     *
3121
-     * @param string $message Plaintext message
3122
-     * @param string $nonce Number to be used Once; must be 24 bytes
3123
-     * @param string $key Encryption key
3124
-     * @return string         Encrypted text which is vulnerable to chosen-
3125
-     *                        ciphertext attacks unless you implement some
3126
-     *                        other mitigation to the ciphertext (i.e.
3127
-     *                        Encrypt then MAC)
3128
-     * @param bool $dontFallback
3129
-     * @throws SodiumException
3130
-     * @throws TypeError
3131
-     * @psalm-suppress MixedArgument
3132
-     */
3133
-    public static function crypto_stream_xchacha20_xor($message, $nonce, $key, $dontFallback = false)
3134
-    {
3135
-        /* Type checks: */
3136
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
3137
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
3138
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
3139
-
3140
-        /* Input validation: */
3141
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_XCHACHA20_NONCEBYTES) {
3142
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_XCHACHA20_NONCEBYTES long.');
3143
-        }
3144
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_XCHACHA20_KEYBYTES) {
3145
-            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_XCHACHA20_KEYBYTES long.');
3146
-        }
3147
-
3148
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3149
-            return sodium_crypto_stream_xchacha20_xor($message, $nonce, $key);
3150
-        }
3151
-        if (PHP_INT_SIZE === 4) {
3152
-            return ParagonIE_Sodium_Core32_XChaCha20::streamXorIc($message, $nonce, $key);
3153
-        }
3154
-        return ParagonIE_Sodium_Core_XChaCha20::streamXorIc($message, $nonce, $key);
3155
-    }
3156
-
3157
-    /**
3158
-     * Return a secure random key for use with crypto_stream_xchacha20
3159
-     *
3160
-     * @return string
3161
-     * @throws Exception
3162
-     * @throws Error
3163
-     */
3164
-    public static function crypto_stream_xchacha20_keygen()
3165
-    {
3166
-        return random_bytes(self::CRYPTO_STREAM_XCHACHA20_KEYBYTES);
3167
-    }
3168
-
3169
-    /**
3170
-     * Cache-timing-safe implementation of hex2bin().
3171
-     *
3172
-     * @param string $string Hexadecimal string
3173
-     * @return string        Raw binary string
3174
-     * @throws SodiumException
3175
-     * @throws TypeError
3176
-     * @psalm-suppress TooFewArguments
3177
-     * @psalm-suppress MixedArgument
3178
-     */
3179
-    public static function hex2bin($string)
3180
-    {
3181
-        /* Type checks: */
3182
-        ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1);
3183
-
3184
-        if (self::useNewSodiumAPI()) {
3185
-            if (is_callable('sodium_hex2bin')) {
3186
-                return (string) sodium_hex2bin($string);
3187
-            }
3188
-        }
3189
-        if (self::use_fallback('hex2bin')) {
3190
-            return (string) call_user_func('\\Sodium\\hex2bin', $string);
3191
-        }
3192
-        return ParagonIE_Sodium_Core_Util::hex2bin($string);
3193
-    }
3194
-
3195
-    /**
3196
-     * Increase a string (little endian)
3197
-     *
3198
-     * @param string $var
3199
-     *
3200
-     * @return void
3201
-     * @throws SodiumException
3202
-     * @throws TypeError
3203
-     * @psalm-suppress MixedArgument
3204
-     */
3205
-    public static function increment(&$var)
3206
-    {
3207
-        /* Type checks: */
3208
-        ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1);
3209
-
3210
-        if (self::useNewSodiumAPI()) {
3211
-            sodium_increment($var);
3212
-            return;
3213
-        }
3214
-        if (self::use_fallback('increment')) {
3215
-            $func = '\\Sodium\\increment';
3216
-            $func($var);
3217
-            return;
3218
-        }
3219
-
3220
-        $len = ParagonIE_Sodium_Core_Util::strlen($var);
3221
-        $c = 1;
3222
-        $copy = '';
3223
-        for ($i = 0; $i < $len; ++$i) {
3224
-            $c += ParagonIE_Sodium_Core_Util::chrToInt(
3225
-                ParagonIE_Sodium_Core_Util::substr($var, $i, 1)
3226
-            );
3227
-            $copy .= ParagonIE_Sodium_Core_Util::intToChr($c);
3228
-            $c >>= 8;
3229
-        }
3230
-        $var = $copy;
3231
-    }
3232
-
3233
-    /**
3234
-     * @param string $str
3235
-     * @return bool
3236
-     *
3237
-     * @throws SodiumException
3238
-     */
3239
-    public static function is_zero($str)
3240
-    {
3241
-        $d = 0;
3242
-        for ($i = 0; $i < 32; ++$i) {
3243
-            $d |= ParagonIE_Sodium_Core_Util::chrToInt($str[$i]);
3244
-        }
3245
-        return ((($d - 1) >> 31) & 1) === 1;
3246
-    }
3247
-
3248
-    /**
3249
-     * The equivalent to the libsodium minor version we aim to be compatible
3250
-     * with (sans pwhash and memzero).
3251
-     *
3252
-     * @return int
3253
-     */
3254
-    public static function library_version_major()
3255
-    {
3256
-        if (self::useNewSodiumAPI() && defined('SODIUM_LIBRARY_MAJOR_VERSION')) {
3257
-            return SODIUM_LIBRARY_MAJOR_VERSION;
3258
-        }
3259
-        if (self::use_fallback('library_version_major')) {
3260
-            /** @psalm-suppress UndefinedFunction */
3261
-            return (int) call_user_func('\\Sodium\\library_version_major');
3262
-        }
3263
-        return self::LIBRARY_VERSION_MAJOR;
3264
-    }
3265
-
3266
-    /**
3267
-     * The equivalent to the libsodium minor version we aim to be compatible
3268
-     * with (sans pwhash and memzero).
3269
-     *
3270
-     * @return int
3271
-     */
3272
-    public static function library_version_minor()
3273
-    {
3274
-        if (self::useNewSodiumAPI() && defined('SODIUM_LIBRARY_MINOR_VERSION')) {
3275
-            return SODIUM_LIBRARY_MINOR_VERSION;
3276
-        }
3277
-        if (self::use_fallback('library_version_minor')) {
3278
-            /** @psalm-suppress UndefinedFunction */
3279
-            return (int) call_user_func('\\Sodium\\library_version_minor');
3280
-        }
3281
-        return self::LIBRARY_VERSION_MINOR;
3282
-    }
3283
-
3284
-    /**
3285
-     * Compare two strings.
3286
-     *
3287
-     * @param string $left
3288
-     * @param string $right
3289
-     * @return int
3290
-     * @throws SodiumException
3291
-     * @throws TypeError
3292
-     * @psalm-suppress MixedArgument
3293
-     */
3294
-    public static function memcmp($left, $right)
3295
-    {
3296
-        /* Type checks: */
3297
-        ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1);
3298
-        ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2);
3299
-
3300
-        if (self::useNewSodiumAPI()) {
3301
-            return sodium_memcmp($left, $right);
3302
-        }
3303
-        if (self::use_fallback('memcmp')) {
3304
-            return (int) call_user_func('\\Sodium\\memcmp', $left, $right);
3305
-        }
3306
-        /** @var string $left */
3307
-        /** @var string $right */
3308
-        return ParagonIE_Sodium_Core_Util::memcmp($left, $right);
3309
-    }
3310
-
3311
-    /**
3312
-     * It's actually not possible to zero memory buffers in PHP. You need the
3313
-     * native library for that.
3314
-     *
3315
-     * @param string|null $var
3316
-     * @param-out string|null $var
3317
-     *
3318
-     * @return void
3319
-     * @throws SodiumException (Unless libsodium is installed)
3320
-     * @throws TypeError
3321
-     * @psalm-suppress TooFewArguments
3322
-     */
3323
-    public static function memzero(&$var)
3324
-    {
3325
-        /* Type checks: */
3326
-        ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1);
3327
-
3328
-        if (self::useNewSodiumAPI()) {
3329
-            /** @psalm-suppress MixedArgument */
3330
-            sodium_memzero($var);
3331
-            return;
3332
-        }
3333
-        if (self::use_fallback('memzero')) {
3334
-            $func = '\\Sodium\\memzero';
3335
-            $func($var);
3336
-            if ($var === null) {
3337
-                return;
3338
-            }
3339
-        }
3340
-        // This is the best we can do.
3341
-        throw new SodiumException(
3342
-            'This is not implemented in sodium_compat, as it is not possible to securely wipe memory from PHP. ' .
3343
-            'To fix this error, make sure libsodium is installed and the PHP extension is enabled.'
3344
-        );
3345
-    }
3346
-
3347
-    /**
3348
-     * @param string $unpadded
3349
-     * @param int $blockSize
3350
-     * @param bool $dontFallback
3351
-     * @return string
3352
-     * @throws SodiumException
3353
-     */
3354
-    public static function pad($unpadded, $blockSize, $dontFallback = false)
3355
-    {
3356
-        /* Type checks: */
3357
-        ParagonIE_Sodium_Core_Util::declareScalarType($unpadded, 'string', 1);
3358
-        ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2);
3359
-
3360
-        $unpadded = (string) $unpadded;
3361
-        $blockSize = (int) $blockSize;
3362
-
3363
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3364
-            return (string) sodium_pad($unpadded, $blockSize);
3365
-        }
3366
-
3367
-        if ($blockSize <= 0) {
3368
-            throw new SodiumException(
3369
-                'block size cannot be less than 1'
3370
-            );
3371
-        }
3372
-        $unpadded_len = ParagonIE_Sodium_Core_Util::strlen($unpadded);
3373
-        $xpadlen = ($blockSize - 1);
3374
-        if (($blockSize & ($blockSize - 1)) === 0) {
3375
-            $xpadlen -= $unpadded_len & ($blockSize - 1);
3376
-        } else {
3377
-            $xpadlen -= $unpadded_len % $blockSize;
3378
-        }
3379
-
3380
-        $xpadded_len = $unpadded_len + $xpadlen;
3381
-        $padded = str_repeat("\0", $xpadded_len - 1);
3382
-        if ($unpadded_len > 0) {
3383
-            $st = 1;
3384
-            $i = 0;
3385
-            $k = $unpadded_len;
3386
-            for ($j = 0; $j <= $xpadded_len; ++$j) {
3387
-                $i = (int) $i;
3388
-                $k = (int) $k;
3389
-                $st = (int) $st;
3390
-                if ($j >= $unpadded_len) {
3391
-                    $padded[$j] = "\0";
3392
-                } else {
3393
-                    $padded[$j] = $unpadded[$j];
3394
-                }
3395
-                /** @var int $k */
3396
-                $k -= $st;
3397
-                $st = (int) (~(
3398
-                            (
3399
-                                (
3400
-                                    ($k >> 48)
3401
-                                        |
3402
-                                    ($k >> 32)
3403
-                                        |
3404
-                                    ($k >> 16)
3405
-                                        |
3406
-                                    $k
3407
-                                ) - 1
3408
-                            ) >> 16
3409
-                        )
3410
-                    ) & 1;
3411
-                $i += $st;
3412
-            }
3413
-        }
3414
-
3415
-        $mask = 0;
3416
-        $tail = $xpadded_len;
3417
-        for ($i = 0; $i < $blockSize; ++$i) {
3418
-            # barrier_mask = (unsigned char)
3419
-            #     (((i ^ xpadlen) - 1U) >> ((sizeof(size_t) - 1U) * CHAR_BIT));
3420
-            $barrier_mask = (($i ^ $xpadlen) -1) >> ((PHP_INT_SIZE << 3) - 1);
3421
-            # tail[-i] = (tail[-i] & mask) | (0x80 & barrier_mask);
3422
-            $padded[$tail - $i] = ParagonIE_Sodium_Core_Util::intToChr(
3423
-                (ParagonIE_Sodium_Core_Util::chrToInt($padded[$tail - $i]) & $mask)
3424
-                    |
3425
-                (0x80 & $barrier_mask)
3426
-            );
3427
-            # mask |= barrier_mask;
3428
-            $mask |= $barrier_mask;
3429
-        }
3430
-        return $padded;
3431
-    }
3432
-
3433
-    /**
3434
-     * @param string $padded
3435
-     * @param int $blockSize
3436
-     * @param bool $dontFallback
3437
-     * @return string
3438
-     * @throws SodiumException
3439
-     */
3440
-    public static function unpad($padded, $blockSize, $dontFallback = false)
3441
-    {
3442
-        /* Type checks: */
3443
-        ParagonIE_Sodium_Core_Util::declareScalarType($padded, 'string', 1);
3444
-        ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2);
3445
-
3446
-        $padded = (string) $padded;
3447
-        $blockSize = (int) $blockSize;
3448
-
3449
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3450
-            return (string) sodium_unpad($padded, $blockSize);
3451
-        }
3452
-        if ($blockSize <= 0) {
3453
-            throw new SodiumException('block size cannot be less than 1');
3454
-        }
3455
-        $padded_len = ParagonIE_Sodium_Core_Util::strlen($padded);
3456
-        if ($padded_len < $blockSize) {
3457
-            throw new SodiumException('invalid padding');
3458
-        }
3459
-
3460
-        # tail = &padded[padded_len - 1U];
3461
-        $tail = $padded_len - 1;
3462
-
3463
-        $acc = 0;
3464
-        $valid = 0;
3465
-        $pad_len = 0;
3466
-
3467
-        $found = 0;
3468
-        for ($i = 0; $i < $blockSize; ++$i) {
3469
-            # c = tail[-i];
3470
-            $c = ParagonIE_Sodium_Core_Util::chrToInt($padded[$tail - $i]);
3471
-
3472
-            # is_barrier =
3473
-            #     (( (acc - 1U) & (pad_len - 1U) & ((c ^ 0x80) - 1U) ) >> 8) & 1U;
3474
-            $is_barrier = (
3475
-                (
3476
-                    ($acc - 1) & ($pad_len - 1) & (($c ^ 80) - 1)
3477
-                ) >> 7
3478
-            ) & 1;
3479
-            $is_barrier &= ~$found;
3480
-            $found |= $is_barrier;
3481
-
3482
-            # acc |= c;
3483
-            $acc |= $c;
3484
-
3485
-            # pad_len |= i & (1U + ~is_barrier);
3486
-            $pad_len |= $i & (1 + ~$is_barrier);
3487
-
3488
-            # valid |= (unsigned char) is_barrier;
3489
-            $valid |= ($is_barrier & 0xff);
3490
-        }
3491
-        # unpadded_len = padded_len - 1U - pad_len;
3492
-        $unpadded_len = $padded_len - 1 - $pad_len;
3493
-        if ($valid !== 1) {
3494
-            throw new SodiumException('invalid padding');
3495
-        }
3496
-        return ParagonIE_Sodium_Core_Util::substr($padded, 0, $unpadded_len);
3497
-    }
3498
-
3499
-    /**
3500
-     * Will sodium_compat run fast on the current hardware and PHP configuration?
3501
-     *
3502
-     * @return bool
3503
-     */
3504
-    public static function polyfill_is_fast()
3505
-    {
3506
-        if (extension_loaded('sodium')) {
3507
-            return true;
3508
-        }
3509
-        if (extension_loaded('libsodium')) {
3510
-            return true;
3511
-        }
3512
-        return PHP_INT_SIZE === 8;
3513
-    }
3514
-
3515
-    /**
3516
-     * Generate a string of bytes from the kernel's CSPRNG.
3517
-     * Proudly uses /dev/urandom (if getrandom(2) is not available).
3518
-     *
3519
-     * @param int $numBytes
3520
-     * @return string
3521
-     * @throws Exception
3522
-     * @throws TypeError
3523
-     */
3524
-    public static function randombytes_buf($numBytes)
3525
-    {
3526
-        /* Type checks: */
3527
-        if (!is_int($numBytes)) {
3528
-            if (is_numeric($numBytes)) {
3529
-                $numBytes = (int) $numBytes;
3530
-            } else {
3531
-                throw new TypeError(
3532
-                    'Argument 1 must be an integer, ' . gettype($numBytes) . ' given.'
3533
-                );
3534
-            }
3535
-        }
3536
-        if (self::use_fallback('randombytes_buf')) {
3537
-            return (string) call_user_func('\\Sodium\\randombytes_buf', $numBytes);
3538
-        }
3539
-        return random_bytes($numBytes);
3540
-    }
3541
-
3542
-    /**
3543
-     * Generate an integer between 0 and $range (non-inclusive).
3544
-     *
3545
-     * @param int $range
3546
-     * @return int
3547
-     * @throws Exception
3548
-     * @throws Error
3549
-     * @throws TypeError
3550
-     */
3551
-    public static function randombytes_uniform($range)
3552
-    {
3553
-        /* Type checks: */
3554
-        if (!is_int($range)) {
3555
-            if (is_numeric($range)) {
3556
-                $range = (int) $range;
3557
-            } else {
3558
-                throw new TypeError(
3559
-                    'Argument 1 must be an integer, ' . gettype($range) . ' given.'
3560
-                );
3561
-            }
3562
-        }
3563
-        if (self::use_fallback('randombytes_uniform')) {
3564
-            return (int) call_user_func('\\Sodium\\randombytes_uniform', $range);
3565
-        }
3566
-        return random_int(0, $range - 1);
3567
-    }
3568
-
3569
-    /**
3570
-     * Generate a random 16-bit integer.
3571
-     *
3572
-     * @return int
3573
-     * @throws Exception
3574
-     * @throws Error
3575
-     * @throws TypeError
3576
-     */
3577
-    public static function randombytes_random16()
3578
-    {
3579
-        if (self::use_fallback('randombytes_random16')) {
3580
-            return (int) call_user_func('\\Sodium\\randombytes_random16');
3581
-        }
3582
-        return random_int(0, 65535);
3583
-    }
3584
-
3585
-    /**
3586
-     * @param string $p
3587
-     * @param bool $dontFallback
3588
-     * @return bool
3589
-     * @throws SodiumException
3590
-     */
3591
-    public static function ristretto255_is_valid_point($p, $dontFallback = false)
3592
-    {
3593
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3594
-            return sodium_crypto_core_ristretto255_is_valid_point($p);
3595
-        }
3596
-        try {
3597
-            $r = ParagonIE_Sodium_Core_Ristretto255::ristretto255_frombytes($p);
3598
-            return $r['res'] === 0 &&
3599
-                ParagonIE_Sodium_Core_Ristretto255::ristretto255_point_is_canonical($p) === 1;
3600
-        } catch (SodiumException $ex) {
3601
-            if ($ex->getMessage() === 'S is not canonical') {
3602
-                return false;
3603
-            }
3604
-            throw $ex;
3605
-        }
3606
-    }
3607
-
3608
-    /**
3609
-     * @param string $p
3610
-     * @param string $q
3611
-     * @param bool $dontFallback
3612
-     * @return string
3613
-     * @throws SodiumException
3614
-     */
3615
-    public static function ristretto255_add($p, $q, $dontFallback = false)
3616
-    {
3617
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3618
-            return sodium_crypto_core_ristretto255_add($p, $q);
3619
-        }
3620
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_add($p, $q);
3621
-    }
3622
-
3623
-    /**
3624
-     * @param string $p
3625
-     * @param string $q
3626
-     * @param bool $dontFallback
3627
-     * @return string
3628
-     * @throws SodiumException
3629
-     */
3630
-    public static function ristretto255_sub($p, $q, $dontFallback = false)
3631
-    {
3632
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3633
-            return sodium_crypto_core_ristretto255_sub($p, $q);
3634
-        }
3635
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_sub($p, $q);
3636
-    }
3637
-
3638
-    /**
3639
-     * @param string $r
3640
-     * @param bool $dontFallback
3641
-     * @return string
3642
-     *
3643
-     * @throws SodiumException
3644
-     */
3645
-    public static function ristretto255_from_hash($r, $dontFallback = false)
3646
-    {
3647
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3648
-            return sodium_crypto_core_ristretto255_from_hash($r);
3649
-        }
3650
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_from_hash($r);
3651
-    }
3652
-
3653
-    /**
3654
-     * @param bool $dontFallback
3655
-     * @return string
3656
-     *
3657
-     * @throws SodiumException
3658
-     */
3659
-    public static function ristretto255_random($dontFallback = false)
3660
-    {
3661
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3662
-            return sodium_crypto_core_ristretto255_random();
3663
-        }
3664
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_random();
3665
-    }
3666
-
3667
-    /**
3668
-     * @param bool $dontFallback
3669
-     * @return string
3670
-     *
3671
-     * @throws SodiumException
3672
-     */
3673
-    public static function ristretto255_scalar_random($dontFallback = false)
3674
-    {
3675
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3676
-            return sodium_crypto_core_ristretto255_scalar_random();
3677
-        }
3678
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_random();
3679
-    }
3680
-
3681
-    /**
3682
-     * @param string $s
3683
-     * @param bool $dontFallback
3684
-     * @return string
3685
-     * @throws SodiumException
3686
-     */
3687
-    public static function ristretto255_scalar_invert($s, $dontFallback = false)
3688
-    {
3689
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3690
-            return sodium_crypto_core_ristretto255_scalar_invert($s);
3691
-        }
3692
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_invert($s);
3693
-    }
3694
-    /**
3695
-     * @param string $s
3696
-     * @param bool $dontFallback
3697
-     * @return string
3698
-     * @throws SodiumException
3699
-     */
3700
-    public static function ristretto255_scalar_negate($s, $dontFallback = false)
3701
-    {
3702
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3703
-            return sodium_crypto_core_ristretto255_scalar_negate($s);
3704
-        }
3705
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_negate($s);
3706
-    }
3707
-
3708
-    /**
3709
-     * @param string $s
3710
-     * @param bool $dontFallback
3711
-     * @return string
3712
-     * @throws SodiumException
3713
-     */
3714
-    public static function ristretto255_scalar_complement($s, $dontFallback = false)
3715
-    {
3716
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3717
-            return sodium_crypto_core_ristretto255_scalar_complement($s);
3718
-        }
3719
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_complement($s);
3720
-    }
3721
-
3722
-    /**
3723
-     * @param string $x
3724
-     * @param string $y
3725
-     * @param bool $dontFallback
3726
-     * @return string
3727
-     * @throws SodiumException
3728
-     */
3729
-    public static function ristretto255_scalar_add($x, $y, $dontFallback = false)
3730
-    {
3731
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3732
-            return sodium_crypto_core_ristretto255_scalar_add($x, $y);
3733
-        }
3734
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_add($x, $y);
3735
-    }
3736
-
3737
-    /**
3738
-     * @param string $x
3739
-     * @param string $y
3740
-     * @param bool $dontFallback
3741
-     * @return string
3742
-     * @throws SodiumException
3743
-     */
3744
-    public static function ristretto255_scalar_sub($x, $y, $dontFallback = false)
3745
-    {
3746
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3747
-            return sodium_crypto_core_ristretto255_scalar_sub($x, $y);
3748
-        }
3749
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_sub($x, $y);
3750
-    }
3751
-
3752
-    /**
3753
-     * @param string $x
3754
-     * @param string $y
3755
-     * @param bool $dontFallback
3756
-     * @return string
3757
-     * @throws SodiumException
3758
-     */
3759
-    public static function ristretto255_scalar_mul($x, $y, $dontFallback = false)
3760
-    {
3761
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3762
-            return sodium_crypto_core_ristretto255_scalar_mul($x, $y);
3763
-        }
3764
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_mul($x, $y);
3765
-    }
3766
-
3767
-    /**
3768
-     * @param string $n
3769
-     * @param string $p
3770
-     * @param bool $dontFallback
3771
-     * @return string
3772
-     * @throws SodiumException
3773
-     */
3774
-    public static function scalarmult_ristretto255($n, $p, $dontFallback = false)
3775
-    {
3776
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3777
-            return sodium_crypto_scalarmult_ristretto255($n, $p);
3778
-        }
3779
-        return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255($n, $p);
3780
-    }
3781
-
3782
-    /**
3783
-     * @param string $n
3784
-     * @param string $p
3785
-     * @param bool $dontFallback
3786
-     * @return string
3787
-     * @throws SodiumException
3788
-     */
3789
-    public static function scalarmult_ristretto255_base($n, $dontFallback = false)
3790
-    {
3791
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3792
-            return sodium_crypto_scalarmult_ristretto255_base($n);
3793
-        }
3794
-        return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255_base($n);
3795
-    }
3796
-
3797
-    /**
3798
-     * @param string $s
3799
-     * @param bool $dontFallback
3800
-     * @return string
3801
-     * @throws SodiumException
3802
-     */
3803
-    public static function ristretto255_scalar_reduce($s, $dontFallback = false)
3804
-    {
3805
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3806
-            return sodium_crypto_core_ristretto255_scalar_reduce($s);
3807
-        }
3808
-        return ParagonIE_Sodium_Core_Ristretto255::sc_reduce($s);
3809
-    }
3810
-
3811
-    /**
3812
-     * Runtime testing method for 32-bit platforms.
3813
-     *
3814
-     * Usage: If runtime_speed_test() returns FALSE, then our 32-bit
3815
-     *        implementation is to slow to use safely without risking timeouts.
3816
-     *        If this happens, install sodium from PECL to get acceptable
3817
-     *        performance.
3818
-     *
3819
-     * @param int $iterations Number of multiplications to attempt
3820
-     * @param int $maxTimeout Milliseconds
3821
-     * @return bool           TRUE if we're fast enough, FALSE is not
3822
-     * @throws SodiumException
3823
-     */
3824
-    public static function runtime_speed_test($iterations, $maxTimeout)
3825
-    {
3826
-        if (self::polyfill_is_fast()) {
3827
-            return true;
3828
-        }
3829
-        /** @var float $end */
3830
-        $end = 0.0;
3831
-        /** @var float $start */
3832
-        $start = microtime(true);
3833
-        /** @var ParagonIE_Sodium_Core32_Int64 $a */
3834
-        $a = ParagonIE_Sodium_Core32_Int64::fromInt(random_int(3, 1 << 16));
3835
-        for ($i = 0; $i < $iterations; ++$i) {
3836
-            /** @var ParagonIE_Sodium_Core32_Int64 $b */
3837
-            $b = ParagonIE_Sodium_Core32_Int64::fromInt(random_int(3, 1 << 16));
3838
-            $a->mulInt64($b);
3839
-        }
3840
-        /** @var float $end */
3841
-        $end = microtime(true);
3842
-        /** @var int $diff */
3843
-        $diff = (int) ceil(($end - $start) * 1000);
3844
-        return $diff < $maxTimeout;
3845
-    }
3846
-
3847
-    /**
3848
-     * Add two numbers (little-endian unsigned), storing the value in the first
3849
-     * parameter.
3850
-     *
3851
-     * This mutates $val.
3852
-     *
3853
-     * @param string $val
3854
-     * @param string $addv
3855
-     * @return void
3856
-     * @throws SodiumException
3857
-     */
3858
-    public static function sub(&$val, $addv)
3859
-    {
3860
-        $val_len = ParagonIE_Sodium_Core_Util::strlen($val);
3861
-        $addv_len = ParagonIE_Sodium_Core_Util::strlen($addv);
3862
-        if ($val_len !== $addv_len) {
3863
-            throw new SodiumException('values must have the same length');
3864
-        }
3865
-        $A = ParagonIE_Sodium_Core_Util::stringToIntArray($val);
3866
-        $B = ParagonIE_Sodium_Core_Util::stringToIntArray($addv);
3867
-
3868
-        $c = 0;
3869
-        for ($i = 0; $i < $val_len; $i++) {
3870
-            $c = ($A[$i] - $B[$i] - $c);
3871
-            $A[$i] = ($c & 0xff);
3872
-            $c = ($c >> 8) & 1;
3873
-        }
3874
-        $val = ParagonIE_Sodium_Core_Util::intArrayToString($A);
3875
-    }
3876
-
3877
-    /**
3878
-     * This emulates libsodium's version_string() function, except ours is
3879
-     * prefixed with 'polyfill-'.
3880
-     *
3881
-     * @return string
3882
-     * @psalm-suppress MixedInferredReturnType
3883
-     * @psalm-suppress UndefinedFunction
3884
-     */
3885
-    public static function version_string()
3886
-    {
3887
-        if (self::useNewSodiumAPI()) {
3888
-            return (string) sodium_version_string();
3889
-        }
3890
-        if (self::use_fallback('version_string')) {
3891
-            return (string) call_user_func('\\Sodium\\version_string');
3892
-        }
3893
-        return (string) self::VERSION_STRING;
3894
-    }
3895
-
3896
-    /**
3897
-     * Should we use the libsodium core function instead?
3898
-     * This is always a good idea, if it's available. (Unless we're in the
3899
-     * middle of running our unit test suite.)
3900
-     *
3901
-     * If ext/libsodium is available, use it. Return TRUE.
3902
-     * Otherwise, we have to use the code provided herein. Return FALSE.
3903
-     *
3904
-     * @param string $sodium_func_name
3905
-     *
3906
-     * @return bool
3907
-     */
3908
-    protected static function use_fallback($sodium_func_name = '')
3909
-    {
3910
-        static $res = null;
3911
-        if ($res === null) {
3912
-            $res = extension_loaded('libsodium') && PHP_VERSION_ID >= 50300;
3913
-        }
3914
-        if ($res === false) {
3915
-            // No libsodium installed
3916
-            return false;
3917
-        }
3918
-        if (self::$disableFallbackForUnitTests) {
3919
-            // Don't fallback. Use the PHP implementation.
3920
-            return false;
3921
-        }
3922
-        if (!empty($sodium_func_name)) {
3923
-            return is_callable('\\Sodium\\' . $sodium_func_name);
3924
-        }
3925
-        return true;
3926
-    }
3927
-
3928
-    /**
3929
-     * Libsodium as implemented in PHP 7.2
3930
-     * and/or ext/sodium (via PECL)
3931
-     *
3932
-     * @ref https://wiki.php.net/rfc/libsodium
3933
-     * @return bool
3934
-     */
3935
-    protected static function useNewSodiumAPI()
3936
-    {
3937
-        static $res = null;
3938
-        if ($res === null) {
3939
-            $res = PHP_VERSION_ID >= 70000 && extension_loaded('sodium');
3940
-        }
3941
-        if (self::$disableFallbackForUnitTests) {
3942
-            // Don't fallback. Use the PHP implementation.
3943
-            return false;
3944
-        }
3945
-        return (bool) $res;
3946
-    }
1571
+			if (ParagonIE_Sodium_Core_Util::strlen($key) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
1572
+				throw new SodiumException('Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.');
1573
+			}
1574
+		}
1575
+		if (PHP_INT_SIZE === 4) {
1576
+			return ParagonIE_Sodium_Crypto32::generichash_init_salt_personal($key, $length, $salt, $personal);
1577
+		}
1578
+		return ParagonIE_Sodium_Crypto::generichash_init_salt_personal($key, $length, $salt, $personal);
1579
+	}
1580
+
1581
+	/**
1582
+	 * Update a BLAKE2b hashing context with additional data.
1583
+	 *
1584
+	 * @param string $ctx    BLAKE2 hashing context. Generated by crypto_generichash_init().
1585
+	 *                       $ctx is passed by reference and gets updated in-place.
1586
+	 * @param-out string $ctx
1587
+	 * @param string $message The message to append to the existing hash state.
1588
+	 * @return void
1589
+	 * @throws SodiumException
1590
+	 * @throws TypeError
1591
+	 * @psalm-suppress MixedArgument
1592
+	 * @psalm-suppress ReferenceConstraintViolation
1593
+	 */
1594
+	public static function crypto_generichash_update(&$ctx, $message)
1595
+	{
1596
+		/* Type checks: */
1597
+		ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1);
1598
+		ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2);
1599
+
1600
+		if (self::useNewSodiumAPI()) {
1601
+			sodium_crypto_generichash_update($ctx, $message);
1602
+			return;
1603
+		}
1604
+		if (self::use_fallback('crypto_generichash_update')) {
1605
+			$func = '\\Sodium\\crypto_generichash_update';
1606
+			$func($ctx, $message);
1607
+			return;
1608
+		}
1609
+		if (PHP_INT_SIZE === 4) {
1610
+			$ctx = ParagonIE_Sodium_Crypto32::generichash_update($ctx, $message);
1611
+		} else {
1612
+			$ctx = ParagonIE_Sodium_Crypto::generichash_update($ctx, $message);
1613
+		}
1614
+	}
1615
+
1616
+	/**
1617
+	 * @return string
1618
+	 * @throws Exception
1619
+	 * @throws Error
1620
+	 */
1621
+	public static function crypto_generichash_keygen()
1622
+	{
1623
+		return random_bytes(self::CRYPTO_GENERICHASH_KEYBYTES);
1624
+	}
1625
+
1626
+	/**
1627
+	 * @param int $subkey_len
1628
+	 * @param int $subkey_id
1629
+	 * @param string $context
1630
+	 * @param string $key
1631
+	 * @return string
1632
+	 * @throws SodiumException
1633
+	 */
1634
+	public static function crypto_kdf_derive_from_key(
1635
+		$subkey_len,
1636
+		$subkey_id,
1637
+		$context,
1638
+		$key
1639
+	) {
1640
+		ParagonIE_Sodium_Core_Util::declareScalarType($subkey_len, 'int', 1);
1641
+		ParagonIE_Sodium_Core_Util::declareScalarType($subkey_id, 'int', 2);
1642
+		ParagonIE_Sodium_Core_Util::declareScalarType($context, 'string', 3);
1643
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
1644
+		$subkey_id = (int) $subkey_id;
1645
+		$subkey_len = (int) $subkey_len;
1646
+		$context = (string) $context;
1647
+		$key = (string) $key;
1648
+
1649
+		if ($subkey_len < self::CRYPTO_KDF_BYTES_MIN) {
1650
+			throw new SodiumException('subkey cannot be smaller than SODIUM_CRYPTO_KDF_BYTES_MIN');
1651
+		}
1652
+		if ($subkey_len > self::CRYPTO_KDF_BYTES_MAX) {
1653
+			throw new SodiumException('subkey cannot be larger than SODIUM_CRYPTO_KDF_BYTES_MAX');
1654
+		}
1655
+		if ($subkey_id < 0) {
1656
+			throw new SodiumException('subkey_id cannot be negative');
1657
+		}
1658
+		if (ParagonIE_Sodium_Core_Util::strlen($context) !== self::CRYPTO_KDF_CONTEXTBYTES) {
1659
+			throw new SodiumException('context should be SODIUM_CRYPTO_KDF_CONTEXTBYTES bytes');
1660
+		}
1661
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_KDF_KEYBYTES) {
1662
+			throw new SodiumException('key should be SODIUM_CRYPTO_KDF_KEYBYTES bytes');
1663
+		}
1664
+
1665
+		$salt = ParagonIE_Sodium_Core_Util::store64_le($subkey_id);
1666
+		$state = self::crypto_generichash_init_salt_personal(
1667
+			$key,
1668
+			$subkey_len,
1669
+			$salt,
1670
+			$context
1671
+		);
1672
+		return self::crypto_generichash_final($state, $subkey_len);
1673
+	}
1674
+
1675
+	/**
1676
+	 * @return string
1677
+	 * @throws Exception
1678
+	 * @throws Error
1679
+	 */
1680
+	public static function crypto_kdf_keygen()
1681
+	{
1682
+		return random_bytes(self::CRYPTO_KDF_KEYBYTES);
1683
+	}
1684
+
1685
+	/**
1686
+	 * Perform a key exchange, between a designated client and a server.
1687
+	 *
1688
+	 * Typically, you would designate one machine to be the client and the
1689
+	 * other to be the server. The first two keys are what you'd expect for
1690
+	 * scalarmult() below, but the latter two public keys don't swap places.
1691
+	 *
1692
+	 * | ALICE                          | BOB                                 |
1693
+	 * | Client                         | Server                              |
1694
+	 * |--------------------------------|-------------------------------------|
1695
+	 * | shared = crypto_kx(            | shared = crypto_kx(                 |
1696
+	 * |     alice_sk,                  |     bob_sk,                         | <- contextual
1697
+	 * |     bob_pk,                    |     alice_pk,                       | <- contextual
1698
+	 * |     alice_pk,                  |     alice_pk,                       | <----- static
1699
+	 * |     bob_pk                     |     bob_pk                          | <----- static
1700
+	 * | )                              | )                                   |
1701
+	 *
1702
+	 * They are used along with the scalarmult product to generate a 256-bit
1703
+	 * BLAKE2b hash unique to the client and server keys.
1704
+	 *
1705
+	 * @param string $my_secret
1706
+	 * @param string $their_public
1707
+	 * @param string $client_public
1708
+	 * @param string $server_public
1709
+	 * @param bool $dontFallback
1710
+	 * @return string
1711
+	 * @throws SodiumException
1712
+	 * @throws TypeError
1713
+	 * @psalm-suppress MixedArgument
1714
+	 */
1715
+	public static function crypto_kx($my_secret, $their_public, $client_public, $server_public, $dontFallback = false)
1716
+	{
1717
+		/* Type checks: */
1718
+		ParagonIE_Sodium_Core_Util::declareScalarType($my_secret, 'string', 1);
1719
+		ParagonIE_Sodium_Core_Util::declareScalarType($their_public, 'string', 2);
1720
+		ParagonIE_Sodium_Core_Util::declareScalarType($client_public, 'string', 3);
1721
+		ParagonIE_Sodium_Core_Util::declareScalarType($server_public, 'string', 4);
1722
+
1723
+		/* Input validation: */
1724
+		if (ParagonIE_Sodium_Core_Util::strlen($my_secret) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
1725
+			throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
1726
+		}
1727
+		if (ParagonIE_Sodium_Core_Util::strlen($their_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1728
+			throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1729
+		}
1730
+		if (ParagonIE_Sodium_Core_Util::strlen($client_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1731
+			throw new SodiumException('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1732
+		}
1733
+		if (ParagonIE_Sodium_Core_Util::strlen($server_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1734
+			throw new SodiumException('Argument 4 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1735
+		}
1736
+
1737
+		if (self::useNewSodiumAPI() && !$dontFallback) {
1738
+			if (is_callable('sodium_crypto_kx')) {
1739
+				return (string) sodium_crypto_kx(
1740
+					$my_secret,
1741
+					$their_public,
1742
+					$client_public,
1743
+					$server_public
1744
+				);
1745
+			}
1746
+		}
1747
+		if (self::use_fallback('crypto_kx')) {
1748
+			return (string) call_user_func(
1749
+				'\\Sodium\\crypto_kx',
1750
+				$my_secret,
1751
+				$their_public,
1752
+				$client_public,
1753
+				$server_public
1754
+			);
1755
+		}
1756
+		if (PHP_INT_SIZE === 4) {
1757
+			return ParagonIE_Sodium_Crypto32::keyExchange(
1758
+				$my_secret,
1759
+				$their_public,
1760
+				$client_public,
1761
+				$server_public
1762
+			);
1763
+		}
1764
+		return ParagonIE_Sodium_Crypto::keyExchange(
1765
+			$my_secret,
1766
+			$their_public,
1767
+			$client_public,
1768
+			$server_public
1769
+		);
1770
+	}
1771
+
1772
+	/**
1773
+	 * @param string $seed
1774
+	 * @return string
1775
+	 * @throws SodiumException
1776
+	 */
1777
+	public static function crypto_kx_seed_keypair($seed)
1778
+	{
1779
+		ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1);
1780
+
1781
+		$seed = (string) $seed;
1782
+
1783
+		if (ParagonIE_Sodium_Core_Util::strlen($seed) !== self::CRYPTO_KX_SEEDBYTES) {
1784
+			throw new SodiumException('seed must be SODIUM_CRYPTO_KX_SEEDBYTES bytes');
1785
+		}
1786
+
1787
+		$sk = self::crypto_generichash($seed, '', self::CRYPTO_KX_SECRETKEYBYTES);
1788
+		$pk = self::crypto_scalarmult_base($sk);
1789
+		return $sk . $pk;
1790
+	}
1791
+
1792
+	/**
1793
+	 * @return string
1794
+	 * @throws Exception
1795
+	 */
1796
+	public static function crypto_kx_keypair()
1797
+	{
1798
+		$sk = self::randombytes_buf(self::CRYPTO_KX_SECRETKEYBYTES);
1799
+		$pk = self::crypto_scalarmult_base($sk);
1800
+		return $sk . $pk;
1801
+	}
1802
+
1803
+	/**
1804
+	 * @param string $keypair
1805
+	 * @param string $serverPublicKey
1806
+	 * @return array{0: string, 1: string}
1807
+	 * @throws SodiumException
1808
+	 */
1809
+	public static function crypto_kx_client_session_keys($keypair, $serverPublicKey)
1810
+	{
1811
+		ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1812
+		ParagonIE_Sodium_Core_Util::declareScalarType($serverPublicKey, 'string', 2);
1813
+
1814
+		$keypair = (string) $keypair;
1815
+		$serverPublicKey = (string) $serverPublicKey;
1816
+
1817
+		if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_KX_KEYPAIRBYTES) {
1818
+			throw new SodiumException('keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes');
1819
+		}
1820
+		if (ParagonIE_Sodium_Core_Util::strlen($serverPublicKey) !== self::CRYPTO_KX_PUBLICKEYBYTES) {
1821
+			throw new SodiumException('public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes');
1822
+		}
1823
+
1824
+		$sk = self::crypto_kx_secretkey($keypair);
1825
+		$pk = self::crypto_kx_publickey($keypair);
1826
+		$h = self::crypto_generichash_init(null, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
1827
+		self::crypto_generichash_update($h, self::crypto_scalarmult($sk, $serverPublicKey));
1828
+		self::crypto_generichash_update($h, $pk);
1829
+		self::crypto_generichash_update($h, $serverPublicKey);
1830
+		$sessionKeys = self::crypto_generichash_final($h, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
1831
+		return array(
1832
+			ParagonIE_Sodium_Core_Util::substr(
1833
+				$sessionKeys,
1834
+				0,
1835
+				self::CRYPTO_KX_SESSIONKEYBYTES
1836
+			),
1837
+			ParagonIE_Sodium_Core_Util::substr(
1838
+				$sessionKeys,
1839
+				self::CRYPTO_KX_SESSIONKEYBYTES,
1840
+				self::CRYPTO_KX_SESSIONKEYBYTES
1841
+			)
1842
+		);
1843
+	}
1844
+
1845
+	/**
1846
+	 * @param string $keypair
1847
+	 * @param string $clientPublicKey
1848
+	 * @return array{0: string, 1: string}
1849
+	 * @throws SodiumException
1850
+	 */
1851
+	public static function crypto_kx_server_session_keys($keypair, $clientPublicKey)
1852
+	{
1853
+		ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1854
+		ParagonIE_Sodium_Core_Util::declareScalarType($clientPublicKey, 'string', 2);
1855
+
1856
+		$keypair = (string) $keypair;
1857
+		$clientPublicKey = (string) $clientPublicKey;
1858
+
1859
+		if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_KX_KEYPAIRBYTES) {
1860
+			throw new SodiumException('keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes');
1861
+		}
1862
+		if (ParagonIE_Sodium_Core_Util::strlen($clientPublicKey) !== self::CRYPTO_KX_PUBLICKEYBYTES) {
1863
+			throw new SodiumException('public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes');
1864
+		}
1865
+
1866
+		$sk = self::crypto_kx_secretkey($keypair);
1867
+		$pk = self::crypto_kx_publickey($keypair);
1868
+		$h = self::crypto_generichash_init(null, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
1869
+		self::crypto_generichash_update($h, self::crypto_scalarmult($sk, $clientPublicKey));
1870
+		self::crypto_generichash_update($h, $clientPublicKey);
1871
+		self::crypto_generichash_update($h, $pk);
1872
+		$sessionKeys = self::crypto_generichash_final($h, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
1873
+		return array(
1874
+			ParagonIE_Sodium_Core_Util::substr(
1875
+				$sessionKeys,
1876
+				self::CRYPTO_KX_SESSIONKEYBYTES,
1877
+				self::CRYPTO_KX_SESSIONKEYBYTES
1878
+			),
1879
+			ParagonIE_Sodium_Core_Util::substr(
1880
+				$sessionKeys,
1881
+				0,
1882
+				self::CRYPTO_KX_SESSIONKEYBYTES
1883
+			)
1884
+		);
1885
+	}
1886
+
1887
+	/**
1888
+	 * @param string $kp
1889
+	 * @return string
1890
+	 * @throws SodiumException
1891
+	 */
1892
+	public static function crypto_kx_secretkey($kp)
1893
+	{
1894
+		return ParagonIE_Sodium_Core_Util::substr(
1895
+			$kp,
1896
+			0,
1897
+			self::CRYPTO_KX_SECRETKEYBYTES
1898
+		);
1899
+	}
1900
+
1901
+	/**
1902
+	 * @param string $kp
1903
+	 * @return string
1904
+	 * @throws SodiumException
1905
+	 */
1906
+	public static function crypto_kx_publickey($kp)
1907
+	{
1908
+		return ParagonIE_Sodium_Core_Util::substr(
1909
+			$kp,
1910
+			self::CRYPTO_KX_SECRETKEYBYTES,
1911
+			self::CRYPTO_KX_PUBLICKEYBYTES
1912
+		);
1913
+	}
1914
+
1915
+	/**
1916
+	 * @param int $outlen
1917
+	 * @param string $passwd
1918
+	 * @param string $salt
1919
+	 * @param int $opslimit
1920
+	 * @param int $memlimit
1921
+	 * @param int|null $alg
1922
+	 * @return string
1923
+	 * @throws SodiumException
1924
+	 * @throws TypeError
1925
+	 * @psalm-suppress MixedArgument
1926
+	 */
1927
+	public static function crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit, $alg = null)
1928
+	{
1929
+		ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1);
1930
+		ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2);
1931
+		ParagonIE_Sodium_Core_Util::declareScalarType($salt,  'string', 3);
1932
+		ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 4);
1933
+		ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 5);
1934
+
1935
+		if (self::useNewSodiumAPI()) {
1936
+			if (!is_null($alg)) {
1937
+				ParagonIE_Sodium_Core_Util::declareScalarType($alg, 'int', 6);
1938
+				return sodium_crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit, $alg);
1939
+			}
1940
+			return sodium_crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit);
1941
+		}
1942
+		if (self::use_fallback('crypto_pwhash')) {
1943
+			return (string) call_user_func('\\Sodium\\crypto_pwhash', $outlen, $passwd, $salt, $opslimit, $memlimit);
1944
+		}
1945
+		// This is the best we can do.
1946
+		throw new SodiumException(
1947
+			'This is not implemented, as it is not possible to implement Argon2i with acceptable performance in pure-PHP'
1948
+		);
1949
+	}
1950
+
1951
+	/**
1952
+	 * !Exclusive to sodium_compat!
1953
+	 *
1954
+	 * This returns TRUE if the native crypto_pwhash API is available by libsodium.
1955
+	 * This returns FALSE if only sodium_compat is available.
1956
+	 *
1957
+	 * @return bool
1958
+	 */
1959
+	public static function crypto_pwhash_is_available()
1960
+	{
1961
+		if (self::useNewSodiumAPI()) {
1962
+			return true;
1963
+		}
1964
+		if (self::use_fallback('crypto_pwhash')) {
1965
+			return true;
1966
+		}
1967
+		return false;
1968
+	}
1969
+
1970
+	/**
1971
+	 * @param string $passwd
1972
+	 * @param int $opslimit
1973
+	 * @param int $memlimit
1974
+	 * @return string
1975
+	 * @throws SodiumException
1976
+	 * @throws TypeError
1977
+	 * @psalm-suppress MixedArgument
1978
+	 */
1979
+	public static function crypto_pwhash_str($passwd, $opslimit, $memlimit)
1980
+	{
1981
+		ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
1982
+		ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2);
1983
+		ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);
1984
+
1985
+		if (self::useNewSodiumAPI()) {
1986
+			return sodium_crypto_pwhash_str($passwd, $opslimit, $memlimit);
1987
+		}
1988
+		if (self::use_fallback('crypto_pwhash_str')) {
1989
+			return (string) call_user_func('\\Sodium\\crypto_pwhash_str', $passwd, $opslimit, $memlimit);
1990
+		}
1991
+		// This is the best we can do.
1992
+		throw new SodiumException(
1993
+			'This is not implemented, as it is not possible to implement Argon2i with acceptable performance in pure-PHP'
1994
+		);
1995
+	}
1996
+
1997
+	/**
1998
+	 * Do we need to rehash this password?
1999
+	 *
2000
+	 * @param string $hash
2001
+	 * @param int $opslimit
2002
+	 * @param int $memlimit
2003
+	 * @return bool
2004
+	 * @throws SodiumException
2005
+	 */
2006
+	public static function crypto_pwhash_str_needs_rehash($hash, $opslimit, $memlimit)
2007
+	{
2008
+		ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 1);
2009
+		ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2);
2010
+		ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);
2011
+
2012
+		// Just grab the first 4 pieces.
2013
+		$pieces = explode('$', (string) $hash);
2014
+		$prefix = implode('$', array_slice($pieces, 0, 4));
2015
+
2016
+		// Rebuild the expected header.
2017
+		/** @var int $ops */
2018
+		$ops = (int) $opslimit;
2019
+		/** @var int $mem */
2020
+		$mem = (int) $memlimit >> 10;
2021
+		$encoded = self::CRYPTO_PWHASH_STRPREFIX . 'v=19$m=' . $mem . ',t=' . $ops . ',p=1';
2022
+
2023
+		// Do they match? If so, we don't need to rehash, so return false.
2024
+		return !ParagonIE_Sodium_Core_Util::hashEquals($encoded, $prefix);
2025
+	}
2026
+
2027
+	/**
2028
+	 * @param string $passwd
2029
+	 * @param string $hash
2030
+	 * @return bool
2031
+	 * @throws SodiumException
2032
+	 * @throws TypeError
2033
+	 * @psalm-suppress MixedArgument
2034
+	 */
2035
+	public static function crypto_pwhash_str_verify($passwd, $hash)
2036
+	{
2037
+		ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
2038
+		ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2);
2039
+
2040
+		if (self::useNewSodiumAPI()) {
2041
+			return (bool) sodium_crypto_pwhash_str_verify($passwd, $hash);
2042
+		}
2043
+		if (self::use_fallback('crypto_pwhash_str_verify')) {
2044
+			return (bool) call_user_func('\\Sodium\\crypto_pwhash_str_verify', $passwd, $hash);
2045
+		}
2046
+		// This is the best we can do.
2047
+		throw new SodiumException(
2048
+			'This is not implemented, as it is not possible to implement Argon2i with acceptable performance in pure-PHP'
2049
+		);
2050
+	}
2051
+
2052
+	/**
2053
+	 * @param int $outlen
2054
+	 * @param string $passwd
2055
+	 * @param string $salt
2056
+	 * @param int $opslimit
2057
+	 * @param int $memlimit
2058
+	 * @return string
2059
+	 * @throws SodiumException
2060
+	 * @throws TypeError
2061
+	 */
2062
+	public static function crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit)
2063
+	{
2064
+		ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1);
2065
+		ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2);
2066
+		ParagonIE_Sodium_Core_Util::declareScalarType($salt,  'string', 3);
2067
+		ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 4);
2068
+		ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 5);
2069
+
2070
+		if (self::useNewSodiumAPI()) {
2071
+			return (string) sodium_crypto_pwhash_scryptsalsa208sha256(
2072
+				(int) $outlen,
2073
+				(string) $passwd,
2074
+				(string) $salt,
2075
+				(int) $opslimit,
2076
+				(int) $memlimit
2077
+			);
2078
+		}
2079
+		if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256')) {
2080
+			return (string) call_user_func(
2081
+				'\\Sodium\\crypto_pwhash_scryptsalsa208sha256',
2082
+				(int) $outlen,
2083
+				(string) $passwd,
2084
+				(string) $salt,
2085
+				(int) $opslimit,
2086
+				(int) $memlimit
2087
+			);
2088
+		}
2089
+		// This is the best we can do.
2090
+		throw new SodiumException(
2091
+			'This is not implemented, as it is not possible to implement Scrypt with acceptable performance in pure-PHP'
2092
+		);
2093
+	}
2094
+
2095
+	/**
2096
+	 * !Exclusive to sodium_compat!
2097
+	 *
2098
+	 * This returns TRUE if the native crypto_pwhash API is available by libsodium.
2099
+	 * This returns FALSE if only sodium_compat is available.
2100
+	 *
2101
+	 * @return bool
2102
+	 */
2103
+	public static function crypto_pwhash_scryptsalsa208sha256_is_available()
2104
+	{
2105
+		if (self::useNewSodiumAPI()) {
2106
+			return true;
2107
+		}
2108
+		if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256')) {
2109
+			return true;
2110
+		}
2111
+		return false;
2112
+	}
2113
+
2114
+	/**
2115
+	 * @param string $passwd
2116
+	 * @param int $opslimit
2117
+	 * @param int $memlimit
2118
+	 * @return string
2119
+	 * @throws SodiumException
2120
+	 * @throws TypeError
2121
+	 */
2122
+	public static function crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit)
2123
+	{
2124
+		ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
2125
+		ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2);
2126
+		ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);
2127
+
2128
+		if (self::useNewSodiumAPI()) {
2129
+			return (string) sodium_crypto_pwhash_scryptsalsa208sha256_str(
2130
+				(string) $passwd,
2131
+				(int) $opslimit,
2132
+				(int) $memlimit
2133
+			);
2134
+		}
2135
+		if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256_str')) {
2136
+			return (string) call_user_func(
2137
+				'\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str',
2138
+				(string) $passwd,
2139
+				(int) $opslimit,
2140
+				(int) $memlimit
2141
+			);
2142
+		}
2143
+		// This is the best we can do.
2144
+		throw new SodiumException(
2145
+			'This is not implemented, as it is not possible to implement Scrypt with acceptable performance in pure-PHP'
2146
+		);
2147
+	}
2148
+
2149
+	/**
2150
+	 * @param string $passwd
2151
+	 * @param string $hash
2152
+	 * @return bool
2153
+	 * @throws SodiumException
2154
+	 * @throws TypeError
2155
+	 */
2156
+	public static function crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash)
2157
+	{
2158
+		ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
2159
+		ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2);
2160
+
2161
+		if (self::useNewSodiumAPI()) {
2162
+			return (bool) sodium_crypto_pwhash_scryptsalsa208sha256_str_verify(
2163
+				(string) $passwd,
2164
+				(string) $hash
2165
+			);
2166
+		}
2167
+		if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256_str_verify')) {
2168
+			return (bool) call_user_func(
2169
+				'\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str_verify',
2170
+				(string) $passwd,
2171
+				(string) $hash
2172
+			);
2173
+		}
2174
+		// This is the best we can do.
2175
+		throw new SodiumException(
2176
+			'This is not implemented, as it is not possible to implement Scrypt with acceptable performance in pure-PHP'
2177
+		);
2178
+	}
2179
+
2180
+	/**
2181
+	 * Calculate the shared secret between your secret key and your
2182
+	 * recipient's public key.
2183
+	 *
2184
+	 * Algorithm: X25519 (ECDH over Curve25519)
2185
+	 *
2186
+	 * @param string $secretKey
2187
+	 * @param string $publicKey
2188
+	 * @return string
2189
+	 * @throws SodiumException
2190
+	 * @throws TypeError
2191
+	 * @psalm-suppress MixedArgument
2192
+	 */
2193
+	public static function crypto_scalarmult($secretKey, $publicKey)
2194
+	{
2195
+		/* Type checks: */
2196
+		ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
2197
+		ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
2198
+
2199
+		/* Input validation: */
2200
+		if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
2201
+			throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
2202
+		}
2203
+		if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
2204
+			throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
2205
+		}
2206
+
2207
+		if (self::useNewSodiumAPI()) {
2208
+			return sodium_crypto_scalarmult($secretKey, $publicKey);
2209
+		}
2210
+		if (self::use_fallback('crypto_scalarmult')) {
2211
+			return (string) call_user_func('\\Sodium\\crypto_scalarmult', $secretKey, $publicKey);
2212
+		}
2213
+
2214
+		/* Output validation: Forbid all-zero keys */
2215
+		if (ParagonIE_Sodium_Core_Util::hashEquals($secretKey, str_repeat("\0", self::CRYPTO_BOX_SECRETKEYBYTES))) {
2216
+			throw new SodiumException('Zero secret key is not allowed');
2217
+		}
2218
+		if (ParagonIE_Sodium_Core_Util::hashEquals($publicKey, str_repeat("\0", self::CRYPTO_BOX_PUBLICKEYBYTES))) {
2219
+			throw new SodiumException('Zero public key is not allowed');
2220
+		}
2221
+		if (PHP_INT_SIZE === 4) {
2222
+			return ParagonIE_Sodium_Crypto32::scalarmult($secretKey, $publicKey);
2223
+		}
2224
+		return ParagonIE_Sodium_Crypto::scalarmult($secretKey, $publicKey);
2225
+	}
2226
+
2227
+	/**
2228
+	 * Calculate an X25519 public key from an X25519 secret key.
2229
+	 *
2230
+	 * @param string $secretKey
2231
+	 * @return string
2232
+	 * @throws SodiumException
2233
+	 * @throws TypeError
2234
+	 * @psalm-suppress TooFewArguments
2235
+	 * @psalm-suppress MixedArgument
2236
+	 */
2237
+	public static function crypto_scalarmult_base($secretKey)
2238
+	{
2239
+		/* Type checks: */
2240
+		ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
2241
+
2242
+		/* Input validation: */
2243
+		if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
2244
+			throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
2245
+		}
2246
+
2247
+		if (self::useNewSodiumAPI()) {
2248
+			return sodium_crypto_scalarmult_base($secretKey);
2249
+		}
2250
+		if (self::use_fallback('crypto_scalarmult_base')) {
2251
+			return (string) call_user_func('\\Sodium\\crypto_scalarmult_base', $secretKey);
2252
+		}
2253
+		if (ParagonIE_Sodium_Core_Util::hashEquals($secretKey, str_repeat("\0", self::CRYPTO_BOX_SECRETKEYBYTES))) {
2254
+			throw new SodiumException('Zero secret key is not allowed');
2255
+		}
2256
+		if (PHP_INT_SIZE === 4) {
2257
+			return ParagonIE_Sodium_Crypto32::scalarmult_base($secretKey);
2258
+		}
2259
+		return ParagonIE_Sodium_Crypto::scalarmult_base($secretKey);
2260
+	}
2261
+
2262
+	/**
2263
+	 * Authenticated symmetric-key encryption.
2264
+	 *
2265
+	 * Algorithm: XSalsa20-Poly1305
2266
+	 *
2267
+	 * @param string $plaintext The message you're encrypting
2268
+	 * @param string $nonce A Number to be used Once; must be 24 bytes
2269
+	 * @param string $key Symmetric encryption key
2270
+	 * @return string           Ciphertext with Poly1305 MAC
2271
+	 * @throws SodiumException
2272
+	 * @throws TypeError
2273
+	 * @psalm-suppress MixedArgument
2274
+	 */
2275
+	public static function crypto_secretbox($plaintext, $nonce, $key)
2276
+	{
2277
+		/* Type checks: */
2278
+		ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
2279
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2280
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2281
+
2282
+		/* Input validation: */
2283
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) {
2284
+			throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2285
+		}
2286
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) {
2287
+			throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
2288
+		}
2289
+
2290
+		if (self::useNewSodiumAPI()) {
2291
+			return sodium_crypto_secretbox($plaintext, $nonce, $key);
2292
+		}
2293
+		if (self::use_fallback('crypto_secretbox')) {
2294
+			return (string) call_user_func('\\Sodium\\crypto_secretbox', $plaintext, $nonce, $key);
2295
+		}
2296
+		if (PHP_INT_SIZE === 4) {
2297
+			return ParagonIE_Sodium_Crypto32::secretbox($plaintext, $nonce, $key);
2298
+		}
2299
+		return ParagonIE_Sodium_Crypto::secretbox($plaintext, $nonce, $key);
2300
+	}
2301
+
2302
+	/**
2303
+	 * Decrypts a message previously encrypted with crypto_secretbox().
2304
+	 *
2305
+	 * @param string $ciphertext Ciphertext with Poly1305 MAC
2306
+	 * @param string $nonce      A Number to be used Once; must be 24 bytes
2307
+	 * @param string $key        Symmetric encryption key
2308
+	 * @return string            Original plaintext message
2309
+	 * @throws SodiumException
2310
+	 * @throws TypeError
2311
+	 * @psalm-suppress MixedArgument
2312
+	 * @psalm-suppress MixedInferredReturnType
2313
+	 * @psalm-suppress MixedReturnStatement
2314
+	 */
2315
+	public static function crypto_secretbox_open($ciphertext, $nonce, $key)
2316
+	{
2317
+		/* Type checks: */
2318
+		ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
2319
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2320
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2321
+
2322
+		/* Input validation: */
2323
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) {
2324
+			throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2325
+		}
2326
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) {
2327
+			throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
2328
+		}
2329
+
2330
+		if (self::useNewSodiumAPI()) {
2331
+			/**
2332
+			 * @psalm-suppress InvalidReturnStatement
2333
+			 * @psalm-suppress FalsableReturnStatement
2334
+			 */
2335
+			return sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
2336
+		}
2337
+		if (self::use_fallback('crypto_secretbox_open')) {
2338
+			return call_user_func('\\Sodium\\crypto_secretbox_open', $ciphertext, $nonce, $key);
2339
+		}
2340
+		if (PHP_INT_SIZE === 4) {
2341
+			return ParagonIE_Sodium_Crypto32::secretbox_open($ciphertext, $nonce, $key);
2342
+		}
2343
+		return ParagonIE_Sodium_Crypto::secretbox_open($ciphertext, $nonce, $key);
2344
+	}
2345
+
2346
+	/**
2347
+	 * Return a secure random key for use with crypto_secretbox
2348
+	 *
2349
+	 * @return string
2350
+	 * @throws Exception
2351
+	 * @throws Error
2352
+	 */
2353
+	public static function crypto_secretbox_keygen()
2354
+	{
2355
+		return random_bytes(self::CRYPTO_SECRETBOX_KEYBYTES);
2356
+	}
2357
+
2358
+	/**
2359
+	 * Authenticated symmetric-key encryption.
2360
+	 *
2361
+	 * Algorithm: XChaCha20-Poly1305
2362
+	 *
2363
+	 * @param string $plaintext The message you're encrypting
2364
+	 * @param string $nonce     A Number to be used Once; must be 24 bytes
2365
+	 * @param string $key       Symmetric encryption key
2366
+	 * @return string           Ciphertext with Poly1305 MAC
2367
+	 * @throws SodiumException
2368
+	 * @throws TypeError
2369
+	 * @psalm-suppress MixedArgument
2370
+	 */
2371
+	public static function crypto_secretbox_xchacha20poly1305($plaintext, $nonce, $key)
2372
+	{
2373
+		/* Type checks: */
2374
+		ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
2375
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2376
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2377
+
2378
+		/* Input validation: */
2379
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) {
2380
+			throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2381
+		}
2382
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) {
2383
+			throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
2384
+		}
2385
+		if (PHP_INT_SIZE === 4) {
2386
+			return ParagonIE_Sodium_Crypto32::secretbox_xchacha20poly1305($plaintext, $nonce, $key);
2387
+		}
2388
+		return ParagonIE_Sodium_Crypto::secretbox_xchacha20poly1305($plaintext, $nonce, $key);
2389
+	}
2390
+	/**
2391
+	 * Decrypts a message previously encrypted with crypto_secretbox_xchacha20poly1305().
2392
+	 *
2393
+	 * @param string $ciphertext Ciphertext with Poly1305 MAC
2394
+	 * @param string $nonce      A Number to be used Once; must be 24 bytes
2395
+	 * @param string $key        Symmetric encryption key
2396
+	 * @return string            Original plaintext message
2397
+	 * @throws SodiumException
2398
+	 * @throws TypeError
2399
+	 * @psalm-suppress MixedArgument
2400
+	 */
2401
+	public static function crypto_secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
2402
+	{
2403
+		/* Type checks: */
2404
+		ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
2405
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2406
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2407
+
2408
+		/* Input validation: */
2409
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) {
2410
+			throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2411
+		}
2412
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) {
2413
+			throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
2414
+		}
2415
+
2416
+		if (PHP_INT_SIZE === 4) {
2417
+			return ParagonIE_Sodium_Crypto32::secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key);
2418
+		}
2419
+		return ParagonIE_Sodium_Crypto::secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key);
2420
+	}
2421
+
2422
+	/**
2423
+	 * @param string $key
2424
+	 * @return array<int, string> Returns a state and a header.
2425
+	 * @throws Exception
2426
+	 * @throws SodiumException
2427
+	 */
2428
+	public static function crypto_secretstream_xchacha20poly1305_init_push($key)
2429
+	{
2430
+		if (PHP_INT_SIZE === 4) {
2431
+			return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_push($key);
2432
+		}
2433
+		return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_push($key);
2434
+	}
2435
+
2436
+	/**
2437
+	 * @param string $header
2438
+	 * @param string $key
2439
+	 * @return string Returns a state.
2440
+	 * @throws Exception
2441
+	 */
2442
+	public static function crypto_secretstream_xchacha20poly1305_init_pull($header, $key)
2443
+	{
2444
+		if (ParagonIE_Sodium_Core_Util::strlen($header) < self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES) {
2445
+			throw new SodiumException(
2446
+				'header size should be SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES bytes'
2447
+			);
2448
+		}
2449
+		if (PHP_INT_SIZE === 4) {
2450
+			return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_pull($key, $header);
2451
+		}
2452
+		return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_pull($key, $header);
2453
+	}
2454
+
2455
+	/**
2456
+	 * @param string $state
2457
+	 * @param string $msg
2458
+	 * @param string $aad
2459
+	 * @param int $tag
2460
+	 * @return string
2461
+	 * @throws SodiumException
2462
+	 */
2463
+	public static function crypto_secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
2464
+	{
2465
+		if (PHP_INT_SIZE === 4) {
2466
+			return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_push(
2467
+				$state,
2468
+				$msg,
2469
+				$aad,
2470
+				$tag
2471
+			);
2472
+		}
2473
+		return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_push(
2474
+			$state,
2475
+			$msg,
2476
+			$aad,
2477
+			$tag
2478
+		);
2479
+	}
2480
+
2481
+	/**
2482
+	 * @param string $state
2483
+	 * @param string $msg
2484
+	 * @param string $aad
2485
+	 * @return bool|array{0: string, 1: int}
2486
+	 * @throws SodiumException
2487
+	 */
2488
+	public static function crypto_secretstream_xchacha20poly1305_pull(&$state, $msg, $aad = '')
2489
+	{
2490
+		if (PHP_INT_SIZE === 4) {
2491
+			return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_pull(
2492
+				$state,
2493
+				$msg,
2494
+				$aad
2495
+			);
2496
+		}
2497
+		return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_pull(
2498
+			$state,
2499
+			$msg,
2500
+			$aad
2501
+		);
2502
+	}
2503
+
2504
+	/**
2505
+	 * @return string
2506
+	 * @throws Exception
2507
+	 */
2508
+	public static function crypto_secretstream_xchacha20poly1305_keygen()
2509
+	{
2510
+		return random_bytes(self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES);
2511
+	}
2512
+
2513
+	/**
2514
+	 * @param string $state
2515
+	 * @return void
2516
+	 * @throws SodiumException
2517
+	 */
2518
+	public static function crypto_secretstream_xchacha20poly1305_rekey(&$state)
2519
+	{
2520
+		if (PHP_INT_SIZE === 4) {
2521
+			ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_rekey($state);
2522
+		} else {
2523
+			ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_rekey($state);
2524
+		}
2525
+	}
2526
+
2527
+	/**
2528
+	 * Calculates a SipHash-2-4 hash of a message for a given key.
2529
+	 *
2530
+	 * @param string $message Input message
2531
+	 * @param string $key SipHash-2-4 key
2532
+	 * @return string         Hash
2533
+	 * @throws SodiumException
2534
+	 * @throws TypeError
2535
+	 * @psalm-suppress MixedArgument
2536
+	 * @psalm-suppress MixedInferredReturnType
2537
+	 * @psalm-suppress MixedReturnStatement
2538
+	 */
2539
+	public static function crypto_shorthash($message, $key)
2540
+	{
2541
+		/* Type checks: */
2542
+		ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
2543
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2);
2544
+
2545
+		/* Input validation: */
2546
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SHORTHASH_KEYBYTES) {
2547
+			throw new SodiumException('Argument 2 must be CRYPTO_SHORTHASH_KEYBYTES long.');
2548
+		}
2549
+
2550
+		if (self::useNewSodiumAPI()) {
2551
+			return sodium_crypto_shorthash($message, $key);
2552
+		}
2553
+		if (self::use_fallback('crypto_shorthash')) {
2554
+			return (string) call_user_func('\\Sodium\\crypto_shorthash', $message, $key);
2555
+		}
2556
+		if (PHP_INT_SIZE === 4) {
2557
+			return ParagonIE_Sodium_Core32_SipHash::sipHash24($message, $key);
2558
+		}
2559
+		return ParagonIE_Sodium_Core_SipHash::sipHash24($message, $key);
2560
+	}
2561
+
2562
+	/**
2563
+	 * Return a secure random key for use with crypto_shorthash
2564
+	 *
2565
+	 * @return string
2566
+	 * @throws Exception
2567
+	 * @throws Error
2568
+	 */
2569
+	public static function crypto_shorthash_keygen()
2570
+	{
2571
+		return random_bytes(self::CRYPTO_SHORTHASH_KEYBYTES);
2572
+	}
2573
+
2574
+	/**
2575
+	 * Returns a signed message. You probably want crypto_sign_detached()
2576
+	 * instead, which only returns the signature.
2577
+	 *
2578
+	 * Algorithm: Ed25519 (EdDSA over Curve25519)
2579
+	 *
2580
+	 * @param string $message Message to be signed.
2581
+	 * @param string $secretKey Secret signing key.
2582
+	 * @return string           Signed message (signature is prefixed).
2583
+	 * @throws SodiumException
2584
+	 * @throws TypeError
2585
+	 * @psalm-suppress MixedArgument
2586
+	 * @psalm-suppress MixedInferredReturnType
2587
+	 * @psalm-suppress MixedReturnStatement
2588
+	 */
2589
+	public static function crypto_sign($message, $secretKey)
2590
+	{
2591
+		/* Type checks: */
2592
+		ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
2593
+		ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2);
2594
+
2595
+		/* Input validation: */
2596
+		if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
2597
+			throw new SodiumException('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long.');
2598
+		}
2599
+
2600
+		if (self::useNewSodiumAPI()) {
2601
+			return sodium_crypto_sign($message, $secretKey);
2602
+		}
2603
+		if (self::use_fallback('crypto_sign')) {
2604
+			return (string) call_user_func('\\Sodium\\crypto_sign', $message, $secretKey);
2605
+		}
2606
+		if (PHP_INT_SIZE === 4) {
2607
+			return ParagonIE_Sodium_Crypto32::sign($message, $secretKey);
2608
+		}
2609
+		return ParagonIE_Sodium_Crypto::sign($message, $secretKey);
2610
+	}
2611
+
2612
+	/**
2613
+	 * Validates a signed message then returns the message.
2614
+	 *
2615
+	 * @param string $signedMessage A signed message
2616
+	 * @param string $publicKey A public key
2617
+	 * @return string               The original message (if the signature is
2618
+	 *                              valid for this public key)
2619
+	 * @throws SodiumException
2620
+	 * @throws TypeError
2621
+	 * @psalm-suppress MixedArgument
2622
+	 * @psalm-suppress MixedInferredReturnType
2623
+	 * @psalm-suppress MixedReturnStatement
2624
+	 */
2625
+	public static function crypto_sign_open($signedMessage, $publicKey)
2626
+	{
2627
+		/* Type checks: */
2628
+		ParagonIE_Sodium_Core_Util::declareScalarType($signedMessage, 'string', 1);
2629
+		ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
2630
+
2631
+		/* Input validation: */
2632
+		if (ParagonIE_Sodium_Core_Util::strlen($signedMessage) < self::CRYPTO_SIGN_BYTES) {
2633
+			throw new SodiumException('Argument 1 must be at least CRYPTO_SIGN_BYTES long.');
2634
+		}
2635
+		if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) {
2636
+			throw new SodiumException('Argument 2 must be CRYPTO_SIGN_PUBLICKEYBYTES long.');
2637
+		}
2638
+
2639
+		if (self::useNewSodiumAPI()) {
2640
+			/**
2641
+			 * @psalm-suppress InvalidReturnStatement
2642
+			 * @psalm-suppress FalsableReturnStatement
2643
+			 */
2644
+			return sodium_crypto_sign_open($signedMessage, $publicKey);
2645
+		}
2646
+		if (self::use_fallback('crypto_sign_open')) {
2647
+			return call_user_func('\\Sodium\\crypto_sign_open', $signedMessage, $publicKey);
2648
+		}
2649
+		if (PHP_INT_SIZE === 4) {
2650
+			return ParagonIE_Sodium_Crypto32::sign_open($signedMessage, $publicKey);
2651
+		}
2652
+		return ParagonIE_Sodium_Crypto::sign_open($signedMessage, $publicKey);
2653
+	}
2654
+
2655
+	/**
2656
+	 * Generate a new random Ed25519 keypair.
2657
+	 *
2658
+	 * @return string
2659
+	 * @throws SodiumException
2660
+	 * @throws TypeError
2661
+	 */
2662
+	public static function crypto_sign_keypair()
2663
+	{
2664
+		if (self::useNewSodiumAPI()) {
2665
+			return sodium_crypto_sign_keypair();
2666
+		}
2667
+		if (self::use_fallback('crypto_sign_keypair')) {
2668
+			return (string) call_user_func('\\Sodium\\crypto_sign_keypair');
2669
+		}
2670
+		if (PHP_INT_SIZE === 4) {
2671
+			return ParagonIE_Sodium_Core32_Ed25519::keypair();
2672
+		}
2673
+		return ParagonIE_Sodium_Core_Ed25519::keypair();
2674
+	}
2675
+
2676
+	/**
2677
+	 * @param string $sk
2678
+	 * @param string $pk
2679
+	 * @return string
2680
+	 * @throws SodiumException
2681
+	 */
2682
+	public static function crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk)
2683
+	{
2684
+		ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1);
2685
+		ParagonIE_Sodium_Core_Util::declareScalarType($pk, 'string', 1);
2686
+		$sk = (string) $sk;
2687
+		$pk = (string) $pk;
2688
+
2689
+		if (ParagonIE_Sodium_Core_Util::strlen($sk) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
2690
+			throw new SodiumException('secretkey should be SODIUM_CRYPTO_SIGN_SECRETKEYBYTES bytes');
2691
+		}
2692
+		if (ParagonIE_Sodium_Core_Util::strlen($pk) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) {
2693
+			throw new SodiumException('publickey should be SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES bytes');
2694
+		}
2695
+
2696
+		if (self::useNewSodiumAPI()) {
2697
+			return sodium_crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk);
2698
+		}
2699
+		return $sk . $pk;
2700
+	}
2701
+
2702
+	/**
2703
+	 * Generate an Ed25519 keypair from a seed.
2704
+	 *
2705
+	 * @param string $seed Input seed
2706
+	 * @return string      Keypair
2707
+	 * @throws SodiumException
2708
+	 * @throws TypeError
2709
+	 * @psalm-suppress MixedArgument
2710
+	 */
2711
+	public static function crypto_sign_seed_keypair($seed)
2712
+	{
2713
+		ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1);
2714
+
2715
+		if (self::useNewSodiumAPI()) {
2716
+			return sodium_crypto_sign_seed_keypair($seed);
2717
+		}
2718
+		if (self::use_fallback('crypto_sign_keypair')) {
2719
+			return (string) call_user_func('\\Sodium\\crypto_sign_seed_keypair', $seed);
2720
+		}
2721
+		$publicKey = '';
2722
+		$secretKey = '';
2723
+		if (PHP_INT_SIZE === 4) {
2724
+			ParagonIE_Sodium_Core32_Ed25519::seed_keypair($publicKey, $secretKey, $seed);
2725
+		} else {
2726
+			ParagonIE_Sodium_Core_Ed25519::seed_keypair($publicKey, $secretKey, $seed);
2727
+		}
2728
+		return $secretKey . $publicKey;
2729
+	}
2730
+
2731
+	/**
2732
+	 * Extract an Ed25519 public key from an Ed25519 keypair.
2733
+	 *
2734
+	 * @param string $keypair Keypair
2735
+	 * @return string         Public key
2736
+	 * @throws SodiumException
2737
+	 * @throws TypeError
2738
+	 * @psalm-suppress MixedArgument
2739
+	 */
2740
+	public static function crypto_sign_publickey($keypair)
2741
+	{
2742
+		/* Type checks: */
2743
+		ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
2744
+
2745
+		/* Input validation: */
2746
+		if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_SIGN_KEYPAIRBYTES) {
2747
+			throw new SodiumException('Argument 1 must be CRYPTO_SIGN_KEYPAIRBYTES long.');
2748
+		}
2749
+
2750
+		if (self::useNewSodiumAPI()) {
2751
+			return sodium_crypto_sign_publickey($keypair);
2752
+		}
2753
+		if (self::use_fallback('crypto_sign_publickey')) {
2754
+			return (string) call_user_func('\\Sodium\\crypto_sign_publickey', $keypair);
2755
+		}
2756
+		if (PHP_INT_SIZE === 4) {
2757
+			return ParagonIE_Sodium_Core32_Ed25519::publickey($keypair);
2758
+		}
2759
+		return ParagonIE_Sodium_Core_Ed25519::publickey($keypair);
2760
+	}
2761
+
2762
+	/**
2763
+	 * Calculate an Ed25519 public key from an Ed25519 secret key.
2764
+	 *
2765
+	 * @param string $secretKey Your Ed25519 secret key
2766
+	 * @return string           The corresponding Ed25519 public key
2767
+	 * @throws SodiumException
2768
+	 * @throws TypeError
2769
+	 * @psalm-suppress MixedArgument
2770
+	 */
2771
+	public static function crypto_sign_publickey_from_secretkey($secretKey)
2772
+	{
2773
+		/* Type checks: */
2774
+		ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
2775
+
2776
+		/* Input validation: */
2777
+		if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
2778
+			throw new SodiumException('Argument 1 must be CRYPTO_SIGN_SECRETKEYBYTES long.');
2779
+		}
2780
+
2781
+		if (self::useNewSodiumAPI()) {
2782
+			return sodium_crypto_sign_publickey_from_secretkey($secretKey);
2783
+		}
2784
+		if (self::use_fallback('crypto_sign_publickey_from_secretkey')) {
2785
+			return (string) call_user_func('\\Sodium\\crypto_sign_publickey_from_secretkey', $secretKey);
2786
+		}
2787
+		if (PHP_INT_SIZE === 4) {
2788
+			return ParagonIE_Sodium_Core32_Ed25519::publickey_from_secretkey($secretKey);
2789
+		}
2790
+		return ParagonIE_Sodium_Core_Ed25519::publickey_from_secretkey($secretKey);
2791
+	}
2792
+
2793
+	/**
2794
+	 * Extract an Ed25519 secret key from an Ed25519 keypair.
2795
+	 *
2796
+	 * @param string $keypair Keypair
2797
+	 * @return string         Secret key
2798
+	 * @throws SodiumException
2799
+	 * @throws TypeError
2800
+	 * @psalm-suppress MixedArgument
2801
+	 */
2802
+	public static function crypto_sign_secretkey($keypair)
2803
+	{
2804
+		/* Type checks: */
2805
+		ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
2806
+
2807
+		/* Input validation: */
2808
+		if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_SIGN_KEYPAIRBYTES) {
2809
+			throw new SodiumException('Argument 1 must be CRYPTO_SIGN_KEYPAIRBYTES long.');
2810
+		}
2811
+
2812
+		if (self::useNewSodiumAPI()) {
2813
+			return sodium_crypto_sign_secretkey($keypair);
2814
+		}
2815
+		if (self::use_fallback('crypto_sign_secretkey')) {
2816
+			return (string) call_user_func('\\Sodium\\crypto_sign_secretkey', $keypair);
2817
+		}
2818
+		if (PHP_INT_SIZE === 4) {
2819
+			return ParagonIE_Sodium_Core32_Ed25519::secretkey($keypair);
2820
+		}
2821
+		return ParagonIE_Sodium_Core_Ed25519::secretkey($keypair);
2822
+	}
2823
+
2824
+	/**
2825
+	 * Calculate the Ed25519 signature of a message and return ONLY the signature.
2826
+	 *
2827
+	 * Algorithm: Ed25519 (EdDSA over Curve25519)
2828
+	 *
2829
+	 * @param string $message Message to be signed
2830
+	 * @param string $secretKey Secret signing key
2831
+	 * @return string           Digital signature
2832
+	 * @throws SodiumException
2833
+	 * @throws TypeError
2834
+	 * @psalm-suppress MixedArgument
2835
+	 */
2836
+	public static function crypto_sign_detached($message, $secretKey)
2837
+	{
2838
+		/* Type checks: */
2839
+		ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
2840
+		ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2);
2841
+
2842
+		/* Input validation: */
2843
+		if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
2844
+			throw new SodiumException('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long.');
2845
+		}
2846
+
2847
+		if (self::useNewSodiumAPI()) {
2848
+			return sodium_crypto_sign_detached($message, $secretKey);
2849
+		}
2850
+		if (self::use_fallback('crypto_sign_detached')) {
2851
+			return (string) call_user_func('\\Sodium\\crypto_sign_detached', $message, $secretKey);
2852
+		}
2853
+		if (PHP_INT_SIZE === 4) {
2854
+			return ParagonIE_Sodium_Crypto32::sign_detached($message, $secretKey);
2855
+		}
2856
+		return ParagonIE_Sodium_Crypto::sign_detached($message, $secretKey);
2857
+	}
2858
+
2859
+	/**
2860
+	 * Verify the Ed25519 signature of a message.
2861
+	 *
2862
+	 * @param string $signature Digital sginature
2863
+	 * @param string $message Message to be verified
2864
+	 * @param string $publicKey Public key
2865
+	 * @return bool             TRUE if this signature is good for this public key;
2866
+	 *                          FALSE otherwise
2867
+	 * @throws SodiumException
2868
+	 * @throws TypeError
2869
+	 * @psalm-suppress MixedArgument
2870
+	 */
2871
+	public static function crypto_sign_verify_detached($signature, $message, $publicKey)
2872
+	{
2873
+		/* Type checks: */
2874
+		ParagonIE_Sodium_Core_Util::declareScalarType($signature, 'string', 1);
2875
+		ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2);
2876
+		ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 3);
2877
+
2878
+		/* Input validation: */
2879
+		if (ParagonIE_Sodium_Core_Util::strlen($signature) !== self::CRYPTO_SIGN_BYTES) {
2880
+			throw new SodiumException('Argument 1 must be CRYPTO_SIGN_BYTES long.');
2881
+		}
2882
+		if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) {
2883
+			throw new SodiumException('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES long.');
2884
+		}
2885
+
2886
+		if (self::useNewSodiumAPI()) {
2887
+			return sodium_crypto_sign_verify_detached($signature, $message, $publicKey);
2888
+		}
2889
+		if (self::use_fallback('crypto_sign_verify_detached')) {
2890
+			return (bool) call_user_func(
2891
+				'\\Sodium\\crypto_sign_verify_detached',
2892
+				$signature,
2893
+				$message,
2894
+				$publicKey
2895
+			);
2896
+		}
2897
+		if (PHP_INT_SIZE === 4) {
2898
+			return ParagonIE_Sodium_Crypto32::sign_verify_detached($signature, $message, $publicKey);
2899
+		}
2900
+		return ParagonIE_Sodium_Crypto::sign_verify_detached($signature, $message, $publicKey);
2901
+	}
2902
+
2903
+	/**
2904
+	 * Convert an Ed25519 public key to a Curve25519 public key
2905
+	 *
2906
+	 * @param string $pk
2907
+	 * @return string
2908
+	 * @throws SodiumException
2909
+	 * @throws TypeError
2910
+	 * @psalm-suppress MixedArgument
2911
+	 */
2912
+	public static function crypto_sign_ed25519_pk_to_curve25519($pk)
2913
+	{
2914
+		/* Type checks: */
2915
+		ParagonIE_Sodium_Core_Util::declareScalarType($pk, 'string', 1);
2916
+
2917
+		/* Input validation: */
2918
+		if (ParagonIE_Sodium_Core_Util::strlen($pk) < self::CRYPTO_SIGN_PUBLICKEYBYTES) {
2919
+			throw new SodiumException('Argument 1 must be at least CRYPTO_SIGN_PUBLICKEYBYTES long.');
2920
+		}
2921
+		if (self::useNewSodiumAPI()) {
2922
+			if (is_callable('crypto_sign_ed25519_pk_to_curve25519')) {
2923
+				return (string) sodium_crypto_sign_ed25519_pk_to_curve25519($pk);
2924
+			}
2925
+		}
2926
+		if (self::use_fallback('crypto_sign_ed25519_pk_to_curve25519')) {
2927
+			return (string) call_user_func('\\Sodium\\crypto_sign_ed25519_pk_to_curve25519', $pk);
2928
+		}
2929
+		if (PHP_INT_SIZE === 4) {
2930
+			return ParagonIE_Sodium_Core32_Ed25519::pk_to_curve25519($pk);
2931
+		}
2932
+		return ParagonIE_Sodium_Core_Ed25519::pk_to_curve25519($pk);
2933
+	}
2934
+
2935
+	/**
2936
+	 * Convert an Ed25519 secret key to a Curve25519 secret key
2937
+	 *
2938
+	 * @param string $sk
2939
+	 * @return string
2940
+	 * @throws SodiumException
2941
+	 * @throws TypeError
2942
+	 * @psalm-suppress MixedArgument
2943
+	 */
2944
+	public static function crypto_sign_ed25519_sk_to_curve25519($sk)
2945
+	{
2946
+		/* Type checks: */
2947
+		ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1);
2948
+
2949
+		/* Input validation: */
2950
+		if (ParagonIE_Sodium_Core_Util::strlen($sk) < self::CRYPTO_SIGN_SEEDBYTES) {
2951
+			throw new SodiumException('Argument 1 must be at least CRYPTO_SIGN_SEEDBYTES long.');
2952
+		}
2953
+		if (self::useNewSodiumAPI()) {
2954
+			if (is_callable('crypto_sign_ed25519_sk_to_curve25519')) {
2955
+				return sodium_crypto_sign_ed25519_sk_to_curve25519($sk);
2956
+			}
2957
+		}
2958
+		if (self::use_fallback('crypto_sign_ed25519_sk_to_curve25519')) {
2959
+			return (string) call_user_func('\\Sodium\\crypto_sign_ed25519_sk_to_curve25519', $sk);
2960
+		}
2961
+
2962
+		$h = hash('sha512', ParagonIE_Sodium_Core_Util::substr($sk, 0, 32), true);
2963
+		$h[0] = ParagonIE_Sodium_Core_Util::intToChr(
2964
+			ParagonIE_Sodium_Core_Util::chrToInt($h[0]) & 248
2965
+		);
2966
+		$h[31] = ParagonIE_Sodium_Core_Util::intToChr(
2967
+			(ParagonIE_Sodium_Core_Util::chrToInt($h[31]) & 127) | 64
2968
+		);
2969
+		return ParagonIE_Sodium_Core_Util::substr($h, 0, 32);
2970
+	}
2971
+
2972
+	/**
2973
+	 * Expand a key and nonce into a keystream of pseudorandom bytes.
2974
+	 *
2975
+	 * @param int $len Number of bytes desired
2976
+	 * @param string $nonce Number to be used Once; must be 24 bytes
2977
+	 * @param string $key XSalsa20 key
2978
+	 * @return string       Pseudorandom stream that can be XORed with messages
2979
+	 *                      to provide encryption (but not authentication; see
2980
+	 *                      Poly1305 or crypto_auth() for that, which is not
2981
+	 *                      optional for security)
2982
+	 * @throws SodiumException
2983
+	 * @throws TypeError
2984
+	 * @psalm-suppress MixedArgument
2985
+	 */
2986
+	public static function crypto_stream($len, $nonce, $key)
2987
+	{
2988
+		/* Type checks: */
2989
+		ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1);
2990
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2991
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2992
+
2993
+		/* Input validation: */
2994
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_NONCEBYTES) {
2995
+			throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2996
+		}
2997
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_KEYBYTES) {
2998
+			throw new SodiumException('Argument 3 must be CRYPTO_STREAM_KEYBYTES long.');
2999
+		}
3000
+
3001
+		if (self::useNewSodiumAPI()) {
3002
+			return sodium_crypto_stream($len, $nonce, $key);
3003
+		}
3004
+		if (self::use_fallback('crypto_stream')) {
3005
+			return (string) call_user_func('\\Sodium\\crypto_stream', $len, $nonce, $key);
3006
+		}
3007
+		if (PHP_INT_SIZE === 4) {
3008
+			return ParagonIE_Sodium_Core32_XSalsa20::xsalsa20($len, $nonce, $key);
3009
+		}
3010
+		return ParagonIE_Sodium_Core_XSalsa20::xsalsa20($len, $nonce, $key);
3011
+	}
3012
+
3013
+	/**
3014
+	 * DANGER! UNAUTHENTICATED ENCRYPTION!
3015
+	 *
3016
+	 * Unless you are following expert advice, do not use this feature.
3017
+	 *
3018
+	 * Algorithm: XSalsa20
3019
+	 *
3020
+	 * This DOES NOT provide ciphertext integrity.
3021
+	 *
3022
+	 * @param string $message Plaintext message
3023
+	 * @param string $nonce Number to be used Once; must be 24 bytes
3024
+	 * @param string $key Encryption key
3025
+	 * @return string         Encrypted text which is vulnerable to chosen-
3026
+	 *                        ciphertext attacks unless you implement some
3027
+	 *                        other mitigation to the ciphertext (i.e.
3028
+	 *                        Encrypt then MAC)
3029
+	 * @throws SodiumException
3030
+	 * @throws TypeError
3031
+	 * @psalm-suppress MixedArgument
3032
+	 */
3033
+	public static function crypto_stream_xor($message, $nonce, $key)
3034
+	{
3035
+		/* Type checks: */
3036
+		ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
3037
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
3038
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
3039
+
3040
+		/* Input validation: */
3041
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_NONCEBYTES) {
3042
+			throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
3043
+		}
3044
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_KEYBYTES) {
3045
+			throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
3046
+		}
3047
+
3048
+		if (self::useNewSodiumAPI()) {
3049
+			return sodium_crypto_stream_xor($message, $nonce, $key);
3050
+		}
3051
+		if (self::use_fallback('crypto_stream_xor')) {
3052
+			return (string) call_user_func('\\Sodium\\crypto_stream_xor', $message, $nonce, $key);
3053
+		}
3054
+		if (PHP_INT_SIZE === 4) {
3055
+			return ParagonIE_Sodium_Core32_XSalsa20::xsalsa20_xor($message, $nonce, $key);
3056
+		}
3057
+		return ParagonIE_Sodium_Core_XSalsa20::xsalsa20_xor($message, $nonce, $key);
3058
+	}
3059
+
3060
+	/**
3061
+	 * Return a secure random key for use with crypto_stream
3062
+	 *
3063
+	 * @return string
3064
+	 * @throws Exception
3065
+	 * @throws Error
3066
+	 */
3067
+	public static function crypto_stream_keygen()
3068
+	{
3069
+		return random_bytes(self::CRYPTO_STREAM_KEYBYTES);
3070
+	}
3071
+
3072
+
3073
+	/**
3074
+	 * Expand a key and nonce into a keystream of pseudorandom bytes.
3075
+	 *
3076
+	 * @param int $len Number of bytes desired
3077
+	 * @param string $nonce Number to be used Once; must be 24 bytes
3078
+	 * @param string $key XChaCha20 key
3079
+	 * @param bool $dontFallback
3080
+	 * @return string       Pseudorandom stream that can be XORed with messages
3081
+	 *                      to provide encryption (but not authentication; see
3082
+	 *                      Poly1305 or crypto_auth() for that, which is not
3083
+	 *                      optional for security)
3084
+	 * @throws SodiumException
3085
+	 * @throws TypeError
3086
+	 * @psalm-suppress MixedArgument
3087
+	 */
3088
+	public static function crypto_stream_xchacha20($len, $nonce, $key, $dontFallback = false)
3089
+	{
3090
+		/* Type checks: */
3091
+		ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1);
3092
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
3093
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
3094
+
3095
+		/* Input validation: */
3096
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_XCHACHA20_NONCEBYTES) {
3097
+			throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_XCHACHA20_NONCEBYTES long.');
3098
+		}
3099
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_XCHACHA20_KEYBYTES) {
3100
+			throw new SodiumException('Argument 3 must be CRYPTO_STREAM_XCHACHA20_KEYBYTES long.');
3101
+		}
3102
+
3103
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3104
+			return sodium_crypto_stream_xchacha20($len, $nonce, $key);
3105
+		}
3106
+		if (PHP_INT_SIZE === 4) {
3107
+			return ParagonIE_Sodium_Core32_XChaCha20::stream($len, $nonce, $key);
3108
+		}
3109
+		return ParagonIE_Sodium_Core_XChaCha20::stream($len, $nonce, $key);
3110
+	}
3111
+
3112
+	/**
3113
+	 * DANGER! UNAUTHENTICATED ENCRYPTION!
3114
+	 *
3115
+	 * Unless you are following expert advice, do not use this feature.
3116
+	 *
3117
+	 * Algorithm: XChaCha20
3118
+	 *
3119
+	 * This DOES NOT provide ciphertext integrity.
3120
+	 *
3121
+	 * @param string $message Plaintext message
3122
+	 * @param string $nonce Number to be used Once; must be 24 bytes
3123
+	 * @param string $key Encryption key
3124
+	 * @return string         Encrypted text which is vulnerable to chosen-
3125
+	 *                        ciphertext attacks unless you implement some
3126
+	 *                        other mitigation to the ciphertext (i.e.
3127
+	 *                        Encrypt then MAC)
3128
+	 * @param bool $dontFallback
3129
+	 * @throws SodiumException
3130
+	 * @throws TypeError
3131
+	 * @psalm-suppress MixedArgument
3132
+	 */
3133
+	public static function crypto_stream_xchacha20_xor($message, $nonce, $key, $dontFallback = false)
3134
+	{
3135
+		/* Type checks: */
3136
+		ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
3137
+		ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
3138
+		ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
3139
+
3140
+		/* Input validation: */
3141
+		if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_XCHACHA20_NONCEBYTES) {
3142
+			throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_XCHACHA20_NONCEBYTES long.');
3143
+		}
3144
+		if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_XCHACHA20_KEYBYTES) {
3145
+			throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_XCHACHA20_KEYBYTES long.');
3146
+		}
3147
+
3148
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3149
+			return sodium_crypto_stream_xchacha20_xor($message, $nonce, $key);
3150
+		}
3151
+		if (PHP_INT_SIZE === 4) {
3152
+			return ParagonIE_Sodium_Core32_XChaCha20::streamXorIc($message, $nonce, $key);
3153
+		}
3154
+		return ParagonIE_Sodium_Core_XChaCha20::streamXorIc($message, $nonce, $key);
3155
+	}
3156
+
3157
+	/**
3158
+	 * Return a secure random key for use with crypto_stream_xchacha20
3159
+	 *
3160
+	 * @return string
3161
+	 * @throws Exception
3162
+	 * @throws Error
3163
+	 */
3164
+	public static function crypto_stream_xchacha20_keygen()
3165
+	{
3166
+		return random_bytes(self::CRYPTO_STREAM_XCHACHA20_KEYBYTES);
3167
+	}
3168
+
3169
+	/**
3170
+	 * Cache-timing-safe implementation of hex2bin().
3171
+	 *
3172
+	 * @param string $string Hexadecimal string
3173
+	 * @return string        Raw binary string
3174
+	 * @throws SodiumException
3175
+	 * @throws TypeError
3176
+	 * @psalm-suppress TooFewArguments
3177
+	 * @psalm-suppress MixedArgument
3178
+	 */
3179
+	public static function hex2bin($string)
3180
+	{
3181
+		/* Type checks: */
3182
+		ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1);
3183
+
3184
+		if (self::useNewSodiumAPI()) {
3185
+			if (is_callable('sodium_hex2bin')) {
3186
+				return (string) sodium_hex2bin($string);
3187
+			}
3188
+		}
3189
+		if (self::use_fallback('hex2bin')) {
3190
+			return (string) call_user_func('\\Sodium\\hex2bin', $string);
3191
+		}
3192
+		return ParagonIE_Sodium_Core_Util::hex2bin($string);
3193
+	}
3194
+
3195
+	/**
3196
+	 * Increase a string (little endian)
3197
+	 *
3198
+	 * @param string $var
3199
+	 *
3200
+	 * @return void
3201
+	 * @throws SodiumException
3202
+	 * @throws TypeError
3203
+	 * @psalm-suppress MixedArgument
3204
+	 */
3205
+	public static function increment(&$var)
3206
+	{
3207
+		/* Type checks: */
3208
+		ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1);
3209
+
3210
+		if (self::useNewSodiumAPI()) {
3211
+			sodium_increment($var);
3212
+			return;
3213
+		}
3214
+		if (self::use_fallback('increment')) {
3215
+			$func = '\\Sodium\\increment';
3216
+			$func($var);
3217
+			return;
3218
+		}
3219
+
3220
+		$len = ParagonIE_Sodium_Core_Util::strlen($var);
3221
+		$c = 1;
3222
+		$copy = '';
3223
+		for ($i = 0; $i < $len; ++$i) {
3224
+			$c += ParagonIE_Sodium_Core_Util::chrToInt(
3225
+				ParagonIE_Sodium_Core_Util::substr($var, $i, 1)
3226
+			);
3227
+			$copy .= ParagonIE_Sodium_Core_Util::intToChr($c);
3228
+			$c >>= 8;
3229
+		}
3230
+		$var = $copy;
3231
+	}
3232
+
3233
+	/**
3234
+	 * @param string $str
3235
+	 * @return bool
3236
+	 *
3237
+	 * @throws SodiumException
3238
+	 */
3239
+	public static function is_zero($str)
3240
+	{
3241
+		$d = 0;
3242
+		for ($i = 0; $i < 32; ++$i) {
3243
+			$d |= ParagonIE_Sodium_Core_Util::chrToInt($str[$i]);
3244
+		}
3245
+		return ((($d - 1) >> 31) & 1) === 1;
3246
+	}
3247
+
3248
+	/**
3249
+	 * The equivalent to the libsodium minor version we aim to be compatible
3250
+	 * with (sans pwhash and memzero).
3251
+	 *
3252
+	 * @return int
3253
+	 */
3254
+	public static function library_version_major()
3255
+	{
3256
+		if (self::useNewSodiumAPI() && defined('SODIUM_LIBRARY_MAJOR_VERSION')) {
3257
+			return SODIUM_LIBRARY_MAJOR_VERSION;
3258
+		}
3259
+		if (self::use_fallback('library_version_major')) {
3260
+			/** @psalm-suppress UndefinedFunction */
3261
+			return (int) call_user_func('\\Sodium\\library_version_major');
3262
+		}
3263
+		return self::LIBRARY_VERSION_MAJOR;
3264
+	}
3265
+
3266
+	/**
3267
+	 * The equivalent to the libsodium minor version we aim to be compatible
3268
+	 * with (sans pwhash and memzero).
3269
+	 *
3270
+	 * @return int
3271
+	 */
3272
+	public static function library_version_minor()
3273
+	{
3274
+		if (self::useNewSodiumAPI() && defined('SODIUM_LIBRARY_MINOR_VERSION')) {
3275
+			return SODIUM_LIBRARY_MINOR_VERSION;
3276
+		}
3277
+		if (self::use_fallback('library_version_minor')) {
3278
+			/** @psalm-suppress UndefinedFunction */
3279
+			return (int) call_user_func('\\Sodium\\library_version_minor');
3280
+		}
3281
+		return self::LIBRARY_VERSION_MINOR;
3282
+	}
3283
+
3284
+	/**
3285
+	 * Compare two strings.
3286
+	 *
3287
+	 * @param string $left
3288
+	 * @param string $right
3289
+	 * @return int
3290
+	 * @throws SodiumException
3291
+	 * @throws TypeError
3292
+	 * @psalm-suppress MixedArgument
3293
+	 */
3294
+	public static function memcmp($left, $right)
3295
+	{
3296
+		/* Type checks: */
3297
+		ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1);
3298
+		ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2);
3299
+
3300
+		if (self::useNewSodiumAPI()) {
3301
+			return sodium_memcmp($left, $right);
3302
+		}
3303
+		if (self::use_fallback('memcmp')) {
3304
+			return (int) call_user_func('\\Sodium\\memcmp', $left, $right);
3305
+		}
3306
+		/** @var string $left */
3307
+		/** @var string $right */
3308
+		return ParagonIE_Sodium_Core_Util::memcmp($left, $right);
3309
+	}
3310
+
3311
+	/**
3312
+	 * It's actually not possible to zero memory buffers in PHP. You need the
3313
+	 * native library for that.
3314
+	 *
3315
+	 * @param string|null $var
3316
+	 * @param-out string|null $var
3317
+	 *
3318
+	 * @return void
3319
+	 * @throws SodiumException (Unless libsodium is installed)
3320
+	 * @throws TypeError
3321
+	 * @psalm-suppress TooFewArguments
3322
+	 */
3323
+	public static function memzero(&$var)
3324
+	{
3325
+		/* Type checks: */
3326
+		ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1);
3327
+
3328
+		if (self::useNewSodiumAPI()) {
3329
+			/** @psalm-suppress MixedArgument */
3330
+			sodium_memzero($var);
3331
+			return;
3332
+		}
3333
+		if (self::use_fallback('memzero')) {
3334
+			$func = '\\Sodium\\memzero';
3335
+			$func($var);
3336
+			if ($var === null) {
3337
+				return;
3338
+			}
3339
+		}
3340
+		// This is the best we can do.
3341
+		throw new SodiumException(
3342
+			'This is not implemented in sodium_compat, as it is not possible to securely wipe memory from PHP. ' .
3343
+			'To fix this error, make sure libsodium is installed and the PHP extension is enabled.'
3344
+		);
3345
+	}
3346
+
3347
+	/**
3348
+	 * @param string $unpadded
3349
+	 * @param int $blockSize
3350
+	 * @param bool $dontFallback
3351
+	 * @return string
3352
+	 * @throws SodiumException
3353
+	 */
3354
+	public static function pad($unpadded, $blockSize, $dontFallback = false)
3355
+	{
3356
+		/* Type checks: */
3357
+		ParagonIE_Sodium_Core_Util::declareScalarType($unpadded, 'string', 1);
3358
+		ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2);
3359
+
3360
+		$unpadded = (string) $unpadded;
3361
+		$blockSize = (int) $blockSize;
3362
+
3363
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3364
+			return (string) sodium_pad($unpadded, $blockSize);
3365
+		}
3366
+
3367
+		if ($blockSize <= 0) {
3368
+			throw new SodiumException(
3369
+				'block size cannot be less than 1'
3370
+			);
3371
+		}
3372
+		$unpadded_len = ParagonIE_Sodium_Core_Util::strlen($unpadded);
3373
+		$xpadlen = ($blockSize - 1);
3374
+		if (($blockSize & ($blockSize - 1)) === 0) {
3375
+			$xpadlen -= $unpadded_len & ($blockSize - 1);
3376
+		} else {
3377
+			$xpadlen -= $unpadded_len % $blockSize;
3378
+		}
3379
+
3380
+		$xpadded_len = $unpadded_len + $xpadlen;
3381
+		$padded = str_repeat("\0", $xpadded_len - 1);
3382
+		if ($unpadded_len > 0) {
3383
+			$st = 1;
3384
+			$i = 0;
3385
+			$k = $unpadded_len;
3386
+			for ($j = 0; $j <= $xpadded_len; ++$j) {
3387
+				$i = (int) $i;
3388
+				$k = (int) $k;
3389
+				$st = (int) $st;
3390
+				if ($j >= $unpadded_len) {
3391
+					$padded[$j] = "\0";
3392
+				} else {
3393
+					$padded[$j] = $unpadded[$j];
3394
+				}
3395
+				/** @var int $k */
3396
+				$k -= $st;
3397
+				$st = (int) (~(
3398
+							(
3399
+								(
3400
+									($k >> 48)
3401
+										|
3402
+									($k >> 32)
3403
+										|
3404
+									($k >> 16)
3405
+										|
3406
+									$k
3407
+								) - 1
3408
+							) >> 16
3409
+						)
3410
+					) & 1;
3411
+				$i += $st;
3412
+			}
3413
+		}
3414
+
3415
+		$mask = 0;
3416
+		$tail = $xpadded_len;
3417
+		for ($i = 0; $i < $blockSize; ++$i) {
3418
+			# barrier_mask = (unsigned char)
3419
+			#     (((i ^ xpadlen) - 1U) >> ((sizeof(size_t) - 1U) * CHAR_BIT));
3420
+			$barrier_mask = (($i ^ $xpadlen) -1) >> ((PHP_INT_SIZE << 3) - 1);
3421
+			# tail[-i] = (tail[-i] & mask) | (0x80 & barrier_mask);
3422
+			$padded[$tail - $i] = ParagonIE_Sodium_Core_Util::intToChr(
3423
+				(ParagonIE_Sodium_Core_Util::chrToInt($padded[$tail - $i]) & $mask)
3424
+					|
3425
+				(0x80 & $barrier_mask)
3426
+			);
3427
+			# mask |= barrier_mask;
3428
+			$mask |= $barrier_mask;
3429
+		}
3430
+		return $padded;
3431
+	}
3432
+
3433
+	/**
3434
+	 * @param string $padded
3435
+	 * @param int $blockSize
3436
+	 * @param bool $dontFallback
3437
+	 * @return string
3438
+	 * @throws SodiumException
3439
+	 */
3440
+	public static function unpad($padded, $blockSize, $dontFallback = false)
3441
+	{
3442
+		/* Type checks: */
3443
+		ParagonIE_Sodium_Core_Util::declareScalarType($padded, 'string', 1);
3444
+		ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2);
3445
+
3446
+		$padded = (string) $padded;
3447
+		$blockSize = (int) $blockSize;
3448
+
3449
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3450
+			return (string) sodium_unpad($padded, $blockSize);
3451
+		}
3452
+		if ($blockSize <= 0) {
3453
+			throw new SodiumException('block size cannot be less than 1');
3454
+		}
3455
+		$padded_len = ParagonIE_Sodium_Core_Util::strlen($padded);
3456
+		if ($padded_len < $blockSize) {
3457
+			throw new SodiumException('invalid padding');
3458
+		}
3459
+
3460
+		# tail = &padded[padded_len - 1U];
3461
+		$tail = $padded_len - 1;
3462
+
3463
+		$acc = 0;
3464
+		$valid = 0;
3465
+		$pad_len = 0;
3466
+
3467
+		$found = 0;
3468
+		for ($i = 0; $i < $blockSize; ++$i) {
3469
+			# c = tail[-i];
3470
+			$c = ParagonIE_Sodium_Core_Util::chrToInt($padded[$tail - $i]);
3471
+
3472
+			# is_barrier =
3473
+			#     (( (acc - 1U) & (pad_len - 1U) & ((c ^ 0x80) - 1U) ) >> 8) & 1U;
3474
+			$is_barrier = (
3475
+				(
3476
+					($acc - 1) & ($pad_len - 1) & (($c ^ 80) - 1)
3477
+				) >> 7
3478
+			) & 1;
3479
+			$is_barrier &= ~$found;
3480
+			$found |= $is_barrier;
3481
+
3482
+			# acc |= c;
3483
+			$acc |= $c;
3484
+
3485
+			# pad_len |= i & (1U + ~is_barrier);
3486
+			$pad_len |= $i & (1 + ~$is_barrier);
3487
+
3488
+			# valid |= (unsigned char) is_barrier;
3489
+			$valid |= ($is_barrier & 0xff);
3490
+		}
3491
+		# unpadded_len = padded_len - 1U - pad_len;
3492
+		$unpadded_len = $padded_len - 1 - $pad_len;
3493
+		if ($valid !== 1) {
3494
+			throw new SodiumException('invalid padding');
3495
+		}
3496
+		return ParagonIE_Sodium_Core_Util::substr($padded, 0, $unpadded_len);
3497
+	}
3498
+
3499
+	/**
3500
+	 * Will sodium_compat run fast on the current hardware and PHP configuration?
3501
+	 *
3502
+	 * @return bool
3503
+	 */
3504
+	public static function polyfill_is_fast()
3505
+	{
3506
+		if (extension_loaded('sodium')) {
3507
+			return true;
3508
+		}
3509
+		if (extension_loaded('libsodium')) {
3510
+			return true;
3511
+		}
3512
+		return PHP_INT_SIZE === 8;
3513
+	}
3514
+
3515
+	/**
3516
+	 * Generate a string of bytes from the kernel's CSPRNG.
3517
+	 * Proudly uses /dev/urandom (if getrandom(2) is not available).
3518
+	 *
3519
+	 * @param int $numBytes
3520
+	 * @return string
3521
+	 * @throws Exception
3522
+	 * @throws TypeError
3523
+	 */
3524
+	public static function randombytes_buf($numBytes)
3525
+	{
3526
+		/* Type checks: */
3527
+		if (!is_int($numBytes)) {
3528
+			if (is_numeric($numBytes)) {
3529
+				$numBytes = (int) $numBytes;
3530
+			} else {
3531
+				throw new TypeError(
3532
+					'Argument 1 must be an integer, ' . gettype($numBytes) . ' given.'
3533
+				);
3534
+			}
3535
+		}
3536
+		if (self::use_fallback('randombytes_buf')) {
3537
+			return (string) call_user_func('\\Sodium\\randombytes_buf', $numBytes);
3538
+		}
3539
+		return random_bytes($numBytes);
3540
+	}
3541
+
3542
+	/**
3543
+	 * Generate an integer between 0 and $range (non-inclusive).
3544
+	 *
3545
+	 * @param int $range
3546
+	 * @return int
3547
+	 * @throws Exception
3548
+	 * @throws Error
3549
+	 * @throws TypeError
3550
+	 */
3551
+	public static function randombytes_uniform($range)
3552
+	{
3553
+		/* Type checks: */
3554
+		if (!is_int($range)) {
3555
+			if (is_numeric($range)) {
3556
+				$range = (int) $range;
3557
+			} else {
3558
+				throw new TypeError(
3559
+					'Argument 1 must be an integer, ' . gettype($range) . ' given.'
3560
+				);
3561
+			}
3562
+		}
3563
+		if (self::use_fallback('randombytes_uniform')) {
3564
+			return (int) call_user_func('\\Sodium\\randombytes_uniform', $range);
3565
+		}
3566
+		return random_int(0, $range - 1);
3567
+	}
3568
+
3569
+	/**
3570
+	 * Generate a random 16-bit integer.
3571
+	 *
3572
+	 * @return int
3573
+	 * @throws Exception
3574
+	 * @throws Error
3575
+	 * @throws TypeError
3576
+	 */
3577
+	public static function randombytes_random16()
3578
+	{
3579
+		if (self::use_fallback('randombytes_random16')) {
3580
+			return (int) call_user_func('\\Sodium\\randombytes_random16');
3581
+		}
3582
+		return random_int(0, 65535);
3583
+	}
3584
+
3585
+	/**
3586
+	 * @param string $p
3587
+	 * @param bool $dontFallback
3588
+	 * @return bool
3589
+	 * @throws SodiumException
3590
+	 */
3591
+	public static function ristretto255_is_valid_point($p, $dontFallback = false)
3592
+	{
3593
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3594
+			return sodium_crypto_core_ristretto255_is_valid_point($p);
3595
+		}
3596
+		try {
3597
+			$r = ParagonIE_Sodium_Core_Ristretto255::ristretto255_frombytes($p);
3598
+			return $r['res'] === 0 &&
3599
+				ParagonIE_Sodium_Core_Ristretto255::ristretto255_point_is_canonical($p) === 1;
3600
+		} catch (SodiumException $ex) {
3601
+			if ($ex->getMessage() === 'S is not canonical') {
3602
+				return false;
3603
+			}
3604
+			throw $ex;
3605
+		}
3606
+	}
3607
+
3608
+	/**
3609
+	 * @param string $p
3610
+	 * @param string $q
3611
+	 * @param bool $dontFallback
3612
+	 * @return string
3613
+	 * @throws SodiumException
3614
+	 */
3615
+	public static function ristretto255_add($p, $q, $dontFallback = false)
3616
+	{
3617
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3618
+			return sodium_crypto_core_ristretto255_add($p, $q);
3619
+		}
3620
+		return ParagonIE_Sodium_Core_Ristretto255::ristretto255_add($p, $q);
3621
+	}
3622
+
3623
+	/**
3624
+	 * @param string $p
3625
+	 * @param string $q
3626
+	 * @param bool $dontFallback
3627
+	 * @return string
3628
+	 * @throws SodiumException
3629
+	 */
3630
+	public static function ristretto255_sub($p, $q, $dontFallback = false)
3631
+	{
3632
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3633
+			return sodium_crypto_core_ristretto255_sub($p, $q);
3634
+		}
3635
+		return ParagonIE_Sodium_Core_Ristretto255::ristretto255_sub($p, $q);
3636
+	}
3637
+
3638
+	/**
3639
+	 * @param string $r
3640
+	 * @param bool $dontFallback
3641
+	 * @return string
3642
+	 *
3643
+	 * @throws SodiumException
3644
+	 */
3645
+	public static function ristretto255_from_hash($r, $dontFallback = false)
3646
+	{
3647
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3648
+			return sodium_crypto_core_ristretto255_from_hash($r);
3649
+		}
3650
+		return ParagonIE_Sodium_Core_Ristretto255::ristretto255_from_hash($r);
3651
+	}
3652
+
3653
+	/**
3654
+	 * @param bool $dontFallback
3655
+	 * @return string
3656
+	 *
3657
+	 * @throws SodiumException
3658
+	 */
3659
+	public static function ristretto255_random($dontFallback = false)
3660
+	{
3661
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3662
+			return sodium_crypto_core_ristretto255_random();
3663
+		}
3664
+		return ParagonIE_Sodium_Core_Ristretto255::ristretto255_random();
3665
+	}
3666
+
3667
+	/**
3668
+	 * @param bool $dontFallback
3669
+	 * @return string
3670
+	 *
3671
+	 * @throws SodiumException
3672
+	 */
3673
+	public static function ristretto255_scalar_random($dontFallback = false)
3674
+	{
3675
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3676
+			return sodium_crypto_core_ristretto255_scalar_random();
3677
+		}
3678
+		return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_random();
3679
+	}
3680
+
3681
+	/**
3682
+	 * @param string $s
3683
+	 * @param bool $dontFallback
3684
+	 * @return string
3685
+	 * @throws SodiumException
3686
+	 */
3687
+	public static function ristretto255_scalar_invert($s, $dontFallback = false)
3688
+	{
3689
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3690
+			return sodium_crypto_core_ristretto255_scalar_invert($s);
3691
+		}
3692
+		return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_invert($s);
3693
+	}
3694
+	/**
3695
+	 * @param string $s
3696
+	 * @param bool $dontFallback
3697
+	 * @return string
3698
+	 * @throws SodiumException
3699
+	 */
3700
+	public static function ristretto255_scalar_negate($s, $dontFallback = false)
3701
+	{
3702
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3703
+			return sodium_crypto_core_ristretto255_scalar_negate($s);
3704
+		}
3705
+		return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_negate($s);
3706
+	}
3707
+
3708
+	/**
3709
+	 * @param string $s
3710
+	 * @param bool $dontFallback
3711
+	 * @return string
3712
+	 * @throws SodiumException
3713
+	 */
3714
+	public static function ristretto255_scalar_complement($s, $dontFallback = false)
3715
+	{
3716
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3717
+			return sodium_crypto_core_ristretto255_scalar_complement($s);
3718
+		}
3719
+		return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_complement($s);
3720
+	}
3721
+
3722
+	/**
3723
+	 * @param string $x
3724
+	 * @param string $y
3725
+	 * @param bool $dontFallback
3726
+	 * @return string
3727
+	 * @throws SodiumException
3728
+	 */
3729
+	public static function ristretto255_scalar_add($x, $y, $dontFallback = false)
3730
+	{
3731
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3732
+			return sodium_crypto_core_ristretto255_scalar_add($x, $y);
3733
+		}
3734
+		return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_add($x, $y);
3735
+	}
3736
+
3737
+	/**
3738
+	 * @param string $x
3739
+	 * @param string $y
3740
+	 * @param bool $dontFallback
3741
+	 * @return string
3742
+	 * @throws SodiumException
3743
+	 */
3744
+	public static function ristretto255_scalar_sub($x, $y, $dontFallback = false)
3745
+	{
3746
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3747
+			return sodium_crypto_core_ristretto255_scalar_sub($x, $y);
3748
+		}
3749
+		return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_sub($x, $y);
3750
+	}
3751
+
3752
+	/**
3753
+	 * @param string $x
3754
+	 * @param string $y
3755
+	 * @param bool $dontFallback
3756
+	 * @return string
3757
+	 * @throws SodiumException
3758
+	 */
3759
+	public static function ristretto255_scalar_mul($x, $y, $dontFallback = false)
3760
+	{
3761
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3762
+			return sodium_crypto_core_ristretto255_scalar_mul($x, $y);
3763
+		}
3764
+		return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_mul($x, $y);
3765
+	}
3766
+
3767
+	/**
3768
+	 * @param string $n
3769
+	 * @param string $p
3770
+	 * @param bool $dontFallback
3771
+	 * @return string
3772
+	 * @throws SodiumException
3773
+	 */
3774
+	public static function scalarmult_ristretto255($n, $p, $dontFallback = false)
3775
+	{
3776
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3777
+			return sodium_crypto_scalarmult_ristretto255($n, $p);
3778
+		}
3779
+		return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255($n, $p);
3780
+	}
3781
+
3782
+	/**
3783
+	 * @param string $n
3784
+	 * @param string $p
3785
+	 * @param bool $dontFallback
3786
+	 * @return string
3787
+	 * @throws SodiumException
3788
+	 */
3789
+	public static function scalarmult_ristretto255_base($n, $dontFallback = false)
3790
+	{
3791
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3792
+			return sodium_crypto_scalarmult_ristretto255_base($n);
3793
+		}
3794
+		return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255_base($n);
3795
+	}
3796
+
3797
+	/**
3798
+	 * @param string $s
3799
+	 * @param bool $dontFallback
3800
+	 * @return string
3801
+	 * @throws SodiumException
3802
+	 */
3803
+	public static function ristretto255_scalar_reduce($s, $dontFallback = false)
3804
+	{
3805
+		if (self::useNewSodiumAPI() && !$dontFallback) {
3806
+			return sodium_crypto_core_ristretto255_scalar_reduce($s);
3807
+		}
3808
+		return ParagonIE_Sodium_Core_Ristretto255::sc_reduce($s);
3809
+	}
3810
+
3811
+	/**
3812
+	 * Runtime testing method for 32-bit platforms.
3813
+	 *
3814
+	 * Usage: If runtime_speed_test() returns FALSE, then our 32-bit
3815
+	 *        implementation is to slow to use safely without risking timeouts.
3816
+	 *        If this happens, install sodium from PECL to get acceptable
3817
+	 *        performance.
3818
+	 *
3819
+	 * @param int $iterations Number of multiplications to attempt
3820
+	 * @param int $maxTimeout Milliseconds
3821
+	 * @return bool           TRUE if we're fast enough, FALSE is not
3822
+	 * @throws SodiumException
3823
+	 */
3824
+	public static function runtime_speed_test($iterations, $maxTimeout)
3825
+	{
3826
+		if (self::polyfill_is_fast()) {
3827
+			return true;
3828
+		}
3829
+		/** @var float $end */
3830
+		$end = 0.0;
3831
+		/** @var float $start */
3832
+		$start = microtime(true);
3833
+		/** @var ParagonIE_Sodium_Core32_Int64 $a */
3834
+		$a = ParagonIE_Sodium_Core32_Int64::fromInt(random_int(3, 1 << 16));
3835
+		for ($i = 0; $i < $iterations; ++$i) {
3836
+			/** @var ParagonIE_Sodium_Core32_Int64 $b */
3837
+			$b = ParagonIE_Sodium_Core32_Int64::fromInt(random_int(3, 1 << 16));
3838
+			$a->mulInt64($b);
3839
+		}
3840
+		/** @var float $end */
3841
+		$end = microtime(true);
3842
+		/** @var int $diff */
3843
+		$diff = (int) ceil(($end - $start) * 1000);
3844
+		return $diff < $maxTimeout;
3845
+	}
3846
+
3847
+	/**
3848
+	 * Add two numbers (little-endian unsigned), storing the value in the first
3849
+	 * parameter.
3850
+	 *
3851
+	 * This mutates $val.
3852
+	 *
3853
+	 * @param string $val
3854
+	 * @param string $addv
3855
+	 * @return void
3856
+	 * @throws SodiumException
3857
+	 */
3858
+	public static function sub(&$val, $addv)
3859
+	{
3860
+		$val_len = ParagonIE_Sodium_Core_Util::strlen($val);
3861
+		$addv_len = ParagonIE_Sodium_Core_Util::strlen($addv);
3862
+		if ($val_len !== $addv_len) {
3863
+			throw new SodiumException('values must have the same length');
3864
+		}
3865
+		$A = ParagonIE_Sodium_Core_Util::stringToIntArray($val);
3866
+		$B = ParagonIE_Sodium_Core_Util::stringToIntArray($addv);
3867
+
3868
+		$c = 0;
3869
+		for ($i = 0; $i < $val_len; $i++) {
3870
+			$c = ($A[$i] - $B[$i] - $c);
3871
+			$A[$i] = ($c & 0xff);
3872
+			$c = ($c >> 8) & 1;
3873
+		}
3874
+		$val = ParagonIE_Sodium_Core_Util::intArrayToString($A);
3875
+	}
3876
+
3877
+	/**
3878
+	 * This emulates libsodium's version_string() function, except ours is
3879
+	 * prefixed with 'polyfill-'.
3880
+	 *
3881
+	 * @return string
3882
+	 * @psalm-suppress MixedInferredReturnType
3883
+	 * @psalm-suppress UndefinedFunction
3884
+	 */
3885
+	public static function version_string()
3886
+	{
3887
+		if (self::useNewSodiumAPI()) {
3888
+			return (string) sodium_version_string();
3889
+		}
3890
+		if (self::use_fallback('version_string')) {
3891
+			return (string) call_user_func('\\Sodium\\version_string');
3892
+		}
3893
+		return (string) self::VERSION_STRING;
3894
+	}
3895
+
3896
+	/**
3897
+	 * Should we use the libsodium core function instead?
3898
+	 * This is always a good idea, if it's available. (Unless we're in the
3899
+	 * middle of running our unit test suite.)
3900
+	 *
3901
+	 * If ext/libsodium is available, use it. Return TRUE.
3902
+	 * Otherwise, we have to use the code provided herein. Return FALSE.
3903
+	 *
3904
+	 * @param string $sodium_func_name
3905
+	 *
3906
+	 * @return bool
3907
+	 */
3908
+	protected static function use_fallback($sodium_func_name = '')
3909
+	{
3910
+		static $res = null;
3911
+		if ($res === null) {
3912
+			$res = extension_loaded('libsodium') && PHP_VERSION_ID >= 50300;
3913
+		}
3914
+		if ($res === false) {
3915
+			// No libsodium installed
3916
+			return false;
3917
+		}
3918
+		if (self::$disableFallbackForUnitTests) {
3919
+			// Don't fallback. Use the PHP implementation.
3920
+			return false;
3921
+		}
3922
+		if (!empty($sodium_func_name)) {
3923
+			return is_callable('\\Sodium\\' . $sodium_func_name);
3924
+		}
3925
+		return true;
3926
+	}
3927
+
3928
+	/**
3929
+	 * Libsodium as implemented in PHP 7.2
3930
+	 * and/or ext/sodium (via PECL)
3931
+	 *
3932
+	 * @ref https://wiki.php.net/rfc/libsodium
3933
+	 * @return bool
3934
+	 */
3935
+	protected static function useNewSodiumAPI()
3936
+	{
3937
+		static $res = null;
3938
+		if ($res === null) {
3939
+			$res = PHP_VERSION_ID >= 70000 && extension_loaded('sodium');
3940
+		}
3941
+		if (self::$disableFallbackForUnitTests) {
3942
+			// Don't fallback. Use the PHP implementation.
3943
+			return false;
3944
+		}
3945
+		return (bool) $res;
3946
+	}
3947 3947
 }
Please login to merge, or discard this patch.
Spacing   +1080 added lines, -1080 removed lines patch added patch discarded remove patch
@@ -21,7 +21,7 @@  discard block
 block discarded – undo
21 21
  * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION * CAUTION *
22 22
  */
23 23
 
24
-if (class_exists('ParagonIE_Sodium_Compat', false)) {
24
+if ( class_exists( 'ParagonIE_Sodium_Compat', false ) ) {
25 25
     return;
26 26
 }
27 27
 
@@ -155,23 +155,23 @@  discard block
 block discarded – undo
155 155
      * @return void
156 156
      * @throws SodiumException
157 157
      */
158
-    public static function add(&$val, $addv)
158
+    public static function add( &$val, $addv )
159 159
     {
160
-        $val_len = ParagonIE_Sodium_Core_Util::strlen($val);
161
-        $addv_len = ParagonIE_Sodium_Core_Util::strlen($addv);
162
-        if ($val_len !== $addv_len) {
163
-            throw new SodiumException('values must have the same length');
160
+        $val_len = ParagonIE_Sodium_Core_Util::strlen( $val );
161
+        $addv_len = ParagonIE_Sodium_Core_Util::strlen( $addv );
162
+        if ( $val_len !== $addv_len ) {
163
+            throw new SodiumException( 'values must have the same length' );
164 164
         }
165
-        $A = ParagonIE_Sodium_Core_Util::stringToIntArray($val);
166
-        $B = ParagonIE_Sodium_Core_Util::stringToIntArray($addv);
165
+        $A = ParagonIE_Sodium_Core_Util::stringToIntArray( $val );
166
+        $B = ParagonIE_Sodium_Core_Util::stringToIntArray( $addv );
167 167
 
168 168
         $c = 0;
169
-        for ($i = 0; $i < $val_len; $i++) {
170
-            $c += ($A[$i] + $B[$i]);
171
-            $A[$i] = ($c & 0xff);
169
+        for ( $i = 0; $i < $val_len; $i++ ) {
170
+            $c += ( $A[ $i ] + $B[ $i ] );
171
+            $A[ $i ] = ( $c & 0xff );
172 172
             $c >>= 8;
173 173
         }
174
-        $val = ParagonIE_Sodium_Core_Util::intArrayToString($A);
174
+        $val = ParagonIE_Sodium_Core_Util::intArrayToString( $A );
175 175
     }
176 176
 
177 177
     /**
@@ -181,40 +181,40 @@  discard block
 block discarded – undo
181 181
      * @return string
182 182
      * @throws SodiumException
183 183
      */
184
-    public static function base642bin($encoded, $variant, $ignore = '')
184
+    public static function base642bin( $encoded, $variant, $ignore = '' )
185 185
     {
186 186
         /* Type checks: */
187
-        ParagonIE_Sodium_Core_Util::declareScalarType($encoded, 'string', 1);
187
+        ParagonIE_Sodium_Core_Util::declareScalarType( $encoded, 'string', 1 );
188 188
 
189 189
         /** @var string $encoded */
190
-        $encoded = (string) $encoded;
191
-        if (ParagonIE_Sodium_Core_Util::strlen($encoded) === 0) {
190
+        $encoded = (string)$encoded;
191
+        if ( ParagonIE_Sodium_Core_Util::strlen( $encoded ) === 0 ) {
192 192
             return '';
193 193
         }
194 194
 
195 195
         // Just strip before decoding
196
-        if (!empty($ignore)) {
197
-            $encoded = str_replace($ignore, '', $encoded);
196
+        if ( ! empty( $ignore ) ) {
197
+            $encoded = str_replace( $ignore, '', $encoded );
198 198
         }
199 199
 
200 200
         try {
201
-            switch ($variant) {
201
+            switch ( $variant ) {
202 202
                 case self::BASE64_VARIANT_ORIGINAL:
203
-                    return ParagonIE_Sodium_Core_Base64_Original::decode($encoded, true);
203
+                    return ParagonIE_Sodium_Core_Base64_Original::decode( $encoded, true );
204 204
                 case self::BASE64_VARIANT_ORIGINAL_NO_PADDING:
205
-                    return ParagonIE_Sodium_Core_Base64_Original::decode($encoded, false);
205
+                    return ParagonIE_Sodium_Core_Base64_Original::decode( $encoded, false );
206 206
                 case self::BASE64_VARIANT_URLSAFE:
207
-                    return ParagonIE_Sodium_Core_Base64_UrlSafe::decode($encoded, true);
207
+                    return ParagonIE_Sodium_Core_Base64_UrlSafe::decode( $encoded, true );
208 208
                 case self::BASE64_VARIANT_URLSAFE_NO_PADDING:
209
-                    return ParagonIE_Sodium_Core_Base64_UrlSafe::decode($encoded, false);
209
+                    return ParagonIE_Sodium_Core_Base64_UrlSafe::decode( $encoded, false );
210 210
                 default:
211
-                    throw new SodiumException('invalid base64 variant identifier');
211
+                    throw new SodiumException( 'invalid base64 variant identifier' );
212 212
             }
213
-        } catch (Exception $ex) {
214
-            if ($ex instanceof SodiumException) {
213
+        } catch ( Exception $ex ) {
214
+            if ( $ex instanceof SodiumException ) {
215 215
                 throw $ex;
216 216
             }
217
-            throw new SodiumException('invalid base64 string');
217
+            throw new SodiumException( 'invalid base64 string' );
218 218
         }
219 219
     }
220 220
 
@@ -224,27 +224,27 @@  discard block
 block discarded – undo
224 224
      * @return string
225 225
      * @throws SodiumException
226 226
      */
227
-    public static function bin2base64($decoded, $variant)
227
+    public static function bin2base64( $decoded, $variant )
228 228
     {
229 229
         /* Type checks: */
230
-        ParagonIE_Sodium_Core_Util::declareScalarType($decoded, 'string', 1);
230
+        ParagonIE_Sodium_Core_Util::declareScalarType( $decoded, 'string', 1 );
231 231
         /** @var string $decoded */
232
-        $decoded = (string) $decoded;
233
-        if (ParagonIE_Sodium_Core_Util::strlen($decoded) === 0) {
232
+        $decoded = (string)$decoded;
233
+        if ( ParagonIE_Sodium_Core_Util::strlen( $decoded ) === 0 ) {
234 234
             return '';
235 235
         }
236 236
 
237
-        switch ($variant) {
237
+        switch ( $variant ) {
238 238
             case self::BASE64_VARIANT_ORIGINAL:
239
-                return ParagonIE_Sodium_Core_Base64_Original::encode($decoded);
239
+                return ParagonIE_Sodium_Core_Base64_Original::encode( $decoded );
240 240
             case self::BASE64_VARIANT_ORIGINAL_NO_PADDING:
241
-                return ParagonIE_Sodium_Core_Base64_Original::encodeUnpadded($decoded);
241
+                return ParagonIE_Sodium_Core_Base64_Original::encodeUnpadded( $decoded );
242 242
             case self::BASE64_VARIANT_URLSAFE:
243
-                return ParagonIE_Sodium_Core_Base64_UrlSafe::encode($decoded);
243
+                return ParagonIE_Sodium_Core_Base64_UrlSafe::encode( $decoded );
244 244
             case self::BASE64_VARIANT_URLSAFE_NO_PADDING:
245
-                return ParagonIE_Sodium_Core_Base64_UrlSafe::encodeUnpadded($decoded);
245
+                return ParagonIE_Sodium_Core_Base64_UrlSafe::encodeUnpadded( $decoded );
246 246
             default:
247
-                throw new SodiumException('invalid base64 variant identifier');
247
+                throw new SodiumException( 'invalid base64 variant identifier' );
248 248
         }
249 249
     }
250 250
 
@@ -257,18 +257,18 @@  discard block
 block discarded – undo
257 257
      * @throws TypeError
258 258
      * @psalm-suppress MixedArgument
259 259
      */
260
-    public static function bin2hex($string)
260
+    public static function bin2hex( $string )
261 261
     {
262 262
         /* Type checks: */
263
-        ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1);
263
+        ParagonIE_Sodium_Core_Util::declareScalarType( $string, 'string', 1 );
264 264
 
265
-        if (self::useNewSodiumAPI()) {
266
-            return (string) sodium_bin2hex($string);
265
+        if ( self::useNewSodiumAPI() ) {
266
+            return (string)sodium_bin2hex( $string );
267 267
         }
268
-        if (self::use_fallback('bin2hex')) {
269
-            return (string) call_user_func('\\Sodium\\bin2hex', $string);
268
+        if ( self::use_fallback( 'bin2hex' ) ) {
269
+            return (string)call_user_func( '\\Sodium\\bin2hex', $string );
270 270
         }
271
-        return ParagonIE_Sodium_Core_Util::bin2hex($string);
271
+        return ParagonIE_Sodium_Core_Util::bin2hex( $string );
272 272
     }
273 273
 
274 274
     /**
@@ -284,19 +284,19 @@  discard block
 block discarded – undo
284 284
      * @throws TypeError
285 285
      * @psalm-suppress MixedArgument
286 286
      */
287
-    public static function compare($left, $right)
287
+    public static function compare( $left, $right )
288 288
     {
289 289
         /* Type checks: */
290
-        ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1);
291
-        ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2);
290
+        ParagonIE_Sodium_Core_Util::declareScalarType( $left, 'string', 1 );
291
+        ParagonIE_Sodium_Core_Util::declareScalarType( $right, 'string', 2 );
292 292
 
293
-        if (self::useNewSodiumAPI()) {
294
-            return (int) sodium_compare($left, $right);
293
+        if ( self::useNewSodiumAPI() ) {
294
+            return (int)sodium_compare( $left, $right );
295 295
         }
296
-        if (self::use_fallback('compare')) {
297
-            return (int) call_user_func('\\Sodium\\compare', $left, $right);
296
+        if ( self::use_fallback( 'compare' ) ) {
297
+            return (int)call_user_func( '\\Sodium\\compare', $left, $right );
298 298
         }
299
-        return ParagonIE_Sodium_Core_Util::compare($left, $right);
299
+        return ParagonIE_Sodium_Core_Util::compare( $left, $right );
300 300
     }
301 301
 
302 302
     /**
@@ -309,21 +309,21 @@  discard block
 block discarded – undo
309 309
      */
310 310
     public static function crypto_aead_aes256gcm_is_available()
311 311
     {
312
-        if (self::useNewSodiumAPI()) {
312
+        if ( self::useNewSodiumAPI() ) {
313 313
             return sodium_crypto_aead_aes256gcm_is_available();
314 314
         }
315
-        if (self::use_fallback('crypto_aead_aes256gcm_is_available')) {
316
-            return call_user_func('\\Sodium\\crypto_aead_aes256gcm_is_available');
315
+        if ( self::use_fallback( 'crypto_aead_aes256gcm_is_available' ) ) {
316
+            return call_user_func( '\\Sodium\\crypto_aead_aes256gcm_is_available' );
317 317
         }
318
-        if (PHP_VERSION_ID < 70100) {
318
+        if ( PHP_VERSION_ID < 70100 ) {
319 319
             // OpenSSL doesn't support AEAD before 7.1.0
320 320
             return false;
321 321
         }
322
-        if (!is_callable('openssl_encrypt') || !is_callable('openssl_decrypt')) {
322
+        if ( ! is_callable( 'openssl_encrypt' ) || ! is_callable( 'openssl_decrypt' ) ) {
323 323
             // OpenSSL isn't installed
324 324
             return false;
325 325
         }
326
-        return (bool) in_array('aes-256-gcm', openssl_get_cipher_methods());
326
+        return (bool)in_array( 'aes-256-gcm', openssl_get_cipher_methods() );
327 327
     }
328 328
 
329 329
     /**
@@ -353,32 +353,32 @@  discard block
 block discarded – undo
353 353
         $nonce = '',
354 354
         $key = ''
355 355
     ) {
356
-        if (!self::crypto_aead_aes256gcm_is_available()) {
357
-            throw new SodiumException('AES-256-GCM is not available');
356
+        if ( ! self::crypto_aead_aes256gcm_is_available() ) {
357
+            throw new SodiumException( 'AES-256-GCM is not available' );
358 358
         }
359
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
360
-        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
361
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
362
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
359
+        ParagonIE_Sodium_Core_Util::declareScalarType( $ciphertext, 'string', 1 );
360
+        ParagonIE_Sodium_Core_Util::declareScalarType( $assocData, 'string', 2 );
361
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 3 );
362
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 4 );
363 363
 
364 364
         /* Input validation: */
365
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AES256GCM_NPUBBYTES) {
366
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_AES256GCM_NPUBBYTES long');
365
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_AEAD_AES256GCM_NPUBBYTES ) {
366
+            throw new SodiumException( 'Nonce must be CRYPTO_AEAD_AES256GCM_NPUBBYTES long' );
367 367
         }
368
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AES256GCM_KEYBYTES) {
369
-            throw new SodiumException('Key must be CRYPTO_AEAD_AES256GCM_KEYBYTES long');
368
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_AEAD_AES256GCM_KEYBYTES ) {
369
+            throw new SodiumException( 'Key must be CRYPTO_AEAD_AES256GCM_KEYBYTES long' );
370 370
         }
371
-        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_AES256GCM_ABYTES) {
372
-            throw new SodiumException('Message must be at least CRYPTO_AEAD_AES256GCM_ABYTES long');
371
+        if ( ParagonIE_Sodium_Core_Util::strlen( $ciphertext ) < self::CRYPTO_AEAD_AES256GCM_ABYTES ) {
372
+            throw new SodiumException( 'Message must be at least CRYPTO_AEAD_AES256GCM_ABYTES long' );
373 373
         }
374
-        if (!is_callable('openssl_decrypt')) {
375
-            throw new SodiumException('The OpenSSL extension is not installed, or openssl_decrypt() is not available');
374
+        if ( ! is_callable( 'openssl_decrypt' ) ) {
375
+            throw new SodiumException( 'The OpenSSL extension is not installed, or openssl_decrypt() is not available' );
376 376
         }
377 377
 
378 378
         /** @var string $ctext */
379
-        $ctext = ParagonIE_Sodium_Core_Util::substr($ciphertext, 0, -self::CRYPTO_AEAD_AES256GCM_ABYTES);
379
+        $ctext = ParagonIE_Sodium_Core_Util::substr( $ciphertext, 0, -self::CRYPTO_AEAD_AES256GCM_ABYTES );
380 380
         /** @var string $authTag */
381
-        $authTag = ParagonIE_Sodium_Core_Util::substr($ciphertext, -self::CRYPTO_AEAD_AES256GCM_ABYTES, 16);
381
+        $authTag = ParagonIE_Sodium_Core_Util::substr( $ciphertext, -self::CRYPTO_AEAD_AES256GCM_ABYTES, 16 );
382 382
         return openssl_decrypt(
383 383
             $ctext,
384 384
             'aes-256-gcm',
@@ -413,24 +413,24 @@  discard block
 block discarded – undo
413 413
         $nonce = '',
414 414
         $key = ''
415 415
     ) {
416
-        if (!self::crypto_aead_aes256gcm_is_available()) {
417
-            throw new SodiumException('AES-256-GCM is not available');
416
+        if ( ! self::crypto_aead_aes256gcm_is_available() ) {
417
+            throw new SodiumException( 'AES-256-GCM is not available' );
418 418
         }
419
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
420
-        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
421
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
422
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
419
+        ParagonIE_Sodium_Core_Util::declareScalarType( $plaintext, 'string', 1 );
420
+        ParagonIE_Sodium_Core_Util::declareScalarType( $assocData, 'string', 2 );
421
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 3 );
422
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 4 );
423 423
 
424 424
         /* Input validation: */
425
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_AES256GCM_NPUBBYTES) {
426
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_AES256GCM_NPUBBYTES long');
425
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_AEAD_AES256GCM_NPUBBYTES ) {
426
+            throw new SodiumException( 'Nonce must be CRYPTO_AEAD_AES256GCM_NPUBBYTES long' );
427 427
         }
428
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_AES256GCM_KEYBYTES) {
429
-            throw new SodiumException('Key must be CRYPTO_AEAD_AES256GCM_KEYBYTES long');
428
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_AEAD_AES256GCM_KEYBYTES ) {
429
+            throw new SodiumException( 'Key must be CRYPTO_AEAD_AES256GCM_KEYBYTES long' );
430 430
         }
431 431
 
432
-        if (!is_callable('openssl_encrypt')) {
433
-            throw new SodiumException('The OpenSSL extension is not installed, or openssl_encrypt() is not available');
432
+        if ( ! is_callable( 'openssl_encrypt' ) ) {
433
+            throw new SodiumException( 'The OpenSSL extension is not installed, or openssl_encrypt() is not available' );
434 434
         }
435 435
 
436 436
         $authTag = '';
@@ -456,7 +456,7 @@  discard block
 block discarded – undo
456 456
      */
457 457
     public static function crypto_aead_aes256gcm_keygen()
458 458
     {
459
-        return random_bytes(self::CRYPTO_AEAD_AES256GCM_KEYBYTES);
459
+        return random_bytes( self::CRYPTO_AEAD_AES256GCM_KEYBYTES );
460 460
     }
461 461
 
462 462
     /**
@@ -487,23 +487,23 @@  discard block
 block discarded – undo
487 487
         $key = ''
488 488
     ) {
489 489
         /* Type checks: */
490
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
491
-        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
492
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
493
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
490
+        ParagonIE_Sodium_Core_Util::declareScalarType( $ciphertext, 'string', 1 );
491
+        ParagonIE_Sodium_Core_Util::declareScalarType( $assocData, 'string', 2 );
492
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 3 );
493
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 4 );
494 494
 
495 495
         /* Input validation: */
496
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES) {
497
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES long');
496
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES ) {
497
+            throw new SodiumException( 'Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES long' );
498 498
         }
499
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
500
-            throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
499
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES ) {
500
+            throw new SodiumException( 'Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long' );
501 501
         }
502
-        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES) {
503
-            throw new SodiumException('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_ABYTES long');
502
+        if ( ParagonIE_Sodium_Core_Util::strlen( $ciphertext ) < self::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES ) {
503
+            throw new SodiumException( 'Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_ABYTES long' );
504 504
         }
505 505
 
506
-        if (self::useNewSodiumAPI()) {
506
+        if ( self::useNewSodiumAPI() ) {
507 507
             /**
508 508
              * @psalm-suppress InvalidReturnStatement
509 509
              * @psalm-suppress FalsableReturnStatement
@@ -515,7 +515,7 @@  discard block
 block discarded – undo
515 515
                 $key
516 516
             );
517 517
         }
518
-        if (self::use_fallback('crypto_aead_chacha20poly1305_decrypt')) {
518
+        if ( self::use_fallback( 'crypto_aead_chacha20poly1305_decrypt' ) ) {
519 519
             return call_user_func(
520 520
                 '\\Sodium\\crypto_aead_chacha20poly1305_decrypt',
521 521
                 $ciphertext,
@@ -524,7 +524,7 @@  discard block
 block discarded – undo
524 524
                 $key
525 525
             );
526 526
         }
527
-        if (PHP_INT_SIZE === 4) {
527
+        if ( PHP_INT_SIZE === 4 ) {
528 528
             return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_decrypt(
529 529
                 $ciphertext,
530 530
                 $assocData,
@@ -567,29 +567,29 @@  discard block
 block discarded – undo
567 567
         $key = ''
568 568
     ) {
569 569
         /* Type checks: */
570
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
571
-        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
572
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
573
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
570
+        ParagonIE_Sodium_Core_Util::declareScalarType( $plaintext, 'string', 1 );
571
+        ParagonIE_Sodium_Core_Util::declareScalarType( $assocData, 'string', 2 );
572
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 3 );
573
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 4 );
574 574
 
575 575
         /* Input validation: */
576
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES) {
577
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES long');
576
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES ) {
577
+            throw new SodiumException( 'Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES long' );
578 578
         }
579
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
580
-            throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
579
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES ) {
580
+            throw new SodiumException( 'Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long' );
581 581
         }
582 582
 
583
-        if (self::useNewSodiumAPI()) {
584
-            return (string) sodium_crypto_aead_chacha20poly1305_encrypt(
583
+        if ( self::useNewSodiumAPI() ) {
584
+            return (string)sodium_crypto_aead_chacha20poly1305_encrypt(
585 585
                 $plaintext,
586 586
                 $assocData,
587 587
                 $nonce,
588 588
                 $key
589 589
             );
590 590
         }
591
-        if (self::use_fallback('crypto_aead_chacha20poly1305_encrypt')) {
592
-            return (string) call_user_func(
591
+        if ( self::use_fallback( 'crypto_aead_chacha20poly1305_encrypt' ) ) {
592
+            return (string)call_user_func(
593 593
                 '\\Sodium\\crypto_aead_chacha20poly1305_encrypt',
594 594
                 $plaintext,
595 595
                 $assocData,
@@ -597,7 +597,7 @@  discard block
 block discarded – undo
597 597
                 $key
598 598
             );
599 599
         }
600
-        if (PHP_INT_SIZE === 4) {
600
+        if ( PHP_INT_SIZE === 4 ) {
601 601
             return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_encrypt(
602 602
                 $plaintext,
603 603
                 $assocData,
@@ -641,23 +641,23 @@  discard block
 block discarded – undo
641 641
         $key = ''
642 642
     ) {
643 643
         /* Type checks: */
644
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
645
-        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
646
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
647
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
644
+        ParagonIE_Sodium_Core_Util::declareScalarType( $ciphertext, 'string', 1 );
645
+        ParagonIE_Sodium_Core_Util::declareScalarType( $assocData, 'string', 2 );
646
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 3 );
647
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 4 );
648 648
 
649 649
         /* Input validation: */
650
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES) {
651
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long');
650
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES ) {
651
+            throw new SodiumException( 'Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long' );
652 652
         }
653
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
654
-            throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
653
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES ) {
654
+            throw new SodiumException( 'Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long' );
655 655
         }
656
-        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES) {
657
-            throw new SodiumException('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_ABYTES long');
656
+        if ( ParagonIE_Sodium_Core_Util::strlen( $ciphertext ) < self::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES ) {
657
+            throw new SodiumException( 'Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_ABYTES long' );
658 658
         }
659 659
 
660
-        if (self::useNewSodiumAPI()) {
660
+        if ( self::useNewSodiumAPI() ) {
661 661
             /**
662 662
              * @psalm-suppress InvalidReturnStatement
663 663
              * @psalm-suppress FalsableReturnStatement
@@ -669,7 +669,7 @@  discard block
 block discarded – undo
669 669
                 $key
670 670
             );
671 671
         }
672
-        if (self::use_fallback('crypto_aead_chacha20poly1305_ietf_decrypt')) {
672
+        if ( self::use_fallback( 'crypto_aead_chacha20poly1305_ietf_decrypt' ) ) {
673 673
             return call_user_func(
674 674
                 '\\Sodium\\crypto_aead_chacha20poly1305_ietf_decrypt',
675 675
                 $ciphertext,
@@ -678,7 +678,7 @@  discard block
 block discarded – undo
678 678
                 $key
679 679
             );
680 680
         }
681
-        if (PHP_INT_SIZE === 4) {
681
+        if ( PHP_INT_SIZE === 4 ) {
682 682
             return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_ietf_decrypt(
683 683
                 $ciphertext,
684 684
                 $assocData,
@@ -704,7 +704,7 @@  discard block
 block discarded – undo
704 704
      */
705 705
     public static function crypto_aead_chacha20poly1305_keygen()
706 706
     {
707
-        return random_bytes(self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES);
707
+        return random_bytes( self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES );
708 708
     }
709 709
 
710 710
     /**
@@ -734,31 +734,31 @@  discard block
 block discarded – undo
734 734
         $key = ''
735 735
     ) {
736 736
         /* Type checks: */
737
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
738
-        if (!is_null($assocData)) {
739
-            ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
737
+        ParagonIE_Sodium_Core_Util::declareScalarType( $plaintext, 'string', 1 );
738
+        if ( ! is_null( $assocData ) ) {
739
+            ParagonIE_Sodium_Core_Util::declareScalarType( $assocData, 'string', 2 );
740 740
         }
741
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
742
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
741
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 3 );
742
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 4 );
743 743
 
744 744
         /* Input validation: */
745
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES) {
746
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long');
745
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES ) {
746
+            throw new SodiumException( 'Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long' );
747 747
         }
748
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
749
-            throw new SodiumException('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
748
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES ) {
749
+            throw new SodiumException( 'Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long' );
750 750
         }
751 751
 
752
-        if (self::useNewSodiumAPI()) {
753
-            return (string) sodium_crypto_aead_chacha20poly1305_ietf_encrypt(
752
+        if ( self::useNewSodiumAPI() ) {
753
+            return (string)sodium_crypto_aead_chacha20poly1305_ietf_encrypt(
754 754
                 $plaintext,
755 755
                 $assocData,
756 756
                 $nonce,
757 757
                 $key
758 758
             );
759 759
         }
760
-        if (self::use_fallback('crypto_aead_chacha20poly1305_ietf_encrypt')) {
761
-            return (string) call_user_func(
760
+        if ( self::use_fallback( 'crypto_aead_chacha20poly1305_ietf_encrypt' ) ) {
761
+            return (string)call_user_func(
762 762
                 '\\Sodium\\crypto_aead_chacha20poly1305_ietf_encrypt',
763 763
                 $plaintext,
764 764
                 $assocData,
@@ -766,7 +766,7 @@  discard block
 block discarded – undo
766 766
                 $key
767 767
             );
768 768
         }
769
-        if (PHP_INT_SIZE === 4) {
769
+        if ( PHP_INT_SIZE === 4 ) {
770 770
             return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_ietf_encrypt(
771 771
                 $plaintext,
772 772
                 $assocData,
@@ -792,7 +792,7 @@  discard block
 block discarded – undo
792 792
      */
793 793
     public static function crypto_aead_chacha20poly1305_ietf_keygen()
794 794
     {
795
-        return random_bytes(self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES);
795
+        return random_bytes( self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES );
796 796
     }
797 797
 
798 798
     /**
@@ -823,27 +823,27 @@  discard block
 block discarded – undo
823 823
         $dontFallback = false
824 824
     ) {
825 825
         /* Type checks: */
826
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
827
-        if (!is_null($assocData)) {
828
-            ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
826
+        ParagonIE_Sodium_Core_Util::declareScalarType( $ciphertext, 'string', 1 );
827
+        if ( ! is_null( $assocData ) ) {
828
+            ParagonIE_Sodium_Core_Util::declareScalarType( $assocData, 'string', 2 );
829 829
         } else {
830 830
             $assocData = '';
831 831
         }
832
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
833
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
832
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 3 );
833
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 4 );
834 834
 
835 835
         /* Input validation: */
836
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES) {
837
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES long');
836
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES ) {
837
+            throw new SodiumException( 'Nonce must be CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES long' );
838 838
         }
839
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES) {
840
-            throw new SodiumException('Key must be CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES long');
839
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES ) {
840
+            throw new SodiumException( 'Key must be CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES long' );
841 841
         }
842
-        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES) {
843
-            throw new SodiumException('Message must be at least CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES long');
842
+        if ( ParagonIE_Sodium_Core_Util::strlen( $ciphertext ) < self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES ) {
843
+            throw new SodiumException( 'Message must be at least CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES long' );
844 844
         }
845
-        if (self::useNewSodiumAPI() && !$dontFallback) {
846
-            if (is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_decrypt')) {
845
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
846
+            if ( is_callable( 'sodium_crypto_aead_xchacha20poly1305_ietf_decrypt' ) ) {
847 847
                 return sodium_crypto_aead_xchacha20poly1305_ietf_decrypt(
848 848
                     $ciphertext,
849 849
                     $assocData,
@@ -853,7 +853,7 @@  discard block
 block discarded – undo
853 853
             }
854 854
         }
855 855
 
856
-        if (PHP_INT_SIZE === 4) {
856
+        if ( PHP_INT_SIZE === 4 ) {
857 857
             return ParagonIE_Sodium_Crypto32::aead_xchacha20poly1305_ietf_decrypt(
858 858
                 $ciphertext,
859 859
                 $assocData,
@@ -898,24 +898,24 @@  discard block
 block discarded – undo
898 898
         $dontFallback = false
899 899
     ) {
900 900
         /* Type checks: */
901
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
902
-        if (!is_null($assocData)) {
903
-            ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
901
+        ParagonIE_Sodium_Core_Util::declareScalarType( $plaintext, 'string', 1 );
902
+        if ( ! is_null( $assocData ) ) {
903
+            ParagonIE_Sodium_Core_Util::declareScalarType( $assocData, 'string', 2 );
904 904
         } else {
905 905
             $assocData = '';
906 906
         }
907
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
908
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
907
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 3 );
908
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 4 );
909 909
 
910 910
         /* Input validation: */
911
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES) {
912
-            throw new SodiumException('Nonce must be CRYPTO_AEAD_XCHACHA20POLY1305_NPUBBYTES long');
911
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES ) {
912
+            throw new SodiumException( 'Nonce must be CRYPTO_AEAD_XCHACHA20POLY1305_NPUBBYTES long' );
913 913
         }
914
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES) {
915
-            throw new SodiumException('Key must be CRYPTO_AEAD_XCHACHA20POLY1305_KEYBYTES long');
914
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES ) {
915
+            throw new SodiumException( 'Key must be CRYPTO_AEAD_XCHACHA20POLY1305_KEYBYTES long' );
916 916
         }
917
-        if (self::useNewSodiumAPI() && !$dontFallback) {
918
-            if (is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')) {
917
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
918
+            if ( is_callable( 'sodium_crypto_aead_xchacha20poly1305_ietf_encrypt' ) ) {
919 919
                 return sodium_crypto_aead_xchacha20poly1305_ietf_encrypt(
920 920
                     $plaintext,
921 921
                     $assocData,
@@ -925,7 +925,7 @@  discard block
 block discarded – undo
925 925
             }
926 926
         }
927 927
 
928
-        if (PHP_INT_SIZE === 4) {
928
+        if ( PHP_INT_SIZE === 4 ) {
929 929
             return ParagonIE_Sodium_Crypto32::aead_xchacha20poly1305_ietf_encrypt(
930 930
                 $plaintext,
931 931
                 $assocData,
@@ -951,7 +951,7 @@  discard block
 block discarded – undo
951 951
      */
952 952
     public static function crypto_aead_xchacha20poly1305_ietf_keygen()
953 953
     {
954
-        return random_bytes(self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES);
954
+        return random_bytes( self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES );
955 955
     }
956 956
 
957 957
     /**
@@ -971,27 +971,27 @@  discard block
 block discarded – undo
971 971
      * @throws TypeError
972 972
      * @psalm-suppress MixedArgument
973 973
      */
974
-    public static function crypto_auth($message, $key)
974
+    public static function crypto_auth( $message, $key )
975 975
     {
976 976
         /* Type checks: */
977
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
978
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2);
977
+        ParagonIE_Sodium_Core_Util::declareScalarType( $message, 'string', 1 );
978
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 2 );
979 979
 
980 980
         /* Input validation: */
981
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AUTH_KEYBYTES) {
982
-            throw new SodiumException('Argument 2 must be CRYPTO_AUTH_KEYBYTES long.');
981
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_AUTH_KEYBYTES ) {
982
+            throw new SodiumException( 'Argument 2 must be CRYPTO_AUTH_KEYBYTES long.' );
983 983
         }
984 984
 
985
-        if (self::useNewSodiumAPI()) {
986
-            return (string) sodium_crypto_auth($message, $key);
985
+        if ( self::useNewSodiumAPI() ) {
986
+            return (string)sodium_crypto_auth( $message, $key );
987 987
         }
988
-        if (self::use_fallback('crypto_auth')) {
989
-            return (string) call_user_func('\\Sodium\\crypto_auth', $message, $key);
988
+        if ( self::use_fallback( 'crypto_auth' ) ) {
989
+            return (string)call_user_func( '\\Sodium\\crypto_auth', $message, $key );
990 990
         }
991
-        if (PHP_INT_SIZE === 4) {
992
-            return ParagonIE_Sodium_Crypto32::auth($message, $key);
991
+        if ( PHP_INT_SIZE === 4 ) {
992
+            return ParagonIE_Sodium_Crypto32::auth( $message, $key );
993 993
         }
994
-        return ParagonIE_Sodium_Crypto::auth($message, $key);
994
+        return ParagonIE_Sodium_Crypto::auth( $message, $key );
995 995
     }
996 996
 
997 997
     /**
@@ -1001,7 +1001,7 @@  discard block
 block discarded – undo
1001 1001
      */
1002 1002
     public static function crypto_auth_keygen()
1003 1003
     {
1004
-        return random_bytes(self::CRYPTO_AUTH_KEYBYTES);
1004
+        return random_bytes( self::CRYPTO_AUTH_KEYBYTES );
1005 1005
     }
1006 1006
 
1007 1007
     /**
@@ -1016,31 +1016,31 @@  discard block
 block discarded – undo
1016 1016
      * @throws TypeError
1017 1017
      * @psalm-suppress MixedArgument
1018 1018
      */
1019
-    public static function crypto_auth_verify($mac, $message, $key)
1019
+    public static function crypto_auth_verify( $mac, $message, $key )
1020 1020
     {
1021 1021
         /* Type checks: */
1022
-        ParagonIE_Sodium_Core_Util::declareScalarType($mac, 'string', 1);
1023
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2);
1024
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
1022
+        ParagonIE_Sodium_Core_Util::declareScalarType( $mac, 'string', 1 );
1023
+        ParagonIE_Sodium_Core_Util::declareScalarType( $message, 'string', 2 );
1024
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 3 );
1025 1025
 
1026 1026
         /* Input validation: */
1027
-        if (ParagonIE_Sodium_Core_Util::strlen($mac) !== self::CRYPTO_AUTH_BYTES) {
1028
-            throw new SodiumException('Argument 1 must be CRYPTO_AUTH_BYTES long.');
1027
+        if ( ParagonIE_Sodium_Core_Util::strlen( $mac ) !== self::CRYPTO_AUTH_BYTES ) {
1028
+            throw new SodiumException( 'Argument 1 must be CRYPTO_AUTH_BYTES long.' );
1029 1029
         }
1030
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AUTH_KEYBYTES) {
1031
-            throw new SodiumException('Argument 3 must be CRYPTO_AUTH_KEYBYTES long.');
1030
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_AUTH_KEYBYTES ) {
1031
+            throw new SodiumException( 'Argument 3 must be CRYPTO_AUTH_KEYBYTES long.' );
1032 1032
         }
1033 1033
 
1034
-        if (self::useNewSodiumAPI()) {
1035
-            return (bool) sodium_crypto_auth_verify($mac, $message, $key);
1034
+        if ( self::useNewSodiumAPI() ) {
1035
+            return (bool)sodium_crypto_auth_verify( $mac, $message, $key );
1036 1036
         }
1037
-        if (self::use_fallback('crypto_auth_verify')) {
1038
-            return (bool) call_user_func('\\Sodium\\crypto_auth_verify', $mac, $message, $key);
1037
+        if ( self::use_fallback( 'crypto_auth_verify' ) ) {
1038
+            return (bool)call_user_func( '\\Sodium\\crypto_auth_verify', $mac, $message, $key );
1039 1039
         }
1040
-        if (PHP_INT_SIZE === 4) {
1041
-            return ParagonIE_Sodium_Crypto32::auth_verify($mac, $message, $key);
1040
+        if ( PHP_INT_SIZE === 4 ) {
1041
+            return ParagonIE_Sodium_Crypto32::auth_verify( $mac, $message, $key );
1042 1042
         }
1043
-        return ParagonIE_Sodium_Crypto::auth_verify($mac, $message, $key);
1043
+        return ParagonIE_Sodium_Crypto::auth_verify( $mac, $message, $key );
1044 1044
     }
1045 1045
 
1046 1046
     /**
@@ -1060,31 +1060,31 @@  discard block
 block discarded – undo
1060 1060
      * @throws TypeError
1061 1061
      * @psalm-suppress MixedArgument
1062 1062
      */
1063
-    public static function crypto_box($plaintext, $nonce, $keypair)
1063
+    public static function crypto_box( $plaintext, $nonce, $keypair )
1064 1064
     {
1065 1065
         /* Type checks: */
1066
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
1067
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
1068
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 3);
1066
+        ParagonIE_Sodium_Core_Util::declareScalarType( $plaintext, 'string', 1 );
1067
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 2 );
1068
+        ParagonIE_Sodium_Core_Util::declareScalarType( $keypair, 'string', 3 );
1069 1069
 
1070 1070
         /* Input validation: */
1071
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_BOX_NONCEBYTES) {
1072
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_NONCEBYTES long.');
1071
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_BOX_NONCEBYTES ) {
1072
+            throw new SodiumException( 'Argument 2 must be CRYPTO_BOX_NONCEBYTES long.' );
1073 1073
         }
1074
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1075
-            throw new SodiumException('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1074
+        if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== self::CRYPTO_BOX_KEYPAIRBYTES ) {
1075
+            throw new SodiumException( 'Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES long.' );
1076 1076
         }
1077 1077
 
1078
-        if (self::useNewSodiumAPI()) {
1079
-            return (string) sodium_crypto_box($plaintext, $nonce, $keypair);
1078
+        if ( self::useNewSodiumAPI() ) {
1079
+            return (string)sodium_crypto_box( $plaintext, $nonce, $keypair );
1080 1080
         }
1081
-        if (self::use_fallback('crypto_box')) {
1082
-            return (string) call_user_func('\\Sodium\\crypto_box', $plaintext, $nonce, $keypair);
1081
+        if ( self::use_fallback( 'crypto_box' ) ) {
1082
+            return (string)call_user_func( '\\Sodium\\crypto_box', $plaintext, $nonce, $keypair );
1083 1083
         }
1084
-        if (PHP_INT_SIZE === 4) {
1085
-            return ParagonIE_Sodium_Crypto32::box($plaintext, $nonce, $keypair);
1084
+        if ( PHP_INT_SIZE === 4 ) {
1085
+            return ParagonIE_Sodium_Crypto32::box( $plaintext, $nonce, $keypair );
1086 1086
         }
1087
-        return ParagonIE_Sodium_Crypto::box($plaintext, $nonce, $keypair);
1087
+        return ParagonIE_Sodium_Crypto::box( $plaintext, $nonce, $keypair );
1088 1088
     }
1089 1089
 
1090 1090
     /**
@@ -1104,27 +1104,27 @@  discard block
 block discarded – undo
1104 1104
      * @throws TypeError
1105 1105
      * @psalm-suppress MixedArgument
1106 1106
      */
1107
-    public static function crypto_box_seal($plaintext, $publicKey)
1107
+    public static function crypto_box_seal( $plaintext, $publicKey )
1108 1108
     {
1109 1109
         /* Type checks: */
1110
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
1111
-        ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
1110
+        ParagonIE_Sodium_Core_Util::declareScalarType( $plaintext, 'string', 1 );
1111
+        ParagonIE_Sodium_Core_Util::declareScalarType( $publicKey, 'string', 2 );
1112 1112
 
1113 1113
         /* Input validation: */
1114
-        if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1115
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1114
+        if ( ParagonIE_Sodium_Core_Util::strlen( $publicKey ) !== self::CRYPTO_BOX_PUBLICKEYBYTES ) {
1115
+            throw new SodiumException( 'Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.' );
1116 1116
         }
1117 1117
 
1118
-        if (self::useNewSodiumAPI()) {
1119
-            return (string) sodium_crypto_box_seal($plaintext, $publicKey);
1118
+        if ( self::useNewSodiumAPI() ) {
1119
+            return (string)sodium_crypto_box_seal( $plaintext, $publicKey );
1120 1120
         }
1121
-        if (self::use_fallback('crypto_box_seal')) {
1122
-            return (string) call_user_func('\\Sodium\\crypto_box_seal', $plaintext, $publicKey);
1121
+        if ( self::use_fallback( 'crypto_box_seal' ) ) {
1122
+            return (string)call_user_func( '\\Sodium\\crypto_box_seal', $plaintext, $publicKey );
1123 1123
         }
1124
-        if (PHP_INT_SIZE === 4) {
1125
-            return ParagonIE_Sodium_Crypto32::box_seal($plaintext, $publicKey);
1124
+        if ( PHP_INT_SIZE === 4 ) {
1125
+            return ParagonIE_Sodium_Crypto32::box_seal( $plaintext, $publicKey );
1126 1126
         }
1127
-        return ParagonIE_Sodium_Crypto::box_seal($plaintext, $publicKey);
1127
+        return ParagonIE_Sodium_Crypto::box_seal( $plaintext, $publicKey );
1128 1128
     }
1129 1129
 
1130 1130
     /**
@@ -1142,31 +1142,31 @@  discard block
 block discarded – undo
1142 1142
      * @psalm-suppress MixedInferredReturnType
1143 1143
      * @psalm-suppress MixedReturnStatement
1144 1144
      */
1145
-    public static function crypto_box_seal_open($ciphertext, $keypair)
1145
+    public static function crypto_box_seal_open( $ciphertext, $keypair )
1146 1146
     {
1147 1147
         /* Type checks: */
1148
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
1149
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 2);
1148
+        ParagonIE_Sodium_Core_Util::declareScalarType( $ciphertext, 'string', 1 );
1149
+        ParagonIE_Sodium_Core_Util::declareScalarType( $keypair, 'string', 2 );
1150 1150
 
1151 1151
         /* Input validation: */
1152
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1153
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1152
+        if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== self::CRYPTO_BOX_KEYPAIRBYTES ) {
1153
+            throw new SodiumException( 'Argument 2 must be CRYPTO_BOX_KEYPAIRBYTES long.' );
1154 1154
         }
1155 1155
 
1156
-        if (self::useNewSodiumAPI()) {
1156
+        if ( self::useNewSodiumAPI() ) {
1157 1157
             /**
1158 1158
              * @psalm-suppress InvalidReturnStatement
1159 1159
              * @psalm-suppress FalsableReturnStatement
1160 1160
              */
1161
-            return sodium_crypto_box_seal_open($ciphertext, $keypair);
1161
+            return sodium_crypto_box_seal_open( $ciphertext, $keypair );
1162 1162
         }
1163
-        if (self::use_fallback('crypto_box_seal_open')) {
1164
-            return call_user_func('\\Sodium\\crypto_box_seal_open', $ciphertext, $keypair);
1163
+        if ( self::use_fallback( 'crypto_box_seal_open' ) ) {
1164
+            return call_user_func( '\\Sodium\\crypto_box_seal_open', $ciphertext, $keypair );
1165 1165
         }
1166
-        if (PHP_INT_SIZE === 4) {
1167
-            return ParagonIE_Sodium_Crypto32::box_seal_open($ciphertext, $keypair);
1166
+        if ( PHP_INT_SIZE === 4 ) {
1167
+            return ParagonIE_Sodium_Crypto32::box_seal_open( $ciphertext, $keypair );
1168 1168
         }
1169
-        return ParagonIE_Sodium_Crypto::box_seal_open($ciphertext, $keypair);
1169
+        return ParagonIE_Sodium_Crypto::box_seal_open( $ciphertext, $keypair );
1170 1170
     }
1171 1171
 
1172 1172
     /**
@@ -1182,13 +1182,13 @@  discard block
 block discarded – undo
1182 1182
      */
1183 1183
     public static function crypto_box_keypair()
1184 1184
     {
1185
-        if (self::useNewSodiumAPI()) {
1186
-            return (string) sodium_crypto_box_keypair();
1185
+        if ( self::useNewSodiumAPI() ) {
1186
+            return (string)sodium_crypto_box_keypair();
1187 1187
         }
1188
-        if (self::use_fallback('crypto_box_keypair')) {
1189
-            return (string) call_user_func('\\Sodium\\crypto_box_keypair');
1188
+        if ( self::use_fallback( 'crypto_box_keypair' ) ) {
1189
+            return (string)call_user_func( '\\Sodium\\crypto_box_keypair' );
1190 1190
         }
1191
-        if (PHP_INT_SIZE === 4) {
1191
+        if ( PHP_INT_SIZE === 4 ) {
1192 1192
             return ParagonIE_Sodium_Crypto32::box_keypair();
1193 1193
         }
1194 1194
         return ParagonIE_Sodium_Crypto::box_keypair();
@@ -1205,30 +1205,30 @@  discard block
 block discarded – undo
1205 1205
      * @throws TypeError
1206 1206
      * @psalm-suppress MixedArgument
1207 1207
      */
1208
-    public static function crypto_box_keypair_from_secretkey_and_publickey($secretKey, $publicKey)
1208
+    public static function crypto_box_keypair_from_secretkey_and_publickey( $secretKey, $publicKey )
1209 1209
     {
1210 1210
         /* Type checks: */
1211
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
1212
-        ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
1211
+        ParagonIE_Sodium_Core_Util::declareScalarType( $secretKey, 'string', 1 );
1212
+        ParagonIE_Sodium_Core_Util::declareScalarType( $publicKey, 'string', 2 );
1213 1213
 
1214 1214
         /* Input validation: */
1215
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
1216
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
1215
+        if ( ParagonIE_Sodium_Core_Util::strlen( $secretKey ) !== self::CRYPTO_BOX_SECRETKEYBYTES ) {
1216
+            throw new SodiumException( 'Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.' );
1217 1217
         }
1218
-        if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1219
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1218
+        if ( ParagonIE_Sodium_Core_Util::strlen( $publicKey ) !== self::CRYPTO_BOX_PUBLICKEYBYTES ) {
1219
+            throw new SodiumException( 'Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.' );
1220 1220
         }
1221 1221
 
1222
-        if (self::useNewSodiumAPI()) {
1223
-            return (string) sodium_crypto_box_keypair_from_secretkey_and_publickey($secretKey, $publicKey);
1222
+        if ( self::useNewSodiumAPI() ) {
1223
+            return (string)sodium_crypto_box_keypair_from_secretkey_and_publickey( $secretKey, $publicKey );
1224 1224
         }
1225
-        if (self::use_fallback('crypto_box_keypair_from_secretkey_and_publickey')) {
1226
-            return (string) call_user_func('\\Sodium\\crypto_box_keypair_from_secretkey_and_publickey', $secretKey, $publicKey);
1225
+        if ( self::use_fallback( 'crypto_box_keypair_from_secretkey_and_publickey' ) ) {
1226
+            return (string)call_user_func( '\\Sodium\\crypto_box_keypair_from_secretkey_and_publickey', $secretKey, $publicKey );
1227 1227
         }
1228
-        if (PHP_INT_SIZE === 4) {
1229
-            return ParagonIE_Sodium_Crypto32::box_keypair_from_secretkey_and_publickey($secretKey, $publicKey);
1228
+        if ( PHP_INT_SIZE === 4 ) {
1229
+            return ParagonIE_Sodium_Crypto32::box_keypair_from_secretkey_and_publickey( $secretKey, $publicKey );
1230 1230
         }
1231
-        return ParagonIE_Sodium_Crypto::box_keypair_from_secretkey_and_publickey($secretKey, $publicKey);
1231
+        return ParagonIE_Sodium_Crypto::box_keypair_from_secretkey_and_publickey( $secretKey, $publicKey );
1232 1232
     }
1233 1233
 
1234 1234
     /**
@@ -1244,38 +1244,38 @@  discard block
 block discarded – undo
1244 1244
      * @psalm-suppress MixedInferredReturnType
1245 1245
      * @psalm-suppress MixedReturnStatement
1246 1246
      */
1247
-    public static function crypto_box_open($ciphertext, $nonce, $keypair)
1247
+    public static function crypto_box_open( $ciphertext, $nonce, $keypair )
1248 1248
     {
1249 1249
         /* Type checks: */
1250
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
1251
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
1252
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 3);
1250
+        ParagonIE_Sodium_Core_Util::declareScalarType( $ciphertext, 'string', 1 );
1251
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 2 );
1252
+        ParagonIE_Sodium_Core_Util::declareScalarType( $keypair, 'string', 3 );
1253 1253
 
1254 1254
         /* Input validation: */
1255
-        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_BOX_MACBYTES) {
1256
-            throw new SodiumException('Argument 1 must be at least CRYPTO_BOX_MACBYTES long.');
1255
+        if ( ParagonIE_Sodium_Core_Util::strlen( $ciphertext ) < self::CRYPTO_BOX_MACBYTES ) {
1256
+            throw new SodiumException( 'Argument 1 must be at least CRYPTO_BOX_MACBYTES long.' );
1257 1257
         }
1258
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_BOX_NONCEBYTES) {
1259
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_NONCEBYTES long.');
1258
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_BOX_NONCEBYTES ) {
1259
+            throw new SodiumException( 'Argument 2 must be CRYPTO_BOX_NONCEBYTES long.' );
1260 1260
         }
1261
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1262
-            throw new SodiumException('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1261
+        if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== self::CRYPTO_BOX_KEYPAIRBYTES ) {
1262
+            throw new SodiumException( 'Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES long.' );
1263 1263
         }
1264 1264
 
1265
-        if (self::useNewSodiumAPI()) {
1265
+        if ( self::useNewSodiumAPI() ) {
1266 1266
             /**
1267 1267
              * @psalm-suppress InvalidReturnStatement
1268 1268
              * @psalm-suppress FalsableReturnStatement
1269 1269
              */
1270
-            return sodium_crypto_box_open($ciphertext, $nonce, $keypair);
1270
+            return sodium_crypto_box_open( $ciphertext, $nonce, $keypair );
1271 1271
         }
1272
-        if (self::use_fallback('crypto_box_open')) {
1273
-            return call_user_func('\\Sodium\\crypto_box_open', $ciphertext, $nonce, $keypair);
1272
+        if ( self::use_fallback( 'crypto_box_open' ) ) {
1273
+            return call_user_func( '\\Sodium\\crypto_box_open', $ciphertext, $nonce, $keypair );
1274 1274
         }
1275
-        if (PHP_INT_SIZE === 4) {
1276
-            return ParagonIE_Sodium_Crypto32::box_open($ciphertext, $nonce, $keypair);
1275
+        if ( PHP_INT_SIZE === 4 ) {
1276
+            return ParagonIE_Sodium_Crypto32::box_open( $ciphertext, $nonce, $keypair );
1277 1277
         }
1278
-        return ParagonIE_Sodium_Crypto::box_open($ciphertext, $nonce, $keypair);
1278
+        return ParagonIE_Sodium_Crypto::box_open( $ciphertext, $nonce, $keypair );
1279 1279
     }
1280 1280
 
1281 1281
     /**
@@ -1287,26 +1287,26 @@  discard block
 block discarded – undo
1287 1287
      * @throws TypeError
1288 1288
      * @psalm-suppress MixedArgument
1289 1289
      */
1290
-    public static function crypto_box_publickey($keypair)
1290
+    public static function crypto_box_publickey( $keypair )
1291 1291
     {
1292 1292
         /* Type checks: */
1293
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1293
+        ParagonIE_Sodium_Core_Util::declareScalarType( $keypair, 'string', 1 );
1294 1294
 
1295 1295
         /* Input validation: */
1296
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1297
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1296
+        if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== self::CRYPTO_BOX_KEYPAIRBYTES ) {
1297
+            throw new SodiumException( 'Argument 1 must be CRYPTO_BOX_KEYPAIRBYTES long.' );
1298 1298
         }
1299 1299
 
1300
-        if (self::useNewSodiumAPI()) {
1301
-            return (string) sodium_crypto_box_publickey($keypair);
1300
+        if ( self::useNewSodiumAPI() ) {
1301
+            return (string)sodium_crypto_box_publickey( $keypair );
1302 1302
         }
1303
-        if (self::use_fallback('crypto_box_publickey')) {
1304
-            return (string) call_user_func('\\Sodium\\crypto_box_publickey', $keypair);
1303
+        if ( self::use_fallback( 'crypto_box_publickey' ) ) {
1304
+            return (string)call_user_func( '\\Sodium\\crypto_box_publickey', $keypair );
1305 1305
         }
1306
-        if (PHP_INT_SIZE === 4) {
1307
-            return ParagonIE_Sodium_Crypto32::box_publickey($keypair);
1306
+        if ( PHP_INT_SIZE === 4 ) {
1307
+            return ParagonIE_Sodium_Crypto32::box_publickey( $keypair );
1308 1308
         }
1309
-        return ParagonIE_Sodium_Crypto::box_publickey($keypair);
1309
+        return ParagonIE_Sodium_Crypto::box_publickey( $keypair );
1310 1310
     }
1311 1311
 
1312 1312
     /**
@@ -1318,26 +1318,26 @@  discard block
 block discarded – undo
1318 1318
      * @throws TypeError
1319 1319
      * @psalm-suppress MixedArgument
1320 1320
      */
1321
-    public static function crypto_box_publickey_from_secretkey($secretKey)
1321
+    public static function crypto_box_publickey_from_secretkey( $secretKey )
1322 1322
     {
1323 1323
         /* Type checks: */
1324
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
1324
+        ParagonIE_Sodium_Core_Util::declareScalarType( $secretKey, 'string', 1 );
1325 1325
 
1326 1326
         /* Input validation: */
1327
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
1328
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
1327
+        if ( ParagonIE_Sodium_Core_Util::strlen( $secretKey ) !== self::CRYPTO_BOX_SECRETKEYBYTES ) {
1328
+            throw new SodiumException( 'Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.' );
1329 1329
         }
1330 1330
 
1331
-        if (self::useNewSodiumAPI()) {
1332
-            return (string) sodium_crypto_box_publickey_from_secretkey($secretKey);
1331
+        if ( self::useNewSodiumAPI() ) {
1332
+            return (string)sodium_crypto_box_publickey_from_secretkey( $secretKey );
1333 1333
         }
1334
-        if (self::use_fallback('crypto_box_publickey_from_secretkey')) {
1335
-            return (string) call_user_func('\\Sodium\\crypto_box_publickey_from_secretkey', $secretKey);
1334
+        if ( self::use_fallback( 'crypto_box_publickey_from_secretkey' ) ) {
1335
+            return (string)call_user_func( '\\Sodium\\crypto_box_publickey_from_secretkey', $secretKey );
1336 1336
         }
1337
-        if (PHP_INT_SIZE === 4) {
1338
-            return ParagonIE_Sodium_Crypto32::box_publickey_from_secretkey($secretKey);
1337
+        if ( PHP_INT_SIZE === 4 ) {
1338
+            return ParagonIE_Sodium_Crypto32::box_publickey_from_secretkey( $secretKey );
1339 1339
         }
1340
-        return ParagonIE_Sodium_Crypto::box_publickey_from_secretkey($secretKey);
1340
+        return ParagonIE_Sodium_Crypto::box_publickey_from_secretkey( $secretKey );
1341 1341
     }
1342 1342
 
1343 1343
     /**
@@ -1349,26 +1349,26 @@  discard block
 block discarded – undo
1349 1349
      * @throws TypeError
1350 1350
      * @psalm-suppress MixedArgument
1351 1351
      */
1352
-    public static function crypto_box_secretkey($keypair)
1352
+    public static function crypto_box_secretkey( $keypair )
1353 1353
     {
1354 1354
         /* Type checks: */
1355
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1355
+        ParagonIE_Sodium_Core_Util::declareScalarType( $keypair, 'string', 1 );
1356 1356
 
1357 1357
         /* Input validation: */
1358
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_BOX_KEYPAIRBYTES) {
1359
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_KEYPAIRBYTES long.');
1358
+        if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== self::CRYPTO_BOX_KEYPAIRBYTES ) {
1359
+            throw new SodiumException( 'Argument 1 must be CRYPTO_BOX_KEYPAIRBYTES long.' );
1360 1360
         }
1361 1361
 
1362
-        if (self::useNewSodiumAPI()) {
1363
-            return (string) sodium_crypto_box_secretkey($keypair);
1362
+        if ( self::useNewSodiumAPI() ) {
1363
+            return (string)sodium_crypto_box_secretkey( $keypair );
1364 1364
         }
1365
-        if (self::use_fallback('crypto_box_secretkey')) {
1366
-            return (string) call_user_func('\\Sodium\\crypto_box_secretkey', $keypair);
1365
+        if ( self::use_fallback( 'crypto_box_secretkey' ) ) {
1366
+            return (string)call_user_func( '\\Sodium\\crypto_box_secretkey', $keypair );
1367 1367
         }
1368
-        if (PHP_INT_SIZE === 4) {
1369
-            return ParagonIE_Sodium_Crypto32::box_secretkey($keypair);
1368
+        if ( PHP_INT_SIZE === 4 ) {
1369
+            return ParagonIE_Sodium_Crypto32::box_secretkey( $keypair );
1370 1370
         }
1371
-        return ParagonIE_Sodium_Crypto::box_secretkey($keypair);
1371
+        return ParagonIE_Sodium_Crypto::box_secretkey( $keypair );
1372 1372
     }
1373 1373
 
1374 1374
     /**
@@ -1381,21 +1381,21 @@  discard block
 block discarded – undo
1381 1381
      * @psalm-suppress MixedArgument
1382 1382
      * @psalm-suppress UndefinedFunction
1383 1383
      */
1384
-    public static function crypto_box_seed_keypair($seed)
1384
+    public static function crypto_box_seed_keypair( $seed )
1385 1385
     {
1386 1386
         /* Type checks: */
1387
-        ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1);
1387
+        ParagonIE_Sodium_Core_Util::declareScalarType( $seed, 'string', 1 );
1388 1388
 
1389
-        if (self::useNewSodiumAPI()) {
1390
-            return (string) sodium_crypto_box_seed_keypair($seed);
1389
+        if ( self::useNewSodiumAPI() ) {
1390
+            return (string)sodium_crypto_box_seed_keypair( $seed );
1391 1391
         }
1392
-        if (self::use_fallback('crypto_box_seed_keypair')) {
1393
-            return (string) call_user_func('\\Sodium\\crypto_box_seed_keypair', $seed);
1392
+        if ( self::use_fallback( 'crypto_box_seed_keypair' ) ) {
1393
+            return (string)call_user_func( '\\Sodium\\crypto_box_seed_keypair', $seed );
1394 1394
         }
1395
-        if (PHP_INT_SIZE === 4) {
1396
-            return ParagonIE_Sodium_Crypto32::box_seed_keypair($seed);
1395
+        if ( PHP_INT_SIZE === 4 ) {
1396
+            return ParagonIE_Sodium_Crypto32::box_seed_keypair( $seed );
1397 1397
         }
1398
-        return ParagonIE_Sodium_Crypto::box_seed_keypair($seed);
1398
+        return ParagonIE_Sodium_Crypto::box_seed_keypair( $seed );
1399 1399
     }
1400 1400
 
1401 1401
     /**
@@ -1411,36 +1411,36 @@  discard block
 block discarded – undo
1411 1411
      * @throws TypeError
1412 1412
      * @psalm-suppress MixedArgument
1413 1413
      */
1414
-    public static function crypto_generichash($message, $key = '', $length = self::CRYPTO_GENERICHASH_BYTES)
1414
+    public static function crypto_generichash( $message, $key = '', $length = self::CRYPTO_GENERICHASH_BYTES )
1415 1415
     {
1416 1416
         /* Type checks: */
1417
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
1418
-        if (is_null($key)) {
1417
+        ParagonIE_Sodium_Core_Util::declareScalarType( $message, 'string', 1 );
1418
+        if ( is_null( $key ) ) {
1419 1419
             $key = '';
1420 1420
         }
1421
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2);
1422
-        ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 3);
1421
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 2 );
1422
+        ParagonIE_Sodium_Core_Util::declareScalarType( $length, 'int', 3 );
1423 1423
 
1424 1424
         /* Input validation: */
1425
-        if (!empty($key)) {
1426
-            if (ParagonIE_Sodium_Core_Util::strlen($key) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
1427
-                throw new SodiumException('Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.');
1425
+        if ( ! empty( $key ) ) {
1426
+            if ( ParagonIE_Sodium_Core_Util::strlen( $key ) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN ) {
1427
+                throw new SodiumException( 'Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.' );
1428 1428
             }
1429
-            if (ParagonIE_Sodium_Core_Util::strlen($key) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
1430
-                throw new SodiumException('Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.');
1429
+            if ( ParagonIE_Sodium_Core_Util::strlen( $key ) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX ) {
1430
+                throw new SodiumException( 'Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.' );
1431 1431
             }
1432 1432
         }
1433 1433
 
1434
-        if (self::useNewSodiumAPI()) {
1435
-            return (string) sodium_crypto_generichash($message, $key, $length);
1434
+        if ( self::useNewSodiumAPI() ) {
1435
+            return (string)sodium_crypto_generichash( $message, $key, $length );
1436 1436
         }
1437
-        if (self::use_fallback('crypto_generichash')) {
1438
-            return (string) call_user_func('\\Sodium\\crypto_generichash', $message, $key, $length);
1437
+        if ( self::use_fallback( 'crypto_generichash' ) ) {
1438
+            return (string)call_user_func( '\\Sodium\\crypto_generichash', $message, $key, $length );
1439 1439
         }
1440
-        if (PHP_INT_SIZE === 4) {
1441
-            return ParagonIE_Sodium_Crypto32::generichash($message, $key, $length);
1440
+        if ( PHP_INT_SIZE === 4 ) {
1441
+            return ParagonIE_Sodium_Crypto32::generichash( $message, $key, $length );
1442 1442
         }
1443
-        return ParagonIE_Sodium_Crypto::generichash($message, $key, $length);
1443
+        return ParagonIE_Sodium_Crypto::generichash( $message, $key, $length );
1444 1444
     }
1445 1445
 
1446 1446
     /**
@@ -1455,36 +1455,36 @@  discard block
 block discarded – undo
1455 1455
      * @psalm-suppress ReferenceConstraintViolation
1456 1456
      * @psalm-suppress ConflictingReferenceConstraint
1457 1457
      */
1458
-    public static function crypto_generichash_final(&$ctx, $length = self::CRYPTO_GENERICHASH_BYTES)
1458
+    public static function crypto_generichash_final( &$ctx, $length = self::CRYPTO_GENERICHASH_BYTES )
1459 1459
     {
1460 1460
         /* Type checks: */
1461
-        ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1);
1462
-        ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2);
1461
+        ParagonIE_Sodium_Core_Util::declareScalarType( $ctx, 'string', 1 );
1462
+        ParagonIE_Sodium_Core_Util::declareScalarType( $length, 'int', 2 );
1463 1463
 
1464
-        if (self::useNewSodiumAPI()) {
1465
-            return sodium_crypto_generichash_final($ctx, $length);
1464
+        if ( self::useNewSodiumAPI() ) {
1465
+            return sodium_crypto_generichash_final( $ctx, $length );
1466 1466
         }
1467
-        if (self::use_fallback('crypto_generichash_final')) {
1467
+        if ( self::use_fallback( 'crypto_generichash_final' ) ) {
1468 1468
             $func = '\\Sodium\\crypto_generichash_final';
1469
-            return (string) $func($ctx, $length);
1469
+            return (string)$func( $ctx, $length );
1470 1470
         }
1471
-        if ($length < 1) {
1471
+        if ( $length < 1 ) {
1472 1472
             try {
1473
-                self::memzero($ctx);
1474
-            } catch (SodiumException $ex) {
1475
-                unset($ctx);
1473
+                self::memzero( $ctx );
1474
+            } catch ( SodiumException $ex ) {
1475
+                unset( $ctx );
1476 1476
             }
1477 1477
             return '';
1478 1478
         }
1479
-        if (PHP_INT_SIZE === 4) {
1480
-            $result = ParagonIE_Sodium_Crypto32::generichash_final($ctx, $length);
1479
+        if ( PHP_INT_SIZE === 4 ) {
1480
+            $result = ParagonIE_Sodium_Crypto32::generichash_final( $ctx, $length );
1481 1481
         } else {
1482
-            $result = ParagonIE_Sodium_Crypto::generichash_final($ctx, $length);
1482
+            $result = ParagonIE_Sodium_Crypto::generichash_final( $ctx, $length );
1483 1483
         }
1484 1484
         try {
1485
-            self::memzero($ctx);
1486
-        } catch (SodiumException $ex) {
1487
-            unset($ctx);
1485
+            self::memzero( $ctx );
1486
+        } catch ( SodiumException $ex ) {
1487
+            unset( $ctx );
1488 1488
         }
1489 1489
         return $result;
1490 1490
     }
@@ -1500,35 +1500,35 @@  discard block
 block discarded – undo
1500 1500
      * @throws TypeError
1501 1501
      * @psalm-suppress MixedArgument
1502 1502
      */
1503
-    public static function crypto_generichash_init($key = '', $length = self::CRYPTO_GENERICHASH_BYTES)
1503
+    public static function crypto_generichash_init( $key = '', $length = self::CRYPTO_GENERICHASH_BYTES )
1504 1504
     {
1505 1505
         /* Type checks: */
1506
-        if (is_null($key)) {
1506
+        if ( is_null( $key ) ) {
1507 1507
             $key = '';
1508 1508
         }
1509
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 1);
1510
-        ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2);
1509
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 1 );
1510
+        ParagonIE_Sodium_Core_Util::declareScalarType( $length, 'int', 2 );
1511 1511
 
1512 1512
         /* Input validation: */
1513
-        if (!empty($key)) {
1514
-            if (ParagonIE_Sodium_Core_Util::strlen($key) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
1515
-                throw new SodiumException('Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.');
1513
+        if ( ! empty( $key ) ) {
1514
+            if ( ParagonIE_Sodium_Core_Util::strlen( $key ) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN ) {
1515
+                throw new SodiumException( 'Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.' );
1516 1516
             }
1517
-            if (ParagonIE_Sodium_Core_Util::strlen($key) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
1518
-                throw new SodiumException('Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.');
1517
+            if ( ParagonIE_Sodium_Core_Util::strlen( $key ) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX ) {
1518
+                throw new SodiumException( 'Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.' );
1519 1519
             }
1520 1520
         }
1521 1521
 
1522
-        if (self::useNewSodiumAPI()) {
1523
-            return sodium_crypto_generichash_init($key, $length);
1522
+        if ( self::useNewSodiumAPI() ) {
1523
+            return sodium_crypto_generichash_init( $key, $length );
1524 1524
         }
1525
-        if (self::use_fallback('crypto_generichash_init')) {
1526
-            return (string) call_user_func('\\Sodium\\crypto_generichash_init', $key, $length);
1525
+        if ( self::use_fallback( 'crypto_generichash_init' ) ) {
1526
+            return (string)call_user_func( '\\Sodium\\crypto_generichash_init', $key, $length );
1527 1527
         }
1528
-        if (PHP_INT_SIZE === 4) {
1529
-            return ParagonIE_Sodium_Crypto32::generichash_init($key, $length);
1528
+        if ( PHP_INT_SIZE === 4 ) {
1529
+            return ParagonIE_Sodium_Crypto32::generichash_init( $key, $length );
1530 1530
         }
1531
-        return ParagonIE_Sodium_Crypto::generichash_init($key, $length);
1531
+        return ParagonIE_Sodium_Crypto::generichash_init( $key, $length );
1532 1532
     }
1533 1533
 
1534 1534
     /**
@@ -1551,31 +1551,31 @@  discard block
 block discarded – undo
1551 1551
         $personal = ''
1552 1552
     ) {
1553 1553
         /* Type checks: */
1554
-        if (is_null($key)) {
1554
+        if ( is_null( $key ) ) {
1555 1555
             $key = '';
1556 1556
         }
1557
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 1);
1558
-        ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2);
1559
-        ParagonIE_Sodium_Core_Util::declareScalarType($salt, 'string', 3);
1560
-        ParagonIE_Sodium_Core_Util::declareScalarType($personal, 'string', 4);
1561
-        $salt = str_pad($salt, 16, "\0", STR_PAD_RIGHT);
1562
-        $personal = str_pad($personal, 16, "\0", STR_PAD_RIGHT);
1557
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 1 );
1558
+        ParagonIE_Sodium_Core_Util::declareScalarType( $length, 'int', 2 );
1559
+        ParagonIE_Sodium_Core_Util::declareScalarType( $salt, 'string', 3 );
1560
+        ParagonIE_Sodium_Core_Util::declareScalarType( $personal, 'string', 4 );
1561
+        $salt = str_pad( $salt, 16, "\0", STR_PAD_RIGHT );
1562
+        $personal = str_pad( $personal, 16, "\0", STR_PAD_RIGHT );
1563 1563
 
1564 1564
         /* Input validation: */
1565
-        if (!empty($key)) {
1565
+        if ( ! empty( $key ) ) {
1566 1566
             /*
1567 1567
             if (ParagonIE_Sodium_Core_Util::strlen($key) < self::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
1568 1568
                 throw new SodiumException('Unsupported key size. Must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes long.');
1569 1569
             }
1570 1570
             */
1571
-            if (ParagonIE_Sodium_Core_Util::strlen($key) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
1572
-                throw new SodiumException('Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.');
1571
+            if ( ParagonIE_Sodium_Core_Util::strlen( $key ) > self::CRYPTO_GENERICHASH_KEYBYTES_MAX ) {
1572
+                throw new SodiumException( 'Unsupported key size. Must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes long.' );
1573 1573
             }
1574 1574
         }
1575
-        if (PHP_INT_SIZE === 4) {
1576
-            return ParagonIE_Sodium_Crypto32::generichash_init_salt_personal($key, $length, $salt, $personal);
1575
+        if ( PHP_INT_SIZE === 4 ) {
1576
+            return ParagonIE_Sodium_Crypto32::generichash_init_salt_personal( $key, $length, $salt, $personal );
1577 1577
         }
1578
-        return ParagonIE_Sodium_Crypto::generichash_init_salt_personal($key, $length, $salt, $personal);
1578
+        return ParagonIE_Sodium_Crypto::generichash_init_salt_personal( $key, $length, $salt, $personal );
1579 1579
     }
1580 1580
 
1581 1581
     /**
@@ -1591,25 +1591,25 @@  discard block
 block discarded – undo
1591 1591
      * @psalm-suppress MixedArgument
1592 1592
      * @psalm-suppress ReferenceConstraintViolation
1593 1593
      */
1594
-    public static function crypto_generichash_update(&$ctx, $message)
1594
+    public static function crypto_generichash_update( &$ctx, $message )
1595 1595
     {
1596 1596
         /* Type checks: */
1597
-        ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1);
1598
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2);
1597
+        ParagonIE_Sodium_Core_Util::declareScalarType( $ctx, 'string', 1 );
1598
+        ParagonIE_Sodium_Core_Util::declareScalarType( $message, 'string', 2 );
1599 1599
 
1600
-        if (self::useNewSodiumAPI()) {
1601
-            sodium_crypto_generichash_update($ctx, $message);
1600
+        if ( self::useNewSodiumAPI() ) {
1601
+            sodium_crypto_generichash_update( $ctx, $message );
1602 1602
             return;
1603 1603
         }
1604
-        if (self::use_fallback('crypto_generichash_update')) {
1604
+        if ( self::use_fallback( 'crypto_generichash_update' ) ) {
1605 1605
             $func = '\\Sodium\\crypto_generichash_update';
1606
-            $func($ctx, $message);
1606
+            $func( $ctx, $message );
1607 1607
             return;
1608 1608
         }
1609
-        if (PHP_INT_SIZE === 4) {
1610
-            $ctx = ParagonIE_Sodium_Crypto32::generichash_update($ctx, $message);
1609
+        if ( PHP_INT_SIZE === 4 ) {
1610
+            $ctx = ParagonIE_Sodium_Crypto32::generichash_update( $ctx, $message );
1611 1611
         } else {
1612
-            $ctx = ParagonIE_Sodium_Crypto::generichash_update($ctx, $message);
1612
+            $ctx = ParagonIE_Sodium_Crypto::generichash_update( $ctx, $message );
1613 1613
         }
1614 1614
     }
1615 1615
 
@@ -1620,7 +1620,7 @@  discard block
 block discarded – undo
1620 1620
      */
1621 1621
     public static function crypto_generichash_keygen()
1622 1622
     {
1623
-        return random_bytes(self::CRYPTO_GENERICHASH_KEYBYTES);
1623
+        return random_bytes( self::CRYPTO_GENERICHASH_KEYBYTES );
1624 1624
     }
1625 1625
 
1626 1626
     /**
@@ -1637,39 +1637,39 @@  discard block
 block discarded – undo
1637 1637
         $context,
1638 1638
         $key
1639 1639
     ) {
1640
-        ParagonIE_Sodium_Core_Util::declareScalarType($subkey_len, 'int', 1);
1641
-        ParagonIE_Sodium_Core_Util::declareScalarType($subkey_id, 'int', 2);
1642
-        ParagonIE_Sodium_Core_Util::declareScalarType($context, 'string', 3);
1643
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
1644
-        $subkey_id = (int) $subkey_id;
1645
-        $subkey_len = (int) $subkey_len;
1646
-        $context = (string) $context;
1647
-        $key = (string) $key;
1640
+        ParagonIE_Sodium_Core_Util::declareScalarType( $subkey_len, 'int', 1 );
1641
+        ParagonIE_Sodium_Core_Util::declareScalarType( $subkey_id, 'int', 2 );
1642
+        ParagonIE_Sodium_Core_Util::declareScalarType( $context, 'string', 3 );
1643
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 4 );
1644
+        $subkey_id = (int)$subkey_id;
1645
+        $subkey_len = (int)$subkey_len;
1646
+        $context = (string)$context;
1647
+        $key = (string)$key;
1648 1648
 
1649
-        if ($subkey_len < self::CRYPTO_KDF_BYTES_MIN) {
1650
-            throw new SodiumException('subkey cannot be smaller than SODIUM_CRYPTO_KDF_BYTES_MIN');
1649
+        if ( $subkey_len < self::CRYPTO_KDF_BYTES_MIN ) {
1650
+            throw new SodiumException( 'subkey cannot be smaller than SODIUM_CRYPTO_KDF_BYTES_MIN' );
1651 1651
         }
1652
-        if ($subkey_len > self::CRYPTO_KDF_BYTES_MAX) {
1653
-            throw new SodiumException('subkey cannot be larger than SODIUM_CRYPTO_KDF_BYTES_MAX');
1652
+        if ( $subkey_len > self::CRYPTO_KDF_BYTES_MAX ) {
1653
+            throw new SodiumException( 'subkey cannot be larger than SODIUM_CRYPTO_KDF_BYTES_MAX' );
1654 1654
         }
1655
-        if ($subkey_id < 0) {
1656
-            throw new SodiumException('subkey_id cannot be negative');
1655
+        if ( $subkey_id < 0 ) {
1656
+            throw new SodiumException( 'subkey_id cannot be negative' );
1657 1657
         }
1658
-        if (ParagonIE_Sodium_Core_Util::strlen($context) !== self::CRYPTO_KDF_CONTEXTBYTES) {
1659
-            throw new SodiumException('context should be SODIUM_CRYPTO_KDF_CONTEXTBYTES bytes');
1658
+        if ( ParagonIE_Sodium_Core_Util::strlen( $context ) !== self::CRYPTO_KDF_CONTEXTBYTES ) {
1659
+            throw new SodiumException( 'context should be SODIUM_CRYPTO_KDF_CONTEXTBYTES bytes' );
1660 1660
         }
1661
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_KDF_KEYBYTES) {
1662
-            throw new SodiumException('key should be SODIUM_CRYPTO_KDF_KEYBYTES bytes');
1661
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_KDF_KEYBYTES ) {
1662
+            throw new SodiumException( 'key should be SODIUM_CRYPTO_KDF_KEYBYTES bytes' );
1663 1663
         }
1664 1664
 
1665
-        $salt = ParagonIE_Sodium_Core_Util::store64_le($subkey_id);
1665
+        $salt = ParagonIE_Sodium_Core_Util::store64_le( $subkey_id );
1666 1666
         $state = self::crypto_generichash_init_salt_personal(
1667 1667
             $key,
1668 1668
             $subkey_len,
1669 1669
             $salt,
1670 1670
             $context
1671 1671
         );
1672
-        return self::crypto_generichash_final($state, $subkey_len);
1672
+        return self::crypto_generichash_final( $state, $subkey_len );
1673 1673
     }
1674 1674
 
1675 1675
     /**
@@ -1679,7 +1679,7 @@  discard block
 block discarded – undo
1679 1679
      */
1680 1680
     public static function crypto_kdf_keygen()
1681 1681
     {
1682
-        return random_bytes(self::CRYPTO_KDF_KEYBYTES);
1682
+        return random_bytes( self::CRYPTO_KDF_KEYBYTES );
1683 1683
     }
1684 1684
 
1685 1685
     /**
@@ -1712,31 +1712,31 @@  discard block
 block discarded – undo
1712 1712
      * @throws TypeError
1713 1713
      * @psalm-suppress MixedArgument
1714 1714
      */
1715
-    public static function crypto_kx($my_secret, $their_public, $client_public, $server_public, $dontFallback = false)
1715
+    public static function crypto_kx( $my_secret, $their_public, $client_public, $server_public, $dontFallback = false )
1716 1716
     {
1717 1717
         /* Type checks: */
1718
-        ParagonIE_Sodium_Core_Util::declareScalarType($my_secret, 'string', 1);
1719
-        ParagonIE_Sodium_Core_Util::declareScalarType($their_public, 'string', 2);
1720
-        ParagonIE_Sodium_Core_Util::declareScalarType($client_public, 'string', 3);
1721
-        ParagonIE_Sodium_Core_Util::declareScalarType($server_public, 'string', 4);
1718
+        ParagonIE_Sodium_Core_Util::declareScalarType( $my_secret, 'string', 1 );
1719
+        ParagonIE_Sodium_Core_Util::declareScalarType( $their_public, 'string', 2 );
1720
+        ParagonIE_Sodium_Core_Util::declareScalarType( $client_public, 'string', 3 );
1721
+        ParagonIE_Sodium_Core_Util::declareScalarType( $server_public, 'string', 4 );
1722 1722
 
1723 1723
         /* Input validation: */
1724
-        if (ParagonIE_Sodium_Core_Util::strlen($my_secret) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
1725
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
1724
+        if ( ParagonIE_Sodium_Core_Util::strlen( $my_secret ) !== self::CRYPTO_BOX_SECRETKEYBYTES ) {
1725
+            throw new SodiumException( 'Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.' );
1726 1726
         }
1727
-        if (ParagonIE_Sodium_Core_Util::strlen($their_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1728
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1727
+        if ( ParagonIE_Sodium_Core_Util::strlen( $their_public ) !== self::CRYPTO_BOX_PUBLICKEYBYTES ) {
1728
+            throw new SodiumException( 'Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.' );
1729 1729
         }
1730
-        if (ParagonIE_Sodium_Core_Util::strlen($client_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1731
-            throw new SodiumException('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1730
+        if ( ParagonIE_Sodium_Core_Util::strlen( $client_public ) !== self::CRYPTO_BOX_PUBLICKEYBYTES ) {
1731
+            throw new SodiumException( 'Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES long.' );
1732 1732
         }
1733
-        if (ParagonIE_Sodium_Core_Util::strlen($server_public) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
1734
-            throw new SodiumException('Argument 4 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
1733
+        if ( ParagonIE_Sodium_Core_Util::strlen( $server_public ) !== self::CRYPTO_BOX_PUBLICKEYBYTES ) {
1734
+            throw new SodiumException( 'Argument 4 must be CRYPTO_BOX_PUBLICKEYBYTES long.' );
1735 1735
         }
1736 1736
 
1737
-        if (self::useNewSodiumAPI() && !$dontFallback) {
1738
-            if (is_callable('sodium_crypto_kx')) {
1739
-                return (string) sodium_crypto_kx(
1737
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
1738
+            if ( is_callable( 'sodium_crypto_kx' ) ) {
1739
+                return (string)sodium_crypto_kx(
1740 1740
                     $my_secret,
1741 1741
                     $their_public,
1742 1742
                     $client_public,
@@ -1744,8 +1744,8 @@  discard block
 block discarded – undo
1744 1744
                 );
1745 1745
             }
1746 1746
         }
1747
-        if (self::use_fallback('crypto_kx')) {
1748
-            return (string) call_user_func(
1747
+        if ( self::use_fallback( 'crypto_kx' ) ) {
1748
+            return (string)call_user_func(
1749 1749
                 '\\Sodium\\crypto_kx',
1750 1750
                 $my_secret,
1751 1751
                 $their_public,
@@ -1753,7 +1753,7 @@  discard block
 block discarded – undo
1753 1753
                 $server_public
1754 1754
             );
1755 1755
         }
1756
-        if (PHP_INT_SIZE === 4) {
1756
+        if ( PHP_INT_SIZE === 4 ) {
1757 1757
             return ParagonIE_Sodium_Crypto32::keyExchange(
1758 1758
                 $my_secret,
1759 1759
                 $their_public,
@@ -1774,18 +1774,18 @@  discard block
 block discarded – undo
1774 1774
      * @return string
1775 1775
      * @throws SodiumException
1776 1776
      */
1777
-    public static function crypto_kx_seed_keypair($seed)
1777
+    public static function crypto_kx_seed_keypair( $seed )
1778 1778
     {
1779
-        ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1);
1779
+        ParagonIE_Sodium_Core_Util::declareScalarType( $seed, 'string', 1 );
1780 1780
 
1781
-        $seed = (string) $seed;
1781
+        $seed = (string)$seed;
1782 1782
 
1783
-        if (ParagonIE_Sodium_Core_Util::strlen($seed) !== self::CRYPTO_KX_SEEDBYTES) {
1784
-            throw new SodiumException('seed must be SODIUM_CRYPTO_KX_SEEDBYTES bytes');
1783
+        if ( ParagonIE_Sodium_Core_Util::strlen( $seed ) !== self::CRYPTO_KX_SEEDBYTES ) {
1784
+            throw new SodiumException( 'seed must be SODIUM_CRYPTO_KX_SEEDBYTES bytes' );
1785 1785
         }
1786 1786
 
1787
-        $sk = self::crypto_generichash($seed, '', self::CRYPTO_KX_SECRETKEYBYTES);
1788
-        $pk = self::crypto_scalarmult_base($sk);
1787
+        $sk = self::crypto_generichash( $seed, '', self::CRYPTO_KX_SECRETKEYBYTES );
1788
+        $pk = self::crypto_scalarmult_base( $sk );
1789 1789
         return $sk . $pk;
1790 1790
     }
1791 1791
 
@@ -1795,8 +1795,8 @@  discard block
 block discarded – undo
1795 1795
      */
1796 1796
     public static function crypto_kx_keypair()
1797 1797
     {
1798
-        $sk = self::randombytes_buf(self::CRYPTO_KX_SECRETKEYBYTES);
1799
-        $pk = self::crypto_scalarmult_base($sk);
1798
+        $sk = self::randombytes_buf( self::CRYPTO_KX_SECRETKEYBYTES );
1799
+        $pk = self::crypto_scalarmult_base( $sk );
1800 1800
         return $sk . $pk;
1801 1801
     }
1802 1802
 
@@ -1806,28 +1806,28 @@  discard block
 block discarded – undo
1806 1806
      * @return array{0: string, 1: string}
1807 1807
      * @throws SodiumException
1808 1808
      */
1809
-    public static function crypto_kx_client_session_keys($keypair, $serverPublicKey)
1809
+    public static function crypto_kx_client_session_keys( $keypair, $serverPublicKey )
1810 1810
     {
1811
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1812
-        ParagonIE_Sodium_Core_Util::declareScalarType($serverPublicKey, 'string', 2);
1811
+        ParagonIE_Sodium_Core_Util::declareScalarType( $keypair, 'string', 1 );
1812
+        ParagonIE_Sodium_Core_Util::declareScalarType( $serverPublicKey, 'string', 2 );
1813 1813
 
1814
-        $keypair = (string) $keypair;
1815
-        $serverPublicKey = (string) $serverPublicKey;
1814
+        $keypair = (string)$keypair;
1815
+        $serverPublicKey = (string)$serverPublicKey;
1816 1816
 
1817
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_KX_KEYPAIRBYTES) {
1818
-            throw new SodiumException('keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes');
1817
+        if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== self::CRYPTO_KX_KEYPAIRBYTES ) {
1818
+            throw new SodiumException( 'keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes' );
1819 1819
         }
1820
-        if (ParagonIE_Sodium_Core_Util::strlen($serverPublicKey) !== self::CRYPTO_KX_PUBLICKEYBYTES) {
1821
-            throw new SodiumException('public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes');
1820
+        if ( ParagonIE_Sodium_Core_Util::strlen( $serverPublicKey ) !== self::CRYPTO_KX_PUBLICKEYBYTES ) {
1821
+            throw new SodiumException( 'public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes' );
1822 1822
         }
1823 1823
 
1824
-        $sk = self::crypto_kx_secretkey($keypair);
1825
-        $pk = self::crypto_kx_publickey($keypair);
1826
-        $h = self::crypto_generichash_init(null, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
1827
-        self::crypto_generichash_update($h, self::crypto_scalarmult($sk, $serverPublicKey));
1828
-        self::crypto_generichash_update($h, $pk);
1829
-        self::crypto_generichash_update($h, $serverPublicKey);
1830
-        $sessionKeys = self::crypto_generichash_final($h, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
1824
+        $sk = self::crypto_kx_secretkey( $keypair );
1825
+        $pk = self::crypto_kx_publickey( $keypair );
1826
+        $h = self::crypto_generichash_init( null, self::CRYPTO_KX_SESSIONKEYBYTES * 2 );
1827
+        self::crypto_generichash_update( $h, self::crypto_scalarmult( $sk, $serverPublicKey ) );
1828
+        self::crypto_generichash_update( $h, $pk );
1829
+        self::crypto_generichash_update( $h, $serverPublicKey );
1830
+        $sessionKeys = self::crypto_generichash_final( $h, self::CRYPTO_KX_SESSIONKEYBYTES * 2 );
1831 1831
         return array(
1832 1832
             ParagonIE_Sodium_Core_Util::substr(
1833 1833
                 $sessionKeys,
@@ -1848,28 +1848,28 @@  discard block
 block discarded – undo
1848 1848
      * @return array{0: string, 1: string}
1849 1849
      * @throws SodiumException
1850 1850
      */
1851
-    public static function crypto_kx_server_session_keys($keypair, $clientPublicKey)
1851
+    public static function crypto_kx_server_session_keys( $keypair, $clientPublicKey )
1852 1852
     {
1853
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1854
-        ParagonIE_Sodium_Core_Util::declareScalarType($clientPublicKey, 'string', 2);
1853
+        ParagonIE_Sodium_Core_Util::declareScalarType( $keypair, 'string', 1 );
1854
+        ParagonIE_Sodium_Core_Util::declareScalarType( $clientPublicKey, 'string', 2 );
1855 1855
 
1856
-        $keypair = (string) $keypair;
1857
-        $clientPublicKey = (string) $clientPublicKey;
1856
+        $keypair = (string)$keypair;
1857
+        $clientPublicKey = (string)$clientPublicKey;
1858 1858
 
1859
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_KX_KEYPAIRBYTES) {
1860
-            throw new SodiumException('keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes');
1859
+        if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== self::CRYPTO_KX_KEYPAIRBYTES ) {
1860
+            throw new SodiumException( 'keypair should be SODIUM_CRYPTO_KX_KEYPAIRBYTES bytes' );
1861 1861
         }
1862
-        if (ParagonIE_Sodium_Core_Util::strlen($clientPublicKey) !== self::CRYPTO_KX_PUBLICKEYBYTES) {
1863
-            throw new SodiumException('public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes');
1862
+        if ( ParagonIE_Sodium_Core_Util::strlen( $clientPublicKey ) !== self::CRYPTO_KX_PUBLICKEYBYTES ) {
1863
+            throw new SodiumException( 'public keys must be SODIUM_CRYPTO_KX_PUBLICKEYBYTES bytes' );
1864 1864
         }
1865 1865
 
1866
-        $sk = self::crypto_kx_secretkey($keypair);
1867
-        $pk = self::crypto_kx_publickey($keypair);
1868
-        $h = self::crypto_generichash_init(null, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
1869
-        self::crypto_generichash_update($h, self::crypto_scalarmult($sk, $clientPublicKey));
1870
-        self::crypto_generichash_update($h, $clientPublicKey);
1871
-        self::crypto_generichash_update($h, $pk);
1872
-        $sessionKeys = self::crypto_generichash_final($h, self::CRYPTO_KX_SESSIONKEYBYTES * 2);
1866
+        $sk = self::crypto_kx_secretkey( $keypair );
1867
+        $pk = self::crypto_kx_publickey( $keypair );
1868
+        $h = self::crypto_generichash_init( null, self::CRYPTO_KX_SESSIONKEYBYTES * 2 );
1869
+        self::crypto_generichash_update( $h, self::crypto_scalarmult( $sk, $clientPublicKey ) );
1870
+        self::crypto_generichash_update( $h, $clientPublicKey );
1871
+        self::crypto_generichash_update( $h, $pk );
1872
+        $sessionKeys = self::crypto_generichash_final( $h, self::CRYPTO_KX_SESSIONKEYBYTES * 2 );
1873 1873
         return array(
1874 1874
             ParagonIE_Sodium_Core_Util::substr(
1875 1875
                 $sessionKeys,
@@ -1889,7 +1889,7 @@  discard block
 block discarded – undo
1889 1889
      * @return string
1890 1890
      * @throws SodiumException
1891 1891
      */
1892
-    public static function crypto_kx_secretkey($kp)
1892
+    public static function crypto_kx_secretkey( $kp )
1893 1893
     {
1894 1894
         return ParagonIE_Sodium_Core_Util::substr(
1895 1895
             $kp,
@@ -1903,7 +1903,7 @@  discard block
 block discarded – undo
1903 1903
      * @return string
1904 1904
      * @throws SodiumException
1905 1905
      */
1906
-    public static function crypto_kx_publickey($kp)
1906
+    public static function crypto_kx_publickey( $kp )
1907 1907
     {
1908 1908
         return ParagonIE_Sodium_Core_Util::substr(
1909 1909
             $kp,
@@ -1924,23 +1924,23 @@  discard block
 block discarded – undo
1924 1924
      * @throws TypeError
1925 1925
      * @psalm-suppress MixedArgument
1926 1926
      */
1927
-    public static function crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit, $alg = null)
1927
+    public static function crypto_pwhash( $outlen, $passwd, $salt, $opslimit, $memlimit, $alg = null )
1928 1928
     {
1929
-        ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1);
1930
-        ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2);
1931
-        ParagonIE_Sodium_Core_Util::declareScalarType($salt,  'string', 3);
1932
-        ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 4);
1933
-        ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 5);
1929
+        ParagonIE_Sodium_Core_Util::declareScalarType( $outlen, 'int', 1 );
1930
+        ParagonIE_Sodium_Core_Util::declareScalarType( $passwd, 'string', 2 );
1931
+        ParagonIE_Sodium_Core_Util::declareScalarType( $salt, 'string', 3 );
1932
+        ParagonIE_Sodium_Core_Util::declareScalarType( $opslimit, 'int', 4 );
1933
+        ParagonIE_Sodium_Core_Util::declareScalarType( $memlimit, 'int', 5 );
1934 1934
 
1935
-        if (self::useNewSodiumAPI()) {
1936
-            if (!is_null($alg)) {
1937
-                ParagonIE_Sodium_Core_Util::declareScalarType($alg, 'int', 6);
1938
-                return sodium_crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit, $alg);
1935
+        if ( self::useNewSodiumAPI() ) {
1936
+            if ( ! is_null( $alg ) ) {
1937
+                ParagonIE_Sodium_Core_Util::declareScalarType( $alg, 'int', 6 );
1938
+                return sodium_crypto_pwhash( $outlen, $passwd, $salt, $opslimit, $memlimit, $alg );
1939 1939
             }
1940
-            return sodium_crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit);
1940
+            return sodium_crypto_pwhash( $outlen, $passwd, $salt, $opslimit, $memlimit );
1941 1941
         }
1942
-        if (self::use_fallback('crypto_pwhash')) {
1943
-            return (string) call_user_func('\\Sodium\\crypto_pwhash', $outlen, $passwd, $salt, $opslimit, $memlimit);
1942
+        if ( self::use_fallback( 'crypto_pwhash' ) ) {
1943
+            return (string)call_user_func( '\\Sodium\\crypto_pwhash', $outlen, $passwd, $salt, $opslimit, $memlimit );
1944 1944
         }
1945 1945
         // This is the best we can do.
1946 1946
         throw new SodiumException(
@@ -1958,10 +1958,10 @@  discard block
 block discarded – undo
1958 1958
      */
1959 1959
     public static function crypto_pwhash_is_available()
1960 1960
     {
1961
-        if (self::useNewSodiumAPI()) {
1961
+        if ( self::useNewSodiumAPI() ) {
1962 1962
             return true;
1963 1963
         }
1964
-        if (self::use_fallback('crypto_pwhash')) {
1964
+        if ( self::use_fallback( 'crypto_pwhash' ) ) {
1965 1965
             return true;
1966 1966
         }
1967 1967
         return false;
@@ -1976,17 +1976,17 @@  discard block
 block discarded – undo
1976 1976
      * @throws TypeError
1977 1977
      * @psalm-suppress MixedArgument
1978 1978
      */
1979
-    public static function crypto_pwhash_str($passwd, $opslimit, $memlimit)
1979
+    public static function crypto_pwhash_str( $passwd, $opslimit, $memlimit )
1980 1980
     {
1981
-        ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
1982
-        ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2);
1983
-        ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);
1981
+        ParagonIE_Sodium_Core_Util::declareScalarType( $passwd, 'string', 1 );
1982
+        ParagonIE_Sodium_Core_Util::declareScalarType( $opslimit, 'int', 2 );
1983
+        ParagonIE_Sodium_Core_Util::declareScalarType( $memlimit, 'int', 3 );
1984 1984
 
1985
-        if (self::useNewSodiumAPI()) {
1986
-            return sodium_crypto_pwhash_str($passwd, $opslimit, $memlimit);
1985
+        if ( self::useNewSodiumAPI() ) {
1986
+            return sodium_crypto_pwhash_str( $passwd, $opslimit, $memlimit );
1987 1987
         }
1988
-        if (self::use_fallback('crypto_pwhash_str')) {
1989
-            return (string) call_user_func('\\Sodium\\crypto_pwhash_str', $passwd, $opslimit, $memlimit);
1988
+        if ( self::use_fallback( 'crypto_pwhash_str' ) ) {
1989
+            return (string)call_user_func( '\\Sodium\\crypto_pwhash_str', $passwd, $opslimit, $memlimit );
1990 1990
         }
1991 1991
         // This is the best we can do.
1992 1992
         throw new SodiumException(
@@ -2003,25 +2003,25 @@  discard block
 block discarded – undo
2003 2003
      * @return bool
2004 2004
      * @throws SodiumException
2005 2005
      */
2006
-    public static function crypto_pwhash_str_needs_rehash($hash, $opslimit, $memlimit)
2006
+    public static function crypto_pwhash_str_needs_rehash( $hash, $opslimit, $memlimit )
2007 2007
     {
2008
-        ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 1);
2009
-        ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2);
2010
-        ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);
2008
+        ParagonIE_Sodium_Core_Util::declareScalarType( $hash, 'string', 1 );
2009
+        ParagonIE_Sodium_Core_Util::declareScalarType( $opslimit, 'int', 2 );
2010
+        ParagonIE_Sodium_Core_Util::declareScalarType( $memlimit, 'int', 3 );
2011 2011
 
2012 2012
         // Just grab the first 4 pieces.
2013
-        $pieces = explode('$', (string) $hash);
2014
-        $prefix = implode('$', array_slice($pieces, 0, 4));
2013
+        $pieces = explode( '$', (string)$hash );
2014
+        $prefix = implode( '$', array_slice( $pieces, 0, 4 ) );
2015 2015
 
2016 2016
         // Rebuild the expected header.
2017 2017
         /** @var int $ops */
2018
-        $ops = (int) $opslimit;
2018
+        $ops = (int)$opslimit;
2019 2019
         /** @var int $mem */
2020
-        $mem = (int) $memlimit >> 10;
2020
+        $mem = (int)$memlimit >> 10;
2021 2021
         $encoded = self::CRYPTO_PWHASH_STRPREFIX . 'v=19$m=' . $mem . ',t=' . $ops . ',p=1';
2022 2022
 
2023 2023
         // Do they match? If so, we don't need to rehash, so return false.
2024
-        return !ParagonIE_Sodium_Core_Util::hashEquals($encoded, $prefix);
2024
+        return ! ParagonIE_Sodium_Core_Util::hashEquals( $encoded, $prefix );
2025 2025
     }
2026 2026
 
2027 2027
     /**
@@ -2032,16 +2032,16 @@  discard block
 block discarded – undo
2032 2032
      * @throws TypeError
2033 2033
      * @psalm-suppress MixedArgument
2034 2034
      */
2035
-    public static function crypto_pwhash_str_verify($passwd, $hash)
2035
+    public static function crypto_pwhash_str_verify( $passwd, $hash )
2036 2036
     {
2037
-        ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
2038
-        ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2);
2037
+        ParagonIE_Sodium_Core_Util::declareScalarType( $passwd, 'string', 1 );
2038
+        ParagonIE_Sodium_Core_Util::declareScalarType( $hash, 'string', 2 );
2039 2039
 
2040
-        if (self::useNewSodiumAPI()) {
2041
-            return (bool) sodium_crypto_pwhash_str_verify($passwd, $hash);
2040
+        if ( self::useNewSodiumAPI() ) {
2041
+            return (bool)sodium_crypto_pwhash_str_verify( $passwd, $hash );
2042 2042
         }
2043
-        if (self::use_fallback('crypto_pwhash_str_verify')) {
2044
-            return (bool) call_user_func('\\Sodium\\crypto_pwhash_str_verify', $passwd, $hash);
2043
+        if ( self::use_fallback( 'crypto_pwhash_str_verify' ) ) {
2044
+            return (bool)call_user_func( '\\Sodium\\crypto_pwhash_str_verify', $passwd, $hash );
2045 2045
         }
2046 2046
         // This is the best we can do.
2047 2047
         throw new SodiumException(
@@ -2059,31 +2059,31 @@  discard block
 block discarded – undo
2059 2059
      * @throws SodiumException
2060 2060
      * @throws TypeError
2061 2061
      */
2062
-    public static function crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit)
2063
-    {
2064
-        ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1);
2065
-        ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2);
2066
-        ParagonIE_Sodium_Core_Util::declareScalarType($salt,  'string', 3);
2067
-        ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 4);
2068
-        ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 5);
2069
-
2070
-        if (self::useNewSodiumAPI()) {
2071
-            return (string) sodium_crypto_pwhash_scryptsalsa208sha256(
2072
-                (int) $outlen,
2073
-                (string) $passwd,
2074
-                (string) $salt,
2075
-                (int) $opslimit,
2076
-                (int) $memlimit
2062
+    public static function crypto_pwhash_scryptsalsa208sha256( $outlen, $passwd, $salt, $opslimit, $memlimit )
2063
+    {
2064
+        ParagonIE_Sodium_Core_Util::declareScalarType( $outlen, 'int', 1 );
2065
+        ParagonIE_Sodium_Core_Util::declareScalarType( $passwd, 'string', 2 );
2066
+        ParagonIE_Sodium_Core_Util::declareScalarType( $salt, 'string', 3 );
2067
+        ParagonIE_Sodium_Core_Util::declareScalarType( $opslimit, 'int', 4 );
2068
+        ParagonIE_Sodium_Core_Util::declareScalarType( $memlimit, 'int', 5 );
2069
+
2070
+        if ( self::useNewSodiumAPI() ) {
2071
+            return (string)sodium_crypto_pwhash_scryptsalsa208sha256(
2072
+                (int)$outlen,
2073
+                (string)$passwd,
2074
+                (string)$salt,
2075
+                (int)$opslimit,
2076
+                (int)$memlimit
2077 2077
             );
2078 2078
         }
2079
-        if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256')) {
2080
-            return (string) call_user_func(
2079
+        if ( self::use_fallback( 'crypto_pwhash_scryptsalsa208sha256' ) ) {
2080
+            return (string)call_user_func(
2081 2081
                 '\\Sodium\\crypto_pwhash_scryptsalsa208sha256',
2082
-                (int) $outlen,
2083
-                (string) $passwd,
2084
-                (string) $salt,
2085
-                (int) $opslimit,
2086
-                (int) $memlimit
2082
+                (int)$outlen,
2083
+                (string)$passwd,
2084
+                (string)$salt,
2085
+                (int)$opslimit,
2086
+                (int)$memlimit
2087 2087
             );
2088 2088
         }
2089 2089
         // This is the best we can do.
@@ -2102,10 +2102,10 @@  discard block
 block discarded – undo
2102 2102
      */
2103 2103
     public static function crypto_pwhash_scryptsalsa208sha256_is_available()
2104 2104
     {
2105
-        if (self::useNewSodiumAPI()) {
2105
+        if ( self::useNewSodiumAPI() ) {
2106 2106
             return true;
2107 2107
         }
2108
-        if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256')) {
2108
+        if ( self::use_fallback( 'crypto_pwhash_scryptsalsa208sha256' ) ) {
2109 2109
             return true;
2110 2110
         }
2111 2111
         return false;
@@ -2119,25 +2119,25 @@  discard block
 block discarded – undo
2119 2119
      * @throws SodiumException
2120 2120
      * @throws TypeError
2121 2121
      */
2122
-    public static function crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit)
2122
+    public static function crypto_pwhash_scryptsalsa208sha256_str( $passwd, $opslimit, $memlimit )
2123 2123
     {
2124
-        ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
2125
-        ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2);
2126
-        ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);
2124
+        ParagonIE_Sodium_Core_Util::declareScalarType( $passwd, 'string', 1 );
2125
+        ParagonIE_Sodium_Core_Util::declareScalarType( $opslimit, 'int', 2 );
2126
+        ParagonIE_Sodium_Core_Util::declareScalarType( $memlimit, 'int', 3 );
2127 2127
 
2128
-        if (self::useNewSodiumAPI()) {
2129
-            return (string) sodium_crypto_pwhash_scryptsalsa208sha256_str(
2130
-                (string) $passwd,
2131
-                (int) $opslimit,
2132
-                (int) $memlimit
2128
+        if ( self::useNewSodiumAPI() ) {
2129
+            return (string)sodium_crypto_pwhash_scryptsalsa208sha256_str(
2130
+                (string)$passwd,
2131
+                (int)$opslimit,
2132
+                (int)$memlimit
2133 2133
             );
2134 2134
         }
2135
-        if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256_str')) {
2136
-            return (string) call_user_func(
2135
+        if ( self::use_fallback( 'crypto_pwhash_scryptsalsa208sha256_str' ) ) {
2136
+            return (string)call_user_func(
2137 2137
                 '\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str',
2138
-                (string) $passwd,
2139
-                (int) $opslimit,
2140
-                (int) $memlimit
2138
+                (string)$passwd,
2139
+                (int)$opslimit,
2140
+                (int)$memlimit
2141 2141
             );
2142 2142
         }
2143 2143
         // This is the best we can do.
@@ -2153,22 +2153,22 @@  discard block
 block discarded – undo
2153 2153
      * @throws SodiumException
2154 2154
      * @throws TypeError
2155 2155
      */
2156
-    public static function crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash)
2156
+    public static function crypto_pwhash_scryptsalsa208sha256_str_verify( $passwd, $hash )
2157 2157
     {
2158
-        ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
2159
-        ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2);
2158
+        ParagonIE_Sodium_Core_Util::declareScalarType( $passwd, 'string', 1 );
2159
+        ParagonIE_Sodium_Core_Util::declareScalarType( $hash, 'string', 2 );
2160 2160
 
2161
-        if (self::useNewSodiumAPI()) {
2162
-            return (bool) sodium_crypto_pwhash_scryptsalsa208sha256_str_verify(
2163
-                (string) $passwd,
2164
-                (string) $hash
2161
+        if ( self::useNewSodiumAPI() ) {
2162
+            return (bool)sodium_crypto_pwhash_scryptsalsa208sha256_str_verify(
2163
+                (string)$passwd,
2164
+                (string)$hash
2165 2165
             );
2166 2166
         }
2167
-        if (self::use_fallback('crypto_pwhash_scryptsalsa208sha256_str_verify')) {
2168
-            return (bool) call_user_func(
2167
+        if ( self::use_fallback( 'crypto_pwhash_scryptsalsa208sha256_str_verify' ) ) {
2168
+            return (bool)call_user_func(
2169 2169
                 '\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str_verify',
2170
-                (string) $passwd,
2171
-                (string) $hash
2170
+                (string)$passwd,
2171
+                (string)$hash
2172 2172
             );
2173 2173
         }
2174 2174
         // This is the best we can do.
@@ -2190,38 +2190,38 @@  discard block
 block discarded – undo
2190 2190
      * @throws TypeError
2191 2191
      * @psalm-suppress MixedArgument
2192 2192
      */
2193
-    public static function crypto_scalarmult($secretKey, $publicKey)
2193
+    public static function crypto_scalarmult( $secretKey, $publicKey )
2194 2194
     {
2195 2195
         /* Type checks: */
2196
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
2197
-        ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
2196
+        ParagonIE_Sodium_Core_Util::declareScalarType( $secretKey, 'string', 1 );
2197
+        ParagonIE_Sodium_Core_Util::declareScalarType( $publicKey, 'string', 2 );
2198 2198
 
2199 2199
         /* Input validation: */
2200
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
2201
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
2200
+        if ( ParagonIE_Sodium_Core_Util::strlen( $secretKey ) !== self::CRYPTO_BOX_SECRETKEYBYTES ) {
2201
+            throw new SodiumException( 'Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.' );
2202 2202
         }
2203
-        if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_BOX_PUBLICKEYBYTES) {
2204
-            throw new SodiumException('Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.');
2203
+        if ( ParagonIE_Sodium_Core_Util::strlen( $publicKey ) !== self::CRYPTO_BOX_PUBLICKEYBYTES ) {
2204
+            throw new SodiumException( 'Argument 2 must be CRYPTO_BOX_PUBLICKEYBYTES long.' );
2205 2205
         }
2206 2206
 
2207
-        if (self::useNewSodiumAPI()) {
2208
-            return sodium_crypto_scalarmult($secretKey, $publicKey);
2207
+        if ( self::useNewSodiumAPI() ) {
2208
+            return sodium_crypto_scalarmult( $secretKey, $publicKey );
2209 2209
         }
2210
-        if (self::use_fallback('crypto_scalarmult')) {
2211
-            return (string) call_user_func('\\Sodium\\crypto_scalarmult', $secretKey, $publicKey);
2210
+        if ( self::use_fallback( 'crypto_scalarmult' ) ) {
2211
+            return (string)call_user_func( '\\Sodium\\crypto_scalarmult', $secretKey, $publicKey );
2212 2212
         }
2213 2213
 
2214 2214
         /* Output validation: Forbid all-zero keys */
2215
-        if (ParagonIE_Sodium_Core_Util::hashEquals($secretKey, str_repeat("\0", self::CRYPTO_BOX_SECRETKEYBYTES))) {
2216
-            throw new SodiumException('Zero secret key is not allowed');
2215
+        if ( ParagonIE_Sodium_Core_Util::hashEquals( $secretKey, str_repeat( "\0", self::CRYPTO_BOX_SECRETKEYBYTES ) ) ) {
2216
+            throw new SodiumException( 'Zero secret key is not allowed' );
2217 2217
         }
2218
-        if (ParagonIE_Sodium_Core_Util::hashEquals($publicKey, str_repeat("\0", self::CRYPTO_BOX_PUBLICKEYBYTES))) {
2219
-            throw new SodiumException('Zero public key is not allowed');
2218
+        if ( ParagonIE_Sodium_Core_Util::hashEquals( $publicKey, str_repeat( "\0", self::CRYPTO_BOX_PUBLICKEYBYTES ) ) ) {
2219
+            throw new SodiumException( 'Zero public key is not allowed' );
2220 2220
         }
2221
-        if (PHP_INT_SIZE === 4) {
2222
-            return ParagonIE_Sodium_Crypto32::scalarmult($secretKey, $publicKey);
2221
+        if ( PHP_INT_SIZE === 4 ) {
2222
+            return ParagonIE_Sodium_Crypto32::scalarmult( $secretKey, $publicKey );
2223 2223
         }
2224
-        return ParagonIE_Sodium_Crypto::scalarmult($secretKey, $publicKey);
2224
+        return ParagonIE_Sodium_Crypto::scalarmult( $secretKey, $publicKey );
2225 2225
     }
2226 2226
 
2227 2227
     /**
@@ -2234,29 +2234,29 @@  discard block
 block discarded – undo
2234 2234
      * @psalm-suppress TooFewArguments
2235 2235
      * @psalm-suppress MixedArgument
2236 2236
      */
2237
-    public static function crypto_scalarmult_base($secretKey)
2237
+    public static function crypto_scalarmult_base( $secretKey )
2238 2238
     {
2239 2239
         /* Type checks: */
2240
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
2240
+        ParagonIE_Sodium_Core_Util::declareScalarType( $secretKey, 'string', 1 );
2241 2241
 
2242 2242
         /* Input validation: */
2243
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_BOX_SECRETKEYBYTES) {
2244
-            throw new SodiumException('Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.');
2243
+        if ( ParagonIE_Sodium_Core_Util::strlen( $secretKey ) !== self::CRYPTO_BOX_SECRETKEYBYTES ) {
2244
+            throw new SodiumException( 'Argument 1 must be CRYPTO_BOX_SECRETKEYBYTES long.' );
2245 2245
         }
2246 2246
 
2247
-        if (self::useNewSodiumAPI()) {
2248
-            return sodium_crypto_scalarmult_base($secretKey);
2247
+        if ( self::useNewSodiumAPI() ) {
2248
+            return sodium_crypto_scalarmult_base( $secretKey );
2249 2249
         }
2250
-        if (self::use_fallback('crypto_scalarmult_base')) {
2251
-            return (string) call_user_func('\\Sodium\\crypto_scalarmult_base', $secretKey);
2250
+        if ( self::use_fallback( 'crypto_scalarmult_base' ) ) {
2251
+            return (string)call_user_func( '\\Sodium\\crypto_scalarmult_base', $secretKey );
2252 2252
         }
2253
-        if (ParagonIE_Sodium_Core_Util::hashEquals($secretKey, str_repeat("\0", self::CRYPTO_BOX_SECRETKEYBYTES))) {
2254
-            throw new SodiumException('Zero secret key is not allowed');
2253
+        if ( ParagonIE_Sodium_Core_Util::hashEquals( $secretKey, str_repeat( "\0", self::CRYPTO_BOX_SECRETKEYBYTES ) ) ) {
2254
+            throw new SodiumException( 'Zero secret key is not allowed' );
2255 2255
         }
2256
-        if (PHP_INT_SIZE === 4) {
2257
-            return ParagonIE_Sodium_Crypto32::scalarmult_base($secretKey);
2256
+        if ( PHP_INT_SIZE === 4 ) {
2257
+            return ParagonIE_Sodium_Crypto32::scalarmult_base( $secretKey );
2258 2258
         }
2259
-        return ParagonIE_Sodium_Crypto::scalarmult_base($secretKey);
2259
+        return ParagonIE_Sodium_Crypto::scalarmult_base( $secretKey );
2260 2260
     }
2261 2261
 
2262 2262
     /**
@@ -2272,31 +2272,31 @@  discard block
 block discarded – undo
2272 2272
      * @throws TypeError
2273 2273
      * @psalm-suppress MixedArgument
2274 2274
      */
2275
-    public static function crypto_secretbox($plaintext, $nonce, $key)
2275
+    public static function crypto_secretbox( $plaintext, $nonce, $key )
2276 2276
     {
2277 2277
         /* Type checks: */
2278
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
2279
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2280
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2278
+        ParagonIE_Sodium_Core_Util::declareScalarType( $plaintext, 'string', 1 );
2279
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 2 );
2280
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 3 );
2281 2281
 
2282 2282
         /* Input validation: */
2283
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) {
2284
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2283
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_SECRETBOX_NONCEBYTES ) {
2284
+            throw new SodiumException( 'Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.' );
2285 2285
         }
2286
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) {
2287
-            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
2286
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_SECRETBOX_KEYBYTES ) {
2287
+            throw new SodiumException( 'Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.' );
2288 2288
         }
2289 2289
 
2290
-        if (self::useNewSodiumAPI()) {
2291
-            return sodium_crypto_secretbox($plaintext, $nonce, $key);
2290
+        if ( self::useNewSodiumAPI() ) {
2291
+            return sodium_crypto_secretbox( $plaintext, $nonce, $key );
2292 2292
         }
2293
-        if (self::use_fallback('crypto_secretbox')) {
2294
-            return (string) call_user_func('\\Sodium\\crypto_secretbox', $plaintext, $nonce, $key);
2293
+        if ( self::use_fallback( 'crypto_secretbox' ) ) {
2294
+            return (string)call_user_func( '\\Sodium\\crypto_secretbox', $plaintext, $nonce, $key );
2295 2295
         }
2296
-        if (PHP_INT_SIZE === 4) {
2297
-            return ParagonIE_Sodium_Crypto32::secretbox($plaintext, $nonce, $key);
2296
+        if ( PHP_INT_SIZE === 4 ) {
2297
+            return ParagonIE_Sodium_Crypto32::secretbox( $plaintext, $nonce, $key );
2298 2298
         }
2299
-        return ParagonIE_Sodium_Crypto::secretbox($plaintext, $nonce, $key);
2299
+        return ParagonIE_Sodium_Crypto::secretbox( $plaintext, $nonce, $key );
2300 2300
     }
2301 2301
 
2302 2302
     /**
@@ -2312,35 +2312,35 @@  discard block
 block discarded – undo
2312 2312
      * @psalm-suppress MixedInferredReturnType
2313 2313
      * @psalm-suppress MixedReturnStatement
2314 2314
      */
2315
-    public static function crypto_secretbox_open($ciphertext, $nonce, $key)
2315
+    public static function crypto_secretbox_open( $ciphertext, $nonce, $key )
2316 2316
     {
2317 2317
         /* Type checks: */
2318
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
2319
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2320
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2318
+        ParagonIE_Sodium_Core_Util::declareScalarType( $ciphertext, 'string', 1 );
2319
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 2 );
2320
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 3 );
2321 2321
 
2322 2322
         /* Input validation: */
2323
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) {
2324
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2323
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_SECRETBOX_NONCEBYTES ) {
2324
+            throw new SodiumException( 'Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.' );
2325 2325
         }
2326
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) {
2327
-            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
2326
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_SECRETBOX_KEYBYTES ) {
2327
+            throw new SodiumException( 'Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.' );
2328 2328
         }
2329 2329
 
2330
-        if (self::useNewSodiumAPI()) {
2330
+        if ( self::useNewSodiumAPI() ) {
2331 2331
             /**
2332 2332
              * @psalm-suppress InvalidReturnStatement
2333 2333
              * @psalm-suppress FalsableReturnStatement
2334 2334
              */
2335
-            return sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
2335
+            return sodium_crypto_secretbox_open( $ciphertext, $nonce, $key );
2336 2336
         }
2337
-        if (self::use_fallback('crypto_secretbox_open')) {
2338
-            return call_user_func('\\Sodium\\crypto_secretbox_open', $ciphertext, $nonce, $key);
2337
+        if ( self::use_fallback( 'crypto_secretbox_open' ) ) {
2338
+            return call_user_func( '\\Sodium\\crypto_secretbox_open', $ciphertext, $nonce, $key );
2339 2339
         }
2340
-        if (PHP_INT_SIZE === 4) {
2341
-            return ParagonIE_Sodium_Crypto32::secretbox_open($ciphertext, $nonce, $key);
2340
+        if ( PHP_INT_SIZE === 4 ) {
2341
+            return ParagonIE_Sodium_Crypto32::secretbox_open( $ciphertext, $nonce, $key );
2342 2342
         }
2343
-        return ParagonIE_Sodium_Crypto::secretbox_open($ciphertext, $nonce, $key);
2343
+        return ParagonIE_Sodium_Crypto::secretbox_open( $ciphertext, $nonce, $key );
2344 2344
     }
2345 2345
 
2346 2346
     /**
@@ -2352,7 +2352,7 @@  discard block
 block discarded – undo
2352 2352
      */
2353 2353
     public static function crypto_secretbox_keygen()
2354 2354
     {
2355
-        return random_bytes(self::CRYPTO_SECRETBOX_KEYBYTES);
2355
+        return random_bytes( self::CRYPTO_SECRETBOX_KEYBYTES );
2356 2356
     }
2357 2357
 
2358 2358
     /**
@@ -2368,24 +2368,24 @@  discard block
 block discarded – undo
2368 2368
      * @throws TypeError
2369 2369
      * @psalm-suppress MixedArgument
2370 2370
      */
2371
-    public static function crypto_secretbox_xchacha20poly1305($plaintext, $nonce, $key)
2371
+    public static function crypto_secretbox_xchacha20poly1305( $plaintext, $nonce, $key )
2372 2372
     {
2373 2373
         /* Type checks: */
2374
-        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
2375
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2376
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2374
+        ParagonIE_Sodium_Core_Util::declareScalarType( $plaintext, 'string', 1 );
2375
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 2 );
2376
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 3 );
2377 2377
 
2378 2378
         /* Input validation: */
2379
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) {
2380
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2379
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_SECRETBOX_NONCEBYTES ) {
2380
+            throw new SodiumException( 'Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.' );
2381 2381
         }
2382
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) {
2383
-            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
2382
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_SECRETBOX_KEYBYTES ) {
2383
+            throw new SodiumException( 'Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.' );
2384 2384
         }
2385
-        if (PHP_INT_SIZE === 4) {
2386
-            return ParagonIE_Sodium_Crypto32::secretbox_xchacha20poly1305($plaintext, $nonce, $key);
2385
+        if ( PHP_INT_SIZE === 4 ) {
2386
+            return ParagonIE_Sodium_Crypto32::secretbox_xchacha20poly1305( $plaintext, $nonce, $key );
2387 2387
         }
2388
-        return ParagonIE_Sodium_Crypto::secretbox_xchacha20poly1305($plaintext, $nonce, $key);
2388
+        return ParagonIE_Sodium_Crypto::secretbox_xchacha20poly1305( $plaintext, $nonce, $key );
2389 2389
     }
2390 2390
     /**
2391 2391
      * Decrypts a message previously encrypted with crypto_secretbox_xchacha20poly1305().
@@ -2398,25 +2398,25 @@  discard block
 block discarded – undo
2398 2398
      * @throws TypeError
2399 2399
      * @psalm-suppress MixedArgument
2400 2400
      */
2401
-    public static function crypto_secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
2401
+    public static function crypto_secretbox_xchacha20poly1305_open( $ciphertext, $nonce, $key )
2402 2402
     {
2403 2403
         /* Type checks: */
2404
-        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
2405
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2406
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2404
+        ParagonIE_Sodium_Core_Util::declareScalarType( $ciphertext, 'string', 1 );
2405
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 2 );
2406
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 3 );
2407 2407
 
2408 2408
         /* Input validation: */
2409
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_SECRETBOX_NONCEBYTES) {
2410
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2409
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_SECRETBOX_NONCEBYTES ) {
2410
+            throw new SodiumException( 'Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.' );
2411 2411
         }
2412
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SECRETBOX_KEYBYTES) {
2413
-            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
2412
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_SECRETBOX_KEYBYTES ) {
2413
+            throw new SodiumException( 'Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.' );
2414 2414
         }
2415 2415
 
2416
-        if (PHP_INT_SIZE === 4) {
2417
-            return ParagonIE_Sodium_Crypto32::secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key);
2416
+        if ( PHP_INT_SIZE === 4 ) {
2417
+            return ParagonIE_Sodium_Crypto32::secretbox_xchacha20poly1305_open( $ciphertext, $nonce, $key );
2418 2418
         }
2419
-        return ParagonIE_Sodium_Crypto::secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key);
2419
+        return ParagonIE_Sodium_Crypto::secretbox_xchacha20poly1305_open( $ciphertext, $nonce, $key );
2420 2420
     }
2421 2421
 
2422 2422
     /**
@@ -2425,12 +2425,12 @@  discard block
 block discarded – undo
2425 2425
      * @throws Exception
2426 2426
      * @throws SodiumException
2427 2427
      */
2428
-    public static function crypto_secretstream_xchacha20poly1305_init_push($key)
2428
+    public static function crypto_secretstream_xchacha20poly1305_init_push( $key )
2429 2429
     {
2430
-        if (PHP_INT_SIZE === 4) {
2431
-            return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_push($key);
2430
+        if ( PHP_INT_SIZE === 4 ) {
2431
+            return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_push( $key );
2432 2432
         }
2433
-        return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_push($key);
2433
+        return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_push( $key );
2434 2434
     }
2435 2435
 
2436 2436
     /**
@@ -2439,17 +2439,17 @@  discard block
 block discarded – undo
2439 2439
      * @return string Returns a state.
2440 2440
      * @throws Exception
2441 2441
      */
2442
-    public static function crypto_secretstream_xchacha20poly1305_init_pull($header, $key)
2442
+    public static function crypto_secretstream_xchacha20poly1305_init_pull( $header, $key )
2443 2443
     {
2444
-        if (ParagonIE_Sodium_Core_Util::strlen($header) < self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES) {
2444
+        if ( ParagonIE_Sodium_Core_Util::strlen( $header ) < self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES ) {
2445 2445
             throw new SodiumException(
2446 2446
                 'header size should be SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES bytes'
2447 2447
             );
2448 2448
         }
2449
-        if (PHP_INT_SIZE === 4) {
2450
-            return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_pull($key, $header);
2449
+        if ( PHP_INT_SIZE === 4 ) {
2450
+            return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_pull( $key, $header );
2451 2451
         }
2452
-        return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_pull($key, $header);
2452
+        return ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_init_pull( $key, $header );
2453 2453
     }
2454 2454
 
2455 2455
     /**
@@ -2460,9 +2460,9 @@  discard block
 block discarded – undo
2460 2460
      * @return string
2461 2461
      * @throws SodiumException
2462 2462
      */
2463
-    public static function crypto_secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
2463
+    public static function crypto_secretstream_xchacha20poly1305_push( &$state, $msg, $aad = '', $tag = 0 )
2464 2464
     {
2465
-        if (PHP_INT_SIZE === 4) {
2465
+        if ( PHP_INT_SIZE === 4 ) {
2466 2466
             return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_push(
2467 2467
                 $state,
2468 2468
                 $msg,
@@ -2485,9 +2485,9 @@  discard block
 block discarded – undo
2485 2485
      * @return bool|array{0: string, 1: int}
2486 2486
      * @throws SodiumException
2487 2487
      */
2488
-    public static function crypto_secretstream_xchacha20poly1305_pull(&$state, $msg, $aad = '')
2488
+    public static function crypto_secretstream_xchacha20poly1305_pull( &$state, $msg, $aad = '' )
2489 2489
     {
2490
-        if (PHP_INT_SIZE === 4) {
2490
+        if ( PHP_INT_SIZE === 4 ) {
2491 2491
             return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_pull(
2492 2492
                 $state,
2493 2493
                 $msg,
@@ -2507,7 +2507,7 @@  discard block
 block discarded – undo
2507 2507
      */
2508 2508
     public static function crypto_secretstream_xchacha20poly1305_keygen()
2509 2509
     {
2510
-        return random_bytes(self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES);
2510
+        return random_bytes( self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES );
2511 2511
     }
2512 2512
 
2513 2513
     /**
@@ -2515,12 +2515,12 @@  discard block
 block discarded – undo
2515 2515
      * @return void
2516 2516
      * @throws SodiumException
2517 2517
      */
2518
-    public static function crypto_secretstream_xchacha20poly1305_rekey(&$state)
2518
+    public static function crypto_secretstream_xchacha20poly1305_rekey( &$state )
2519 2519
     {
2520
-        if (PHP_INT_SIZE === 4) {
2521
-            ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_rekey($state);
2520
+        if ( PHP_INT_SIZE === 4 ) {
2521
+            ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_rekey( $state );
2522 2522
         } else {
2523
-            ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_rekey($state);
2523
+            ParagonIE_Sodium_Crypto::secretstream_xchacha20poly1305_rekey( $state );
2524 2524
         }
2525 2525
     }
2526 2526
 
@@ -2536,27 +2536,27 @@  discard block
 block discarded – undo
2536 2536
      * @psalm-suppress MixedInferredReturnType
2537 2537
      * @psalm-suppress MixedReturnStatement
2538 2538
      */
2539
-    public static function crypto_shorthash($message, $key)
2539
+    public static function crypto_shorthash( $message, $key )
2540 2540
     {
2541 2541
         /* Type checks: */
2542
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
2543
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2);
2542
+        ParagonIE_Sodium_Core_Util::declareScalarType( $message, 'string', 1 );
2543
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 2 );
2544 2544
 
2545 2545
         /* Input validation: */
2546
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_SHORTHASH_KEYBYTES) {
2547
-            throw new SodiumException('Argument 2 must be CRYPTO_SHORTHASH_KEYBYTES long.');
2546
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_SHORTHASH_KEYBYTES ) {
2547
+            throw new SodiumException( 'Argument 2 must be CRYPTO_SHORTHASH_KEYBYTES long.' );
2548 2548
         }
2549 2549
 
2550
-        if (self::useNewSodiumAPI()) {
2551
-            return sodium_crypto_shorthash($message, $key);
2550
+        if ( self::useNewSodiumAPI() ) {
2551
+            return sodium_crypto_shorthash( $message, $key );
2552 2552
         }
2553
-        if (self::use_fallback('crypto_shorthash')) {
2554
-            return (string) call_user_func('\\Sodium\\crypto_shorthash', $message, $key);
2553
+        if ( self::use_fallback( 'crypto_shorthash' ) ) {
2554
+            return (string)call_user_func( '\\Sodium\\crypto_shorthash', $message, $key );
2555 2555
         }
2556
-        if (PHP_INT_SIZE === 4) {
2557
-            return ParagonIE_Sodium_Core32_SipHash::sipHash24($message, $key);
2556
+        if ( PHP_INT_SIZE === 4 ) {
2557
+            return ParagonIE_Sodium_Core32_SipHash::sipHash24( $message, $key );
2558 2558
         }
2559
-        return ParagonIE_Sodium_Core_SipHash::sipHash24($message, $key);
2559
+        return ParagonIE_Sodium_Core_SipHash::sipHash24( $message, $key );
2560 2560
     }
2561 2561
 
2562 2562
     /**
@@ -2568,7 +2568,7 @@  discard block
 block discarded – undo
2568 2568
      */
2569 2569
     public static function crypto_shorthash_keygen()
2570 2570
     {
2571
-        return random_bytes(self::CRYPTO_SHORTHASH_KEYBYTES);
2571
+        return random_bytes( self::CRYPTO_SHORTHASH_KEYBYTES );
2572 2572
     }
2573 2573
 
2574 2574
     /**
@@ -2586,27 +2586,27 @@  discard block
 block discarded – undo
2586 2586
      * @psalm-suppress MixedInferredReturnType
2587 2587
      * @psalm-suppress MixedReturnStatement
2588 2588
      */
2589
-    public static function crypto_sign($message, $secretKey)
2589
+    public static function crypto_sign( $message, $secretKey )
2590 2590
     {
2591 2591
         /* Type checks: */
2592
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
2593
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2);
2592
+        ParagonIE_Sodium_Core_Util::declareScalarType( $message, 'string', 1 );
2593
+        ParagonIE_Sodium_Core_Util::declareScalarType( $secretKey, 'string', 2 );
2594 2594
 
2595 2595
         /* Input validation: */
2596
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
2597
-            throw new SodiumException('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long.');
2596
+        if ( ParagonIE_Sodium_Core_Util::strlen( $secretKey ) !== self::CRYPTO_SIGN_SECRETKEYBYTES ) {
2597
+            throw new SodiumException( 'Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long.' );
2598 2598
         }
2599 2599
 
2600
-        if (self::useNewSodiumAPI()) {
2601
-            return sodium_crypto_sign($message, $secretKey);
2600
+        if ( self::useNewSodiumAPI() ) {
2601
+            return sodium_crypto_sign( $message, $secretKey );
2602 2602
         }
2603
-        if (self::use_fallback('crypto_sign')) {
2604
-            return (string) call_user_func('\\Sodium\\crypto_sign', $message, $secretKey);
2603
+        if ( self::use_fallback( 'crypto_sign' ) ) {
2604
+            return (string)call_user_func( '\\Sodium\\crypto_sign', $message, $secretKey );
2605 2605
         }
2606
-        if (PHP_INT_SIZE === 4) {
2607
-            return ParagonIE_Sodium_Crypto32::sign($message, $secretKey);
2606
+        if ( PHP_INT_SIZE === 4 ) {
2607
+            return ParagonIE_Sodium_Crypto32::sign( $message, $secretKey );
2608 2608
         }
2609
-        return ParagonIE_Sodium_Crypto::sign($message, $secretKey);
2609
+        return ParagonIE_Sodium_Crypto::sign( $message, $secretKey );
2610 2610
     }
2611 2611
 
2612 2612
     /**
@@ -2622,34 +2622,34 @@  discard block
 block discarded – undo
2622 2622
      * @psalm-suppress MixedInferredReturnType
2623 2623
      * @psalm-suppress MixedReturnStatement
2624 2624
      */
2625
-    public static function crypto_sign_open($signedMessage, $publicKey)
2625
+    public static function crypto_sign_open( $signedMessage, $publicKey )
2626 2626
     {
2627 2627
         /* Type checks: */
2628
-        ParagonIE_Sodium_Core_Util::declareScalarType($signedMessage, 'string', 1);
2629
-        ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
2628
+        ParagonIE_Sodium_Core_Util::declareScalarType( $signedMessage, 'string', 1 );
2629
+        ParagonIE_Sodium_Core_Util::declareScalarType( $publicKey, 'string', 2 );
2630 2630
 
2631 2631
         /* Input validation: */
2632
-        if (ParagonIE_Sodium_Core_Util::strlen($signedMessage) < self::CRYPTO_SIGN_BYTES) {
2633
-            throw new SodiumException('Argument 1 must be at least CRYPTO_SIGN_BYTES long.');
2632
+        if ( ParagonIE_Sodium_Core_Util::strlen( $signedMessage ) < self::CRYPTO_SIGN_BYTES ) {
2633
+            throw new SodiumException( 'Argument 1 must be at least CRYPTO_SIGN_BYTES long.' );
2634 2634
         }
2635
-        if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) {
2636
-            throw new SodiumException('Argument 2 must be CRYPTO_SIGN_PUBLICKEYBYTES long.');
2635
+        if ( ParagonIE_Sodium_Core_Util::strlen( $publicKey ) !== self::CRYPTO_SIGN_PUBLICKEYBYTES ) {
2636
+            throw new SodiumException( 'Argument 2 must be CRYPTO_SIGN_PUBLICKEYBYTES long.' );
2637 2637
         }
2638 2638
 
2639
-        if (self::useNewSodiumAPI()) {
2639
+        if ( self::useNewSodiumAPI() ) {
2640 2640
             /**
2641 2641
              * @psalm-suppress InvalidReturnStatement
2642 2642
              * @psalm-suppress FalsableReturnStatement
2643 2643
              */
2644
-            return sodium_crypto_sign_open($signedMessage, $publicKey);
2644
+            return sodium_crypto_sign_open( $signedMessage, $publicKey );
2645 2645
         }
2646
-        if (self::use_fallback('crypto_sign_open')) {
2647
-            return call_user_func('\\Sodium\\crypto_sign_open', $signedMessage, $publicKey);
2646
+        if ( self::use_fallback( 'crypto_sign_open' ) ) {
2647
+            return call_user_func( '\\Sodium\\crypto_sign_open', $signedMessage, $publicKey );
2648 2648
         }
2649
-        if (PHP_INT_SIZE === 4) {
2650
-            return ParagonIE_Sodium_Crypto32::sign_open($signedMessage, $publicKey);
2649
+        if ( PHP_INT_SIZE === 4 ) {
2650
+            return ParagonIE_Sodium_Crypto32::sign_open( $signedMessage, $publicKey );
2651 2651
         }
2652
-        return ParagonIE_Sodium_Crypto::sign_open($signedMessage, $publicKey);
2652
+        return ParagonIE_Sodium_Crypto::sign_open( $signedMessage, $publicKey );
2653 2653
     }
2654 2654
 
2655 2655
     /**
@@ -2661,13 +2661,13 @@  discard block
 block discarded – undo
2661 2661
      */
2662 2662
     public static function crypto_sign_keypair()
2663 2663
     {
2664
-        if (self::useNewSodiumAPI()) {
2664
+        if ( self::useNewSodiumAPI() ) {
2665 2665
             return sodium_crypto_sign_keypair();
2666 2666
         }
2667
-        if (self::use_fallback('crypto_sign_keypair')) {
2668
-            return (string) call_user_func('\\Sodium\\crypto_sign_keypair');
2667
+        if ( self::use_fallback( 'crypto_sign_keypair' ) ) {
2668
+            return (string)call_user_func( '\\Sodium\\crypto_sign_keypair' );
2669 2669
         }
2670
-        if (PHP_INT_SIZE === 4) {
2670
+        if ( PHP_INT_SIZE === 4 ) {
2671 2671
             return ParagonIE_Sodium_Core32_Ed25519::keypair();
2672 2672
         }
2673 2673
         return ParagonIE_Sodium_Core_Ed25519::keypair();
@@ -2679,22 +2679,22 @@  discard block
 block discarded – undo
2679 2679
      * @return string
2680 2680
      * @throws SodiumException
2681 2681
      */
2682
-    public static function crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk)
2682
+    public static function crypto_sign_keypair_from_secretkey_and_publickey( $sk, $pk )
2683 2683
     {
2684
-        ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1);
2685
-        ParagonIE_Sodium_Core_Util::declareScalarType($pk, 'string', 1);
2686
-        $sk = (string) $sk;
2687
-        $pk = (string) $pk;
2684
+        ParagonIE_Sodium_Core_Util::declareScalarType( $sk, 'string', 1 );
2685
+        ParagonIE_Sodium_Core_Util::declareScalarType( $pk, 'string', 1 );
2686
+        $sk = (string)$sk;
2687
+        $pk = (string)$pk;
2688 2688
 
2689
-        if (ParagonIE_Sodium_Core_Util::strlen($sk) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
2690
-            throw new SodiumException('secretkey should be SODIUM_CRYPTO_SIGN_SECRETKEYBYTES bytes');
2689
+        if ( ParagonIE_Sodium_Core_Util::strlen( $sk ) !== self::CRYPTO_SIGN_SECRETKEYBYTES ) {
2690
+            throw new SodiumException( 'secretkey should be SODIUM_CRYPTO_SIGN_SECRETKEYBYTES bytes' );
2691 2691
         }
2692
-        if (ParagonIE_Sodium_Core_Util::strlen($pk) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) {
2693
-            throw new SodiumException('publickey should be SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES bytes');
2692
+        if ( ParagonIE_Sodium_Core_Util::strlen( $pk ) !== self::CRYPTO_SIGN_PUBLICKEYBYTES ) {
2693
+            throw new SodiumException( 'publickey should be SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES bytes' );
2694 2694
         }
2695 2695
 
2696
-        if (self::useNewSodiumAPI()) {
2697
-            return sodium_crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk);
2696
+        if ( self::useNewSodiumAPI() ) {
2697
+            return sodium_crypto_sign_keypair_from_secretkey_and_publickey( $sk, $pk );
2698 2698
         }
2699 2699
         return $sk . $pk;
2700 2700
     }
@@ -2708,22 +2708,22 @@  discard block
 block discarded – undo
2708 2708
      * @throws TypeError
2709 2709
      * @psalm-suppress MixedArgument
2710 2710
      */
2711
-    public static function crypto_sign_seed_keypair($seed)
2711
+    public static function crypto_sign_seed_keypair( $seed )
2712 2712
     {
2713
-        ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1);
2713
+        ParagonIE_Sodium_Core_Util::declareScalarType( $seed, 'string', 1 );
2714 2714
 
2715
-        if (self::useNewSodiumAPI()) {
2716
-            return sodium_crypto_sign_seed_keypair($seed);
2715
+        if ( self::useNewSodiumAPI() ) {
2716
+            return sodium_crypto_sign_seed_keypair( $seed );
2717 2717
         }
2718
-        if (self::use_fallback('crypto_sign_keypair')) {
2719
-            return (string) call_user_func('\\Sodium\\crypto_sign_seed_keypair', $seed);
2718
+        if ( self::use_fallback( 'crypto_sign_keypair' ) ) {
2719
+            return (string)call_user_func( '\\Sodium\\crypto_sign_seed_keypair', $seed );
2720 2720
         }
2721 2721
         $publicKey = '';
2722 2722
         $secretKey = '';
2723
-        if (PHP_INT_SIZE === 4) {
2724
-            ParagonIE_Sodium_Core32_Ed25519::seed_keypair($publicKey, $secretKey, $seed);
2723
+        if ( PHP_INT_SIZE === 4 ) {
2724
+            ParagonIE_Sodium_Core32_Ed25519::seed_keypair( $publicKey, $secretKey, $seed );
2725 2725
         } else {
2726
-            ParagonIE_Sodium_Core_Ed25519::seed_keypair($publicKey, $secretKey, $seed);
2726
+            ParagonIE_Sodium_Core_Ed25519::seed_keypair( $publicKey, $secretKey, $seed );
2727 2727
         }
2728 2728
         return $secretKey . $publicKey;
2729 2729
     }
@@ -2737,26 +2737,26 @@  discard block
 block discarded – undo
2737 2737
      * @throws TypeError
2738 2738
      * @psalm-suppress MixedArgument
2739 2739
      */
2740
-    public static function crypto_sign_publickey($keypair)
2740
+    public static function crypto_sign_publickey( $keypair )
2741 2741
     {
2742 2742
         /* Type checks: */
2743
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
2743
+        ParagonIE_Sodium_Core_Util::declareScalarType( $keypair, 'string', 1 );
2744 2744
 
2745 2745
         /* Input validation: */
2746
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_SIGN_KEYPAIRBYTES) {
2747
-            throw new SodiumException('Argument 1 must be CRYPTO_SIGN_KEYPAIRBYTES long.');
2746
+        if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== self::CRYPTO_SIGN_KEYPAIRBYTES ) {
2747
+            throw new SodiumException( 'Argument 1 must be CRYPTO_SIGN_KEYPAIRBYTES long.' );
2748 2748
         }
2749 2749
 
2750
-        if (self::useNewSodiumAPI()) {
2751
-            return sodium_crypto_sign_publickey($keypair);
2750
+        if ( self::useNewSodiumAPI() ) {
2751
+            return sodium_crypto_sign_publickey( $keypair );
2752 2752
         }
2753
-        if (self::use_fallback('crypto_sign_publickey')) {
2754
-            return (string) call_user_func('\\Sodium\\crypto_sign_publickey', $keypair);
2753
+        if ( self::use_fallback( 'crypto_sign_publickey' ) ) {
2754
+            return (string)call_user_func( '\\Sodium\\crypto_sign_publickey', $keypair );
2755 2755
         }
2756
-        if (PHP_INT_SIZE === 4) {
2757
-            return ParagonIE_Sodium_Core32_Ed25519::publickey($keypair);
2756
+        if ( PHP_INT_SIZE === 4 ) {
2757
+            return ParagonIE_Sodium_Core32_Ed25519::publickey( $keypair );
2758 2758
         }
2759
-        return ParagonIE_Sodium_Core_Ed25519::publickey($keypair);
2759
+        return ParagonIE_Sodium_Core_Ed25519::publickey( $keypair );
2760 2760
     }
2761 2761
 
2762 2762
     /**
@@ -2768,26 +2768,26 @@  discard block
 block discarded – undo
2768 2768
      * @throws TypeError
2769 2769
      * @psalm-suppress MixedArgument
2770 2770
      */
2771
-    public static function crypto_sign_publickey_from_secretkey($secretKey)
2771
+    public static function crypto_sign_publickey_from_secretkey( $secretKey )
2772 2772
     {
2773 2773
         /* Type checks: */
2774
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
2774
+        ParagonIE_Sodium_Core_Util::declareScalarType( $secretKey, 'string', 1 );
2775 2775
 
2776 2776
         /* Input validation: */
2777
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
2778
-            throw new SodiumException('Argument 1 must be CRYPTO_SIGN_SECRETKEYBYTES long.');
2777
+        if ( ParagonIE_Sodium_Core_Util::strlen( $secretKey ) !== self::CRYPTO_SIGN_SECRETKEYBYTES ) {
2778
+            throw new SodiumException( 'Argument 1 must be CRYPTO_SIGN_SECRETKEYBYTES long.' );
2779 2779
         }
2780 2780
 
2781
-        if (self::useNewSodiumAPI()) {
2782
-            return sodium_crypto_sign_publickey_from_secretkey($secretKey);
2781
+        if ( self::useNewSodiumAPI() ) {
2782
+            return sodium_crypto_sign_publickey_from_secretkey( $secretKey );
2783 2783
         }
2784
-        if (self::use_fallback('crypto_sign_publickey_from_secretkey')) {
2785
-            return (string) call_user_func('\\Sodium\\crypto_sign_publickey_from_secretkey', $secretKey);
2784
+        if ( self::use_fallback( 'crypto_sign_publickey_from_secretkey' ) ) {
2785
+            return (string)call_user_func( '\\Sodium\\crypto_sign_publickey_from_secretkey', $secretKey );
2786 2786
         }
2787
-        if (PHP_INT_SIZE === 4) {
2788
-            return ParagonIE_Sodium_Core32_Ed25519::publickey_from_secretkey($secretKey);
2787
+        if ( PHP_INT_SIZE === 4 ) {
2788
+            return ParagonIE_Sodium_Core32_Ed25519::publickey_from_secretkey( $secretKey );
2789 2789
         }
2790
-        return ParagonIE_Sodium_Core_Ed25519::publickey_from_secretkey($secretKey);
2790
+        return ParagonIE_Sodium_Core_Ed25519::publickey_from_secretkey( $secretKey );
2791 2791
     }
2792 2792
 
2793 2793
     /**
@@ -2799,26 +2799,26 @@  discard block
 block discarded – undo
2799 2799
      * @throws TypeError
2800 2800
      * @psalm-suppress MixedArgument
2801 2801
      */
2802
-    public static function crypto_sign_secretkey($keypair)
2802
+    public static function crypto_sign_secretkey( $keypair )
2803 2803
     {
2804 2804
         /* Type checks: */
2805
-        ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
2805
+        ParagonIE_Sodium_Core_Util::declareScalarType( $keypair, 'string', 1 );
2806 2806
 
2807 2807
         /* Input validation: */
2808
-        if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== self::CRYPTO_SIGN_KEYPAIRBYTES) {
2809
-            throw new SodiumException('Argument 1 must be CRYPTO_SIGN_KEYPAIRBYTES long.');
2808
+        if ( ParagonIE_Sodium_Core_Util::strlen( $keypair ) !== self::CRYPTO_SIGN_KEYPAIRBYTES ) {
2809
+            throw new SodiumException( 'Argument 1 must be CRYPTO_SIGN_KEYPAIRBYTES long.' );
2810 2810
         }
2811 2811
 
2812
-        if (self::useNewSodiumAPI()) {
2813
-            return sodium_crypto_sign_secretkey($keypair);
2812
+        if ( self::useNewSodiumAPI() ) {
2813
+            return sodium_crypto_sign_secretkey( $keypair );
2814 2814
         }
2815
-        if (self::use_fallback('crypto_sign_secretkey')) {
2816
-            return (string) call_user_func('\\Sodium\\crypto_sign_secretkey', $keypair);
2815
+        if ( self::use_fallback( 'crypto_sign_secretkey' ) ) {
2816
+            return (string)call_user_func( '\\Sodium\\crypto_sign_secretkey', $keypair );
2817 2817
         }
2818
-        if (PHP_INT_SIZE === 4) {
2819
-            return ParagonIE_Sodium_Core32_Ed25519::secretkey($keypair);
2818
+        if ( PHP_INT_SIZE === 4 ) {
2819
+            return ParagonIE_Sodium_Core32_Ed25519::secretkey( $keypair );
2820 2820
         }
2821
-        return ParagonIE_Sodium_Core_Ed25519::secretkey($keypair);
2821
+        return ParagonIE_Sodium_Core_Ed25519::secretkey( $keypair );
2822 2822
     }
2823 2823
 
2824 2824
     /**
@@ -2833,27 +2833,27 @@  discard block
 block discarded – undo
2833 2833
      * @throws TypeError
2834 2834
      * @psalm-suppress MixedArgument
2835 2835
      */
2836
-    public static function crypto_sign_detached($message, $secretKey)
2836
+    public static function crypto_sign_detached( $message, $secretKey )
2837 2837
     {
2838 2838
         /* Type checks: */
2839
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
2840
-        ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2);
2839
+        ParagonIE_Sodium_Core_Util::declareScalarType( $message, 'string', 1 );
2840
+        ParagonIE_Sodium_Core_Util::declareScalarType( $secretKey, 'string', 2 );
2841 2841
 
2842 2842
         /* Input validation: */
2843
-        if (ParagonIE_Sodium_Core_Util::strlen($secretKey) !== self::CRYPTO_SIGN_SECRETKEYBYTES) {
2844
-            throw new SodiumException('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long.');
2843
+        if ( ParagonIE_Sodium_Core_Util::strlen( $secretKey ) !== self::CRYPTO_SIGN_SECRETKEYBYTES ) {
2844
+            throw new SodiumException( 'Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES long.' );
2845 2845
         }
2846 2846
 
2847
-        if (self::useNewSodiumAPI()) {
2848
-            return sodium_crypto_sign_detached($message, $secretKey);
2847
+        if ( self::useNewSodiumAPI() ) {
2848
+            return sodium_crypto_sign_detached( $message, $secretKey );
2849 2849
         }
2850
-        if (self::use_fallback('crypto_sign_detached')) {
2851
-            return (string) call_user_func('\\Sodium\\crypto_sign_detached', $message, $secretKey);
2850
+        if ( self::use_fallback( 'crypto_sign_detached' ) ) {
2851
+            return (string)call_user_func( '\\Sodium\\crypto_sign_detached', $message, $secretKey );
2852 2852
         }
2853
-        if (PHP_INT_SIZE === 4) {
2854
-            return ParagonIE_Sodium_Crypto32::sign_detached($message, $secretKey);
2853
+        if ( PHP_INT_SIZE === 4 ) {
2854
+            return ParagonIE_Sodium_Crypto32::sign_detached( $message, $secretKey );
2855 2855
         }
2856
-        return ParagonIE_Sodium_Crypto::sign_detached($message, $secretKey);
2856
+        return ParagonIE_Sodium_Crypto::sign_detached( $message, $secretKey );
2857 2857
     }
2858 2858
 
2859 2859
     /**
@@ -2868,36 +2868,36 @@  discard block
 block discarded – undo
2868 2868
      * @throws TypeError
2869 2869
      * @psalm-suppress MixedArgument
2870 2870
      */
2871
-    public static function crypto_sign_verify_detached($signature, $message, $publicKey)
2871
+    public static function crypto_sign_verify_detached( $signature, $message, $publicKey )
2872 2872
     {
2873 2873
         /* Type checks: */
2874
-        ParagonIE_Sodium_Core_Util::declareScalarType($signature, 'string', 1);
2875
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2);
2876
-        ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 3);
2874
+        ParagonIE_Sodium_Core_Util::declareScalarType( $signature, 'string', 1 );
2875
+        ParagonIE_Sodium_Core_Util::declareScalarType( $message, 'string', 2 );
2876
+        ParagonIE_Sodium_Core_Util::declareScalarType( $publicKey, 'string', 3 );
2877 2877
 
2878 2878
         /* Input validation: */
2879
-        if (ParagonIE_Sodium_Core_Util::strlen($signature) !== self::CRYPTO_SIGN_BYTES) {
2880
-            throw new SodiumException('Argument 1 must be CRYPTO_SIGN_BYTES long.');
2879
+        if ( ParagonIE_Sodium_Core_Util::strlen( $signature ) !== self::CRYPTO_SIGN_BYTES ) {
2880
+            throw new SodiumException( 'Argument 1 must be CRYPTO_SIGN_BYTES long.' );
2881 2881
         }
2882
-        if (ParagonIE_Sodium_Core_Util::strlen($publicKey) !== self::CRYPTO_SIGN_PUBLICKEYBYTES) {
2883
-            throw new SodiumException('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES long.');
2882
+        if ( ParagonIE_Sodium_Core_Util::strlen( $publicKey ) !== self::CRYPTO_SIGN_PUBLICKEYBYTES ) {
2883
+            throw new SodiumException( 'Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES long.' );
2884 2884
         }
2885 2885
 
2886
-        if (self::useNewSodiumAPI()) {
2887
-            return sodium_crypto_sign_verify_detached($signature, $message, $publicKey);
2886
+        if ( self::useNewSodiumAPI() ) {
2887
+            return sodium_crypto_sign_verify_detached( $signature, $message, $publicKey );
2888 2888
         }
2889
-        if (self::use_fallback('crypto_sign_verify_detached')) {
2890
-            return (bool) call_user_func(
2889
+        if ( self::use_fallback( 'crypto_sign_verify_detached' ) ) {
2890
+            return (bool)call_user_func(
2891 2891
                 '\\Sodium\\crypto_sign_verify_detached',
2892 2892
                 $signature,
2893 2893
                 $message,
2894 2894
                 $publicKey
2895 2895
             );
2896 2896
         }
2897
-        if (PHP_INT_SIZE === 4) {
2898
-            return ParagonIE_Sodium_Crypto32::sign_verify_detached($signature, $message, $publicKey);
2897
+        if ( PHP_INT_SIZE === 4 ) {
2898
+            return ParagonIE_Sodium_Crypto32::sign_verify_detached( $signature, $message, $publicKey );
2899 2899
         }
2900
-        return ParagonIE_Sodium_Crypto::sign_verify_detached($signature, $message, $publicKey);
2900
+        return ParagonIE_Sodium_Crypto::sign_verify_detached( $signature, $message, $publicKey );
2901 2901
     }
2902 2902
 
2903 2903
     /**
@@ -2909,27 +2909,27 @@  discard block
 block discarded – undo
2909 2909
      * @throws TypeError
2910 2910
      * @psalm-suppress MixedArgument
2911 2911
      */
2912
-    public static function crypto_sign_ed25519_pk_to_curve25519($pk)
2912
+    public static function crypto_sign_ed25519_pk_to_curve25519( $pk )
2913 2913
     {
2914 2914
         /* Type checks: */
2915
-        ParagonIE_Sodium_Core_Util::declareScalarType($pk, 'string', 1);
2915
+        ParagonIE_Sodium_Core_Util::declareScalarType( $pk, 'string', 1 );
2916 2916
 
2917 2917
         /* Input validation: */
2918
-        if (ParagonIE_Sodium_Core_Util::strlen($pk) < self::CRYPTO_SIGN_PUBLICKEYBYTES) {
2919
-            throw new SodiumException('Argument 1 must be at least CRYPTO_SIGN_PUBLICKEYBYTES long.');
2918
+        if ( ParagonIE_Sodium_Core_Util::strlen( $pk ) < self::CRYPTO_SIGN_PUBLICKEYBYTES ) {
2919
+            throw new SodiumException( 'Argument 1 must be at least CRYPTO_SIGN_PUBLICKEYBYTES long.' );
2920 2920
         }
2921
-        if (self::useNewSodiumAPI()) {
2922
-            if (is_callable('crypto_sign_ed25519_pk_to_curve25519')) {
2923
-                return (string) sodium_crypto_sign_ed25519_pk_to_curve25519($pk);
2921
+        if ( self::useNewSodiumAPI() ) {
2922
+            if ( is_callable( 'crypto_sign_ed25519_pk_to_curve25519' ) ) {
2923
+                return (string)sodium_crypto_sign_ed25519_pk_to_curve25519( $pk );
2924 2924
             }
2925 2925
         }
2926
-        if (self::use_fallback('crypto_sign_ed25519_pk_to_curve25519')) {
2927
-            return (string) call_user_func('\\Sodium\\crypto_sign_ed25519_pk_to_curve25519', $pk);
2926
+        if ( self::use_fallback( 'crypto_sign_ed25519_pk_to_curve25519' ) ) {
2927
+            return (string)call_user_func( '\\Sodium\\crypto_sign_ed25519_pk_to_curve25519', $pk );
2928 2928
         }
2929
-        if (PHP_INT_SIZE === 4) {
2930
-            return ParagonIE_Sodium_Core32_Ed25519::pk_to_curve25519($pk);
2929
+        if ( PHP_INT_SIZE === 4 ) {
2930
+            return ParagonIE_Sodium_Core32_Ed25519::pk_to_curve25519( $pk );
2931 2931
         }
2932
-        return ParagonIE_Sodium_Core_Ed25519::pk_to_curve25519($pk);
2932
+        return ParagonIE_Sodium_Core_Ed25519::pk_to_curve25519( $pk );
2933 2933
     }
2934 2934
 
2935 2935
     /**
@@ -2941,32 +2941,32 @@  discard block
 block discarded – undo
2941 2941
      * @throws TypeError
2942 2942
      * @psalm-suppress MixedArgument
2943 2943
      */
2944
-    public static function crypto_sign_ed25519_sk_to_curve25519($sk)
2944
+    public static function crypto_sign_ed25519_sk_to_curve25519( $sk )
2945 2945
     {
2946 2946
         /* Type checks: */
2947
-        ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1);
2947
+        ParagonIE_Sodium_Core_Util::declareScalarType( $sk, 'string', 1 );
2948 2948
 
2949 2949
         /* Input validation: */
2950
-        if (ParagonIE_Sodium_Core_Util::strlen($sk) < self::CRYPTO_SIGN_SEEDBYTES) {
2951
-            throw new SodiumException('Argument 1 must be at least CRYPTO_SIGN_SEEDBYTES long.');
2950
+        if ( ParagonIE_Sodium_Core_Util::strlen( $sk ) < self::CRYPTO_SIGN_SEEDBYTES ) {
2951
+            throw new SodiumException( 'Argument 1 must be at least CRYPTO_SIGN_SEEDBYTES long.' );
2952 2952
         }
2953
-        if (self::useNewSodiumAPI()) {
2954
-            if (is_callable('crypto_sign_ed25519_sk_to_curve25519')) {
2955
-                return sodium_crypto_sign_ed25519_sk_to_curve25519($sk);
2953
+        if ( self::useNewSodiumAPI() ) {
2954
+            if ( is_callable( 'crypto_sign_ed25519_sk_to_curve25519' ) ) {
2955
+                return sodium_crypto_sign_ed25519_sk_to_curve25519( $sk );
2956 2956
             }
2957 2957
         }
2958
-        if (self::use_fallback('crypto_sign_ed25519_sk_to_curve25519')) {
2959
-            return (string) call_user_func('\\Sodium\\crypto_sign_ed25519_sk_to_curve25519', $sk);
2958
+        if ( self::use_fallback( 'crypto_sign_ed25519_sk_to_curve25519' ) ) {
2959
+            return (string)call_user_func( '\\Sodium\\crypto_sign_ed25519_sk_to_curve25519', $sk );
2960 2960
         }
2961 2961
 
2962
-        $h = hash('sha512', ParagonIE_Sodium_Core_Util::substr($sk, 0, 32), true);
2963
-        $h[0] = ParagonIE_Sodium_Core_Util::intToChr(
2964
-            ParagonIE_Sodium_Core_Util::chrToInt($h[0]) & 248
2962
+        $h = hash( 'sha512', ParagonIE_Sodium_Core_Util::substr( $sk, 0, 32 ), true );
2963
+        $h[ 0 ] = ParagonIE_Sodium_Core_Util::intToChr(
2964
+            ParagonIE_Sodium_Core_Util::chrToInt( $h[ 0 ] ) & 248
2965 2965
         );
2966
-        $h[31] = ParagonIE_Sodium_Core_Util::intToChr(
2967
-            (ParagonIE_Sodium_Core_Util::chrToInt($h[31]) & 127) | 64
2966
+        $h[ 31 ] = ParagonIE_Sodium_Core_Util::intToChr(
2967
+            ( ParagonIE_Sodium_Core_Util::chrToInt( $h[ 31 ] ) & 127 ) | 64
2968 2968
         );
2969
-        return ParagonIE_Sodium_Core_Util::substr($h, 0, 32);
2969
+        return ParagonIE_Sodium_Core_Util::substr( $h, 0, 32 );
2970 2970
     }
2971 2971
 
2972 2972
     /**
@@ -2983,31 +2983,31 @@  discard block
 block discarded – undo
2983 2983
      * @throws TypeError
2984 2984
      * @psalm-suppress MixedArgument
2985 2985
      */
2986
-    public static function crypto_stream($len, $nonce, $key)
2986
+    public static function crypto_stream( $len, $nonce, $key )
2987 2987
     {
2988 2988
         /* Type checks: */
2989
-        ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1);
2990
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
2991
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
2989
+        ParagonIE_Sodium_Core_Util::declareScalarType( $len, 'int', 1 );
2990
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 2 );
2991
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 3 );
2992 2992
 
2993 2993
         /* Input validation: */
2994
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_NONCEBYTES) {
2995
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
2994
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_STREAM_NONCEBYTES ) {
2995
+            throw new SodiumException( 'Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.' );
2996 2996
         }
2997
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_KEYBYTES) {
2998
-            throw new SodiumException('Argument 3 must be CRYPTO_STREAM_KEYBYTES long.');
2997
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_STREAM_KEYBYTES ) {
2998
+            throw new SodiumException( 'Argument 3 must be CRYPTO_STREAM_KEYBYTES long.' );
2999 2999
         }
3000 3000
 
3001
-        if (self::useNewSodiumAPI()) {
3002
-            return sodium_crypto_stream($len, $nonce, $key);
3001
+        if ( self::useNewSodiumAPI() ) {
3002
+            return sodium_crypto_stream( $len, $nonce, $key );
3003 3003
         }
3004
-        if (self::use_fallback('crypto_stream')) {
3005
-            return (string) call_user_func('\\Sodium\\crypto_stream', $len, $nonce, $key);
3004
+        if ( self::use_fallback( 'crypto_stream' ) ) {
3005
+            return (string)call_user_func( '\\Sodium\\crypto_stream', $len, $nonce, $key );
3006 3006
         }
3007
-        if (PHP_INT_SIZE === 4) {
3008
-            return ParagonIE_Sodium_Core32_XSalsa20::xsalsa20($len, $nonce, $key);
3007
+        if ( PHP_INT_SIZE === 4 ) {
3008
+            return ParagonIE_Sodium_Core32_XSalsa20::xsalsa20( $len, $nonce, $key );
3009 3009
         }
3010
-        return ParagonIE_Sodium_Core_XSalsa20::xsalsa20($len, $nonce, $key);
3010
+        return ParagonIE_Sodium_Core_XSalsa20::xsalsa20( $len, $nonce, $key );
3011 3011
     }
3012 3012
 
3013 3013
     /**
@@ -3030,31 +3030,31 @@  discard block
 block discarded – undo
3030 3030
      * @throws TypeError
3031 3031
      * @psalm-suppress MixedArgument
3032 3032
      */
3033
-    public static function crypto_stream_xor($message, $nonce, $key)
3033
+    public static function crypto_stream_xor( $message, $nonce, $key )
3034 3034
     {
3035 3035
         /* Type checks: */
3036
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
3037
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
3038
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
3036
+        ParagonIE_Sodium_Core_Util::declareScalarType( $message, 'string', 1 );
3037
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 2 );
3038
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 3 );
3039 3039
 
3040 3040
         /* Input validation: */
3041
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_NONCEBYTES) {
3042
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.');
3041
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_STREAM_NONCEBYTES ) {
3042
+            throw new SodiumException( 'Argument 2 must be CRYPTO_SECRETBOX_NONCEBYTES long.' );
3043 3043
         }
3044
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_KEYBYTES) {
3045
-            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.');
3044
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_STREAM_KEYBYTES ) {
3045
+            throw new SodiumException( 'Argument 3 must be CRYPTO_SECRETBOX_KEYBYTES long.' );
3046 3046
         }
3047 3047
 
3048
-        if (self::useNewSodiumAPI()) {
3049
-            return sodium_crypto_stream_xor($message, $nonce, $key);
3048
+        if ( self::useNewSodiumAPI() ) {
3049
+            return sodium_crypto_stream_xor( $message, $nonce, $key );
3050 3050
         }
3051
-        if (self::use_fallback('crypto_stream_xor')) {
3052
-            return (string) call_user_func('\\Sodium\\crypto_stream_xor', $message, $nonce, $key);
3051
+        if ( self::use_fallback( 'crypto_stream_xor' ) ) {
3052
+            return (string)call_user_func( '\\Sodium\\crypto_stream_xor', $message, $nonce, $key );
3053 3053
         }
3054
-        if (PHP_INT_SIZE === 4) {
3055
-            return ParagonIE_Sodium_Core32_XSalsa20::xsalsa20_xor($message, $nonce, $key);
3054
+        if ( PHP_INT_SIZE === 4 ) {
3055
+            return ParagonIE_Sodium_Core32_XSalsa20::xsalsa20_xor( $message, $nonce, $key );
3056 3056
         }
3057
-        return ParagonIE_Sodium_Core_XSalsa20::xsalsa20_xor($message, $nonce, $key);
3057
+        return ParagonIE_Sodium_Core_XSalsa20::xsalsa20_xor( $message, $nonce, $key );
3058 3058
     }
3059 3059
 
3060 3060
     /**
@@ -3066,7 +3066,7 @@  discard block
 block discarded – undo
3066 3066
      */
3067 3067
     public static function crypto_stream_keygen()
3068 3068
     {
3069
-        return random_bytes(self::CRYPTO_STREAM_KEYBYTES);
3069
+        return random_bytes( self::CRYPTO_STREAM_KEYBYTES );
3070 3070
     }
3071 3071
 
3072 3072
 
@@ -3085,28 +3085,28 @@  discard block
 block discarded – undo
3085 3085
      * @throws TypeError
3086 3086
      * @psalm-suppress MixedArgument
3087 3087
      */
3088
-    public static function crypto_stream_xchacha20($len, $nonce, $key, $dontFallback = false)
3088
+    public static function crypto_stream_xchacha20( $len, $nonce, $key, $dontFallback = false )
3089 3089
     {
3090 3090
         /* Type checks: */
3091
-        ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1);
3092
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
3093
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
3091
+        ParagonIE_Sodium_Core_Util::declareScalarType( $len, 'int', 1 );
3092
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 2 );
3093
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 3 );
3094 3094
 
3095 3095
         /* Input validation: */
3096
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_XCHACHA20_NONCEBYTES) {
3097
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_XCHACHA20_NONCEBYTES long.');
3096
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_STREAM_XCHACHA20_NONCEBYTES ) {
3097
+            throw new SodiumException( 'Argument 2 must be CRYPTO_SECRETBOX_XCHACHA20_NONCEBYTES long.' );
3098 3098
         }
3099
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_XCHACHA20_KEYBYTES) {
3100
-            throw new SodiumException('Argument 3 must be CRYPTO_STREAM_XCHACHA20_KEYBYTES long.');
3099
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_STREAM_XCHACHA20_KEYBYTES ) {
3100
+            throw new SodiumException( 'Argument 3 must be CRYPTO_STREAM_XCHACHA20_KEYBYTES long.' );
3101 3101
         }
3102 3102
 
3103
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3104
-            return sodium_crypto_stream_xchacha20($len, $nonce, $key);
3103
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3104
+            return sodium_crypto_stream_xchacha20( $len, $nonce, $key );
3105 3105
         }
3106
-        if (PHP_INT_SIZE === 4) {
3107
-            return ParagonIE_Sodium_Core32_XChaCha20::stream($len, $nonce, $key);
3106
+        if ( PHP_INT_SIZE === 4 ) {
3107
+            return ParagonIE_Sodium_Core32_XChaCha20::stream( $len, $nonce, $key );
3108 3108
         }
3109
-        return ParagonIE_Sodium_Core_XChaCha20::stream($len, $nonce, $key);
3109
+        return ParagonIE_Sodium_Core_XChaCha20::stream( $len, $nonce, $key );
3110 3110
     }
3111 3111
 
3112 3112
     /**
@@ -3130,28 +3130,28 @@  discard block
 block discarded – undo
3130 3130
      * @throws TypeError
3131 3131
      * @psalm-suppress MixedArgument
3132 3132
      */
3133
-    public static function crypto_stream_xchacha20_xor($message, $nonce, $key, $dontFallback = false)
3133
+    public static function crypto_stream_xchacha20_xor( $message, $nonce, $key, $dontFallback = false )
3134 3134
     {
3135 3135
         /* Type checks: */
3136
-        ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
3137
-        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
3138
-        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 3);
3136
+        ParagonIE_Sodium_Core_Util::declareScalarType( $message, 'string', 1 );
3137
+        ParagonIE_Sodium_Core_Util::declareScalarType( $nonce, 'string', 2 );
3138
+        ParagonIE_Sodium_Core_Util::declareScalarType( $key, 'string', 3 );
3139 3139
 
3140 3140
         /* Input validation: */
3141
-        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_STREAM_XCHACHA20_NONCEBYTES) {
3142
-            throw new SodiumException('Argument 2 must be CRYPTO_SECRETBOX_XCHACHA20_NONCEBYTES long.');
3141
+        if ( ParagonIE_Sodium_Core_Util::strlen( $nonce ) !== self::CRYPTO_STREAM_XCHACHA20_NONCEBYTES ) {
3142
+            throw new SodiumException( 'Argument 2 must be CRYPTO_SECRETBOX_XCHACHA20_NONCEBYTES long.' );
3143 3143
         }
3144
-        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_STREAM_XCHACHA20_KEYBYTES) {
3145
-            throw new SodiumException('Argument 3 must be CRYPTO_SECRETBOX_XCHACHA20_KEYBYTES long.');
3144
+        if ( ParagonIE_Sodium_Core_Util::strlen( $key ) !== self::CRYPTO_STREAM_XCHACHA20_KEYBYTES ) {
3145
+            throw new SodiumException( 'Argument 3 must be CRYPTO_SECRETBOX_XCHACHA20_KEYBYTES long.' );
3146 3146
         }
3147 3147
 
3148
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3149
-            return sodium_crypto_stream_xchacha20_xor($message, $nonce, $key);
3148
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3149
+            return sodium_crypto_stream_xchacha20_xor( $message, $nonce, $key );
3150 3150
         }
3151
-        if (PHP_INT_SIZE === 4) {
3152
-            return ParagonIE_Sodium_Core32_XChaCha20::streamXorIc($message, $nonce, $key);
3151
+        if ( PHP_INT_SIZE === 4 ) {
3152
+            return ParagonIE_Sodium_Core32_XChaCha20::streamXorIc( $message, $nonce, $key );
3153 3153
         }
3154
-        return ParagonIE_Sodium_Core_XChaCha20::streamXorIc($message, $nonce, $key);
3154
+        return ParagonIE_Sodium_Core_XChaCha20::streamXorIc( $message, $nonce, $key );
3155 3155
     }
3156 3156
 
3157 3157
     /**
@@ -3163,7 +3163,7 @@  discard block
 block discarded – undo
3163 3163
      */
3164 3164
     public static function crypto_stream_xchacha20_keygen()
3165 3165
     {
3166
-        return random_bytes(self::CRYPTO_STREAM_XCHACHA20_KEYBYTES);
3166
+        return random_bytes( self::CRYPTO_STREAM_XCHACHA20_KEYBYTES );
3167 3167
     }
3168 3168
 
3169 3169
     /**
@@ -3176,20 +3176,20 @@  discard block
 block discarded – undo
3176 3176
      * @psalm-suppress TooFewArguments
3177 3177
      * @psalm-suppress MixedArgument
3178 3178
      */
3179
-    public static function hex2bin($string)
3179
+    public static function hex2bin( $string )
3180 3180
     {
3181 3181
         /* Type checks: */
3182
-        ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1);
3182
+        ParagonIE_Sodium_Core_Util::declareScalarType( $string, 'string', 1 );
3183 3183
 
3184
-        if (self::useNewSodiumAPI()) {
3185
-            if (is_callable('sodium_hex2bin')) {
3186
-                return (string) sodium_hex2bin($string);
3184
+        if ( self::useNewSodiumAPI() ) {
3185
+            if ( is_callable( 'sodium_hex2bin' ) ) {
3186
+                return (string)sodium_hex2bin( $string );
3187 3187
             }
3188 3188
         }
3189
-        if (self::use_fallback('hex2bin')) {
3190
-            return (string) call_user_func('\\Sodium\\hex2bin', $string);
3189
+        if ( self::use_fallback( 'hex2bin' ) ) {
3190
+            return (string)call_user_func( '\\Sodium\\hex2bin', $string );
3191 3191
         }
3192
-        return ParagonIE_Sodium_Core_Util::hex2bin($string);
3192
+        return ParagonIE_Sodium_Core_Util::hex2bin( $string );
3193 3193
     }
3194 3194
 
3195 3195
     /**
@@ -3202,29 +3202,29 @@  discard block
 block discarded – undo
3202 3202
      * @throws TypeError
3203 3203
      * @psalm-suppress MixedArgument
3204 3204
      */
3205
-    public static function increment(&$var)
3205
+    public static function increment( &$var )
3206 3206
     {
3207 3207
         /* Type checks: */
3208
-        ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1);
3208
+        ParagonIE_Sodium_Core_Util::declareScalarType( $var, 'string', 1 );
3209 3209
 
3210
-        if (self::useNewSodiumAPI()) {
3211
-            sodium_increment($var);
3210
+        if ( self::useNewSodiumAPI() ) {
3211
+            sodium_increment( $var );
3212 3212
             return;
3213 3213
         }
3214
-        if (self::use_fallback('increment')) {
3214
+        if ( self::use_fallback( 'increment' ) ) {
3215 3215
             $func = '\\Sodium\\increment';
3216
-            $func($var);
3216
+            $func( $var );
3217 3217
             return;
3218 3218
         }
3219 3219
 
3220
-        $len = ParagonIE_Sodium_Core_Util::strlen($var);
3220
+        $len = ParagonIE_Sodium_Core_Util::strlen( $var );
3221 3221
         $c = 1;
3222 3222
         $copy = '';
3223
-        for ($i = 0; $i < $len; ++$i) {
3223
+        for ( $i = 0; $i < $len; ++$i ) {
3224 3224
             $c += ParagonIE_Sodium_Core_Util::chrToInt(
3225
-                ParagonIE_Sodium_Core_Util::substr($var, $i, 1)
3225
+                ParagonIE_Sodium_Core_Util::substr( $var, $i, 1 )
3226 3226
             );
3227
-            $copy .= ParagonIE_Sodium_Core_Util::intToChr($c);
3227
+            $copy .= ParagonIE_Sodium_Core_Util::intToChr( $c );
3228 3228
             $c >>= 8;
3229 3229
         }
3230 3230
         $var = $copy;
@@ -3236,13 +3236,13 @@  discard block
 block discarded – undo
3236 3236
      *
3237 3237
      * @throws SodiumException
3238 3238
      */
3239
-    public static function is_zero($str)
3239
+    public static function is_zero( $str )
3240 3240
     {
3241 3241
         $d = 0;
3242
-        for ($i = 0; $i < 32; ++$i) {
3243
-            $d |= ParagonIE_Sodium_Core_Util::chrToInt($str[$i]);
3242
+        for ( $i = 0; $i < 32; ++$i ) {
3243
+            $d |= ParagonIE_Sodium_Core_Util::chrToInt( $str[ $i ] );
3244 3244
         }
3245
-        return ((($d - 1) >> 31) & 1) === 1;
3245
+        return ( ( ( $d - 1 ) >> 31 ) & 1 ) === 1;
3246 3246
     }
3247 3247
 
3248 3248
     /**
@@ -3253,12 +3253,12 @@  discard block
 block discarded – undo
3253 3253
      */
3254 3254
     public static function library_version_major()
3255 3255
     {
3256
-        if (self::useNewSodiumAPI() && defined('SODIUM_LIBRARY_MAJOR_VERSION')) {
3256
+        if ( self::useNewSodiumAPI() && defined( 'SODIUM_LIBRARY_MAJOR_VERSION' ) ) {
3257 3257
             return SODIUM_LIBRARY_MAJOR_VERSION;
3258 3258
         }
3259
-        if (self::use_fallback('library_version_major')) {
3259
+        if ( self::use_fallback( 'library_version_major' ) ) {
3260 3260
             /** @psalm-suppress UndefinedFunction */
3261
-            return (int) call_user_func('\\Sodium\\library_version_major');
3261
+            return (int)call_user_func( '\\Sodium\\library_version_major' );
3262 3262
         }
3263 3263
         return self::LIBRARY_VERSION_MAJOR;
3264 3264
     }
@@ -3271,12 +3271,12 @@  discard block
 block discarded – undo
3271 3271
      */
3272 3272
     public static function library_version_minor()
3273 3273
     {
3274
-        if (self::useNewSodiumAPI() && defined('SODIUM_LIBRARY_MINOR_VERSION')) {
3274
+        if ( self::useNewSodiumAPI() && defined( 'SODIUM_LIBRARY_MINOR_VERSION' ) ) {
3275 3275
             return SODIUM_LIBRARY_MINOR_VERSION;
3276 3276
         }
3277
-        if (self::use_fallback('library_version_minor')) {
3277
+        if ( self::use_fallback( 'library_version_minor' ) ) {
3278 3278
             /** @psalm-suppress UndefinedFunction */
3279
-            return (int) call_user_func('\\Sodium\\library_version_minor');
3279
+            return (int)call_user_func( '\\Sodium\\library_version_minor' );
3280 3280
         }
3281 3281
         return self::LIBRARY_VERSION_MINOR;
3282 3282
     }
@@ -3291,21 +3291,21 @@  discard block
 block discarded – undo
3291 3291
      * @throws TypeError
3292 3292
      * @psalm-suppress MixedArgument
3293 3293
      */
3294
-    public static function memcmp($left, $right)
3294
+    public static function memcmp( $left, $right )
3295 3295
     {
3296 3296
         /* Type checks: */
3297
-        ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1);
3298
-        ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2);
3297
+        ParagonIE_Sodium_Core_Util::declareScalarType( $left, 'string', 1 );
3298
+        ParagonIE_Sodium_Core_Util::declareScalarType( $right, 'string', 2 );
3299 3299
 
3300
-        if (self::useNewSodiumAPI()) {
3301
-            return sodium_memcmp($left, $right);
3300
+        if ( self::useNewSodiumAPI() ) {
3301
+            return sodium_memcmp( $left, $right );
3302 3302
         }
3303
-        if (self::use_fallback('memcmp')) {
3304
-            return (int) call_user_func('\\Sodium\\memcmp', $left, $right);
3303
+        if ( self::use_fallback( 'memcmp' ) ) {
3304
+            return (int)call_user_func( '\\Sodium\\memcmp', $left, $right );
3305 3305
         }
3306 3306
         /** @var string $left */
3307 3307
         /** @var string $right */
3308
-        return ParagonIE_Sodium_Core_Util::memcmp($left, $right);
3308
+        return ParagonIE_Sodium_Core_Util::memcmp( $left, $right );
3309 3309
     }
3310 3310
 
3311 3311
     /**
@@ -3320,20 +3320,20 @@  discard block
 block discarded – undo
3320 3320
      * @throws TypeError
3321 3321
      * @psalm-suppress TooFewArguments
3322 3322
      */
3323
-    public static function memzero(&$var)
3323
+    public static function memzero( &$var )
3324 3324
     {
3325 3325
         /* Type checks: */
3326
-        ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1);
3326
+        ParagonIE_Sodium_Core_Util::declareScalarType( $var, 'string', 1 );
3327 3327
 
3328
-        if (self::useNewSodiumAPI()) {
3328
+        if ( self::useNewSodiumAPI() ) {
3329 3329
             /** @psalm-suppress MixedArgument */
3330
-            sodium_memzero($var);
3330
+            sodium_memzero( $var );
3331 3331
             return;
3332 3332
         }
3333
-        if (self::use_fallback('memzero')) {
3333
+        if ( self::use_fallback( 'memzero' ) ) {
3334 3334
             $func = '\\Sodium\\memzero';
3335
-            $func($var);
3336
-            if ($var === null) {
3335
+            $func( $var );
3336
+            if ( $var === null ) {
3337 3337
                 return;
3338 3338
             }
3339 3339
         }
@@ -3351,57 +3351,57 @@  discard block
 block discarded – undo
3351 3351
      * @return string
3352 3352
      * @throws SodiumException
3353 3353
      */
3354
-    public static function pad($unpadded, $blockSize, $dontFallback = false)
3354
+    public static function pad( $unpadded, $blockSize, $dontFallback = false )
3355 3355
     {
3356 3356
         /* Type checks: */
3357
-        ParagonIE_Sodium_Core_Util::declareScalarType($unpadded, 'string', 1);
3358
-        ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2);
3357
+        ParagonIE_Sodium_Core_Util::declareScalarType( $unpadded, 'string', 1 );
3358
+        ParagonIE_Sodium_Core_Util::declareScalarType( $blockSize, 'int', 2 );
3359 3359
 
3360
-        $unpadded = (string) $unpadded;
3361
-        $blockSize = (int) $blockSize;
3360
+        $unpadded = (string)$unpadded;
3361
+        $blockSize = (int)$blockSize;
3362 3362
 
3363
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3364
-            return (string) sodium_pad($unpadded, $blockSize);
3363
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3364
+            return (string)sodium_pad( $unpadded, $blockSize );
3365 3365
         }
3366 3366
 
3367
-        if ($blockSize <= 0) {
3367
+        if ( $blockSize <= 0 ) {
3368 3368
             throw new SodiumException(
3369 3369
                 'block size cannot be less than 1'
3370 3370
             );
3371 3371
         }
3372
-        $unpadded_len = ParagonIE_Sodium_Core_Util::strlen($unpadded);
3373
-        $xpadlen = ($blockSize - 1);
3374
-        if (($blockSize & ($blockSize - 1)) === 0) {
3375
-            $xpadlen -= $unpadded_len & ($blockSize - 1);
3372
+        $unpadded_len = ParagonIE_Sodium_Core_Util::strlen( $unpadded );
3373
+        $xpadlen = ( $blockSize - 1 );
3374
+        if ( ( $blockSize & ( $blockSize - 1 ) ) === 0 ) {
3375
+            $xpadlen -= $unpadded_len & ( $blockSize - 1 );
3376 3376
         } else {
3377 3377
             $xpadlen -= $unpadded_len % $blockSize;
3378 3378
         }
3379 3379
 
3380 3380
         $xpadded_len = $unpadded_len + $xpadlen;
3381
-        $padded = str_repeat("\0", $xpadded_len - 1);
3382
-        if ($unpadded_len > 0) {
3381
+        $padded = str_repeat( "\0", $xpadded_len - 1 );
3382
+        if ( $unpadded_len > 0 ) {
3383 3383
             $st = 1;
3384 3384
             $i = 0;
3385 3385
             $k = $unpadded_len;
3386
-            for ($j = 0; $j <= $xpadded_len; ++$j) {
3387
-                $i = (int) $i;
3388
-                $k = (int) $k;
3389
-                $st = (int) $st;
3390
-                if ($j >= $unpadded_len) {
3391
-                    $padded[$j] = "\0";
3386
+            for ( $j = 0; $j <= $xpadded_len; ++$j ) {
3387
+                $i = (int)$i;
3388
+                $k = (int)$k;
3389
+                $st = (int)$st;
3390
+                if ( $j >= $unpadded_len ) {
3391
+                    $padded[ $j ] = "\0";
3392 3392
                 } else {
3393
-                    $padded[$j] = $unpadded[$j];
3393
+                    $padded[ $j ] = $unpadded[ $j ];
3394 3394
                 }
3395 3395
                 /** @var int $k */
3396 3396
                 $k -= $st;
3397
-                $st = (int) (~(
3397
+                $st = (int)( ~(
3398 3398
                             (
3399 3399
                                 (
3400
-                                    ($k >> 48)
3400
+                                    ( $k >> 48 )
3401 3401
                                         |
3402
-                                    ($k >> 32)
3402
+                                    ( $k >> 32 )
3403 3403
                                         |
3404
-                                    ($k >> 16)
3404
+                                    ( $k >> 16 )
3405 3405
                                         |
3406 3406
                                     $k
3407 3407
                                 ) - 1
@@ -3414,15 +3414,15 @@  discard block
 block discarded – undo
3414 3414
 
3415 3415
         $mask = 0;
3416 3416
         $tail = $xpadded_len;
3417
-        for ($i = 0; $i < $blockSize; ++$i) {
3417
+        for ( $i = 0; $i < $blockSize; ++$i ) {
3418 3418
             # barrier_mask = (unsigned char)
3419 3419
             #     (((i ^ xpadlen) - 1U) >> ((sizeof(size_t) - 1U) * CHAR_BIT));
3420
-            $barrier_mask = (($i ^ $xpadlen) -1) >> ((PHP_INT_SIZE << 3) - 1);
3420
+            $barrier_mask = ( ( $i ^ $xpadlen ) - 1 ) >> ( ( PHP_INT_SIZE << 3 ) - 1 );
3421 3421
             # tail[-i] = (tail[-i] & mask) | (0x80 & barrier_mask);
3422
-            $padded[$tail - $i] = ParagonIE_Sodium_Core_Util::intToChr(
3423
-                (ParagonIE_Sodium_Core_Util::chrToInt($padded[$tail - $i]) & $mask)
3422
+            $padded[ $tail - $i ] = ParagonIE_Sodium_Core_Util::intToChr(
3423
+                ( ParagonIE_Sodium_Core_Util::chrToInt( $padded[ $tail - $i ] ) & $mask )
3424 3424
                     |
3425
-                (0x80 & $barrier_mask)
3425
+                ( 0x80 & $barrier_mask )
3426 3426
             );
3427 3427
             # mask |= barrier_mask;
3428 3428
             $mask |= $barrier_mask;
@@ -3437,24 +3437,24 @@  discard block
 block discarded – undo
3437 3437
      * @return string
3438 3438
      * @throws SodiumException
3439 3439
      */
3440
-    public static function unpad($padded, $blockSize, $dontFallback = false)
3440
+    public static function unpad( $padded, $blockSize, $dontFallback = false )
3441 3441
     {
3442 3442
         /* Type checks: */
3443
-        ParagonIE_Sodium_Core_Util::declareScalarType($padded, 'string', 1);
3444
-        ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2);
3443
+        ParagonIE_Sodium_Core_Util::declareScalarType( $padded, 'string', 1 );
3444
+        ParagonIE_Sodium_Core_Util::declareScalarType( $blockSize, 'int', 2 );
3445 3445
 
3446
-        $padded = (string) $padded;
3447
-        $blockSize = (int) $blockSize;
3446
+        $padded = (string)$padded;
3447
+        $blockSize = (int)$blockSize;
3448 3448
 
3449
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3450
-            return (string) sodium_unpad($padded, $blockSize);
3449
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3450
+            return (string)sodium_unpad( $padded, $blockSize );
3451 3451
         }
3452
-        if ($blockSize <= 0) {
3453
-            throw new SodiumException('block size cannot be less than 1');
3452
+        if ( $blockSize <= 0 ) {
3453
+            throw new SodiumException( 'block size cannot be less than 1' );
3454 3454
         }
3455
-        $padded_len = ParagonIE_Sodium_Core_Util::strlen($padded);
3456
-        if ($padded_len < $blockSize) {
3457
-            throw new SodiumException('invalid padding');
3455
+        $padded_len = ParagonIE_Sodium_Core_Util::strlen( $padded );
3456
+        if ( $padded_len < $blockSize ) {
3457
+            throw new SodiumException( 'invalid padding' );
3458 3458
         }
3459 3459
 
3460 3460
         # tail = &padded[padded_len - 1U];
@@ -3465,15 +3465,15 @@  discard block
 block discarded – undo
3465 3465
         $pad_len = 0;
3466 3466
 
3467 3467
         $found = 0;
3468
-        for ($i = 0; $i < $blockSize; ++$i) {
3468
+        for ( $i = 0; $i < $blockSize; ++$i ) {
3469 3469
             # c = tail[-i];
3470
-            $c = ParagonIE_Sodium_Core_Util::chrToInt($padded[$tail - $i]);
3470
+            $c = ParagonIE_Sodium_Core_Util::chrToInt( $padded[ $tail - $i ] );
3471 3471
 
3472 3472
             # is_barrier =
3473 3473
             #     (( (acc - 1U) & (pad_len - 1U) & ((c ^ 0x80) - 1U) ) >> 8) & 1U;
3474 3474
             $is_barrier = (
3475 3475
                 (
3476
-                    ($acc - 1) & ($pad_len - 1) & (($c ^ 80) - 1)
3476
+                    ( $acc - 1 ) & ( $pad_len - 1 ) & ( ( $c ^ 80 ) - 1 )
3477 3477
                 ) >> 7
3478 3478
             ) & 1;
3479 3479
             $is_barrier &= ~$found;
@@ -3483,17 +3483,17 @@  discard block
 block discarded – undo
3483 3483
             $acc |= $c;
3484 3484
 
3485 3485
             # pad_len |= i & (1U + ~is_barrier);
3486
-            $pad_len |= $i & (1 + ~$is_barrier);
3486
+            $pad_len |= $i & ( 1 + ~$is_barrier );
3487 3487
 
3488 3488
             # valid |= (unsigned char) is_barrier;
3489
-            $valid |= ($is_barrier & 0xff);
3489
+            $valid |= ( $is_barrier & 0xff );
3490 3490
         }
3491 3491
         # unpadded_len = padded_len - 1U - pad_len;
3492 3492
         $unpadded_len = $padded_len - 1 - $pad_len;
3493
-        if ($valid !== 1) {
3494
-            throw new SodiumException('invalid padding');
3493
+        if ( $valid !== 1 ) {
3494
+            throw new SodiumException( 'invalid padding' );
3495 3495
         }
3496
-        return ParagonIE_Sodium_Core_Util::substr($padded, 0, $unpadded_len);
3496
+        return ParagonIE_Sodium_Core_Util::substr( $padded, 0, $unpadded_len );
3497 3497
     }
3498 3498
 
3499 3499
     /**
@@ -3503,10 +3503,10 @@  discard block
 block discarded – undo
3503 3503
      */
3504 3504
     public static function polyfill_is_fast()
3505 3505
     {
3506
-        if (extension_loaded('sodium')) {
3506
+        if ( extension_loaded( 'sodium' ) ) {
3507 3507
             return true;
3508 3508
         }
3509
-        if (extension_loaded('libsodium')) {
3509
+        if ( extension_loaded( 'libsodium' ) ) {
3510 3510
             return true;
3511 3511
         }
3512 3512
         return PHP_INT_SIZE === 8;
@@ -3521,22 +3521,22 @@  discard block
 block discarded – undo
3521 3521
      * @throws Exception
3522 3522
      * @throws TypeError
3523 3523
      */
3524
-    public static function randombytes_buf($numBytes)
3524
+    public static function randombytes_buf( $numBytes )
3525 3525
     {
3526 3526
         /* Type checks: */
3527
-        if (!is_int($numBytes)) {
3528
-            if (is_numeric($numBytes)) {
3529
-                $numBytes = (int) $numBytes;
3527
+        if ( ! is_int( $numBytes ) ) {
3528
+            if ( is_numeric( $numBytes ) ) {
3529
+                $numBytes = (int)$numBytes;
3530 3530
             } else {
3531 3531
                 throw new TypeError(
3532
-                    'Argument 1 must be an integer, ' . gettype($numBytes) . ' given.'
3532
+                    'Argument 1 must be an integer, ' . gettype( $numBytes ) . ' given.'
3533 3533
                 );
3534 3534
             }
3535 3535
         }
3536
-        if (self::use_fallback('randombytes_buf')) {
3537
-            return (string) call_user_func('\\Sodium\\randombytes_buf', $numBytes);
3536
+        if ( self::use_fallback( 'randombytes_buf' ) ) {
3537
+            return (string)call_user_func( '\\Sodium\\randombytes_buf', $numBytes );
3538 3538
         }
3539
-        return random_bytes($numBytes);
3539
+        return random_bytes( $numBytes );
3540 3540
     }
3541 3541
 
3542 3542
     /**
@@ -3548,22 +3548,22 @@  discard block
 block discarded – undo
3548 3548
      * @throws Error
3549 3549
      * @throws TypeError
3550 3550
      */
3551
-    public static function randombytes_uniform($range)
3551
+    public static function randombytes_uniform( $range )
3552 3552
     {
3553 3553
         /* Type checks: */
3554
-        if (!is_int($range)) {
3555
-            if (is_numeric($range)) {
3556
-                $range = (int) $range;
3554
+        if ( ! is_int( $range ) ) {
3555
+            if ( is_numeric( $range ) ) {
3556
+                $range = (int)$range;
3557 3557
             } else {
3558 3558
                 throw new TypeError(
3559
-                    'Argument 1 must be an integer, ' . gettype($range) . ' given.'
3559
+                    'Argument 1 must be an integer, ' . gettype( $range ) . ' given.'
3560 3560
                 );
3561 3561
             }
3562 3562
         }
3563
-        if (self::use_fallback('randombytes_uniform')) {
3564
-            return (int) call_user_func('\\Sodium\\randombytes_uniform', $range);
3563
+        if ( self::use_fallback( 'randombytes_uniform' ) ) {
3564
+            return (int)call_user_func( '\\Sodium\\randombytes_uniform', $range );
3565 3565
         }
3566
-        return random_int(0, $range - 1);
3566
+        return random_int( 0, $range - 1 );
3567 3567
     }
3568 3568
 
3569 3569
     /**
@@ -3576,10 +3576,10 @@  discard block
 block discarded – undo
3576 3576
      */
3577 3577
     public static function randombytes_random16()
3578 3578
     {
3579
-        if (self::use_fallback('randombytes_random16')) {
3580
-            return (int) call_user_func('\\Sodium\\randombytes_random16');
3579
+        if ( self::use_fallback( 'randombytes_random16' ) ) {
3580
+            return (int)call_user_func( '\\Sodium\\randombytes_random16' );
3581 3581
         }
3582
-        return random_int(0, 65535);
3582
+        return random_int( 0, 65535 );
3583 3583
     }
3584 3584
 
3585 3585
     /**
@@ -3588,17 +3588,17 @@  discard block
 block discarded – undo
3588 3588
      * @return bool
3589 3589
      * @throws SodiumException
3590 3590
      */
3591
-    public static function ristretto255_is_valid_point($p, $dontFallback = false)
3591
+    public static function ristretto255_is_valid_point( $p, $dontFallback = false )
3592 3592
     {
3593
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3594
-            return sodium_crypto_core_ristretto255_is_valid_point($p);
3593
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3594
+            return sodium_crypto_core_ristretto255_is_valid_point( $p );
3595 3595
         }
3596 3596
         try {
3597
-            $r = ParagonIE_Sodium_Core_Ristretto255::ristretto255_frombytes($p);
3598
-            return $r['res'] === 0 &&
3599
-                ParagonIE_Sodium_Core_Ristretto255::ristretto255_point_is_canonical($p) === 1;
3600
-        } catch (SodiumException $ex) {
3601
-            if ($ex->getMessage() === 'S is not canonical') {
3597
+            $r = ParagonIE_Sodium_Core_Ristretto255::ristretto255_frombytes( $p );
3598
+            return $r[ 'res' ] === 0 &&
3599
+                ParagonIE_Sodium_Core_Ristretto255::ristretto255_point_is_canonical( $p ) === 1;
3600
+        } catch ( SodiumException $ex ) {
3601
+            if ( $ex->getMessage() === 'S is not canonical' ) {
3602 3602
                 return false;
3603 3603
             }
3604 3604
             throw $ex;
@@ -3612,12 +3612,12 @@  discard block
 block discarded – undo
3612 3612
      * @return string
3613 3613
      * @throws SodiumException
3614 3614
      */
3615
-    public static function ristretto255_add($p, $q, $dontFallback = false)
3615
+    public static function ristretto255_add( $p, $q, $dontFallback = false )
3616 3616
     {
3617
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3618
-            return sodium_crypto_core_ristretto255_add($p, $q);
3617
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3618
+            return sodium_crypto_core_ristretto255_add( $p, $q );
3619 3619
         }
3620
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_add($p, $q);
3620
+        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_add( $p, $q );
3621 3621
     }
3622 3622
 
3623 3623
     /**
@@ -3627,12 +3627,12 @@  discard block
 block discarded – undo
3627 3627
      * @return string
3628 3628
      * @throws SodiumException
3629 3629
      */
3630
-    public static function ristretto255_sub($p, $q, $dontFallback = false)
3630
+    public static function ristretto255_sub( $p, $q, $dontFallback = false )
3631 3631
     {
3632
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3633
-            return sodium_crypto_core_ristretto255_sub($p, $q);
3632
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3633
+            return sodium_crypto_core_ristretto255_sub( $p, $q );
3634 3634
         }
3635
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_sub($p, $q);
3635
+        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_sub( $p, $q );
3636 3636
     }
3637 3637
 
3638 3638
     /**
@@ -3642,12 +3642,12 @@  discard block
 block discarded – undo
3642 3642
      *
3643 3643
      * @throws SodiumException
3644 3644
      */
3645
-    public static function ristretto255_from_hash($r, $dontFallback = false)
3645
+    public static function ristretto255_from_hash( $r, $dontFallback = false )
3646 3646
     {
3647
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3648
-            return sodium_crypto_core_ristretto255_from_hash($r);
3647
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3648
+            return sodium_crypto_core_ristretto255_from_hash( $r );
3649 3649
         }
3650
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_from_hash($r);
3650
+        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_from_hash( $r );
3651 3651
     }
3652 3652
 
3653 3653
     /**
@@ -3656,9 +3656,9 @@  discard block
 block discarded – undo
3656 3656
      *
3657 3657
      * @throws SodiumException
3658 3658
      */
3659
-    public static function ristretto255_random($dontFallback = false)
3659
+    public static function ristretto255_random( $dontFallback = false )
3660 3660
     {
3661
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3661
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3662 3662
             return sodium_crypto_core_ristretto255_random();
3663 3663
         }
3664 3664
         return ParagonIE_Sodium_Core_Ristretto255::ristretto255_random();
@@ -3670,9 +3670,9 @@  discard block
 block discarded – undo
3670 3670
      *
3671 3671
      * @throws SodiumException
3672 3672
      */
3673
-    public static function ristretto255_scalar_random($dontFallback = false)
3673
+    public static function ristretto255_scalar_random( $dontFallback = false )
3674 3674
     {
3675
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3675
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3676 3676
             return sodium_crypto_core_ristretto255_scalar_random();
3677 3677
         }
3678 3678
         return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_random();
@@ -3684,12 +3684,12 @@  discard block
 block discarded – undo
3684 3684
      * @return string
3685 3685
      * @throws SodiumException
3686 3686
      */
3687
-    public static function ristretto255_scalar_invert($s, $dontFallback = false)
3687
+    public static function ristretto255_scalar_invert( $s, $dontFallback = false )
3688 3688
     {
3689
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3690
-            return sodium_crypto_core_ristretto255_scalar_invert($s);
3689
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3690
+            return sodium_crypto_core_ristretto255_scalar_invert( $s );
3691 3691
         }
3692
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_invert($s);
3692
+        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_invert( $s );
3693 3693
     }
3694 3694
     /**
3695 3695
      * @param string $s
@@ -3697,12 +3697,12 @@  discard block
 block discarded – undo
3697 3697
      * @return string
3698 3698
      * @throws SodiumException
3699 3699
      */
3700
-    public static function ristretto255_scalar_negate($s, $dontFallback = false)
3700
+    public static function ristretto255_scalar_negate( $s, $dontFallback = false )
3701 3701
     {
3702
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3703
-            return sodium_crypto_core_ristretto255_scalar_negate($s);
3702
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3703
+            return sodium_crypto_core_ristretto255_scalar_negate( $s );
3704 3704
         }
3705
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_negate($s);
3705
+        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_negate( $s );
3706 3706
     }
3707 3707
 
3708 3708
     /**
@@ -3711,12 +3711,12 @@  discard block
 block discarded – undo
3711 3711
      * @return string
3712 3712
      * @throws SodiumException
3713 3713
      */
3714
-    public static function ristretto255_scalar_complement($s, $dontFallback = false)
3714
+    public static function ristretto255_scalar_complement( $s, $dontFallback = false )
3715 3715
     {
3716
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3717
-            return sodium_crypto_core_ristretto255_scalar_complement($s);
3716
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3717
+            return sodium_crypto_core_ristretto255_scalar_complement( $s );
3718 3718
         }
3719
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_complement($s);
3719
+        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_complement( $s );
3720 3720
     }
3721 3721
 
3722 3722
     /**
@@ -3726,12 +3726,12 @@  discard block
 block discarded – undo
3726 3726
      * @return string
3727 3727
      * @throws SodiumException
3728 3728
      */
3729
-    public static function ristretto255_scalar_add($x, $y, $dontFallback = false)
3729
+    public static function ristretto255_scalar_add( $x, $y, $dontFallback = false )
3730 3730
     {
3731
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3732
-            return sodium_crypto_core_ristretto255_scalar_add($x, $y);
3731
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3732
+            return sodium_crypto_core_ristretto255_scalar_add( $x, $y );
3733 3733
         }
3734
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_add($x, $y);
3734
+        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_add( $x, $y );
3735 3735
     }
3736 3736
 
3737 3737
     /**
@@ -3741,12 +3741,12 @@  discard block
 block discarded – undo
3741 3741
      * @return string
3742 3742
      * @throws SodiumException
3743 3743
      */
3744
-    public static function ristretto255_scalar_sub($x, $y, $dontFallback = false)
3744
+    public static function ristretto255_scalar_sub( $x, $y, $dontFallback = false )
3745 3745
     {
3746
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3747
-            return sodium_crypto_core_ristretto255_scalar_sub($x, $y);
3746
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3747
+            return sodium_crypto_core_ristretto255_scalar_sub( $x, $y );
3748 3748
         }
3749
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_sub($x, $y);
3749
+        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_sub( $x, $y );
3750 3750
     }
3751 3751
 
3752 3752
     /**
@@ -3756,12 +3756,12 @@  discard block
 block discarded – undo
3756 3756
      * @return string
3757 3757
      * @throws SodiumException
3758 3758
      */
3759
-    public static function ristretto255_scalar_mul($x, $y, $dontFallback = false)
3759
+    public static function ristretto255_scalar_mul( $x, $y, $dontFallback = false )
3760 3760
     {
3761
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3762
-            return sodium_crypto_core_ristretto255_scalar_mul($x, $y);
3761
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3762
+            return sodium_crypto_core_ristretto255_scalar_mul( $x, $y );
3763 3763
         }
3764
-        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_mul($x, $y);
3764
+        return ParagonIE_Sodium_Core_Ristretto255::ristretto255_scalar_mul( $x, $y );
3765 3765
     }
3766 3766
 
3767 3767
     /**
@@ -3771,12 +3771,12 @@  discard block
 block discarded – undo
3771 3771
      * @return string
3772 3772
      * @throws SodiumException
3773 3773
      */
3774
-    public static function scalarmult_ristretto255($n, $p, $dontFallback = false)
3774
+    public static function scalarmult_ristretto255( $n, $p, $dontFallback = false )
3775 3775
     {
3776
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3777
-            return sodium_crypto_scalarmult_ristretto255($n, $p);
3776
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3777
+            return sodium_crypto_scalarmult_ristretto255( $n, $p );
3778 3778
         }
3779
-        return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255($n, $p);
3779
+        return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255( $n, $p );
3780 3780
     }
3781 3781
 
3782 3782
     /**
@@ -3786,12 +3786,12 @@  discard block
 block discarded – undo
3786 3786
      * @return string
3787 3787
      * @throws SodiumException
3788 3788
      */
3789
-    public static function scalarmult_ristretto255_base($n, $dontFallback = false)
3789
+    public static function scalarmult_ristretto255_base( $n, $dontFallback = false )
3790 3790
     {
3791
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3792
-            return sodium_crypto_scalarmult_ristretto255_base($n);
3791
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3792
+            return sodium_crypto_scalarmult_ristretto255_base( $n );
3793 3793
         }
3794
-        return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255_base($n);
3794
+        return ParagonIE_Sodium_Core_Ristretto255::scalarmult_ristretto255_base( $n );
3795 3795
     }
3796 3796
 
3797 3797
     /**
@@ -3800,12 +3800,12 @@  discard block
 block discarded – undo
3800 3800
      * @return string
3801 3801
      * @throws SodiumException
3802 3802
      */
3803
-    public static function ristretto255_scalar_reduce($s, $dontFallback = false)
3803
+    public static function ristretto255_scalar_reduce( $s, $dontFallback = false )
3804 3804
     {
3805
-        if (self::useNewSodiumAPI() && !$dontFallback) {
3806
-            return sodium_crypto_core_ristretto255_scalar_reduce($s);
3805
+        if ( self::useNewSodiumAPI() && ! $dontFallback ) {
3806
+            return sodium_crypto_core_ristretto255_scalar_reduce( $s );
3807 3807
         }
3808
-        return ParagonIE_Sodium_Core_Ristretto255::sc_reduce($s);
3808
+        return ParagonIE_Sodium_Core_Ristretto255::sc_reduce( $s );
3809 3809
     }
3810 3810
 
3811 3811
     /**
@@ -3821,26 +3821,26 @@  discard block
 block discarded – undo
3821 3821
      * @return bool           TRUE if we're fast enough, FALSE is not
3822 3822
      * @throws SodiumException
3823 3823
      */
3824
-    public static function runtime_speed_test($iterations, $maxTimeout)
3824
+    public static function runtime_speed_test( $iterations, $maxTimeout )
3825 3825
     {
3826
-        if (self::polyfill_is_fast()) {
3826
+        if ( self::polyfill_is_fast() ) {
3827 3827
             return true;
3828 3828
         }
3829 3829
         /** @var float $end */
3830 3830
         $end = 0.0;
3831 3831
         /** @var float $start */
3832
-        $start = microtime(true);
3832
+        $start = microtime( true );
3833 3833
         /** @var ParagonIE_Sodium_Core32_Int64 $a */
3834
-        $a = ParagonIE_Sodium_Core32_Int64::fromInt(random_int(3, 1 << 16));
3835
-        for ($i = 0; $i < $iterations; ++$i) {
3834
+        $a = ParagonIE_Sodium_Core32_Int64::fromInt( random_int( 3, 1 << 16 ) );
3835
+        for ( $i = 0; $i < $iterations; ++$i ) {
3836 3836
             /** @var ParagonIE_Sodium_Core32_Int64 $b */
3837
-            $b = ParagonIE_Sodium_Core32_Int64::fromInt(random_int(3, 1 << 16));
3838
-            $a->mulInt64($b);
3837
+            $b = ParagonIE_Sodium_Core32_Int64::fromInt( random_int( 3, 1 << 16 ) );
3838
+            $a->mulInt64( $b );
3839 3839
         }
3840 3840
         /** @var float $end */
3841
-        $end = microtime(true);
3841
+        $end = microtime( true );
3842 3842
         /** @var int $diff */
3843
-        $diff = (int) ceil(($end - $start) * 1000);
3843
+        $diff = (int)ceil( ( $end - $start ) * 1000 );
3844 3844
         return $diff < $maxTimeout;
3845 3845
     }
3846 3846
 
@@ -3855,23 +3855,23 @@  discard block
 block discarded – undo
3855 3855
      * @return void
3856 3856
      * @throws SodiumException
3857 3857
      */
3858
-    public static function sub(&$val, $addv)
3858
+    public static function sub( &$val, $addv )
3859 3859
     {
3860
-        $val_len = ParagonIE_Sodium_Core_Util::strlen($val);
3861
-        $addv_len = ParagonIE_Sodium_Core_Util::strlen($addv);
3862
-        if ($val_len !== $addv_len) {
3863
-            throw new SodiumException('values must have the same length');
3860
+        $val_len = ParagonIE_Sodium_Core_Util::strlen( $val );
3861
+        $addv_len = ParagonIE_Sodium_Core_Util::strlen( $addv );
3862
+        if ( $val_len !== $addv_len ) {
3863
+            throw new SodiumException( 'values must have the same length' );
3864 3864
         }
3865
-        $A = ParagonIE_Sodium_Core_Util::stringToIntArray($val);
3866
-        $B = ParagonIE_Sodium_Core_Util::stringToIntArray($addv);
3865
+        $A = ParagonIE_Sodium_Core_Util::stringToIntArray( $val );
3866
+        $B = ParagonIE_Sodium_Core_Util::stringToIntArray( $addv );
3867 3867
 
3868 3868
         $c = 0;
3869
-        for ($i = 0; $i < $val_len; $i++) {
3870
-            $c = ($A[$i] - $B[$i] - $c);
3871
-            $A[$i] = ($c & 0xff);
3872
-            $c = ($c >> 8) & 1;
3869
+        for ( $i = 0; $i < $val_len; $i++ ) {
3870
+            $c = ( $A[ $i ] - $B[ $i ] - $c );
3871
+            $A[ $i ] = ( $c & 0xff );
3872
+            $c = ( $c >> 8 ) & 1;
3873 3873
         }
3874
-        $val = ParagonIE_Sodium_Core_Util::intArrayToString($A);
3874
+        $val = ParagonIE_Sodium_Core_Util::intArrayToString( $A );
3875 3875
     }
3876 3876
 
3877 3877
     /**
@@ -3884,13 +3884,13 @@  discard block
 block discarded – undo
3884 3884
      */
3885 3885
     public static function version_string()
3886 3886
     {
3887
-        if (self::useNewSodiumAPI()) {
3888
-            return (string) sodium_version_string();
3887
+        if ( self::useNewSodiumAPI() ) {
3888
+            return (string)sodium_version_string();
3889 3889
         }
3890
-        if (self::use_fallback('version_string')) {
3891
-            return (string) call_user_func('\\Sodium\\version_string');
3890
+        if ( self::use_fallback( 'version_string' ) ) {
3891
+            return (string)call_user_func( '\\Sodium\\version_string' );
3892 3892
         }
3893
-        return (string) self::VERSION_STRING;
3893
+        return (string)self::VERSION_STRING;
3894 3894
     }
3895 3895
 
3896 3896
     /**
@@ -3905,22 +3905,22 @@  discard block
 block discarded – undo
3905 3905
      *
3906 3906
      * @return bool
3907 3907
      */
3908
-    protected static function use_fallback($sodium_func_name = '')
3908
+    protected static function use_fallback( $sodium_func_name = '' )
3909 3909
     {
3910 3910
         static $res = null;
3911
-        if ($res === null) {
3912
-            $res = extension_loaded('libsodium') && PHP_VERSION_ID >= 50300;
3911
+        if ( $res === null ) {
3912
+            $res = extension_loaded( 'libsodium' ) && PHP_VERSION_ID >= 50300;
3913 3913
         }
3914
-        if ($res === false) {
3914
+        if ( $res === false ) {
3915 3915
             // No libsodium installed
3916 3916
             return false;
3917 3917
         }
3918
-        if (self::$disableFallbackForUnitTests) {
3918
+        if ( self::$disableFallbackForUnitTests ) {
3919 3919
             // Don't fallback. Use the PHP implementation.
3920 3920
             return false;
3921 3921
         }
3922
-        if (!empty($sodium_func_name)) {
3923
-            return is_callable('\\Sodium\\' . $sodium_func_name);
3922
+        if ( ! empty( $sodium_func_name ) ) {
3923
+            return is_callable( '\\Sodium\\' . $sodium_func_name );
3924 3924
         }
3925 3925
         return true;
3926 3926
     }
@@ -3935,13 +3935,13 @@  discard block
 block discarded – undo
3935 3935
     protected static function useNewSodiumAPI()
3936 3936
     {
3937 3937
         static $res = null;
3938
-        if ($res === null) {
3939
-            $res = PHP_VERSION_ID >= 70000 && extension_loaded('sodium');
3938
+        if ( $res === null ) {
3939
+            $res = PHP_VERSION_ID >= 70000 && extension_loaded( 'sodium' );
3940 3940
         }
3941
-        if (self::$disableFallbackForUnitTests) {
3941
+        if ( self::$disableFallbackForUnitTests ) {
3942 3942
             // Don't fallback. Use the PHP implementation.
3943 3943
             return false;
3944 3944
         }
3945
-        return (bool) $res;
3945
+        return (bool)$res;
3946 3946
     }
3947 3947
 }
Please login to merge, or discard this patch.
Braces   +112 added lines, -224 removed lines patch added patch discarded remove patch
@@ -25,8 +25,7 @@  discard block
 block discarded – undo
25 25
     return;
26 26
 }
27 27
 
28
-class ParagonIE_Sodium_Compat
29
-{
28
+class ParagonIE_Sodium_Compat {
30 29
     /**
31 30
      * This parameter prevents the use of the PECL extension.
32 31
      * It should only be used for unit testing.
@@ -155,8 +154,7 @@  discard block
 block discarded – undo
155 154
      * @return void
156 155
      * @throws SodiumException
157 156
      */
158
-    public static function add(&$val, $addv)
159
-    {
157
+    public static function add(&$val, $addv) {
160 158
         $val_len = ParagonIE_Sodium_Core_Util::strlen($val);
161 159
         $addv_len = ParagonIE_Sodium_Core_Util::strlen($addv);
162 160
         if ($val_len !== $addv_len) {
@@ -181,8 +179,7 @@  discard block
 block discarded – undo
181 179
      * @return string
182 180
      * @throws SodiumException
183 181
      */
184
-    public static function base642bin($encoded, $variant, $ignore = '')
185
-    {
182
+    public static function base642bin($encoded, $variant, $ignore = '') {
186 183
         /* Type checks: */
187 184
         ParagonIE_Sodium_Core_Util::declareScalarType($encoded, 'string', 1);
188 185
 
@@ -224,8 +221,7 @@  discard block
 block discarded – undo
224 221
      * @return string
225 222
      * @throws SodiumException
226 223
      */
227
-    public static function bin2base64($decoded, $variant)
228
-    {
224
+    public static function bin2base64($decoded, $variant) {
229 225
         /* Type checks: */
230 226
         ParagonIE_Sodium_Core_Util::declareScalarType($decoded, 'string', 1);
231 227
         /** @var string $decoded */
@@ -257,8 +253,7 @@  discard block
 block discarded – undo
257 253
      * @throws TypeError
258 254
      * @psalm-suppress MixedArgument
259 255
      */
260
-    public static function bin2hex($string)
261
-    {
256
+    public static function bin2hex($string) {
262 257
         /* Type checks: */
263 258
         ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1);
264 259
 
@@ -284,8 +279,7 @@  discard block
 block discarded – undo
284 279
      * @throws TypeError
285 280
      * @psalm-suppress MixedArgument
286 281
      */
287
-    public static function compare($left, $right)
288
-    {
282
+    public static function compare($left, $right) {
289 283
         /* Type checks: */
290 284
         ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1);
291 285
         ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2);
@@ -307,8 +301,7 @@  discard block
 block discarded – undo
307 301
      * @psalm-suppress MixedInferredReturnType
308 302
      * @psalm-suppress MixedReturnStatement
309 303
      */
310
-    public static function crypto_aead_aes256gcm_is_available()
311
-    {
304
+    public static function crypto_aead_aes256gcm_is_available() {
312 305
         if (self::useNewSodiumAPI()) {
313 306
             return sodium_crypto_aead_aes256gcm_is_available();
314 307
         }
@@ -454,8 +447,7 @@  discard block
 block discarded – undo
454 447
      * @throws Exception
455 448
      * @throws Error
456 449
      */
457
-    public static function crypto_aead_aes256gcm_keygen()
458
-    {
450
+    public static function crypto_aead_aes256gcm_keygen() {
459 451
         return random_bytes(self::CRYPTO_AEAD_AES256GCM_KEYBYTES);
460 452
     }
461 453
 
@@ -702,8 +694,7 @@  discard block
 block discarded – undo
702 694
      * @throws Exception
703 695
      * @throws Error
704 696
      */
705
-    public static function crypto_aead_chacha20poly1305_keygen()
706
-    {
697
+    public static function crypto_aead_chacha20poly1305_keygen() {
707 698
         return random_bytes(self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES);
708 699
     }
709 700
 
@@ -790,8 +781,7 @@  discard block
 block discarded – undo
790 781
      * @throws Exception
791 782
      * @throws Error
792 783
      */
793
-    public static function crypto_aead_chacha20poly1305_ietf_keygen()
794
-    {
784
+    public static function crypto_aead_chacha20poly1305_ietf_keygen() {
795 785
         return random_bytes(self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES);
796 786
     }
797 787
 
@@ -949,8 +939,7 @@  discard block
 block discarded – undo
949 939
      * @throws Exception
950 940
      * @throws Error
951 941
      */
952
-    public static function crypto_aead_xchacha20poly1305_ietf_keygen()
953
-    {
942
+    public static function crypto_aead_xchacha20poly1305_ietf_keygen() {
954 943
         return random_bytes(self::CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES);
955 944
     }
956 945
 
@@ -971,8 +960,7 @@  discard block
 block discarded – undo
971 960
      * @throws TypeError
972 961
      * @psalm-suppress MixedArgument
973 962
      */
974
-    public static function crypto_auth($message, $key)
975
-    {
963
+    public static function crypto_auth($message, $key) {
976 964
         /* Type checks: */
977 965
         ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
978 966
         ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2);
@@ -999,8 +987,7 @@  discard block
 block discarded – undo
999 987
      * @throws Exception
1000 988
      * @throws Error
1001 989
      */
1002
-    public static function crypto_auth_keygen()
1003
-    {
990
+    public static function crypto_auth_keygen() {
1004 991
         return random_bytes(self::CRYPTO_AUTH_KEYBYTES);
1005 992
     }
1006 993
 
@@ -1016,8 +1003,7 @@  discard block
 block discarded – undo
1016 1003
      * @throws TypeError
1017 1004
      * @psalm-suppress MixedArgument
1018 1005
      */
1019
-    public static function crypto_auth_verify($mac, $message, $key)
1020
-    {
1006
+    public static function crypto_auth_verify($mac, $message, $key) {
1021 1007
         /* Type checks: */
1022 1008
         ParagonIE_Sodium_Core_Util::declareScalarType($mac, 'string', 1);
1023 1009
         ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2);
@@ -1060,8 +1046,7 @@  discard block
 block discarded – undo
1060 1046
      * @throws TypeError
1061 1047
      * @psalm-suppress MixedArgument
1062 1048
      */
1063
-    public static function crypto_box($plaintext, $nonce, $keypair)
1064
-    {
1049
+    public static function crypto_box($plaintext, $nonce, $keypair) {
1065 1050
         /* Type checks: */
1066 1051
         ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
1067 1052
         ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
@@ -1104,8 +1089,7 @@  discard block
 block discarded – undo
1104 1089
      * @throws TypeError
1105 1090
      * @psalm-suppress MixedArgument
1106 1091
      */
1107
-    public static function crypto_box_seal($plaintext, $publicKey)
1108
-    {
1092
+    public static function crypto_box_seal($plaintext, $publicKey) {
1109 1093
         /* Type checks: */
1110 1094
         ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
1111 1095
         ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
@@ -1142,8 +1126,7 @@  discard block
 block discarded – undo
1142 1126
      * @psalm-suppress MixedInferredReturnType
1143 1127
      * @psalm-suppress MixedReturnStatement
1144 1128
      */
1145
-    public static function crypto_box_seal_open($ciphertext, $keypair)
1146
-    {
1129
+    public static function crypto_box_seal_open($ciphertext, $keypair) {
1147 1130
         /* Type checks: */
1148 1131
         ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
1149 1132
         ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 2);
@@ -1180,8 +1163,7 @@  discard block
 block discarded – undo
1180 1163
      * @throws TypeError
1181 1164
      * @psalm-suppress MixedArgument
1182 1165
      */
1183
-    public static function crypto_box_keypair()
1184
-    {
1166
+    public static function crypto_box_keypair() {
1185 1167
         if (self::useNewSodiumAPI()) {
1186 1168
             return (string) sodium_crypto_box_keypair();
1187 1169
         }
@@ -1205,8 +1187,7 @@  discard block
 block discarded – undo
1205 1187
      * @throws TypeError
1206 1188
      * @psalm-suppress MixedArgument
1207 1189
      */
1208
-    public static function crypto_box_keypair_from_secretkey_and_publickey($secretKey, $publicKey)
1209
-    {
1190
+    public static function crypto_box_keypair_from_secretkey_and_publickey($secretKey, $publicKey) {
1210 1191
         /* Type checks: */
1211 1192
         ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
1212 1193
         ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
@@ -1244,8 +1225,7 @@  discard block
 block discarded – undo
1244 1225
      * @psalm-suppress MixedInferredReturnType
1245 1226
      * @psalm-suppress MixedReturnStatement
1246 1227
      */
1247
-    public static function crypto_box_open($ciphertext, $nonce, $keypair)
1248
-    {
1228
+    public static function crypto_box_open($ciphertext, $nonce, $keypair) {
1249 1229
         /* Type checks: */
1250 1230
         ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
1251 1231
         ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
@@ -1287,8 +1267,7 @@  discard block
 block discarded – undo
1287 1267
      * @throws TypeError
1288 1268
      * @psalm-suppress MixedArgument
1289 1269
      */
1290
-    public static function crypto_box_publickey($keypair)
1291
-    {
1270
+    public static function crypto_box_publickey($keypair) {
1292 1271
         /* Type checks: */
1293 1272
         ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1294 1273
 
@@ -1318,8 +1297,7 @@  discard block
 block discarded – undo
1318 1297
      * @throws TypeError
1319 1298
      * @psalm-suppress MixedArgument
1320 1299
      */
1321
-    public static function crypto_box_publickey_from_secretkey($secretKey)
1322
-    {
1300
+    public static function crypto_box_publickey_from_secretkey($secretKey) {
1323 1301
         /* Type checks: */
1324 1302
         ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
1325 1303
 
@@ -1349,8 +1327,7 @@  discard block
 block discarded – undo
1349 1327
      * @throws TypeError
1350 1328
      * @psalm-suppress MixedArgument
1351 1329
      */
1352
-    public static function crypto_box_secretkey($keypair)
1353
-    {
1330
+    public static function crypto_box_secretkey($keypair) {
1354 1331
         /* Type checks: */
1355 1332
         ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1356 1333
 
@@ -1381,8 +1358,7 @@  discard block
 block discarded – undo
1381 1358
      * @psalm-suppress MixedArgument
1382 1359
      * @psalm-suppress UndefinedFunction
1383 1360
      */
1384
-    public static function crypto_box_seed_keypair($seed)
1385
-    {
1361
+    public static function crypto_box_seed_keypair($seed) {
1386 1362
         /* Type checks: */
1387 1363
         ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1);
1388 1364
 
@@ -1411,8 +1387,7 @@  discard block
 block discarded – undo
1411 1387
      * @throws TypeError
1412 1388
      * @psalm-suppress MixedArgument
1413 1389
      */
1414
-    public static function crypto_generichash($message, $key = '', $length = self::CRYPTO_GENERICHASH_BYTES)
1415
-    {
1390
+    public static function crypto_generichash($message, $key = '', $length = self::CRYPTO_GENERICHASH_BYTES) {
1416 1391
         /* Type checks: */
1417 1392
         ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
1418 1393
         if (is_null($key)) {
@@ -1455,8 +1430,7 @@  discard block
 block discarded – undo
1455 1430
      * @psalm-suppress ReferenceConstraintViolation
1456 1431
      * @psalm-suppress ConflictingReferenceConstraint
1457 1432
      */
1458
-    public static function crypto_generichash_final(&$ctx, $length = self::CRYPTO_GENERICHASH_BYTES)
1459
-    {
1433
+    public static function crypto_generichash_final(&$ctx, $length = self::CRYPTO_GENERICHASH_BYTES) {
1460 1434
         /* Type checks: */
1461 1435
         ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1);
1462 1436
         ParagonIE_Sodium_Core_Util::declareScalarType($length, 'int', 2);
@@ -1500,8 +1474,7 @@  discard block
 block discarded – undo
1500 1474
      * @throws TypeError
1501 1475
      * @psalm-suppress MixedArgument
1502 1476
      */
1503
-    public static function crypto_generichash_init($key = '', $length = self::CRYPTO_GENERICHASH_BYTES)
1504
-    {
1477
+    public static function crypto_generichash_init($key = '', $length = self::CRYPTO_GENERICHASH_BYTES) {
1505 1478
         /* Type checks: */
1506 1479
         if (is_null($key)) {
1507 1480
             $key = '';
@@ -1591,8 +1564,7 @@  discard block
 block discarded – undo
1591 1564
      * @psalm-suppress MixedArgument
1592 1565
      * @psalm-suppress ReferenceConstraintViolation
1593 1566
      */
1594
-    public static function crypto_generichash_update(&$ctx, $message)
1595
-    {
1567
+    public static function crypto_generichash_update(&$ctx, $message) {
1596 1568
         /* Type checks: */
1597 1569
         ParagonIE_Sodium_Core_Util::declareScalarType($ctx, 'string', 1);
1598 1570
         ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2);
@@ -1618,8 +1590,7 @@  discard block
 block discarded – undo
1618 1590
      * @throws Exception
1619 1591
      * @throws Error
1620 1592
      */
1621
-    public static function crypto_generichash_keygen()
1622
-    {
1593
+    public static function crypto_generichash_keygen() {
1623 1594
         return random_bytes(self::CRYPTO_GENERICHASH_KEYBYTES);
1624 1595
     }
1625 1596
 
@@ -1677,8 +1648,7 @@  discard block
 block discarded – undo
1677 1648
      * @throws Exception
1678 1649
      * @throws Error
1679 1650
      */
1680
-    public static function crypto_kdf_keygen()
1681
-    {
1651
+    public static function crypto_kdf_keygen() {
1682 1652
         return random_bytes(self::CRYPTO_KDF_KEYBYTES);
1683 1653
     }
1684 1654
 
@@ -1712,8 +1682,7 @@  discard block
 block discarded – undo
1712 1682
      * @throws TypeError
1713 1683
      * @psalm-suppress MixedArgument
1714 1684
      */
1715
-    public static function crypto_kx($my_secret, $their_public, $client_public, $server_public, $dontFallback = false)
1716
-    {
1685
+    public static function crypto_kx($my_secret, $their_public, $client_public, $server_public, $dontFallback = false) {
1717 1686
         /* Type checks: */
1718 1687
         ParagonIE_Sodium_Core_Util::declareScalarType($my_secret, 'string', 1);
1719 1688
         ParagonIE_Sodium_Core_Util::declareScalarType($their_public, 'string', 2);
@@ -1774,8 +1743,7 @@  discard block
 block discarded – undo
1774 1743
      * @return string
1775 1744
      * @throws SodiumException
1776 1745
      */
1777
-    public static function crypto_kx_seed_keypair($seed)
1778
-    {
1746
+    public static function crypto_kx_seed_keypair($seed) {
1779 1747
         ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1);
1780 1748
 
1781 1749
         $seed = (string) $seed;
@@ -1793,8 +1761,7 @@  discard block
 block discarded – undo
1793 1761
      * @return string
1794 1762
      * @throws Exception
1795 1763
      */
1796
-    public static function crypto_kx_keypair()
1797
-    {
1764
+    public static function crypto_kx_keypair() {
1798 1765
         $sk = self::randombytes_buf(self::CRYPTO_KX_SECRETKEYBYTES);
1799 1766
         $pk = self::crypto_scalarmult_base($sk);
1800 1767
         return $sk . $pk;
@@ -1806,8 +1773,7 @@  discard block
 block discarded – undo
1806 1773
      * @return array{0: string, 1: string}
1807 1774
      * @throws SodiumException
1808 1775
      */
1809
-    public static function crypto_kx_client_session_keys($keypair, $serverPublicKey)
1810
-    {
1776
+    public static function crypto_kx_client_session_keys($keypair, $serverPublicKey) {
1811 1777
         ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1812 1778
         ParagonIE_Sodium_Core_Util::declareScalarType($serverPublicKey, 'string', 2);
1813 1779
 
@@ -1848,8 +1814,7 @@  discard block
 block discarded – undo
1848 1814
      * @return array{0: string, 1: string}
1849 1815
      * @throws SodiumException
1850 1816
      */
1851
-    public static function crypto_kx_server_session_keys($keypair, $clientPublicKey)
1852
-    {
1817
+    public static function crypto_kx_server_session_keys($keypair, $clientPublicKey) {
1853 1818
         ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
1854 1819
         ParagonIE_Sodium_Core_Util::declareScalarType($clientPublicKey, 'string', 2);
1855 1820
 
@@ -1889,8 +1854,7 @@  discard block
 block discarded – undo
1889 1854
      * @return string
1890 1855
      * @throws SodiumException
1891 1856
      */
1892
-    public static function crypto_kx_secretkey($kp)
1893
-    {
1857
+    public static function crypto_kx_secretkey($kp) {
1894 1858
         return ParagonIE_Sodium_Core_Util::substr(
1895 1859
             $kp,
1896 1860
             0,
@@ -1903,8 +1867,7 @@  discard block
 block discarded – undo
1903 1867
      * @return string
1904 1868
      * @throws SodiumException
1905 1869
      */
1906
-    public static function crypto_kx_publickey($kp)
1907
-    {
1870
+    public static function crypto_kx_publickey($kp) {
1908 1871
         return ParagonIE_Sodium_Core_Util::substr(
1909 1872
             $kp,
1910 1873
             self::CRYPTO_KX_SECRETKEYBYTES,
@@ -1924,8 +1887,7 @@  discard block
 block discarded – undo
1924 1887
      * @throws TypeError
1925 1888
      * @psalm-suppress MixedArgument
1926 1889
      */
1927
-    public static function crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit, $alg = null)
1928
-    {
1890
+    public static function crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit, $alg = null) {
1929 1891
         ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1);
1930 1892
         ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2);
1931 1893
         ParagonIE_Sodium_Core_Util::declareScalarType($salt,  'string', 3);
@@ -1956,8 +1918,7 @@  discard block
 block discarded – undo
1956 1918
      *
1957 1919
      * @return bool
1958 1920
      */
1959
-    public static function crypto_pwhash_is_available()
1960
-    {
1921
+    public static function crypto_pwhash_is_available() {
1961 1922
         if (self::useNewSodiumAPI()) {
1962 1923
             return true;
1963 1924
         }
@@ -1976,8 +1937,7 @@  discard block
 block discarded – undo
1976 1937
      * @throws TypeError
1977 1938
      * @psalm-suppress MixedArgument
1978 1939
      */
1979
-    public static function crypto_pwhash_str($passwd, $opslimit, $memlimit)
1980
-    {
1940
+    public static function crypto_pwhash_str($passwd, $opslimit, $memlimit) {
1981 1941
         ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
1982 1942
         ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2);
1983 1943
         ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);
@@ -2003,8 +1963,7 @@  discard block
 block discarded – undo
2003 1963
      * @return bool
2004 1964
      * @throws SodiumException
2005 1965
      */
2006
-    public static function crypto_pwhash_str_needs_rehash($hash, $opslimit, $memlimit)
2007
-    {
1966
+    public static function crypto_pwhash_str_needs_rehash($hash, $opslimit, $memlimit) {
2008 1967
         ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 1);
2009 1968
         ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2);
2010 1969
         ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);
@@ -2032,8 +1991,7 @@  discard block
 block discarded – undo
2032 1991
      * @throws TypeError
2033 1992
      * @psalm-suppress MixedArgument
2034 1993
      */
2035
-    public static function crypto_pwhash_str_verify($passwd, $hash)
2036
-    {
1994
+    public static function crypto_pwhash_str_verify($passwd, $hash) {
2037 1995
         ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
2038 1996
         ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2);
2039 1997
 
@@ -2059,8 +2017,7 @@  discard block
 block discarded – undo
2059 2017
      * @throws SodiumException
2060 2018
      * @throws TypeError
2061 2019
      */
2062
-    public static function crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit)
2063
-    {
2020
+    public static function crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit) {
2064 2021
         ParagonIE_Sodium_Core_Util::declareScalarType($outlen, 'int', 1);
2065 2022
         ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 2);
2066 2023
         ParagonIE_Sodium_Core_Util::declareScalarType($salt,  'string', 3);
@@ -2100,8 +2057,7 @@  discard block
 block discarded – undo
2100 2057
      *
2101 2058
      * @return bool
2102 2059
      */
2103
-    public static function crypto_pwhash_scryptsalsa208sha256_is_available()
2104
-    {
2060
+    public static function crypto_pwhash_scryptsalsa208sha256_is_available() {
2105 2061
         if (self::useNewSodiumAPI()) {
2106 2062
             return true;
2107 2063
         }
@@ -2119,8 +2075,7 @@  discard block
 block discarded – undo
2119 2075
      * @throws SodiumException
2120 2076
      * @throws TypeError
2121 2077
      */
2122
-    public static function crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit)
2123
-    {
2078
+    public static function crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit) {
2124 2079
         ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
2125 2080
         ParagonIE_Sodium_Core_Util::declareScalarType($opslimit, 'int', 2);
2126 2081
         ParagonIE_Sodium_Core_Util::declareScalarType($memlimit, 'int', 3);
@@ -2153,8 +2108,7 @@  discard block
 block discarded – undo
2153 2108
      * @throws SodiumException
2154 2109
      * @throws TypeError
2155 2110
      */
2156
-    public static function crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash)
2157
-    {
2111
+    public static function crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash) {
2158 2112
         ParagonIE_Sodium_Core_Util::declareScalarType($passwd, 'string', 1);
2159 2113
         ParagonIE_Sodium_Core_Util::declareScalarType($hash, 'string', 2);
2160 2114
 
@@ -2190,8 +2144,7 @@  discard block
 block discarded – undo
2190 2144
      * @throws TypeError
2191 2145
      * @psalm-suppress MixedArgument
2192 2146
      */
2193
-    public static function crypto_scalarmult($secretKey, $publicKey)
2194
-    {
2147
+    public static function crypto_scalarmult($secretKey, $publicKey) {
2195 2148
         /* Type checks: */
2196 2149
         ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
2197 2150
         ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
@@ -2234,8 +2187,7 @@  discard block
 block discarded – undo
2234 2187
      * @psalm-suppress TooFewArguments
2235 2188
      * @psalm-suppress MixedArgument
2236 2189
      */
2237
-    public static function crypto_scalarmult_base($secretKey)
2238
-    {
2190
+    public static function crypto_scalarmult_base($secretKey) {
2239 2191
         /* Type checks: */
2240 2192
         ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
2241 2193
 
@@ -2272,8 +2224,7 @@  discard block
 block discarded – undo
2272 2224
      * @throws TypeError
2273 2225
      * @psalm-suppress MixedArgument
2274 2226
      */
2275
-    public static function crypto_secretbox($plaintext, $nonce, $key)
2276
-    {
2227
+    public static function crypto_secretbox($plaintext, $nonce, $key) {
2277 2228
         /* Type checks: */
2278 2229
         ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
2279 2230
         ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
@@ -2312,8 +2263,7 @@  discard block
 block discarded – undo
2312 2263
      * @psalm-suppress MixedInferredReturnType
2313 2264
      * @psalm-suppress MixedReturnStatement
2314 2265
      */
2315
-    public static function crypto_secretbox_open($ciphertext, $nonce, $key)
2316
-    {
2266
+    public static function crypto_secretbox_open($ciphertext, $nonce, $key) {
2317 2267
         /* Type checks: */
2318 2268
         ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
2319 2269
         ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
@@ -2350,8 +2300,7 @@  discard block
 block discarded – undo
2350 2300
      * @throws Exception
2351 2301
      * @throws Error
2352 2302
      */
2353
-    public static function crypto_secretbox_keygen()
2354
-    {
2303
+    public static function crypto_secretbox_keygen() {
2355 2304
         return random_bytes(self::CRYPTO_SECRETBOX_KEYBYTES);
2356 2305
     }
2357 2306
 
@@ -2368,8 +2317,7 @@  discard block
 block discarded – undo
2368 2317
      * @throws TypeError
2369 2318
      * @psalm-suppress MixedArgument
2370 2319
      */
2371
-    public static function crypto_secretbox_xchacha20poly1305($plaintext, $nonce, $key)
2372
-    {
2320
+    public static function crypto_secretbox_xchacha20poly1305($plaintext, $nonce, $key) {
2373 2321
         /* Type checks: */
2374 2322
         ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
2375 2323
         ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
@@ -2398,8 +2346,7 @@  discard block
 block discarded – undo
2398 2346
      * @throws TypeError
2399 2347
      * @psalm-suppress MixedArgument
2400 2348
      */
2401
-    public static function crypto_secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
2402
-    {
2349
+    public static function crypto_secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) {
2403 2350
         /* Type checks: */
2404 2351
         ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
2405 2352
         ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
@@ -2425,8 +2372,7 @@  discard block
 block discarded – undo
2425 2372
      * @throws Exception
2426 2373
      * @throws SodiumException
2427 2374
      */
2428
-    public static function crypto_secretstream_xchacha20poly1305_init_push($key)
2429
-    {
2375
+    public static function crypto_secretstream_xchacha20poly1305_init_push($key) {
2430 2376
         if (PHP_INT_SIZE === 4) {
2431 2377
             return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_init_push($key);
2432 2378
         }
@@ -2439,8 +2385,7 @@  discard block
 block discarded – undo
2439 2385
      * @return string Returns a state.
2440 2386
      * @throws Exception
2441 2387
      */
2442
-    public static function crypto_secretstream_xchacha20poly1305_init_pull($header, $key)
2443
-    {
2388
+    public static function crypto_secretstream_xchacha20poly1305_init_pull($header, $key) {
2444 2389
         if (ParagonIE_Sodium_Core_Util::strlen($header) < self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES) {
2445 2390
             throw new SodiumException(
2446 2391
                 'header size should be SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES bytes'
@@ -2460,8 +2405,7 @@  discard block
 block discarded – undo
2460 2405
      * @return string
2461 2406
      * @throws SodiumException
2462 2407
      */
2463
-    public static function crypto_secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
2464
-    {
2408
+    public static function crypto_secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0) {
2465 2409
         if (PHP_INT_SIZE === 4) {
2466 2410
             return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_push(
2467 2411
                 $state,
@@ -2485,8 +2429,7 @@  discard block
 block discarded – undo
2485 2429
      * @return bool|array{0: string, 1: int}
2486 2430
      * @throws SodiumException
2487 2431
      */
2488
-    public static function crypto_secretstream_xchacha20poly1305_pull(&$state, $msg, $aad = '')
2489
-    {
2432
+    public static function crypto_secretstream_xchacha20poly1305_pull(&$state, $msg, $aad = '') {
2490 2433
         if (PHP_INT_SIZE === 4) {
2491 2434
             return ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_pull(
2492 2435
                 $state,
@@ -2505,8 +2448,7 @@  discard block
 block discarded – undo
2505 2448
      * @return string
2506 2449
      * @throws Exception
2507 2450
      */
2508
-    public static function crypto_secretstream_xchacha20poly1305_keygen()
2509
-    {
2451
+    public static function crypto_secretstream_xchacha20poly1305_keygen() {
2510 2452
         return random_bytes(self::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES);
2511 2453
     }
2512 2454
 
@@ -2515,8 +2457,7 @@  discard block
 block discarded – undo
2515 2457
      * @return void
2516 2458
      * @throws SodiumException
2517 2459
      */
2518
-    public static function crypto_secretstream_xchacha20poly1305_rekey(&$state)
2519
-    {
2460
+    public static function crypto_secretstream_xchacha20poly1305_rekey(&$state) {
2520 2461
         if (PHP_INT_SIZE === 4) {
2521 2462
             ParagonIE_Sodium_Crypto32::secretstream_xchacha20poly1305_rekey($state);
2522 2463
         } else {
@@ -2536,8 +2477,7 @@  discard block
 block discarded – undo
2536 2477
      * @psalm-suppress MixedInferredReturnType
2537 2478
      * @psalm-suppress MixedReturnStatement
2538 2479
      */
2539
-    public static function crypto_shorthash($message, $key)
2540
-    {
2480
+    public static function crypto_shorthash($message, $key) {
2541 2481
         /* Type checks: */
2542 2482
         ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
2543 2483
         ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 2);
@@ -2566,8 +2506,7 @@  discard block
 block discarded – undo
2566 2506
      * @throws Exception
2567 2507
      * @throws Error
2568 2508
      */
2569
-    public static function crypto_shorthash_keygen()
2570
-    {
2509
+    public static function crypto_shorthash_keygen() {
2571 2510
         return random_bytes(self::CRYPTO_SHORTHASH_KEYBYTES);
2572 2511
     }
2573 2512
 
@@ -2586,8 +2525,7 @@  discard block
 block discarded – undo
2586 2525
      * @psalm-suppress MixedInferredReturnType
2587 2526
      * @psalm-suppress MixedReturnStatement
2588 2527
      */
2589
-    public static function crypto_sign($message, $secretKey)
2590
-    {
2528
+    public static function crypto_sign($message, $secretKey) {
2591 2529
         /* Type checks: */
2592 2530
         ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
2593 2531
         ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2);
@@ -2622,8 +2560,7 @@  discard block
 block discarded – undo
2622 2560
      * @psalm-suppress MixedInferredReturnType
2623 2561
      * @psalm-suppress MixedReturnStatement
2624 2562
      */
2625
-    public static function crypto_sign_open($signedMessage, $publicKey)
2626
-    {
2563
+    public static function crypto_sign_open($signedMessage, $publicKey) {
2627 2564
         /* Type checks: */
2628 2565
         ParagonIE_Sodium_Core_Util::declareScalarType($signedMessage, 'string', 1);
2629 2566
         ParagonIE_Sodium_Core_Util::declareScalarType($publicKey, 'string', 2);
@@ -2659,8 +2596,7 @@  discard block
 block discarded – undo
2659 2596
      * @throws SodiumException
2660 2597
      * @throws TypeError
2661 2598
      */
2662
-    public static function crypto_sign_keypair()
2663
-    {
2599
+    public static function crypto_sign_keypair() {
2664 2600
         if (self::useNewSodiumAPI()) {
2665 2601
             return sodium_crypto_sign_keypair();
2666 2602
         }
@@ -2679,8 +2615,7 @@  discard block
 block discarded – undo
2679 2615
      * @return string
2680 2616
      * @throws SodiumException
2681 2617
      */
2682
-    public static function crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk)
2683
-    {
2618
+    public static function crypto_sign_keypair_from_secretkey_and_publickey($sk, $pk) {
2684 2619
         ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1);
2685 2620
         ParagonIE_Sodium_Core_Util::declareScalarType($pk, 'string', 1);
2686 2621
         $sk = (string) $sk;
@@ -2708,8 +2643,7 @@  discard block
 block discarded – undo
2708 2643
      * @throws TypeError
2709 2644
      * @psalm-suppress MixedArgument
2710 2645
      */
2711
-    public static function crypto_sign_seed_keypair($seed)
2712
-    {
2646
+    public static function crypto_sign_seed_keypair($seed) {
2713 2647
         ParagonIE_Sodium_Core_Util::declareScalarType($seed, 'string', 1);
2714 2648
 
2715 2649
         if (self::useNewSodiumAPI()) {
@@ -2737,8 +2671,7 @@  discard block
 block discarded – undo
2737 2671
      * @throws TypeError
2738 2672
      * @psalm-suppress MixedArgument
2739 2673
      */
2740
-    public static function crypto_sign_publickey($keypair)
2741
-    {
2674
+    public static function crypto_sign_publickey($keypair) {
2742 2675
         /* Type checks: */
2743 2676
         ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
2744 2677
 
@@ -2768,8 +2701,7 @@  discard block
 block discarded – undo
2768 2701
      * @throws TypeError
2769 2702
      * @psalm-suppress MixedArgument
2770 2703
      */
2771
-    public static function crypto_sign_publickey_from_secretkey($secretKey)
2772
-    {
2704
+    public static function crypto_sign_publickey_from_secretkey($secretKey) {
2773 2705
         /* Type checks: */
2774 2706
         ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 1);
2775 2707
 
@@ -2799,8 +2731,7 @@  discard block
 block discarded – undo
2799 2731
      * @throws TypeError
2800 2732
      * @psalm-suppress MixedArgument
2801 2733
      */
2802
-    public static function crypto_sign_secretkey($keypair)
2803
-    {
2734
+    public static function crypto_sign_secretkey($keypair) {
2804 2735
         /* Type checks: */
2805 2736
         ParagonIE_Sodium_Core_Util::declareScalarType($keypair, 'string', 1);
2806 2737
 
@@ -2833,8 +2764,7 @@  discard block
 block discarded – undo
2833 2764
      * @throws TypeError
2834 2765
      * @psalm-suppress MixedArgument
2835 2766
      */
2836
-    public static function crypto_sign_detached($message, $secretKey)
2837
-    {
2767
+    public static function crypto_sign_detached($message, $secretKey) {
2838 2768
         /* Type checks: */
2839 2769
         ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
2840 2770
         ParagonIE_Sodium_Core_Util::declareScalarType($secretKey, 'string', 2);
@@ -2868,8 +2798,7 @@  discard block
 block discarded – undo
2868 2798
      * @throws TypeError
2869 2799
      * @psalm-suppress MixedArgument
2870 2800
      */
2871
-    public static function crypto_sign_verify_detached($signature, $message, $publicKey)
2872
-    {
2801
+    public static function crypto_sign_verify_detached($signature, $message, $publicKey) {
2873 2802
         /* Type checks: */
2874 2803
         ParagonIE_Sodium_Core_Util::declareScalarType($signature, 'string', 1);
2875 2804
         ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 2);
@@ -2909,8 +2838,7 @@  discard block
 block discarded – undo
2909 2838
      * @throws TypeError
2910 2839
      * @psalm-suppress MixedArgument
2911 2840
      */
2912
-    public static function crypto_sign_ed25519_pk_to_curve25519($pk)
2913
-    {
2841
+    public static function crypto_sign_ed25519_pk_to_curve25519($pk) {
2914 2842
         /* Type checks: */
2915 2843
         ParagonIE_Sodium_Core_Util::declareScalarType($pk, 'string', 1);
2916 2844
 
@@ -2941,8 +2869,7 @@  discard block
 block discarded – undo
2941 2869
      * @throws TypeError
2942 2870
      * @psalm-suppress MixedArgument
2943 2871
      */
2944
-    public static function crypto_sign_ed25519_sk_to_curve25519($sk)
2945
-    {
2872
+    public static function crypto_sign_ed25519_sk_to_curve25519($sk) {
2946 2873
         /* Type checks: */
2947 2874
         ParagonIE_Sodium_Core_Util::declareScalarType($sk, 'string', 1);
2948 2875
 
@@ -2983,8 +2910,7 @@  discard block
 block discarded – undo
2983 2910
      * @throws TypeError
2984 2911
      * @psalm-suppress MixedArgument
2985 2912
      */
2986
-    public static function crypto_stream($len, $nonce, $key)
2987
-    {
2913
+    public static function crypto_stream($len, $nonce, $key) {
2988 2914
         /* Type checks: */
2989 2915
         ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1);
2990 2916
         ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
@@ -3030,8 +2956,7 @@  discard block
 block discarded – undo
3030 2956
      * @throws TypeError
3031 2957
      * @psalm-suppress MixedArgument
3032 2958
      */
3033
-    public static function crypto_stream_xor($message, $nonce, $key)
3034
-    {
2959
+    public static function crypto_stream_xor($message, $nonce, $key) {
3035 2960
         /* Type checks: */
3036 2961
         ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
3037 2962
         ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
@@ -3064,8 +2989,7 @@  discard block
 block discarded – undo
3064 2989
      * @throws Exception
3065 2990
      * @throws Error
3066 2991
      */
3067
-    public static function crypto_stream_keygen()
3068
-    {
2992
+    public static function crypto_stream_keygen() {
3069 2993
         return random_bytes(self::CRYPTO_STREAM_KEYBYTES);
3070 2994
     }
3071 2995
 
@@ -3085,8 +3009,7 @@  discard block
 block discarded – undo
3085 3009
      * @throws TypeError
3086 3010
      * @psalm-suppress MixedArgument
3087 3011
      */
3088
-    public static function crypto_stream_xchacha20($len, $nonce, $key, $dontFallback = false)
3089
-    {
3012
+    public static function crypto_stream_xchacha20($len, $nonce, $key, $dontFallback = false) {
3090 3013
         /* Type checks: */
3091 3014
         ParagonIE_Sodium_Core_Util::declareScalarType($len, 'int', 1);
3092 3015
         ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
@@ -3130,8 +3053,7 @@  discard block
 block discarded – undo
3130 3053
      * @throws TypeError
3131 3054
      * @psalm-suppress MixedArgument
3132 3055
      */
3133
-    public static function crypto_stream_xchacha20_xor($message, $nonce, $key, $dontFallback = false)
3134
-    {
3056
+    public static function crypto_stream_xchacha20_xor($message, $nonce, $key, $dontFallback = false) {
3135 3057
         /* Type checks: */
3136 3058
         ParagonIE_Sodium_Core_Util::declareScalarType($message, 'string', 1);
3137 3059
         ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 2);
@@ -3161,8 +3083,7 @@  discard block
 block discarded – undo
3161 3083
      * @throws Exception
3162 3084
      * @throws Error
3163 3085
      */
3164
-    public static function crypto_stream_xchacha20_keygen()
3165
-    {
3086
+    public static function crypto_stream_xchacha20_keygen() {
3166 3087
         return random_bytes(self::CRYPTO_STREAM_XCHACHA20_KEYBYTES);
3167 3088
     }
3168 3089
 
@@ -3176,8 +3097,7 @@  discard block
 block discarded – undo
3176 3097
      * @psalm-suppress TooFewArguments
3177 3098
      * @psalm-suppress MixedArgument
3178 3099
      */
3179
-    public static function hex2bin($string)
3180
-    {
3100
+    public static function hex2bin($string) {
3181 3101
         /* Type checks: */
3182 3102
         ParagonIE_Sodium_Core_Util::declareScalarType($string, 'string', 1);
3183 3103
 
@@ -3202,8 +3122,7 @@  discard block
 block discarded – undo
3202 3122
      * @throws TypeError
3203 3123
      * @psalm-suppress MixedArgument
3204 3124
      */
3205
-    public static function increment(&$var)
3206
-    {
3125
+    public static function increment(&$var) {
3207 3126
         /* Type checks: */
3208 3127
         ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1);
3209 3128
 
@@ -3236,8 +3155,7 @@  discard block
 block discarded – undo
3236 3155
      *
3237 3156
      * @throws SodiumException
3238 3157
      */
3239
-    public static function is_zero($str)
3240
-    {
3158
+    public static function is_zero($str) {
3241 3159
         $d = 0;
3242 3160
         for ($i = 0; $i < 32; ++$i) {
3243 3161
             $d |= ParagonIE_Sodium_Core_Util::chrToInt($str[$i]);
@@ -3251,8 +3169,7 @@  discard block
 block discarded – undo
3251 3169
      *
3252 3170
      * @return int
3253 3171
      */
3254
-    public static function library_version_major()
3255
-    {
3172
+    public static function library_version_major() {
3256 3173
         if (self::useNewSodiumAPI() && defined('SODIUM_LIBRARY_MAJOR_VERSION')) {
3257 3174
             return SODIUM_LIBRARY_MAJOR_VERSION;
3258 3175
         }
@@ -3269,8 +3186,7 @@  discard block
 block discarded – undo
3269 3186
      *
3270 3187
      * @return int
3271 3188
      */
3272
-    public static function library_version_minor()
3273
-    {
3189
+    public static function library_version_minor() {
3274 3190
         if (self::useNewSodiumAPI() && defined('SODIUM_LIBRARY_MINOR_VERSION')) {
3275 3191
             return SODIUM_LIBRARY_MINOR_VERSION;
3276 3192
         }
@@ -3291,8 +3207,7 @@  discard block
 block discarded – undo
3291 3207
      * @throws TypeError
3292 3208
      * @psalm-suppress MixedArgument
3293 3209
      */
3294
-    public static function memcmp($left, $right)
3295
-    {
3210
+    public static function memcmp($left, $right) {
3296 3211
         /* Type checks: */
3297 3212
         ParagonIE_Sodium_Core_Util::declareScalarType($left, 'string', 1);
3298 3213
         ParagonIE_Sodium_Core_Util::declareScalarType($right, 'string', 2);
@@ -3320,8 +3235,7 @@  discard block
 block discarded – undo
3320 3235
      * @throws TypeError
3321 3236
      * @psalm-suppress TooFewArguments
3322 3237
      */
3323
-    public static function memzero(&$var)
3324
-    {
3238
+    public static function memzero(&$var) {
3325 3239
         /* Type checks: */
3326 3240
         ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1);
3327 3241
 
@@ -3351,8 +3265,7 @@  discard block
 block discarded – undo
3351 3265
      * @return string
3352 3266
      * @throws SodiumException
3353 3267
      */
3354
-    public static function pad($unpadded, $blockSize, $dontFallback = false)
3355
-    {
3268
+    public static function pad($unpadded, $blockSize, $dontFallback = false) {
3356 3269
         /* Type checks: */
3357 3270
         ParagonIE_Sodium_Core_Util::declareScalarType($unpadded, 'string', 1);
3358 3271
         ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2);
@@ -3437,8 +3350,7 @@  discard block
 block discarded – undo
3437 3350
      * @return string
3438 3351
      * @throws SodiumException
3439 3352
      */
3440
-    public static function unpad($padded, $blockSize, $dontFallback = false)
3441
-    {
3353
+    public static function unpad($padded, $blockSize, $dontFallback = false) {
3442 3354
         /* Type checks: */
3443 3355
         ParagonIE_Sodium_Core_Util::declareScalarType($padded, 'string', 1);
3444 3356
         ParagonIE_Sodium_Core_Util::declareScalarType($blockSize, 'int', 2);
@@ -3501,8 +3413,7 @@  discard block
 block discarded – undo
3501 3413
      *
3502 3414
      * @return bool
3503 3415
      */
3504
-    public static function polyfill_is_fast()
3505
-    {
3416
+    public static function polyfill_is_fast() {
3506 3417
         if (extension_loaded('sodium')) {
3507 3418
             return true;
3508 3419
         }
@@ -3521,8 +3432,7 @@  discard block
 block discarded – undo
3521 3432
      * @throws Exception
3522 3433
      * @throws TypeError
3523 3434
      */
3524
-    public static function randombytes_buf($numBytes)
3525
-    {
3435
+    public static function randombytes_buf($numBytes) {
3526 3436
         /* Type checks: */
3527 3437
         if (!is_int($numBytes)) {
3528 3438
             if (is_numeric($numBytes)) {
@@ -3548,8 +3458,7 @@  discard block
 block discarded – undo
3548 3458
      * @throws Error
3549 3459
      * @throws TypeError
3550 3460
      */
3551
-    public static function randombytes_uniform($range)
3552
-    {
3461
+    public static function randombytes_uniform($range) {
3553 3462
         /* Type checks: */
3554 3463
         if (!is_int($range)) {
3555 3464
             if (is_numeric($range)) {
@@ -3574,8 +3483,7 @@  discard block
 block discarded – undo
3574 3483
      * @throws Error
3575 3484
      * @throws TypeError
3576 3485
      */
3577
-    public static function randombytes_random16()
3578
-    {
3486
+    public static function randombytes_random16() {
3579 3487
         if (self::use_fallback('randombytes_random16')) {
3580 3488
             return (int) call_user_func('\\Sodium\\randombytes_random16');
3581 3489
         }
@@ -3588,8 +3496,7 @@  discard block
 block discarded – undo
3588 3496
      * @return bool
3589 3497
      * @throws SodiumException
3590 3498
      */
3591
-    public static function ristretto255_is_valid_point($p, $dontFallback = false)
3592
-    {
3499
+    public static function ristretto255_is_valid_point($p, $dontFallback = false) {
3593 3500
         if (self::useNewSodiumAPI() && !$dontFallback) {
3594 3501
             return sodium_crypto_core_ristretto255_is_valid_point($p);
3595 3502
         }
@@ -3612,8 +3519,7 @@  discard block
 block discarded – undo
3612 3519
      * @return string
3613 3520
      * @throws SodiumException
3614 3521
      */
3615
-    public static function ristretto255_add($p, $q, $dontFallback = false)
3616
-    {
3522
+    public static function ristretto255_add($p, $q, $dontFallback = false) {
3617 3523
         if (self::useNewSodiumAPI() && !$dontFallback) {
3618 3524
             return sodium_crypto_core_ristretto255_add($p, $q);
3619 3525
         }
@@ -3627,8 +3533,7 @@  discard block
 block discarded – undo
3627 3533
      * @return string
3628 3534
      * @throws SodiumException
3629 3535
      */
3630
-    public static function ristretto255_sub($p, $q, $dontFallback = false)
3631
-    {
3536
+    public static function ristretto255_sub($p, $q, $dontFallback = false) {
3632 3537
         if (self::useNewSodiumAPI() && !$dontFallback) {
3633 3538
             return sodium_crypto_core_ristretto255_sub($p, $q);
3634 3539
         }
@@ -3642,8 +3547,7 @@  discard block
 block discarded – undo
3642 3547
      *
3643 3548
      * @throws SodiumException
3644 3549
      */
3645
-    public static function ristretto255_from_hash($r, $dontFallback = false)
3646
-    {
3550
+    public static function ristretto255_from_hash($r, $dontFallback = false) {
3647 3551
         if (self::useNewSodiumAPI() && !$dontFallback) {
3648 3552
             return sodium_crypto_core_ristretto255_from_hash($r);
3649 3553
         }
@@ -3656,8 +3560,7 @@  discard block
 block discarded – undo
3656 3560
      *
3657 3561
      * @throws SodiumException
3658 3562
      */
3659
-    public static function ristretto255_random($dontFallback = false)
3660
-    {
3563
+    public static function ristretto255_random($dontFallback = false) {
3661 3564
         if (self::useNewSodiumAPI() && !$dontFallback) {
3662 3565
             return sodium_crypto_core_ristretto255_random();
3663 3566
         }
@@ -3670,8 +3573,7 @@  discard block
 block discarded – undo
3670 3573
      *
3671 3574
      * @throws SodiumException
3672 3575
      */
3673
-    public static function ristretto255_scalar_random($dontFallback = false)
3674
-    {
3576
+    public static function ristretto255_scalar_random($dontFallback = false) {
3675 3577
         if (self::useNewSodiumAPI() && !$dontFallback) {
3676 3578
             return sodium_crypto_core_ristretto255_scalar_random();
3677 3579
         }
@@ -3684,8 +3586,7 @@  discard block
 block discarded – undo
3684 3586
      * @return string
3685 3587
      * @throws SodiumException
3686 3588
      */
3687
-    public static function ristretto255_scalar_invert($s, $dontFallback = false)
3688
-    {
3589
+    public static function ristretto255_scalar_invert($s, $dontFallback = false) {
3689 3590
         if (self::useNewSodiumAPI() && !$dontFallback) {
3690 3591
             return sodium_crypto_core_ristretto255_scalar_invert($s);
3691 3592
         }
@@ -3697,8 +3598,7 @@  discard block
 block discarded – undo
3697 3598
      * @return string
3698 3599
      * @throws SodiumException
3699 3600
      */
3700
-    public static function ristretto255_scalar_negate($s, $dontFallback = false)
3701
-    {
3601
+    public static function ristretto255_scalar_negate($s, $dontFallback = false) {
3702 3602
         if (self::useNewSodiumAPI() && !$dontFallback) {
3703 3603
             return sodium_crypto_core_ristretto255_scalar_negate($s);
3704 3604
         }
@@ -3711,8 +3611,7 @@  discard block
 block discarded – undo
3711 3611
      * @return string
3712 3612
      * @throws SodiumException
3713 3613
      */
3714
-    public static function ristretto255_scalar_complement($s, $dontFallback = false)
3715
-    {
3614
+    public static function ristretto255_scalar_complement($s, $dontFallback = false) {
3716 3615
         if (self::useNewSodiumAPI() && !$dontFallback) {
3717 3616
             return sodium_crypto_core_ristretto255_scalar_complement($s);
3718 3617
         }
@@ -3726,8 +3625,7 @@  discard block
 block discarded – undo
3726 3625
      * @return string
3727 3626
      * @throws SodiumException
3728 3627
      */
3729
-    public static function ristretto255_scalar_add($x, $y, $dontFallback = false)
3730
-    {
3628
+    public static function ristretto255_scalar_add($x, $y, $dontFallback = false) {
3731 3629
         if (self::useNewSodiumAPI() && !$dontFallback) {
3732 3630
             return sodium_crypto_core_ristretto255_scalar_add($x, $y);
3733 3631
         }
@@ -3741,8 +3639,7 @@  discard block
 block discarded – undo
3741 3639
      * @return string
3742 3640
      * @throws SodiumException
3743 3641
      */
3744
-    public static function ristretto255_scalar_sub($x, $y, $dontFallback = false)
3745
-    {
3642
+    public static function ristretto255_scalar_sub($x, $y, $dontFallback = false) {
3746 3643
         if (self::useNewSodiumAPI() && !$dontFallback) {
3747 3644
             return sodium_crypto_core_ristretto255_scalar_sub($x, $y);
3748 3645
         }
@@ -3756,8 +3653,7 @@  discard block
 block discarded – undo
3756 3653
      * @return string
3757 3654
      * @throws SodiumException
3758 3655
      */
3759
-    public static function ristretto255_scalar_mul($x, $y, $dontFallback = false)
3760
-    {
3656
+    public static function ristretto255_scalar_mul($x, $y, $dontFallback = false) {
3761 3657
         if (self::useNewSodiumAPI() && !$dontFallback) {
3762 3658
             return sodium_crypto_core_ristretto255_scalar_mul($x, $y);
3763 3659
         }
@@ -3771,8 +3667,7 @@  discard block
 block discarded – undo
3771 3667
      * @return string
3772 3668
      * @throws SodiumException
3773 3669
      */
3774
-    public static function scalarmult_ristretto255($n, $p, $dontFallback = false)
3775
-    {
3670
+    public static function scalarmult_ristretto255($n, $p, $dontFallback = false) {
3776 3671
         if (self::useNewSodiumAPI() && !$dontFallback) {
3777 3672
             return sodium_crypto_scalarmult_ristretto255($n, $p);
3778 3673
         }
@@ -3786,8 +3681,7 @@  discard block
 block discarded – undo
3786 3681
      * @return string
3787 3682
      * @throws SodiumException
3788 3683
      */
3789
-    public static function scalarmult_ristretto255_base($n, $dontFallback = false)
3790
-    {
3684
+    public static function scalarmult_ristretto255_base($n, $dontFallback = false) {
3791 3685
         if (self::useNewSodiumAPI() && !$dontFallback) {
3792 3686
             return sodium_crypto_scalarmult_ristretto255_base($n);
3793 3687
         }
@@ -3800,8 +3694,7 @@  discard block
 block discarded – undo
3800 3694
      * @return string
3801 3695
      * @throws SodiumException
3802 3696
      */
3803
-    public static function ristretto255_scalar_reduce($s, $dontFallback = false)
3804
-    {
3697
+    public static function ristretto255_scalar_reduce($s, $dontFallback = false) {
3805 3698
         if (self::useNewSodiumAPI() && !$dontFallback) {
3806 3699
             return sodium_crypto_core_ristretto255_scalar_reduce($s);
3807 3700
         }
@@ -3821,8 +3714,7 @@  discard block
 block discarded – undo
3821 3714
      * @return bool           TRUE if we're fast enough, FALSE is not
3822 3715
      * @throws SodiumException
3823 3716
      */
3824
-    public static function runtime_speed_test($iterations, $maxTimeout)
3825
-    {
3717
+    public static function runtime_speed_test($iterations, $maxTimeout) {
3826 3718
         if (self::polyfill_is_fast()) {
3827 3719
             return true;
3828 3720
         }
@@ -3855,8 +3747,7 @@  discard block
 block discarded – undo
3855 3747
      * @return void
3856 3748
      * @throws SodiumException
3857 3749
      */
3858
-    public static function sub(&$val, $addv)
3859
-    {
3750
+    public static function sub(&$val, $addv) {
3860 3751
         $val_len = ParagonIE_Sodium_Core_Util::strlen($val);
3861 3752
         $addv_len = ParagonIE_Sodium_Core_Util::strlen($addv);
3862 3753
         if ($val_len !== $addv_len) {
@@ -3882,8 +3773,7 @@  discard block
 block discarded – undo
3882 3773
      * @psalm-suppress MixedInferredReturnType
3883 3774
      * @psalm-suppress UndefinedFunction
3884 3775
      */
3885
-    public static function version_string()
3886
-    {
3776
+    public static function version_string() {
3887 3777
         if (self::useNewSodiumAPI()) {
3888 3778
             return (string) sodium_version_string();
3889 3779
         }
@@ -3905,8 +3795,7 @@  discard block
 block discarded – undo
3905 3795
      *
3906 3796
      * @return bool
3907 3797
      */
3908
-    protected static function use_fallback($sodium_func_name = '')
3909
-    {
3798
+    protected static function use_fallback($sodium_func_name = '') {
3910 3799
         static $res = null;
3911 3800
         if ($res === null) {
3912 3801
             $res = extension_loaded('libsodium') && PHP_VERSION_ID >= 50300;
@@ -3932,8 +3821,7 @@  discard block
 block discarded – undo
3932 3821
      * @ref https://wiki.php.net/rfc/libsodium
3933 3822
      * @return bool
3934 3823
      */
3935
-    protected static function useNewSodiumAPI()
3936
-    {
3824
+    protected static function useNewSodiumAPI() {
3937 3825
         static $res = null;
3938 3826
         if ($res === null) {
3939 3827
             $res = PHP_VERSION_ID >= 70000 && extension_loaded('sodium');
Please login to merge, or discard this patch.
vendor/paragonie/sodium_compat/src/Crypto32.php 3 patches
Indentation   +1638 added lines, -1638 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 if (class_exists('ParagonIE_Sodium_Crypto32', false)) {
4
-    return;
4
+	return;
5 5
 }
6 6
 
7 7
 /**
@@ -14,1641 +14,1641 @@  discard block
 block discarded – undo
14 14
  */
15 15
 abstract class ParagonIE_Sodium_Crypto32
16 16
 {
17
-    const aead_chacha20poly1305_KEYBYTES = 32;
18
-    const aead_chacha20poly1305_NSECBYTES = 0;
19
-    const aead_chacha20poly1305_NPUBBYTES = 8;
20
-    const aead_chacha20poly1305_ABYTES = 16;
21
-
22
-    const aead_chacha20poly1305_IETF_KEYBYTES = 32;
23
-    const aead_chacha20poly1305_IETF_NSECBYTES = 0;
24
-    const aead_chacha20poly1305_IETF_NPUBBYTES = 12;
25
-    const aead_chacha20poly1305_IETF_ABYTES = 16;
26
-
27
-    const aead_xchacha20poly1305_IETF_KEYBYTES = 32;
28
-    const aead_xchacha20poly1305_IETF_NSECBYTES = 0;
29
-    const aead_xchacha20poly1305_IETF_NPUBBYTES = 24;
30
-    const aead_xchacha20poly1305_IETF_ABYTES = 16;
31
-
32
-    const box_curve25519xsalsa20poly1305_SEEDBYTES = 32;
33
-    const box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32;
34
-    const box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32;
35
-    const box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32;
36
-    const box_curve25519xsalsa20poly1305_NONCEBYTES = 24;
37
-    const box_curve25519xsalsa20poly1305_MACBYTES = 16;
38
-    const box_curve25519xsalsa20poly1305_BOXZEROBYTES = 16;
39
-    const box_curve25519xsalsa20poly1305_ZEROBYTES = 32;
40
-
41
-    const onetimeauth_poly1305_BYTES = 16;
42
-    const onetimeauth_poly1305_KEYBYTES = 32;
43
-
44
-    const secretbox_xsalsa20poly1305_KEYBYTES = 32;
45
-    const secretbox_xsalsa20poly1305_NONCEBYTES = 24;
46
-    const secretbox_xsalsa20poly1305_MACBYTES = 16;
47
-    const secretbox_xsalsa20poly1305_BOXZEROBYTES = 16;
48
-    const secretbox_xsalsa20poly1305_ZEROBYTES = 32;
49
-
50
-    const secretbox_xchacha20poly1305_KEYBYTES = 32;
51
-    const secretbox_xchacha20poly1305_NONCEBYTES = 24;
52
-    const secretbox_xchacha20poly1305_MACBYTES = 16;
53
-    const secretbox_xchacha20poly1305_BOXZEROBYTES = 16;
54
-    const secretbox_xchacha20poly1305_ZEROBYTES = 32;
55
-
56
-    const stream_salsa20_KEYBYTES = 32;
57
-
58
-    /**
59
-     * AEAD Decryption with ChaCha20-Poly1305
60
-     *
61
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
62
-     *
63
-     * @param string $message
64
-     * @param string $ad
65
-     * @param string $nonce
66
-     * @param string $key
67
-     * @return string
68
-     * @throws SodiumException
69
-     * @throws TypeError
70
-     */
71
-    public static function aead_chacha20poly1305_decrypt(
72
-        $message = '',
73
-        $ad = '',
74
-        $nonce = '',
75
-        $key = ''
76
-    ) {
77
-        /** @var int $len - Length of message (ciphertext + MAC) */
78
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
79
-
80
-        /** @var int  $clen - Length of ciphertext */
81
-        $clen = $len - self::aead_chacha20poly1305_ABYTES;
82
-
83
-        /** @var int $adlen - Length of associated data */
84
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
85
-
86
-        /** @var string $mac - Message authentication code */
87
-        $mac = ParagonIE_Sodium_Core32_Util::substr(
88
-            $message,
89
-            $clen,
90
-            self::aead_chacha20poly1305_ABYTES
91
-        );
92
-
93
-        /** @var string $ciphertext - The encrypted message (sans MAC) */
94
-        $ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 0, $clen);
95
-
96
-        /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
97
-        $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
98
-            32,
99
-            $nonce,
100
-            $key
101
-        );
102
-
103
-        /* Recalculate the Poly1305 authentication tag (MAC): */
104
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
105
-        try {
106
-            ParagonIE_Sodium_Compat::memzero($block0);
107
-        } catch (SodiumException $ex) {
108
-            $block0 = null;
109
-        }
110
-        $state->update($ad);
111
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
112
-        $state->update($ciphertext);
113
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
114
-        $computed_mac = $state->finish();
115
-
116
-        /* Compare the given MAC with the recalculated MAC: */
117
-        if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
118
-            throw new SodiumException('Invalid MAC');
119
-        }
120
-
121
-        // Here, we know that the MAC is valid, so we decrypt and return the plaintext
122
-        return ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
123
-            $ciphertext,
124
-            $nonce,
125
-            $key,
126
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
127
-        );
128
-    }
129
-
130
-    /**
131
-     * AEAD Encryption with ChaCha20-Poly1305
132
-     *
133
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
134
-     *
135
-     * @param string $message
136
-     * @param string $ad
137
-     * @param string $nonce
138
-     * @param string $key
139
-     * @return string
140
-     * @throws SodiumException
141
-     * @throws TypeError
142
-     */
143
-    public static function aead_chacha20poly1305_encrypt(
144
-        $message = '',
145
-        $ad = '',
146
-        $nonce = '',
147
-        $key = ''
148
-    ) {
149
-        /** @var int $len - Length of the plaintext message */
150
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
151
-
152
-        /** @var int $adlen - Length of the associated data */
153
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
154
-
155
-        /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
156
-        $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
157
-            32,
158
-            $nonce,
159
-            $key
160
-        );
161
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
162
-        try {
163
-            ParagonIE_Sodium_Compat::memzero($block0);
164
-        } catch (SodiumException $ex) {
165
-            $block0 = null;
166
-        }
167
-
168
-        /** @var string $ciphertext - Raw encrypted data */
169
-        $ciphertext = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
170
-            $message,
171
-            $nonce,
172
-            $key,
173
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
174
-        );
175
-
176
-        $state->update($ad);
177
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
178
-        $state->update($ciphertext);
179
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
180
-        return $ciphertext . $state->finish();
181
-    }
182
-
183
-    /**
184
-     * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
185
-     *
186
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
187
-     *
188
-     * @param string $message
189
-     * @param string $ad
190
-     * @param string $nonce
191
-     * @param string $key
192
-     * @return string
193
-     * @throws SodiumException
194
-     * @throws TypeError
195
-     */
196
-    public static function aead_chacha20poly1305_ietf_decrypt(
197
-        $message = '',
198
-        $ad = '',
199
-        $nonce = '',
200
-        $key = ''
201
-    ) {
202
-        /** @var int $adlen - Length of associated data */
203
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
204
-
205
-        /** @var int $len - Length of message (ciphertext + MAC) */
206
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
207
-
208
-        /** @var int  $clen - Length of ciphertext */
209
-        $clen = $len - self::aead_chacha20poly1305_IETF_ABYTES;
210
-
211
-        /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
212
-        $block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream(
213
-            32,
214
-            $nonce,
215
-            $key
216
-        );
217
-
218
-        /** @var string $mac - Message authentication code */
219
-        $mac = ParagonIE_Sodium_Core32_Util::substr(
220
-            $message,
221
-            $len - self::aead_chacha20poly1305_IETF_ABYTES,
222
-            self::aead_chacha20poly1305_IETF_ABYTES
223
-        );
224
-
225
-        /** @var string $ciphertext - The encrypted message (sans MAC) */
226
-        $ciphertext = ParagonIE_Sodium_Core32_Util::substr(
227
-            $message,
228
-            0,
229
-            $len - self::aead_chacha20poly1305_IETF_ABYTES
230
-        );
231
-
232
-        /* Recalculate the Poly1305 authentication tag (MAC): */
233
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
234
-        try {
235
-            ParagonIE_Sodium_Compat::memzero($block0);
236
-        } catch (SodiumException $ex) {
237
-            $block0 = null;
238
-        }
239
-        $state->update($ad);
240
-        $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
241
-        $state->update($ciphertext);
242
-        $state->update(str_repeat("\x00", (0x10 - $clen) & 0xf));
243
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
244
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
245
-        $computed_mac = $state->finish();
246
-
247
-        /* Compare the given MAC with the recalculated MAC: */
248
-        if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
249
-            throw new SodiumException('Invalid MAC');
250
-        }
251
-
252
-        // Here, we know that the MAC is valid, so we decrypt and return the plaintext
253
-        return ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
254
-            $ciphertext,
255
-            $nonce,
256
-            $key,
257
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
258
-        );
259
-    }
260
-
261
-    /**
262
-     * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
263
-     *
264
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
265
-     *
266
-     * @param string $message
267
-     * @param string $ad
268
-     * @param string $nonce
269
-     * @param string $key
270
-     * @return string
271
-     * @throws SodiumException
272
-     * @throws TypeError
273
-     */
274
-    public static function aead_chacha20poly1305_ietf_encrypt(
275
-        $message = '',
276
-        $ad = '',
277
-        $nonce = '',
278
-        $key = ''
279
-    ) {
280
-        /** @var int $len - Length of the plaintext message */
281
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
282
-
283
-        /** @var int $adlen - Length of the associated data */
284
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
285
-
286
-        /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
287
-        $block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream(
288
-            32,
289
-            $nonce,
290
-            $key
291
-        );
292
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
293
-        try {
294
-            ParagonIE_Sodium_Compat::memzero($block0);
295
-        } catch (SodiumException $ex) {
296
-            $block0 = null;
297
-        }
298
-
299
-        /** @var string $ciphertext - Raw encrypted data */
300
-        $ciphertext = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
301
-            $message,
302
-            $nonce,
303
-            $key,
304
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
305
-        );
306
-
307
-        $state->update($ad);
308
-        $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
309
-        $state->update($ciphertext);
310
-        $state->update(str_repeat("\x00", ((0x10 - $len) & 0xf)));
311
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
312
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
313
-        return $ciphertext . $state->finish();
314
-    }
315
-
316
-    /**
317
-     * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
318
-     *
319
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
320
-     *
321
-     * @param string $message
322
-     * @param string $ad
323
-     * @param string $nonce
324
-     * @param string $key
325
-     * @return string
326
-     * @throws SodiumException
327
-     * @throws TypeError
328
-     */
329
-    public static function aead_xchacha20poly1305_ietf_decrypt(
330
-        $message = '',
331
-        $ad = '',
332
-        $nonce = '',
333
-        $key = ''
334
-    ) {
335
-        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
336
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
337
-            $key
338
-        );
339
-        $nonceLast = "\x00\x00\x00\x00" .
340
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
341
-
342
-        return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey);
343
-    }
344
-
345
-    /**
346
-     * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
347
-     *
348
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
349
-     *
350
-     * @param string $message
351
-     * @param string $ad
352
-     * @param string $nonce
353
-     * @param string $key
354
-     * @return string
355
-     * @throws SodiumException
356
-     * @throws TypeError
357
-     */
358
-    public static function aead_xchacha20poly1305_ietf_encrypt(
359
-        $message = '',
360
-        $ad = '',
361
-        $nonce = '',
362
-        $key = ''
363
-    ) {
364
-        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
365
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
366
-            $key
367
-        );
368
-        $nonceLast = "\x00\x00\x00\x00" .
369
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
370
-
371
-        return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey);
372
-    }
373
-
374
-    /**
375
-     * HMAC-SHA-512-256 (a.k.a. the leftmost 256 bits of HMAC-SHA-512)
376
-     *
377
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
378
-     *
379
-     * @param string $message
380
-     * @param string $key
381
-     * @return string
382
-     * @throws TypeError
383
-     */
384
-    public static function auth($message, $key)
385
-    {
386
-        return ParagonIE_Sodium_Core32_Util::substr(
387
-            hash_hmac('sha512', $message, $key, true),
388
-            0,
389
-            32
390
-        );
391
-    }
392
-
393
-    /**
394
-     * HMAC-SHA-512-256 validation. Constant-time via hash_equals().
395
-     *
396
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
397
-     *
398
-     * @param string $mac
399
-     * @param string $message
400
-     * @param string $key
401
-     * @return bool
402
-     * @throws SodiumException
403
-     * @throws TypeError
404
-     */
405
-    public static function auth_verify($mac, $message, $key)
406
-    {
407
-        return ParagonIE_Sodium_Core32_Util::hashEquals(
408
-            $mac,
409
-            self::auth($message, $key)
410
-        );
411
-    }
412
-
413
-    /**
414
-     * X25519 key exchange followed by XSalsa20Poly1305 symmetric encryption
415
-     *
416
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
417
-     *
418
-     * @param string $plaintext
419
-     * @param string $nonce
420
-     * @param string $keypair
421
-     * @return string
422
-     * @throws SodiumException
423
-     * @throws TypeError
424
-     */
425
-    public static function box($plaintext, $nonce, $keypair)
426
-    {
427
-        return self::secretbox(
428
-            $plaintext,
429
-            $nonce,
430
-            self::box_beforenm(
431
-                self::box_secretkey($keypair),
432
-                self::box_publickey($keypair)
433
-            )
434
-        );
435
-    }
436
-
437
-    /**
438
-     * X25519-XSalsa20-Poly1305 with one ephemeral X25519 keypair.
439
-     *
440
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
441
-     *
442
-     * @param string $message
443
-     * @param string $publicKey
444
-     * @return string
445
-     * @throws SodiumException
446
-     * @throws TypeError
447
-     */
448
-    public static function box_seal($message, $publicKey)
449
-    {
450
-        /** @var string $ephemeralKeypair */
451
-        $ephemeralKeypair = self::box_keypair();
452
-
453
-        /** @var string $ephemeralSK */
454
-        $ephemeralSK = self::box_secretkey($ephemeralKeypair);
455
-
456
-        /** @var string $ephemeralPK */
457
-        $ephemeralPK = self::box_publickey($ephemeralKeypair);
458
-
459
-        /** @var string $nonce */
460
-        $nonce = self::generichash(
461
-            $ephemeralPK . $publicKey,
462
-            '',
463
-            24
464
-        );
465
-
466
-        /** @var string $keypair - The combined keypair used in crypto_box() */
467
-        $keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey);
468
-
469
-        /** @var string $ciphertext Ciphertext + MAC from crypto_box */
470
-        $ciphertext = self::box($message, $nonce, $keypair);
471
-        try {
472
-            ParagonIE_Sodium_Compat::memzero($ephemeralKeypair);
473
-            ParagonIE_Sodium_Compat::memzero($ephemeralSK);
474
-            ParagonIE_Sodium_Compat::memzero($nonce);
475
-        } catch (SodiumException $ex) {
476
-            $ephemeralKeypair = null;
477
-            $ephemeralSK = null;
478
-            $nonce = null;
479
-        }
480
-        return $ephemeralPK . $ciphertext;
481
-    }
482
-
483
-    /**
484
-     * Opens a message encrypted via box_seal().
485
-     *
486
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
487
-     *
488
-     * @param string $message
489
-     * @param string $keypair
490
-     * @return string
491
-     * @throws SodiumException
492
-     * @throws TypeError
493
-     */
494
-    public static function box_seal_open($message, $keypair)
495
-    {
496
-        /** @var string $ephemeralPK */
497
-        $ephemeralPK = ParagonIE_Sodium_Core32_Util::substr($message, 0, 32);
498
-
499
-        /** @var string $ciphertext (ciphertext + MAC) */
500
-        $ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 32);
501
-
502
-        /** @var string $secretKey */
503
-        $secretKey = self::box_secretkey($keypair);
504
-
505
-        /** @var string $publicKey */
506
-        $publicKey = self::box_publickey($keypair);
507
-
508
-        /** @var string $nonce */
509
-        $nonce = self::generichash(
510
-            $ephemeralPK . $publicKey,
511
-            '',
512
-            24
513
-        );
514
-
515
-        /** @var string $keypair */
516
-        $keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK);
517
-
518
-        /** @var string $m */
519
-        $m = self::box_open($ciphertext, $nonce, $keypair);
520
-        try {
521
-            ParagonIE_Sodium_Compat::memzero($secretKey);
522
-            ParagonIE_Sodium_Compat::memzero($ephemeralPK);
523
-            ParagonIE_Sodium_Compat::memzero($nonce);
524
-        } catch (SodiumException $ex) {
525
-            $secretKey = null;
526
-            $ephemeralPK = null;
527
-            $nonce = null;
528
-        }
529
-        return $m;
530
-    }
531
-
532
-    /**
533
-     * Used by crypto_box() to get the crypto_secretbox() key.
534
-     *
535
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
536
-     *
537
-     * @param string $sk
538
-     * @param string $pk
539
-     * @return string
540
-     * @throws SodiumException
541
-     * @throws TypeError
542
-     */
543
-    public static function box_beforenm($sk, $pk)
544
-    {
545
-        return ParagonIE_Sodium_Core32_HSalsa20::hsalsa20(
546
-            str_repeat("\x00", 16),
547
-            self::scalarmult($sk, $pk)
548
-        );
549
-    }
550
-
551
-    /**
552
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
553
-     *
554
-     * @return string
555
-     * @throws Exception
556
-     * @throws SodiumException
557
-     * @throws TypeError
558
-     */
559
-    public static function box_keypair()
560
-    {
561
-        $sKey = random_bytes(32);
562
-        $pKey = self::scalarmult_base($sKey);
563
-        return $sKey . $pKey;
564
-    }
565
-
566
-    /**
567
-     * @param string $seed
568
-     * @return string
569
-     * @throws SodiumException
570
-     * @throws TypeError
571
-     */
572
-    public static function box_seed_keypair($seed)
573
-    {
574
-        $sKey = ParagonIE_Sodium_Core32_Util::substr(
575
-            hash('sha512', $seed, true),
576
-            0,
577
-            32
578
-        );
579
-        $pKey = self::scalarmult_base($sKey);
580
-        return $sKey . $pKey;
581
-    }
582
-
583
-    /**
584
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
585
-     *
586
-     * @param string $sKey
587
-     * @param string $pKey
588
-     * @return string
589
-     * @throws TypeError
590
-     */
591
-    public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey)
592
-    {
593
-        return ParagonIE_Sodium_Core32_Util::substr($sKey, 0, 32) .
594
-            ParagonIE_Sodium_Core32_Util::substr($pKey, 0, 32);
595
-    }
596
-
597
-    /**
598
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
599
-     *
600
-     * @param string $keypair
601
-     * @return string
602
-     * @throws RangeException
603
-     * @throws TypeError
604
-     */
605
-    public static function box_secretkey($keypair)
606
-    {
607
-        if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== 64) {
608
-            throw new RangeException(
609
-                'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
610
-            );
611
-        }
612
-        return ParagonIE_Sodium_Core32_Util::substr($keypair, 0, 32);
613
-    }
614
-
615
-    /**
616
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
617
-     *
618
-     * @param string $keypair
619
-     * @return string
620
-     * @throws RangeException
621
-     * @throws TypeError
622
-     */
623
-    public static function box_publickey($keypair)
624
-    {
625
-        if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
626
-            throw new RangeException(
627
-                'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
628
-            );
629
-        }
630
-        return ParagonIE_Sodium_Core32_Util::substr($keypair, 32, 32);
631
-    }
632
-
633
-    /**
634
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
635
-     *
636
-     * @param string $sKey
637
-     * @return string
638
-     * @throws RangeException
639
-     * @throws SodiumException
640
-     * @throws TypeError
641
-     */
642
-    public static function box_publickey_from_secretkey($sKey)
643
-    {
644
-        if (ParagonIE_Sodium_Core32_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) {
645
-            throw new RangeException(
646
-                'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.'
647
-            );
648
-        }
649
-        return self::scalarmult_base($sKey);
650
-    }
651
-
652
-    /**
653
-     * Decrypt a message encrypted with box().
654
-     *
655
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
656
-     *
657
-     * @param string $ciphertext
658
-     * @param string $nonce
659
-     * @param string $keypair
660
-     * @return string
661
-     * @throws SodiumException
662
-     * @throws TypeError
663
-     */
664
-    public static function box_open($ciphertext, $nonce, $keypair)
665
-    {
666
-        return self::secretbox_open(
667
-            $ciphertext,
668
-            $nonce,
669
-            self::box_beforenm(
670
-                self::box_secretkey($keypair),
671
-                self::box_publickey($keypair)
672
-            )
673
-        );
674
-    }
675
-
676
-    /**
677
-     * Calculate a BLAKE2b hash.
678
-     *
679
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
680
-     *
681
-     * @param string $message
682
-     * @param string|null $key
683
-     * @param int $outlen
684
-     * @return string
685
-     * @throws RangeException
686
-     * @throws SodiumException
687
-     * @throws TypeError
688
-     */
689
-    public static function generichash($message, $key = '', $outlen = 32)
690
-    {
691
-        // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
692
-        ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
693
-
694
-        $k = null;
695
-        if (!empty($key)) {
696
-            /** @var SplFixedArray $k */
697
-            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
698
-            if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
699
-                throw new RangeException('Invalid key size');
700
-            }
701
-        }
702
-
703
-        /** @var SplFixedArray $in */
704
-        $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message);
705
-
706
-        /** @var SplFixedArray $ctx */
707
-        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outlen);
708
-        ParagonIE_Sodium_Core32_BLAKE2b::update($ctx, $in, $in->count());
709
-
710
-        /** @var SplFixedArray $out */
711
-        $out = new SplFixedArray($outlen);
712
-        $out = ParagonIE_Sodium_Core32_BLAKE2b::finish($ctx, $out);
713
-
714
-        /** @var array<int, int> */
715
-        $outArray = $out->toArray();
716
-        return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray);
717
-    }
718
-
719
-    /**
720
-     * Finalize a BLAKE2b hashing context, returning the hash.
721
-     *
722
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
723
-     *
724
-     * @param string $ctx
725
-     * @param int $outlen
726
-     * @return string
727
-     * @throws SodiumException
728
-     * @throws TypeError
729
-     */
730
-    public static function generichash_final($ctx, $outlen = 32)
731
-    {
732
-        if (!is_string($ctx)) {
733
-            throw new TypeError('Context must be a string');
734
-        }
735
-        $out = new SplFixedArray($outlen);
736
-
737
-        /** @var SplFixedArray $context */
738
-        $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx);
739
-
740
-        /** @var SplFixedArray $out */
741
-        $out = ParagonIE_Sodium_Core32_BLAKE2b::finish($context, $out);
742
-
743
-        /** @var array<int, int> */
744
-        $outArray = $out->toArray();
745
-        return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray);
746
-    }
747
-
748
-    /**
749
-     * Initialize a hashing context for BLAKE2b.
750
-     *
751
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
752
-     *
753
-     * @param string $key
754
-     * @param int $outputLength
755
-     * @return string
756
-     * @throws RangeException
757
-     * @throws SodiumException
758
-     * @throws TypeError
759
-     */
760
-    public static function generichash_init($key = '', $outputLength = 32)
761
-    {
762
-        // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
763
-        ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
764
-
765
-        $k = null;
766
-        if (!empty($key)) {
767
-            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
768
-            if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
769
-                throw new RangeException('Invalid key size');
770
-            }
771
-        }
772
-
773
-        /** @var SplFixedArray $ctx */
774
-        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outputLength);
775
-
776
-        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($ctx);
777
-    }
778
-
779
-    /**
780
-     * Initialize a hashing context for BLAKE2b.
781
-     *
782
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
783
-     *
784
-     * @param string $key
785
-     * @param int $outputLength
786
-     * @param string $salt
787
-     * @param string $personal
788
-     * @return string
789
-     * @throws RangeException
790
-     * @throws SodiumException
791
-     * @throws TypeError
792
-     */
793
-    public static function generichash_init_salt_personal(
794
-        $key = '',
795
-        $outputLength = 32,
796
-        $salt = '',
797
-        $personal = ''
798
-    ) {
799
-        // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
800
-        ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
801
-
802
-        $k = null;
803
-        if (!empty($key)) {
804
-            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
805
-            if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
806
-                throw new RangeException('Invalid key size');
807
-            }
808
-        }
809
-        if (!empty($salt)) {
810
-            $s = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($salt);
811
-        } else {
812
-            $s = null;
813
-        }
814
-        if (!empty($salt)) {
815
-            $p = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($personal);
816
-        } else {
817
-            $p = null;
818
-        }
819
-
820
-        /** @var SplFixedArray $ctx */
821
-        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outputLength, $s, $p);
822
-
823
-        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($ctx);
824
-    }
825
-
826
-    /**
827
-     * Update a hashing context for BLAKE2b with $message
828
-     *
829
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
830
-     *
831
-     * @param string $ctx
832
-     * @param string $message
833
-     * @return string
834
-     * @throws SodiumException
835
-     * @throws TypeError
836
-     */
837
-    public static function generichash_update($ctx, $message)
838
-    {
839
-        // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
840
-        ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
841
-
842
-        /** @var SplFixedArray $context */
843
-        $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx);
844
-
845
-        /** @var SplFixedArray $in */
846
-        $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message);
847
-
848
-        ParagonIE_Sodium_Core32_BLAKE2b::update($context, $in, $in->count());
849
-
850
-        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($context);
851
-    }
852
-
853
-    /**
854
-     * Libsodium's crypto_kx().
855
-     *
856
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
857
-     *
858
-     * @param string $my_sk
859
-     * @param string $their_pk
860
-     * @param string $client_pk
861
-     * @param string $server_pk
862
-     * @return string
863
-     * @throws SodiumException
864
-     * @throws TypeError
865
-     */
866
-    public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk)
867
-    {
868
-        return self::generichash(
869
-            self::scalarmult($my_sk, $their_pk) .
870
-            $client_pk .
871
-            $server_pk
872
-        );
873
-    }
874
-
875
-    /**
876
-     * ECDH over Curve25519
877
-     *
878
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
879
-     *
880
-     * @param string $sKey
881
-     * @param string $pKey
882
-     * @return string
883
-     *
884
-     * @throws SodiumException
885
-     * @throws TypeError
886
-     */
887
-    public static function scalarmult($sKey, $pKey)
888
-    {
889
-        $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey);
890
-        self::scalarmult_throw_if_zero($q);
891
-        return $q;
892
-    }
893
-
894
-    /**
895
-     * ECDH over Curve25519, using the basepoint.
896
-     * Used to get a secret key from a public key.
897
-     *
898
-     * @param string $secret
899
-     * @return string
900
-     *
901
-     * @throws SodiumException
902
-     * @throws TypeError
903
-     */
904
-    public static function scalarmult_base($secret)
905
-    {
906
-        $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10_base($secret);
907
-        self::scalarmult_throw_if_zero($q);
908
-        return $q;
909
-    }
910
-
911
-    /**
912
-     * This throws an Error if a zero public key was passed to the function.
913
-     *
914
-     * @param string $q
915
-     * @return void
916
-     * @throws SodiumException
917
-     * @throws TypeError
918
-     */
919
-    protected static function scalarmult_throw_if_zero($q)
920
-    {
921
-        $d = 0;
922
-        for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) {
923
-            $d |= ParagonIE_Sodium_Core32_Util::chrToInt($q[$i]);
924
-        }
925
-
926
-        /* branch-free variant of === 0 */
927
-        if (-(1 & (($d - 1) >> 8))) {
928
-            throw new SodiumException('Zero public key is not allowed');
929
-        }
930
-    }
931
-
932
-    /**
933
-     * XSalsa20-Poly1305 authenticated symmetric-key encryption.
934
-     *
935
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
936
-     *
937
-     * @param string $plaintext
938
-     * @param string $nonce
939
-     * @param string $key
940
-     * @return string
941
-     * @throws SodiumException
942
-     * @throws TypeError
943
-     */
944
-    public static function secretbox($plaintext, $nonce, $key)
945
-    {
946
-        /** @var string $subkey */
947
-        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
948
-
949
-        /** @var string $block0 */
950
-        $block0 = str_repeat("\x00", 32);
951
-
952
-        /** @var int $mlen - Length of the plaintext message */
953
-        $mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext);
954
-        $mlen0 = $mlen;
955
-        if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) {
956
-            $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES;
957
-        }
958
-        $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
959
-
960
-        /** @var string $block0 */
961
-        $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor(
962
-            $block0,
963
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
964
-            $subkey
965
-        );
966
-
967
-        /** @var string $c */
968
-        $c = ParagonIE_Sodium_Core32_Util::substr(
969
-            $block0,
970
-            self::secretbox_xsalsa20poly1305_ZEROBYTES
971
-        );
972
-        if ($mlen > $mlen0) {
973
-            $c .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
974
-                ParagonIE_Sodium_Core32_Util::substr(
975
-                    $plaintext,
976
-                    self::secretbox_xsalsa20poly1305_ZEROBYTES
977
-                ),
978
-                ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
979
-                1,
980
-                $subkey
981
-            );
982
-        }
983
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State(
984
-            ParagonIE_Sodium_Core32_Util::substr(
985
-                $block0,
986
-                0,
987
-                self::onetimeauth_poly1305_KEYBYTES
988
-            )
989
-        );
990
-        try {
991
-            ParagonIE_Sodium_Compat::memzero($block0);
992
-            ParagonIE_Sodium_Compat::memzero($subkey);
993
-        } catch (SodiumException $ex) {
994
-            $block0 = null;
995
-            $subkey = null;
996
-        }
997
-
998
-        $state->update($c);
999
-
1000
-        /** @var string $c - MAC || ciphertext */
1001
-        $c = $state->finish() . $c;
1002
-        unset($state);
1003
-
1004
-        return $c;
1005
-    }
1006
-
1007
-    /**
1008
-     * Decrypt a ciphertext generated via secretbox().
1009
-     *
1010
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1011
-     *
1012
-     * @param string $ciphertext
1013
-     * @param string $nonce
1014
-     * @param string $key
1015
-     * @return string
1016
-     * @throws SodiumException
1017
-     * @throws TypeError
1018
-     */
1019
-    public static function secretbox_open($ciphertext, $nonce, $key)
1020
-    {
1021
-        /** @var string $mac */
1022
-        $mac = ParagonIE_Sodium_Core32_Util::substr(
1023
-            $ciphertext,
1024
-            0,
1025
-            self::secretbox_xsalsa20poly1305_MACBYTES
1026
-        );
1027
-
1028
-        /** @var string $c */
1029
-        $c = ParagonIE_Sodium_Core32_Util::substr(
1030
-            $ciphertext,
1031
-            self::secretbox_xsalsa20poly1305_MACBYTES
1032
-        );
1033
-
1034
-        /** @var int $clen */
1035
-        $clen = ParagonIE_Sodium_Core32_Util::strlen($c);
1036
-
1037
-        /** @var string $subkey */
1038
-        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
1039
-
1040
-        /** @var string $block0 */
1041
-        $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20(
1042
-            64,
1043
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1044
-            $subkey
1045
-        );
1046
-        $verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify(
1047
-            $mac,
1048
-            $c,
1049
-            ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32)
1050
-        );
1051
-        if (!$verified) {
1052
-            try {
1053
-                ParagonIE_Sodium_Compat::memzero($subkey);
1054
-            } catch (SodiumException $ex) {
1055
-                $subkey = null;
1056
-            }
1057
-            throw new SodiumException('Invalid MAC');
1058
-        }
1059
-
1060
-        /** @var string $m - Decrypted message */
1061
-        $m = ParagonIE_Sodium_Core32_Util::xorStrings(
1062
-            ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES),
1063
-            ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES)
1064
-        );
1065
-        if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) {
1066
-            // We had more than 1 block, so let's continue to decrypt the rest.
1067
-            $m .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
1068
-                ParagonIE_Sodium_Core32_Util::substr(
1069
-                    $c,
1070
-                    self::secretbox_xsalsa20poly1305_ZEROBYTES
1071
-                ),
1072
-                ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1073
-                1,
1074
-                (string) $subkey
1075
-            );
1076
-        }
1077
-        return $m;
1078
-    }
1079
-
1080
-    /**
1081
-     * XChaCha20-Poly1305 authenticated symmetric-key encryption.
1082
-     *
1083
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1084
-     *
1085
-     * @param string $plaintext
1086
-     * @param string $nonce
1087
-     * @param string $key
1088
-     * @return string
1089
-     * @throws SodiumException
1090
-     * @throws TypeError
1091
-     */
1092
-    public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key)
1093
-    {
1094
-        /** @var string $subkey */
1095
-        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
1096
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
1097
-            $key
1098
-        );
1099
-        $nonceLast = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
1100
-
1101
-        /** @var string $block0 */
1102
-        $block0 = str_repeat("\x00", 32);
1103
-
1104
-        /** @var int $mlen - Length of the plaintext message */
1105
-        $mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext);
1106
-        $mlen0 = $mlen;
1107
-        if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) {
1108
-            $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES;
1109
-        }
1110
-        $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
1111
-
1112
-        /** @var string $block0 */
1113
-        $block0 = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1114
-            $block0,
1115
-            $nonceLast,
1116
-            $subkey
1117
-        );
1118
-
1119
-        /** @var string $c */
1120
-        $c = ParagonIE_Sodium_Core32_Util::substr(
1121
-            $block0,
1122
-            self::secretbox_xchacha20poly1305_ZEROBYTES
1123
-        );
1124
-        if ($mlen > $mlen0) {
1125
-            $c .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1126
-                ParagonIE_Sodium_Core32_Util::substr(
1127
-                    $plaintext,
1128
-                    self::secretbox_xchacha20poly1305_ZEROBYTES
1129
-                ),
1130
-                $nonceLast,
1131
-                $subkey,
1132
-                ParagonIE_Sodium_Core32_Util::store64_le(1)
1133
-            );
1134
-        }
1135
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State(
1136
-            ParagonIE_Sodium_Core32_Util::substr(
1137
-                $block0,
1138
-                0,
1139
-                self::onetimeauth_poly1305_KEYBYTES
1140
-            )
1141
-        );
1142
-        try {
1143
-            ParagonIE_Sodium_Compat::memzero($block0);
1144
-            ParagonIE_Sodium_Compat::memzero($subkey);
1145
-        } catch (SodiumException $ex) {
1146
-            $block0 = null;
1147
-            $subkey = null;
1148
-        }
1149
-
1150
-        $state->update($c);
1151
-
1152
-        /** @var string $c - MAC || ciphertext */
1153
-        $c = $state->finish() . $c;
1154
-        unset($state);
1155
-
1156
-        return $c;
1157
-    }
1158
-
1159
-    /**
1160
-     * Decrypt a ciphertext generated via secretbox_xchacha20poly1305().
1161
-     *
1162
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1163
-     *
1164
-     * @param string $ciphertext
1165
-     * @param string $nonce
1166
-     * @param string $key
1167
-     * @return string
1168
-     * @throws SodiumException
1169
-     * @throws TypeError
1170
-     */
1171
-    public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
1172
-    {
1173
-        /** @var string $mac */
1174
-        $mac = ParagonIE_Sodium_Core32_Util::substr(
1175
-            $ciphertext,
1176
-            0,
1177
-            self::secretbox_xchacha20poly1305_MACBYTES
1178
-        );
1179
-
1180
-        /** @var string $c */
1181
-        $c = ParagonIE_Sodium_Core32_Util::substr(
1182
-            $ciphertext,
1183
-            self::secretbox_xchacha20poly1305_MACBYTES
1184
-        );
1185
-
1186
-        /** @var int $clen */
1187
-        $clen = ParagonIE_Sodium_Core32_Util::strlen($c);
1188
-
1189
-        /** @var string $subkey */
1190
-        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hchacha20($nonce, $key);
1191
-
1192
-        /** @var string $block0 */
1193
-        $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
1194
-            64,
1195
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1196
-            $subkey
1197
-        );
1198
-        $verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify(
1199
-            $mac,
1200
-            $c,
1201
-            ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32)
1202
-        );
1203
-
1204
-        if (!$verified) {
1205
-            try {
1206
-                ParagonIE_Sodium_Compat::memzero($subkey);
1207
-            } catch (SodiumException $ex) {
1208
-                $subkey = null;
1209
-            }
1210
-            throw new SodiumException('Invalid MAC');
1211
-        }
1212
-
1213
-        /** @var string $m - Decrypted message */
1214
-        $m = ParagonIE_Sodium_Core32_Util::xorStrings(
1215
-            ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES),
1216
-            ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES)
1217
-        );
1218
-
1219
-        if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) {
1220
-            // We had more than 1 block, so let's continue to decrypt the rest.
1221
-            $m .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1222
-                ParagonIE_Sodium_Core32_Util::substr(
1223
-                    $c,
1224
-                    self::secretbox_xchacha20poly1305_ZEROBYTES
1225
-                ),
1226
-                ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1227
-                (string) $subkey,
1228
-                ParagonIE_Sodium_Core32_Util::store64_le(1)
1229
-            );
1230
-        }
1231
-        return $m;
1232
-    }
1233
-
1234
-    /**
1235
-     * @param string $key
1236
-     * @return array<int, string> Returns a state and a header.
1237
-     * @throws Exception
1238
-     * @throws SodiumException
1239
-     */
1240
-    public static function secretstream_xchacha20poly1305_init_push($key)
1241
-    {
1242
-        # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES);
1243
-        $out = random_bytes(24);
1244
-
1245
-        # crypto_core_hchacha20(state->k, out, k, NULL);
1246
-        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20($out, $key);
1247
-        $state = new ParagonIE_Sodium_Core32_SecretStream_State(
1248
-            $subkey,
1249
-            ParagonIE_Sodium_Core32_Util::substr($out, 16, 8) . str_repeat("\0", 4)
1250
-        );
1251
-
1252
-        # _crypto_secretstream_xchacha20poly1305_counter_reset(state);
1253
-        $state->counterReset();
1254
-
1255
-        # memcpy(STATE_INONCE(state), out + crypto_core_hchacha20_INPUTBYTES,
1256
-        #        crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1257
-        # memset(state->_pad, 0, sizeof state->_pad);
1258
-        return array(
1259
-            $state->toString(),
1260
-            $out
1261
-        );
1262
-    }
1263
-
1264
-    /**
1265
-     * @param string $key
1266
-     * @param string $header
1267
-     * @return string Returns a state.
1268
-     * @throws Exception
1269
-     */
1270
-    public static function secretstream_xchacha20poly1305_init_pull($key, $header)
1271
-    {
1272
-        # crypto_core_hchacha20(state->k, in, k, NULL);
1273
-        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
1274
-            ParagonIE_Sodium_Core32_Util::substr($header, 0, 16),
1275
-            $key
1276
-        );
1277
-        $state = new ParagonIE_Sodium_Core32_SecretStream_State(
1278
-            $subkey,
1279
-            ParagonIE_Sodium_Core32_Util::substr($header, 16)
1280
-        );
1281
-        $state->counterReset();
1282
-        # memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES,
1283
-        #     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1284
-        # memset(state->_pad, 0, sizeof state->_pad);
1285
-        # return 0;
1286
-        return $state->toString();
1287
-    }
1288
-
1289
-    /**
1290
-     * @param string $state
1291
-     * @param string $msg
1292
-     * @param string $aad
1293
-     * @param int $tag
1294
-     * @return string
1295
-     * @throws SodiumException
1296
-     */
1297
-    public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
1298
-    {
1299
-        $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
1300
-        # crypto_onetimeauth_poly1305_state poly1305_state;
1301
-        # unsigned char                     block[64U];
1302
-        # unsigned char                     slen[8U];
1303
-        # unsigned char                    *c;
1304
-        # unsigned char                    *mac;
1305
-
1306
-        $msglen = ParagonIE_Sodium_Core32_Util::strlen($msg);
1307
-        $aadlen = ParagonIE_Sodium_Core32_Util::strlen($aad);
1308
-
1309
-        if ((($msglen + 63) >> 6) > 0xfffffffe) {
1310
-            throw new SodiumException(
1311
-                'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
1312
-            );
1313
-        }
1314
-
1315
-        # if (outlen_p != NULL) {
1316
-        #     *outlen_p = 0U;
1317
-        # }
1318
-        # if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
1319
-        #     sodium_misuse();
1320
-        # }
1321
-
1322
-        # crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
1323
-        # crypto_onetimeauth_poly1305_init(&poly1305_state, block);
1324
-        # sodium_memzero(block, sizeof block);
1325
-        $auth = new ParagonIE_Sodium_Core32_Poly1305_State(
1326
-            ParagonIE_Sodium_Core32_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
1327
-        );
1328
-
1329
-        # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
1330
-        $auth->update($aad);
1331
-
1332
-        # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
1333
-        #     (0x10 - adlen) & 0xf);
1334
-        $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
1335
-
1336
-        # memset(block, 0, sizeof block);
1337
-        # block[0] = tag;
1338
-        # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
1339
-        #                                    state->nonce, 1U, state->k);
1340
-        $block = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1341
-            ParagonIE_Sodium_Core32_Util::intToChr($tag) . str_repeat("\0", 63),
1342
-            $st->getCombinedNonce(),
1343
-            $st->getKey(),
1344
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
1345
-        );
1346
-
1347
-        # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
1348
-        $auth->update($block);
1349
-
1350
-        # out[0] = block[0];
1351
-        $out = $block[0];
1352
-        # c = out + (sizeof tag);
1353
-        # crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k);
1354
-        $cipher = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1355
-            $msg,
1356
-            $st->getCombinedNonce(),
1357
-            $st->getKey(),
1358
-            ParagonIE_Sodium_Core32_Util::store64_le(2)
1359
-        );
1360
-
1361
-        # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
1362
-        $auth->update($cipher);
1363
-
1364
-        $out .= $cipher;
1365
-        unset($cipher);
1366
-
1367
-        # crypto_onetimeauth_poly1305_update
1368
-        # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
1369
-        $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
1370
-
1371
-        # STORE64_LE(slen, (uint64_t) adlen);
1372
-        $slen = ParagonIE_Sodium_Core32_Util::store64_le($aadlen);
1373
-
1374
-        # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1375
-        $auth->update($slen);
1376
-
1377
-        # STORE64_LE(slen, (sizeof block) + mlen);
1378
-        $slen = ParagonIE_Sodium_Core32_Util::store64_le(64 + $msglen);
1379
-
1380
-        # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1381
-        $auth->update($slen);
1382
-
1383
-        # mac = c + mlen;
1384
-        # crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
1385
-        $mac = $auth->finish();
1386
-        $out .= $mac;
1387
-
1388
-        # sodium_memzero(&poly1305_state, sizeof poly1305_state);
1389
-        unset($auth);
1390
-
1391
-
1392
-        # XOR_BUF(STATE_INONCE(state), mac,
1393
-        #     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1394
-        $st->xorNonce($mac);
1395
-
1396
-        # sodium_increment(STATE_COUNTER(state),
1397
-        #     crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
1398
-        $st->incrementCounter();
1399
-        // Overwrite by reference:
1400
-        $state = $st->toString();
1401
-
1402
-        /** @var bool $rekey */
1403
-        $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
1404
-        # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
1405
-        #     sodium_is_zero(STATE_COUNTER(state),
1406
-        #         crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
1407
-        #     crypto_secretstream_xchacha20poly1305_rekey(state);
1408
-        # }
1409
-        if ($rekey || $st->needsRekey()) {
1410
-            // DO REKEY
1411
-            self::secretstream_xchacha20poly1305_rekey($state);
1412
-        }
1413
-        # if (outlen_p != NULL) {
1414
-        #     *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen;
1415
-        # }
1416
-        return $out;
1417
-    }
1418
-
1419
-    /**
1420
-     * @param string $state
1421
-     * @param string $cipher
1422
-     * @param string $aad
1423
-     * @return bool|array{0: string, 1: int}
1424
-     * @throws SodiumException
1425
-     */
1426
-    public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '')
1427
-    {
1428
-        $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
1429
-
1430
-        $cipherlen = ParagonIE_Sodium_Core32_Util::strlen($cipher);
1431
-        #     mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES;
1432
-        $msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES;
1433
-        $aadlen = ParagonIE_Sodium_Core32_Util::strlen($aad);
1434
-
1435
-        #     if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
1436
-        #         sodium_misuse();
1437
-        #     }
1438
-        if ((($msglen + 63) >> 6) > 0xfffffffe) {
1439
-            throw new SodiumException(
1440
-                'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
1441
-            );
1442
-        }
1443
-
1444
-        #     crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
1445
-        #     crypto_onetimeauth_poly1305_init(&poly1305_state, block);
1446
-        #     sodium_memzero(block, sizeof block);
1447
-        $auth = new ParagonIE_Sodium_Core32_Poly1305_State(
1448
-            ParagonIE_Sodium_Core32_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
1449
-        );
1450
-
1451
-        #     crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
1452
-        $auth->update($aad);
1453
-
1454
-        #     crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
1455
-        #         (0x10 - adlen) & 0xf);
1456
-        $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
1457
-
1458
-
1459
-        #     memset(block, 0, sizeof block);
1460
-        #     block[0] = in[0];
1461
-        #     crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
1462
-        #                                        state->nonce, 1U, state->k);
1463
-        $block = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1464
-            $cipher[0] . str_repeat("\0", 63),
1465
-            $st->getCombinedNonce(),
1466
-            $st->getKey(),
1467
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
1468
-        );
1469
-        #     tag = block[0];
1470
-        #     block[0] = in[0];
1471
-        #     crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
1472
-        $tag = ParagonIE_Sodium_Core32_Util::chrToInt($block[0]);
1473
-        $block[0] = $cipher[0];
1474
-        $auth->update($block);
1475
-
1476
-
1477
-        #     c = in + (sizeof tag);
1478
-        #     crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
1479
-        $auth->update(ParagonIE_Sodium_Core32_Util::substr($cipher, 1, $msglen));
1480
-
1481
-        #     crypto_onetimeauth_poly1305_update
1482
-        #     (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
1483
-        $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
1484
-
1485
-        #     STORE64_LE(slen, (uint64_t) adlen);
1486
-        #     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1487
-        $slen = ParagonIE_Sodium_Core32_Util::store64_le($aadlen);
1488
-        $auth->update($slen);
1489
-
1490
-        #     STORE64_LE(slen, (sizeof block) + mlen);
1491
-        #     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1492
-        $slen = ParagonIE_Sodium_Core32_Util::store64_le(64 + $msglen);
1493
-        $auth->update($slen);
1494
-
1495
-        #     crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
1496
-        #     sodium_memzero(&poly1305_state, sizeof poly1305_state);
1497
-        $mac = $auth->finish();
1498
-
1499
-        #     stored_mac = c + mlen;
1500
-        #     if (sodium_memcmp(mac, stored_mac, sizeof mac) != 0) {
1501
-        #     sodium_memzero(mac, sizeof mac);
1502
-        #         return -1;
1503
-        #     }
1504
-
1505
-        $stored = ParagonIE_Sodium_Core32_Util::substr($cipher, $msglen + 1, 16);
1506
-        if (!ParagonIE_Sodium_Core32_Util::hashEquals($mac, $stored)) {
1507
-            return false;
1508
-        }
1509
-
1510
-        #     crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k);
1511
-        $out = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1512
-            ParagonIE_Sodium_Core32_Util::substr($cipher, 1, $msglen),
1513
-            $st->getCombinedNonce(),
1514
-            $st->getKey(),
1515
-            ParagonIE_Sodium_Core32_Util::store64_le(2)
1516
-        );
1517
-
1518
-        #     XOR_BUF(STATE_INONCE(state), mac,
1519
-        #         crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1520
-        $st->xorNonce($mac);
1521
-
1522
-        #     sodium_increment(STATE_COUNTER(state),
1523
-        #         crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
1524
-        $st->incrementCounter();
1525
-
1526
-        #     if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
1527
-        #         sodium_is_zero(STATE_COUNTER(state),
1528
-        #             crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
1529
-        #         crypto_secretstream_xchacha20poly1305_rekey(state);
1530
-        #     }
1531
-
1532
-        // Overwrite by reference:
1533
-        $state = $st->toString();
1534
-
1535
-        /** @var bool $rekey */
1536
-        $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
1537
-        if ($rekey || $st->needsRekey()) {
1538
-            // DO REKEY
1539
-            self::secretstream_xchacha20poly1305_rekey($state);
1540
-        }
1541
-        return array($out, $tag);
1542
-    }
1543
-
1544
-    /**
1545
-     * @param string $state
1546
-     * @return void
1547
-     * @throws SodiumException
1548
-     */
1549
-    public static function secretstream_xchacha20poly1305_rekey(&$state)
1550
-    {
1551
-        $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
1552
-        # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES +
1553
-        # crypto_secretstream_xchacha20poly1305_INONCEBYTES];
1554
-        # size_t        i;
1555
-        # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
1556
-        #     new_key_and_inonce[i] = state->k[i];
1557
-        # }
1558
-        $new_key_and_inonce = $st->getKey();
1559
-
1560
-        # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
1561
-        #     new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] =
1562
-        #         STATE_INONCE(state)[i];
1563
-        # }
1564
-        $new_key_and_inonce .= ParagonIE_Sodium_Core32_Util::substR($st->getNonce(), 0, 8);
1565
-
1566
-        # crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce,
1567
-        #                                 sizeof new_key_and_inonce,
1568
-        #                                 state->nonce, state->k);
1569
-
1570
-        $st->rekey(ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1571
-            $new_key_and_inonce,
1572
-            $st->getCombinedNonce(),
1573
-            $st->getKey(),
1574
-            ParagonIE_Sodium_Core32_Util::store64_le(0)
1575
-        ));
1576
-
1577
-        # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
1578
-        #     state->k[i] = new_key_and_inonce[i];
1579
-        # }
1580
-        # for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
1581
-        #     STATE_INONCE(state)[i] =
1582
-        #          new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i];
1583
-        # }
1584
-        # _crypto_secretstream_xchacha20poly1305_counter_reset(state);
1585
-        $st->counterReset();
1586
-
1587
-        $state = $st->toString();
1588
-    }
1589
-
1590
-    /**
1591
-     * Detached Ed25519 signature.
1592
-     *
1593
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1594
-     *
1595
-     * @param string $message
1596
-     * @param string $sk
1597
-     * @return string
1598
-     * @throws SodiumException
1599
-     * @throws TypeError
1600
-     */
1601
-    public static function sign_detached($message, $sk)
1602
-    {
1603
-        return ParagonIE_Sodium_Core32_Ed25519::sign_detached($message, $sk);
1604
-    }
1605
-
1606
-    /**
1607
-     * Attached Ed25519 signature. (Returns a signed message.)
1608
-     *
1609
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1610
-     *
1611
-     * @param string $message
1612
-     * @param string $sk
1613
-     * @return string
1614
-     * @throws SodiumException
1615
-     * @throws TypeError
1616
-     */
1617
-    public static function sign($message, $sk)
1618
-    {
1619
-        return ParagonIE_Sodium_Core32_Ed25519::sign($message, $sk);
1620
-    }
1621
-
1622
-    /**
1623
-     * Opens a signed message. If valid, returns the message.
1624
-     *
1625
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1626
-     *
1627
-     * @param string $signedMessage
1628
-     * @param string $pk
1629
-     * @return string
1630
-     * @throws SodiumException
1631
-     * @throws TypeError
1632
-     */
1633
-    public static function sign_open($signedMessage, $pk)
1634
-    {
1635
-        return ParagonIE_Sodium_Core32_Ed25519::sign_open($signedMessage, $pk);
1636
-    }
1637
-
1638
-    /**
1639
-     * Verify a detached signature of a given message and public key.
1640
-     *
1641
-     * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1642
-     *
1643
-     * @param string $signature
1644
-     * @param string $message
1645
-     * @param string $pk
1646
-     * @return bool
1647
-     * @throws SodiumException
1648
-     * @throws TypeError
1649
-     */
1650
-    public static function sign_verify_detached($signature, $message, $pk)
1651
-    {
1652
-        return ParagonIE_Sodium_Core32_Ed25519::verify_detached($signature, $message, $pk);
1653
-    }
17
+	const aead_chacha20poly1305_KEYBYTES = 32;
18
+	const aead_chacha20poly1305_NSECBYTES = 0;
19
+	const aead_chacha20poly1305_NPUBBYTES = 8;
20
+	const aead_chacha20poly1305_ABYTES = 16;
21
+
22
+	const aead_chacha20poly1305_IETF_KEYBYTES = 32;
23
+	const aead_chacha20poly1305_IETF_NSECBYTES = 0;
24
+	const aead_chacha20poly1305_IETF_NPUBBYTES = 12;
25
+	const aead_chacha20poly1305_IETF_ABYTES = 16;
26
+
27
+	const aead_xchacha20poly1305_IETF_KEYBYTES = 32;
28
+	const aead_xchacha20poly1305_IETF_NSECBYTES = 0;
29
+	const aead_xchacha20poly1305_IETF_NPUBBYTES = 24;
30
+	const aead_xchacha20poly1305_IETF_ABYTES = 16;
31
+
32
+	const box_curve25519xsalsa20poly1305_SEEDBYTES = 32;
33
+	const box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32;
34
+	const box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32;
35
+	const box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32;
36
+	const box_curve25519xsalsa20poly1305_NONCEBYTES = 24;
37
+	const box_curve25519xsalsa20poly1305_MACBYTES = 16;
38
+	const box_curve25519xsalsa20poly1305_BOXZEROBYTES = 16;
39
+	const box_curve25519xsalsa20poly1305_ZEROBYTES = 32;
40
+
41
+	const onetimeauth_poly1305_BYTES = 16;
42
+	const onetimeauth_poly1305_KEYBYTES = 32;
43
+
44
+	const secretbox_xsalsa20poly1305_KEYBYTES = 32;
45
+	const secretbox_xsalsa20poly1305_NONCEBYTES = 24;
46
+	const secretbox_xsalsa20poly1305_MACBYTES = 16;
47
+	const secretbox_xsalsa20poly1305_BOXZEROBYTES = 16;
48
+	const secretbox_xsalsa20poly1305_ZEROBYTES = 32;
49
+
50
+	const secretbox_xchacha20poly1305_KEYBYTES = 32;
51
+	const secretbox_xchacha20poly1305_NONCEBYTES = 24;
52
+	const secretbox_xchacha20poly1305_MACBYTES = 16;
53
+	const secretbox_xchacha20poly1305_BOXZEROBYTES = 16;
54
+	const secretbox_xchacha20poly1305_ZEROBYTES = 32;
55
+
56
+	const stream_salsa20_KEYBYTES = 32;
57
+
58
+	/**
59
+	 * AEAD Decryption with ChaCha20-Poly1305
60
+	 *
61
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
62
+	 *
63
+	 * @param string $message
64
+	 * @param string $ad
65
+	 * @param string $nonce
66
+	 * @param string $key
67
+	 * @return string
68
+	 * @throws SodiumException
69
+	 * @throws TypeError
70
+	 */
71
+	public static function aead_chacha20poly1305_decrypt(
72
+		$message = '',
73
+		$ad = '',
74
+		$nonce = '',
75
+		$key = ''
76
+	) {
77
+		/** @var int $len - Length of message (ciphertext + MAC) */
78
+		$len = ParagonIE_Sodium_Core32_Util::strlen($message);
79
+
80
+		/** @var int  $clen - Length of ciphertext */
81
+		$clen = $len - self::aead_chacha20poly1305_ABYTES;
82
+
83
+		/** @var int $adlen - Length of associated data */
84
+		$adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
85
+
86
+		/** @var string $mac - Message authentication code */
87
+		$mac = ParagonIE_Sodium_Core32_Util::substr(
88
+			$message,
89
+			$clen,
90
+			self::aead_chacha20poly1305_ABYTES
91
+		);
92
+
93
+		/** @var string $ciphertext - The encrypted message (sans MAC) */
94
+		$ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 0, $clen);
95
+
96
+		/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
97
+		$block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
98
+			32,
99
+			$nonce,
100
+			$key
101
+		);
102
+
103
+		/* Recalculate the Poly1305 authentication tag (MAC): */
104
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
105
+		try {
106
+			ParagonIE_Sodium_Compat::memzero($block0);
107
+		} catch (SodiumException $ex) {
108
+			$block0 = null;
109
+		}
110
+		$state->update($ad);
111
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
112
+		$state->update($ciphertext);
113
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
114
+		$computed_mac = $state->finish();
115
+
116
+		/* Compare the given MAC with the recalculated MAC: */
117
+		if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
118
+			throw new SodiumException('Invalid MAC');
119
+		}
120
+
121
+		// Here, we know that the MAC is valid, so we decrypt and return the plaintext
122
+		return ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
123
+			$ciphertext,
124
+			$nonce,
125
+			$key,
126
+			ParagonIE_Sodium_Core32_Util::store64_le(1)
127
+		);
128
+	}
129
+
130
+	/**
131
+	 * AEAD Encryption with ChaCha20-Poly1305
132
+	 *
133
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
134
+	 *
135
+	 * @param string $message
136
+	 * @param string $ad
137
+	 * @param string $nonce
138
+	 * @param string $key
139
+	 * @return string
140
+	 * @throws SodiumException
141
+	 * @throws TypeError
142
+	 */
143
+	public static function aead_chacha20poly1305_encrypt(
144
+		$message = '',
145
+		$ad = '',
146
+		$nonce = '',
147
+		$key = ''
148
+	) {
149
+		/** @var int $len - Length of the plaintext message */
150
+		$len = ParagonIE_Sodium_Core32_Util::strlen($message);
151
+
152
+		/** @var int $adlen - Length of the associated data */
153
+		$adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
154
+
155
+		/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
156
+		$block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
157
+			32,
158
+			$nonce,
159
+			$key
160
+		);
161
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
162
+		try {
163
+			ParagonIE_Sodium_Compat::memzero($block0);
164
+		} catch (SodiumException $ex) {
165
+			$block0 = null;
166
+		}
167
+
168
+		/** @var string $ciphertext - Raw encrypted data */
169
+		$ciphertext = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
170
+			$message,
171
+			$nonce,
172
+			$key,
173
+			ParagonIE_Sodium_Core32_Util::store64_le(1)
174
+		);
175
+
176
+		$state->update($ad);
177
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
178
+		$state->update($ciphertext);
179
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
180
+		return $ciphertext . $state->finish();
181
+	}
182
+
183
+	/**
184
+	 * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
185
+	 *
186
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
187
+	 *
188
+	 * @param string $message
189
+	 * @param string $ad
190
+	 * @param string $nonce
191
+	 * @param string $key
192
+	 * @return string
193
+	 * @throws SodiumException
194
+	 * @throws TypeError
195
+	 */
196
+	public static function aead_chacha20poly1305_ietf_decrypt(
197
+		$message = '',
198
+		$ad = '',
199
+		$nonce = '',
200
+		$key = ''
201
+	) {
202
+		/** @var int $adlen - Length of associated data */
203
+		$adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
204
+
205
+		/** @var int $len - Length of message (ciphertext + MAC) */
206
+		$len = ParagonIE_Sodium_Core32_Util::strlen($message);
207
+
208
+		/** @var int  $clen - Length of ciphertext */
209
+		$clen = $len - self::aead_chacha20poly1305_IETF_ABYTES;
210
+
211
+		/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
212
+		$block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream(
213
+			32,
214
+			$nonce,
215
+			$key
216
+		);
217
+
218
+		/** @var string $mac - Message authentication code */
219
+		$mac = ParagonIE_Sodium_Core32_Util::substr(
220
+			$message,
221
+			$len - self::aead_chacha20poly1305_IETF_ABYTES,
222
+			self::aead_chacha20poly1305_IETF_ABYTES
223
+		);
224
+
225
+		/** @var string $ciphertext - The encrypted message (sans MAC) */
226
+		$ciphertext = ParagonIE_Sodium_Core32_Util::substr(
227
+			$message,
228
+			0,
229
+			$len - self::aead_chacha20poly1305_IETF_ABYTES
230
+		);
231
+
232
+		/* Recalculate the Poly1305 authentication tag (MAC): */
233
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
234
+		try {
235
+			ParagonIE_Sodium_Compat::memzero($block0);
236
+		} catch (SodiumException $ex) {
237
+			$block0 = null;
238
+		}
239
+		$state->update($ad);
240
+		$state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
241
+		$state->update($ciphertext);
242
+		$state->update(str_repeat("\x00", (0x10 - $clen) & 0xf));
243
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
244
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
245
+		$computed_mac = $state->finish();
246
+
247
+		/* Compare the given MAC with the recalculated MAC: */
248
+		if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
249
+			throw new SodiumException('Invalid MAC');
250
+		}
251
+
252
+		// Here, we know that the MAC is valid, so we decrypt and return the plaintext
253
+		return ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
254
+			$ciphertext,
255
+			$nonce,
256
+			$key,
257
+			ParagonIE_Sodium_Core32_Util::store64_le(1)
258
+		);
259
+	}
260
+
261
+	/**
262
+	 * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
263
+	 *
264
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
265
+	 *
266
+	 * @param string $message
267
+	 * @param string $ad
268
+	 * @param string $nonce
269
+	 * @param string $key
270
+	 * @return string
271
+	 * @throws SodiumException
272
+	 * @throws TypeError
273
+	 */
274
+	public static function aead_chacha20poly1305_ietf_encrypt(
275
+		$message = '',
276
+		$ad = '',
277
+		$nonce = '',
278
+		$key = ''
279
+	) {
280
+		/** @var int $len - Length of the plaintext message */
281
+		$len = ParagonIE_Sodium_Core32_Util::strlen($message);
282
+
283
+		/** @var int $adlen - Length of the associated data */
284
+		$adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
285
+
286
+		/** @var string The first block of the chacha20 keystream, used as a poly1305 key */
287
+		$block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream(
288
+			32,
289
+			$nonce,
290
+			$key
291
+		);
292
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
293
+		try {
294
+			ParagonIE_Sodium_Compat::memzero($block0);
295
+		} catch (SodiumException $ex) {
296
+			$block0 = null;
297
+		}
298
+
299
+		/** @var string $ciphertext - Raw encrypted data */
300
+		$ciphertext = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
301
+			$message,
302
+			$nonce,
303
+			$key,
304
+			ParagonIE_Sodium_Core32_Util::store64_le(1)
305
+		);
306
+
307
+		$state->update($ad);
308
+		$state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
309
+		$state->update($ciphertext);
310
+		$state->update(str_repeat("\x00", ((0x10 - $len) & 0xf)));
311
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
312
+		$state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
313
+		return $ciphertext . $state->finish();
314
+	}
315
+
316
+	/**
317
+	 * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
318
+	 *
319
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
320
+	 *
321
+	 * @param string $message
322
+	 * @param string $ad
323
+	 * @param string $nonce
324
+	 * @param string $key
325
+	 * @return string
326
+	 * @throws SodiumException
327
+	 * @throws TypeError
328
+	 */
329
+	public static function aead_xchacha20poly1305_ietf_decrypt(
330
+		$message = '',
331
+		$ad = '',
332
+		$nonce = '',
333
+		$key = ''
334
+	) {
335
+		$subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
336
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
337
+			$key
338
+		);
339
+		$nonceLast = "\x00\x00\x00\x00" .
340
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
341
+
342
+		return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey);
343
+	}
344
+
345
+	/**
346
+	 * AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
347
+	 *
348
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
349
+	 *
350
+	 * @param string $message
351
+	 * @param string $ad
352
+	 * @param string $nonce
353
+	 * @param string $key
354
+	 * @return string
355
+	 * @throws SodiumException
356
+	 * @throws TypeError
357
+	 */
358
+	public static function aead_xchacha20poly1305_ietf_encrypt(
359
+		$message = '',
360
+		$ad = '',
361
+		$nonce = '',
362
+		$key = ''
363
+	) {
364
+		$subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
365
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
366
+			$key
367
+		);
368
+		$nonceLast = "\x00\x00\x00\x00" .
369
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
370
+
371
+		return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey);
372
+	}
373
+
374
+	/**
375
+	 * HMAC-SHA-512-256 (a.k.a. the leftmost 256 bits of HMAC-SHA-512)
376
+	 *
377
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
378
+	 *
379
+	 * @param string $message
380
+	 * @param string $key
381
+	 * @return string
382
+	 * @throws TypeError
383
+	 */
384
+	public static function auth($message, $key)
385
+	{
386
+		return ParagonIE_Sodium_Core32_Util::substr(
387
+			hash_hmac('sha512', $message, $key, true),
388
+			0,
389
+			32
390
+		);
391
+	}
392
+
393
+	/**
394
+	 * HMAC-SHA-512-256 validation. Constant-time via hash_equals().
395
+	 *
396
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
397
+	 *
398
+	 * @param string $mac
399
+	 * @param string $message
400
+	 * @param string $key
401
+	 * @return bool
402
+	 * @throws SodiumException
403
+	 * @throws TypeError
404
+	 */
405
+	public static function auth_verify($mac, $message, $key)
406
+	{
407
+		return ParagonIE_Sodium_Core32_Util::hashEquals(
408
+			$mac,
409
+			self::auth($message, $key)
410
+		);
411
+	}
412
+
413
+	/**
414
+	 * X25519 key exchange followed by XSalsa20Poly1305 symmetric encryption
415
+	 *
416
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
417
+	 *
418
+	 * @param string $plaintext
419
+	 * @param string $nonce
420
+	 * @param string $keypair
421
+	 * @return string
422
+	 * @throws SodiumException
423
+	 * @throws TypeError
424
+	 */
425
+	public static function box($plaintext, $nonce, $keypair)
426
+	{
427
+		return self::secretbox(
428
+			$plaintext,
429
+			$nonce,
430
+			self::box_beforenm(
431
+				self::box_secretkey($keypair),
432
+				self::box_publickey($keypair)
433
+			)
434
+		);
435
+	}
436
+
437
+	/**
438
+	 * X25519-XSalsa20-Poly1305 with one ephemeral X25519 keypair.
439
+	 *
440
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
441
+	 *
442
+	 * @param string $message
443
+	 * @param string $publicKey
444
+	 * @return string
445
+	 * @throws SodiumException
446
+	 * @throws TypeError
447
+	 */
448
+	public static function box_seal($message, $publicKey)
449
+	{
450
+		/** @var string $ephemeralKeypair */
451
+		$ephemeralKeypair = self::box_keypair();
452
+
453
+		/** @var string $ephemeralSK */
454
+		$ephemeralSK = self::box_secretkey($ephemeralKeypair);
455
+
456
+		/** @var string $ephemeralPK */
457
+		$ephemeralPK = self::box_publickey($ephemeralKeypair);
458
+
459
+		/** @var string $nonce */
460
+		$nonce = self::generichash(
461
+			$ephemeralPK . $publicKey,
462
+			'',
463
+			24
464
+		);
465
+
466
+		/** @var string $keypair - The combined keypair used in crypto_box() */
467
+		$keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey);
468
+
469
+		/** @var string $ciphertext Ciphertext + MAC from crypto_box */
470
+		$ciphertext = self::box($message, $nonce, $keypair);
471
+		try {
472
+			ParagonIE_Sodium_Compat::memzero($ephemeralKeypair);
473
+			ParagonIE_Sodium_Compat::memzero($ephemeralSK);
474
+			ParagonIE_Sodium_Compat::memzero($nonce);
475
+		} catch (SodiumException $ex) {
476
+			$ephemeralKeypair = null;
477
+			$ephemeralSK = null;
478
+			$nonce = null;
479
+		}
480
+		return $ephemeralPK . $ciphertext;
481
+	}
482
+
483
+	/**
484
+	 * Opens a message encrypted via box_seal().
485
+	 *
486
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
487
+	 *
488
+	 * @param string $message
489
+	 * @param string $keypair
490
+	 * @return string
491
+	 * @throws SodiumException
492
+	 * @throws TypeError
493
+	 */
494
+	public static function box_seal_open($message, $keypair)
495
+	{
496
+		/** @var string $ephemeralPK */
497
+		$ephemeralPK = ParagonIE_Sodium_Core32_Util::substr($message, 0, 32);
498
+
499
+		/** @var string $ciphertext (ciphertext + MAC) */
500
+		$ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 32);
501
+
502
+		/** @var string $secretKey */
503
+		$secretKey = self::box_secretkey($keypair);
504
+
505
+		/** @var string $publicKey */
506
+		$publicKey = self::box_publickey($keypair);
507
+
508
+		/** @var string $nonce */
509
+		$nonce = self::generichash(
510
+			$ephemeralPK . $publicKey,
511
+			'',
512
+			24
513
+		);
514
+
515
+		/** @var string $keypair */
516
+		$keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK);
517
+
518
+		/** @var string $m */
519
+		$m = self::box_open($ciphertext, $nonce, $keypair);
520
+		try {
521
+			ParagonIE_Sodium_Compat::memzero($secretKey);
522
+			ParagonIE_Sodium_Compat::memzero($ephemeralPK);
523
+			ParagonIE_Sodium_Compat::memzero($nonce);
524
+		} catch (SodiumException $ex) {
525
+			$secretKey = null;
526
+			$ephemeralPK = null;
527
+			$nonce = null;
528
+		}
529
+		return $m;
530
+	}
531
+
532
+	/**
533
+	 * Used by crypto_box() to get the crypto_secretbox() key.
534
+	 *
535
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
536
+	 *
537
+	 * @param string $sk
538
+	 * @param string $pk
539
+	 * @return string
540
+	 * @throws SodiumException
541
+	 * @throws TypeError
542
+	 */
543
+	public static function box_beforenm($sk, $pk)
544
+	{
545
+		return ParagonIE_Sodium_Core32_HSalsa20::hsalsa20(
546
+			str_repeat("\x00", 16),
547
+			self::scalarmult($sk, $pk)
548
+		);
549
+	}
550
+
551
+	/**
552
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
553
+	 *
554
+	 * @return string
555
+	 * @throws Exception
556
+	 * @throws SodiumException
557
+	 * @throws TypeError
558
+	 */
559
+	public static function box_keypair()
560
+	{
561
+		$sKey = random_bytes(32);
562
+		$pKey = self::scalarmult_base($sKey);
563
+		return $sKey . $pKey;
564
+	}
565
+
566
+	/**
567
+	 * @param string $seed
568
+	 * @return string
569
+	 * @throws SodiumException
570
+	 * @throws TypeError
571
+	 */
572
+	public static function box_seed_keypair($seed)
573
+	{
574
+		$sKey = ParagonIE_Sodium_Core32_Util::substr(
575
+			hash('sha512', $seed, true),
576
+			0,
577
+			32
578
+		);
579
+		$pKey = self::scalarmult_base($sKey);
580
+		return $sKey . $pKey;
581
+	}
582
+
583
+	/**
584
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
585
+	 *
586
+	 * @param string $sKey
587
+	 * @param string $pKey
588
+	 * @return string
589
+	 * @throws TypeError
590
+	 */
591
+	public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey)
592
+	{
593
+		return ParagonIE_Sodium_Core32_Util::substr($sKey, 0, 32) .
594
+			ParagonIE_Sodium_Core32_Util::substr($pKey, 0, 32);
595
+	}
596
+
597
+	/**
598
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
599
+	 *
600
+	 * @param string $keypair
601
+	 * @return string
602
+	 * @throws RangeException
603
+	 * @throws TypeError
604
+	 */
605
+	public static function box_secretkey($keypair)
606
+	{
607
+		if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== 64) {
608
+			throw new RangeException(
609
+				'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
610
+			);
611
+		}
612
+		return ParagonIE_Sodium_Core32_Util::substr($keypair, 0, 32);
613
+	}
614
+
615
+	/**
616
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
617
+	 *
618
+	 * @param string $keypair
619
+	 * @return string
620
+	 * @throws RangeException
621
+	 * @throws TypeError
622
+	 */
623
+	public static function box_publickey($keypair)
624
+	{
625
+		if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
626
+			throw new RangeException(
627
+				'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
628
+			);
629
+		}
630
+		return ParagonIE_Sodium_Core32_Util::substr($keypair, 32, 32);
631
+	}
632
+
633
+	/**
634
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
635
+	 *
636
+	 * @param string $sKey
637
+	 * @return string
638
+	 * @throws RangeException
639
+	 * @throws SodiumException
640
+	 * @throws TypeError
641
+	 */
642
+	public static function box_publickey_from_secretkey($sKey)
643
+	{
644
+		if (ParagonIE_Sodium_Core32_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) {
645
+			throw new RangeException(
646
+				'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.'
647
+			);
648
+		}
649
+		return self::scalarmult_base($sKey);
650
+	}
651
+
652
+	/**
653
+	 * Decrypt a message encrypted with box().
654
+	 *
655
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
656
+	 *
657
+	 * @param string $ciphertext
658
+	 * @param string $nonce
659
+	 * @param string $keypair
660
+	 * @return string
661
+	 * @throws SodiumException
662
+	 * @throws TypeError
663
+	 */
664
+	public static function box_open($ciphertext, $nonce, $keypair)
665
+	{
666
+		return self::secretbox_open(
667
+			$ciphertext,
668
+			$nonce,
669
+			self::box_beforenm(
670
+				self::box_secretkey($keypair),
671
+				self::box_publickey($keypair)
672
+			)
673
+		);
674
+	}
675
+
676
+	/**
677
+	 * Calculate a BLAKE2b hash.
678
+	 *
679
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
680
+	 *
681
+	 * @param string $message
682
+	 * @param string|null $key
683
+	 * @param int $outlen
684
+	 * @return string
685
+	 * @throws RangeException
686
+	 * @throws SodiumException
687
+	 * @throws TypeError
688
+	 */
689
+	public static function generichash($message, $key = '', $outlen = 32)
690
+	{
691
+		// This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
692
+		ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
693
+
694
+		$k = null;
695
+		if (!empty($key)) {
696
+			/** @var SplFixedArray $k */
697
+			$k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
698
+			if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
699
+				throw new RangeException('Invalid key size');
700
+			}
701
+		}
702
+
703
+		/** @var SplFixedArray $in */
704
+		$in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message);
705
+
706
+		/** @var SplFixedArray $ctx */
707
+		$ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outlen);
708
+		ParagonIE_Sodium_Core32_BLAKE2b::update($ctx, $in, $in->count());
709
+
710
+		/** @var SplFixedArray $out */
711
+		$out = new SplFixedArray($outlen);
712
+		$out = ParagonIE_Sodium_Core32_BLAKE2b::finish($ctx, $out);
713
+
714
+		/** @var array<int, int> */
715
+		$outArray = $out->toArray();
716
+		return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray);
717
+	}
718
+
719
+	/**
720
+	 * Finalize a BLAKE2b hashing context, returning the hash.
721
+	 *
722
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
723
+	 *
724
+	 * @param string $ctx
725
+	 * @param int $outlen
726
+	 * @return string
727
+	 * @throws SodiumException
728
+	 * @throws TypeError
729
+	 */
730
+	public static function generichash_final($ctx, $outlen = 32)
731
+	{
732
+		if (!is_string($ctx)) {
733
+			throw new TypeError('Context must be a string');
734
+		}
735
+		$out = new SplFixedArray($outlen);
736
+
737
+		/** @var SplFixedArray $context */
738
+		$context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx);
739
+
740
+		/** @var SplFixedArray $out */
741
+		$out = ParagonIE_Sodium_Core32_BLAKE2b::finish($context, $out);
742
+
743
+		/** @var array<int, int> */
744
+		$outArray = $out->toArray();
745
+		return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray);
746
+	}
747
+
748
+	/**
749
+	 * Initialize a hashing context for BLAKE2b.
750
+	 *
751
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
752
+	 *
753
+	 * @param string $key
754
+	 * @param int $outputLength
755
+	 * @return string
756
+	 * @throws RangeException
757
+	 * @throws SodiumException
758
+	 * @throws TypeError
759
+	 */
760
+	public static function generichash_init($key = '', $outputLength = 32)
761
+	{
762
+		// This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
763
+		ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
764
+
765
+		$k = null;
766
+		if (!empty($key)) {
767
+			$k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
768
+			if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
769
+				throw new RangeException('Invalid key size');
770
+			}
771
+		}
772
+
773
+		/** @var SplFixedArray $ctx */
774
+		$ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outputLength);
775
+
776
+		return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($ctx);
777
+	}
778
+
779
+	/**
780
+	 * Initialize a hashing context for BLAKE2b.
781
+	 *
782
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
783
+	 *
784
+	 * @param string $key
785
+	 * @param int $outputLength
786
+	 * @param string $salt
787
+	 * @param string $personal
788
+	 * @return string
789
+	 * @throws RangeException
790
+	 * @throws SodiumException
791
+	 * @throws TypeError
792
+	 */
793
+	public static function generichash_init_salt_personal(
794
+		$key = '',
795
+		$outputLength = 32,
796
+		$salt = '',
797
+		$personal = ''
798
+	) {
799
+		// This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
800
+		ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
801
+
802
+		$k = null;
803
+		if (!empty($key)) {
804
+			$k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
805
+			if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
806
+				throw new RangeException('Invalid key size');
807
+			}
808
+		}
809
+		if (!empty($salt)) {
810
+			$s = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($salt);
811
+		} else {
812
+			$s = null;
813
+		}
814
+		if (!empty($salt)) {
815
+			$p = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($personal);
816
+		} else {
817
+			$p = null;
818
+		}
819
+
820
+		/** @var SplFixedArray $ctx */
821
+		$ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outputLength, $s, $p);
822
+
823
+		return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($ctx);
824
+	}
825
+
826
+	/**
827
+	 * Update a hashing context for BLAKE2b with $message
828
+	 *
829
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
830
+	 *
831
+	 * @param string $ctx
832
+	 * @param string $message
833
+	 * @return string
834
+	 * @throws SodiumException
835
+	 * @throws TypeError
836
+	 */
837
+	public static function generichash_update($ctx, $message)
838
+	{
839
+		// This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
840
+		ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
841
+
842
+		/** @var SplFixedArray $context */
843
+		$context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx);
844
+
845
+		/** @var SplFixedArray $in */
846
+		$in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message);
847
+
848
+		ParagonIE_Sodium_Core32_BLAKE2b::update($context, $in, $in->count());
849
+
850
+		return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($context);
851
+	}
852
+
853
+	/**
854
+	 * Libsodium's crypto_kx().
855
+	 *
856
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
857
+	 *
858
+	 * @param string $my_sk
859
+	 * @param string $their_pk
860
+	 * @param string $client_pk
861
+	 * @param string $server_pk
862
+	 * @return string
863
+	 * @throws SodiumException
864
+	 * @throws TypeError
865
+	 */
866
+	public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk)
867
+	{
868
+		return self::generichash(
869
+			self::scalarmult($my_sk, $their_pk) .
870
+			$client_pk .
871
+			$server_pk
872
+		);
873
+	}
874
+
875
+	/**
876
+	 * ECDH over Curve25519
877
+	 *
878
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
879
+	 *
880
+	 * @param string $sKey
881
+	 * @param string $pKey
882
+	 * @return string
883
+	 *
884
+	 * @throws SodiumException
885
+	 * @throws TypeError
886
+	 */
887
+	public static function scalarmult($sKey, $pKey)
888
+	{
889
+		$q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey);
890
+		self::scalarmult_throw_if_zero($q);
891
+		return $q;
892
+	}
893
+
894
+	/**
895
+	 * ECDH over Curve25519, using the basepoint.
896
+	 * Used to get a secret key from a public key.
897
+	 *
898
+	 * @param string $secret
899
+	 * @return string
900
+	 *
901
+	 * @throws SodiumException
902
+	 * @throws TypeError
903
+	 */
904
+	public static function scalarmult_base($secret)
905
+	{
906
+		$q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10_base($secret);
907
+		self::scalarmult_throw_if_zero($q);
908
+		return $q;
909
+	}
910
+
911
+	/**
912
+	 * This throws an Error if a zero public key was passed to the function.
913
+	 *
914
+	 * @param string $q
915
+	 * @return void
916
+	 * @throws SodiumException
917
+	 * @throws TypeError
918
+	 */
919
+	protected static function scalarmult_throw_if_zero($q)
920
+	{
921
+		$d = 0;
922
+		for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) {
923
+			$d |= ParagonIE_Sodium_Core32_Util::chrToInt($q[$i]);
924
+		}
925
+
926
+		/* branch-free variant of === 0 */
927
+		if (-(1 & (($d - 1) >> 8))) {
928
+			throw new SodiumException('Zero public key is not allowed');
929
+		}
930
+	}
931
+
932
+	/**
933
+	 * XSalsa20-Poly1305 authenticated symmetric-key encryption.
934
+	 *
935
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
936
+	 *
937
+	 * @param string $plaintext
938
+	 * @param string $nonce
939
+	 * @param string $key
940
+	 * @return string
941
+	 * @throws SodiumException
942
+	 * @throws TypeError
943
+	 */
944
+	public static function secretbox($plaintext, $nonce, $key)
945
+	{
946
+		/** @var string $subkey */
947
+		$subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
948
+
949
+		/** @var string $block0 */
950
+		$block0 = str_repeat("\x00", 32);
951
+
952
+		/** @var int $mlen - Length of the plaintext message */
953
+		$mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext);
954
+		$mlen0 = $mlen;
955
+		if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) {
956
+			$mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES;
957
+		}
958
+		$block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
959
+
960
+		/** @var string $block0 */
961
+		$block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor(
962
+			$block0,
963
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
964
+			$subkey
965
+		);
966
+
967
+		/** @var string $c */
968
+		$c = ParagonIE_Sodium_Core32_Util::substr(
969
+			$block0,
970
+			self::secretbox_xsalsa20poly1305_ZEROBYTES
971
+		);
972
+		if ($mlen > $mlen0) {
973
+			$c .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
974
+				ParagonIE_Sodium_Core32_Util::substr(
975
+					$plaintext,
976
+					self::secretbox_xsalsa20poly1305_ZEROBYTES
977
+				),
978
+				ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
979
+				1,
980
+				$subkey
981
+			);
982
+		}
983
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State(
984
+			ParagonIE_Sodium_Core32_Util::substr(
985
+				$block0,
986
+				0,
987
+				self::onetimeauth_poly1305_KEYBYTES
988
+			)
989
+		);
990
+		try {
991
+			ParagonIE_Sodium_Compat::memzero($block0);
992
+			ParagonIE_Sodium_Compat::memzero($subkey);
993
+		} catch (SodiumException $ex) {
994
+			$block0 = null;
995
+			$subkey = null;
996
+		}
997
+
998
+		$state->update($c);
999
+
1000
+		/** @var string $c - MAC || ciphertext */
1001
+		$c = $state->finish() . $c;
1002
+		unset($state);
1003
+
1004
+		return $c;
1005
+	}
1006
+
1007
+	/**
1008
+	 * Decrypt a ciphertext generated via secretbox().
1009
+	 *
1010
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1011
+	 *
1012
+	 * @param string $ciphertext
1013
+	 * @param string $nonce
1014
+	 * @param string $key
1015
+	 * @return string
1016
+	 * @throws SodiumException
1017
+	 * @throws TypeError
1018
+	 */
1019
+	public static function secretbox_open($ciphertext, $nonce, $key)
1020
+	{
1021
+		/** @var string $mac */
1022
+		$mac = ParagonIE_Sodium_Core32_Util::substr(
1023
+			$ciphertext,
1024
+			0,
1025
+			self::secretbox_xsalsa20poly1305_MACBYTES
1026
+		);
1027
+
1028
+		/** @var string $c */
1029
+		$c = ParagonIE_Sodium_Core32_Util::substr(
1030
+			$ciphertext,
1031
+			self::secretbox_xsalsa20poly1305_MACBYTES
1032
+		);
1033
+
1034
+		/** @var int $clen */
1035
+		$clen = ParagonIE_Sodium_Core32_Util::strlen($c);
1036
+
1037
+		/** @var string $subkey */
1038
+		$subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
1039
+
1040
+		/** @var string $block0 */
1041
+		$block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20(
1042
+			64,
1043
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1044
+			$subkey
1045
+		);
1046
+		$verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify(
1047
+			$mac,
1048
+			$c,
1049
+			ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32)
1050
+		);
1051
+		if (!$verified) {
1052
+			try {
1053
+				ParagonIE_Sodium_Compat::memzero($subkey);
1054
+			} catch (SodiumException $ex) {
1055
+				$subkey = null;
1056
+			}
1057
+			throw new SodiumException('Invalid MAC');
1058
+		}
1059
+
1060
+		/** @var string $m - Decrypted message */
1061
+		$m = ParagonIE_Sodium_Core32_Util::xorStrings(
1062
+			ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES),
1063
+			ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES)
1064
+		);
1065
+		if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) {
1066
+			// We had more than 1 block, so let's continue to decrypt the rest.
1067
+			$m .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
1068
+				ParagonIE_Sodium_Core32_Util::substr(
1069
+					$c,
1070
+					self::secretbox_xsalsa20poly1305_ZEROBYTES
1071
+				),
1072
+				ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1073
+				1,
1074
+				(string) $subkey
1075
+			);
1076
+		}
1077
+		return $m;
1078
+	}
1079
+
1080
+	/**
1081
+	 * XChaCha20-Poly1305 authenticated symmetric-key encryption.
1082
+	 *
1083
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1084
+	 *
1085
+	 * @param string $plaintext
1086
+	 * @param string $nonce
1087
+	 * @param string $key
1088
+	 * @return string
1089
+	 * @throws SodiumException
1090
+	 * @throws TypeError
1091
+	 */
1092
+	public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key)
1093
+	{
1094
+		/** @var string $subkey */
1095
+		$subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
1096
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
1097
+			$key
1098
+		);
1099
+		$nonceLast = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
1100
+
1101
+		/** @var string $block0 */
1102
+		$block0 = str_repeat("\x00", 32);
1103
+
1104
+		/** @var int $mlen - Length of the plaintext message */
1105
+		$mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext);
1106
+		$mlen0 = $mlen;
1107
+		if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) {
1108
+			$mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES;
1109
+		}
1110
+		$block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
1111
+
1112
+		/** @var string $block0 */
1113
+		$block0 = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1114
+			$block0,
1115
+			$nonceLast,
1116
+			$subkey
1117
+		);
1118
+
1119
+		/** @var string $c */
1120
+		$c = ParagonIE_Sodium_Core32_Util::substr(
1121
+			$block0,
1122
+			self::secretbox_xchacha20poly1305_ZEROBYTES
1123
+		);
1124
+		if ($mlen > $mlen0) {
1125
+			$c .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1126
+				ParagonIE_Sodium_Core32_Util::substr(
1127
+					$plaintext,
1128
+					self::secretbox_xchacha20poly1305_ZEROBYTES
1129
+				),
1130
+				$nonceLast,
1131
+				$subkey,
1132
+				ParagonIE_Sodium_Core32_Util::store64_le(1)
1133
+			);
1134
+		}
1135
+		$state = new ParagonIE_Sodium_Core32_Poly1305_State(
1136
+			ParagonIE_Sodium_Core32_Util::substr(
1137
+				$block0,
1138
+				0,
1139
+				self::onetimeauth_poly1305_KEYBYTES
1140
+			)
1141
+		);
1142
+		try {
1143
+			ParagonIE_Sodium_Compat::memzero($block0);
1144
+			ParagonIE_Sodium_Compat::memzero($subkey);
1145
+		} catch (SodiumException $ex) {
1146
+			$block0 = null;
1147
+			$subkey = null;
1148
+		}
1149
+
1150
+		$state->update($c);
1151
+
1152
+		/** @var string $c - MAC || ciphertext */
1153
+		$c = $state->finish() . $c;
1154
+		unset($state);
1155
+
1156
+		return $c;
1157
+	}
1158
+
1159
+	/**
1160
+	 * Decrypt a ciphertext generated via secretbox_xchacha20poly1305().
1161
+	 *
1162
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1163
+	 *
1164
+	 * @param string $ciphertext
1165
+	 * @param string $nonce
1166
+	 * @param string $key
1167
+	 * @return string
1168
+	 * @throws SodiumException
1169
+	 * @throws TypeError
1170
+	 */
1171
+	public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
1172
+	{
1173
+		/** @var string $mac */
1174
+		$mac = ParagonIE_Sodium_Core32_Util::substr(
1175
+			$ciphertext,
1176
+			0,
1177
+			self::secretbox_xchacha20poly1305_MACBYTES
1178
+		);
1179
+
1180
+		/** @var string $c */
1181
+		$c = ParagonIE_Sodium_Core32_Util::substr(
1182
+			$ciphertext,
1183
+			self::secretbox_xchacha20poly1305_MACBYTES
1184
+		);
1185
+
1186
+		/** @var int $clen */
1187
+		$clen = ParagonIE_Sodium_Core32_Util::strlen($c);
1188
+
1189
+		/** @var string $subkey */
1190
+		$subkey = ParagonIE_Sodium_Core32_HChaCha20::hchacha20($nonce, $key);
1191
+
1192
+		/** @var string $block0 */
1193
+		$block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
1194
+			64,
1195
+			ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1196
+			$subkey
1197
+		);
1198
+		$verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify(
1199
+			$mac,
1200
+			$c,
1201
+			ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32)
1202
+		);
1203
+
1204
+		if (!$verified) {
1205
+			try {
1206
+				ParagonIE_Sodium_Compat::memzero($subkey);
1207
+			} catch (SodiumException $ex) {
1208
+				$subkey = null;
1209
+			}
1210
+			throw new SodiumException('Invalid MAC');
1211
+		}
1212
+
1213
+		/** @var string $m - Decrypted message */
1214
+		$m = ParagonIE_Sodium_Core32_Util::xorStrings(
1215
+			ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES),
1216
+			ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES)
1217
+		);
1218
+
1219
+		if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) {
1220
+			// We had more than 1 block, so let's continue to decrypt the rest.
1221
+			$m .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1222
+				ParagonIE_Sodium_Core32_Util::substr(
1223
+					$c,
1224
+					self::secretbox_xchacha20poly1305_ZEROBYTES
1225
+				),
1226
+				ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1227
+				(string) $subkey,
1228
+				ParagonIE_Sodium_Core32_Util::store64_le(1)
1229
+			);
1230
+		}
1231
+		return $m;
1232
+	}
1233
+
1234
+	/**
1235
+	 * @param string $key
1236
+	 * @return array<int, string> Returns a state and a header.
1237
+	 * @throws Exception
1238
+	 * @throws SodiumException
1239
+	 */
1240
+	public static function secretstream_xchacha20poly1305_init_push($key)
1241
+	{
1242
+		# randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES);
1243
+		$out = random_bytes(24);
1244
+
1245
+		# crypto_core_hchacha20(state->k, out, k, NULL);
1246
+		$subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20($out, $key);
1247
+		$state = new ParagonIE_Sodium_Core32_SecretStream_State(
1248
+			$subkey,
1249
+			ParagonIE_Sodium_Core32_Util::substr($out, 16, 8) . str_repeat("\0", 4)
1250
+		);
1251
+
1252
+		# _crypto_secretstream_xchacha20poly1305_counter_reset(state);
1253
+		$state->counterReset();
1254
+
1255
+		# memcpy(STATE_INONCE(state), out + crypto_core_hchacha20_INPUTBYTES,
1256
+		#        crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1257
+		# memset(state->_pad, 0, sizeof state->_pad);
1258
+		return array(
1259
+			$state->toString(),
1260
+			$out
1261
+		);
1262
+	}
1263
+
1264
+	/**
1265
+	 * @param string $key
1266
+	 * @param string $header
1267
+	 * @return string Returns a state.
1268
+	 * @throws Exception
1269
+	 */
1270
+	public static function secretstream_xchacha20poly1305_init_pull($key, $header)
1271
+	{
1272
+		# crypto_core_hchacha20(state->k, in, k, NULL);
1273
+		$subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
1274
+			ParagonIE_Sodium_Core32_Util::substr($header, 0, 16),
1275
+			$key
1276
+		);
1277
+		$state = new ParagonIE_Sodium_Core32_SecretStream_State(
1278
+			$subkey,
1279
+			ParagonIE_Sodium_Core32_Util::substr($header, 16)
1280
+		);
1281
+		$state->counterReset();
1282
+		# memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES,
1283
+		#     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1284
+		# memset(state->_pad, 0, sizeof state->_pad);
1285
+		# return 0;
1286
+		return $state->toString();
1287
+	}
1288
+
1289
+	/**
1290
+	 * @param string $state
1291
+	 * @param string $msg
1292
+	 * @param string $aad
1293
+	 * @param int $tag
1294
+	 * @return string
1295
+	 * @throws SodiumException
1296
+	 */
1297
+	public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
1298
+	{
1299
+		$st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
1300
+		# crypto_onetimeauth_poly1305_state poly1305_state;
1301
+		# unsigned char                     block[64U];
1302
+		# unsigned char                     slen[8U];
1303
+		# unsigned char                    *c;
1304
+		# unsigned char                    *mac;
1305
+
1306
+		$msglen = ParagonIE_Sodium_Core32_Util::strlen($msg);
1307
+		$aadlen = ParagonIE_Sodium_Core32_Util::strlen($aad);
1308
+
1309
+		if ((($msglen + 63) >> 6) > 0xfffffffe) {
1310
+			throw new SodiumException(
1311
+				'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
1312
+			);
1313
+		}
1314
+
1315
+		# if (outlen_p != NULL) {
1316
+		#     *outlen_p = 0U;
1317
+		# }
1318
+		# if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
1319
+		#     sodium_misuse();
1320
+		# }
1321
+
1322
+		# crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
1323
+		# crypto_onetimeauth_poly1305_init(&poly1305_state, block);
1324
+		# sodium_memzero(block, sizeof block);
1325
+		$auth = new ParagonIE_Sodium_Core32_Poly1305_State(
1326
+			ParagonIE_Sodium_Core32_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
1327
+		);
1328
+
1329
+		# crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
1330
+		$auth->update($aad);
1331
+
1332
+		# crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
1333
+		#     (0x10 - adlen) & 0xf);
1334
+		$auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
1335
+
1336
+		# memset(block, 0, sizeof block);
1337
+		# block[0] = tag;
1338
+		# crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
1339
+		#                                    state->nonce, 1U, state->k);
1340
+		$block = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1341
+			ParagonIE_Sodium_Core32_Util::intToChr($tag) . str_repeat("\0", 63),
1342
+			$st->getCombinedNonce(),
1343
+			$st->getKey(),
1344
+			ParagonIE_Sodium_Core32_Util::store64_le(1)
1345
+		);
1346
+
1347
+		# crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
1348
+		$auth->update($block);
1349
+
1350
+		# out[0] = block[0];
1351
+		$out = $block[0];
1352
+		# c = out + (sizeof tag);
1353
+		# crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k);
1354
+		$cipher = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1355
+			$msg,
1356
+			$st->getCombinedNonce(),
1357
+			$st->getKey(),
1358
+			ParagonIE_Sodium_Core32_Util::store64_le(2)
1359
+		);
1360
+
1361
+		# crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
1362
+		$auth->update($cipher);
1363
+
1364
+		$out .= $cipher;
1365
+		unset($cipher);
1366
+
1367
+		# crypto_onetimeauth_poly1305_update
1368
+		# (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
1369
+		$auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
1370
+
1371
+		# STORE64_LE(slen, (uint64_t) adlen);
1372
+		$slen = ParagonIE_Sodium_Core32_Util::store64_le($aadlen);
1373
+
1374
+		# crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1375
+		$auth->update($slen);
1376
+
1377
+		# STORE64_LE(slen, (sizeof block) + mlen);
1378
+		$slen = ParagonIE_Sodium_Core32_Util::store64_le(64 + $msglen);
1379
+
1380
+		# crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1381
+		$auth->update($slen);
1382
+
1383
+		# mac = c + mlen;
1384
+		# crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
1385
+		$mac = $auth->finish();
1386
+		$out .= $mac;
1387
+
1388
+		# sodium_memzero(&poly1305_state, sizeof poly1305_state);
1389
+		unset($auth);
1390
+
1391
+
1392
+		# XOR_BUF(STATE_INONCE(state), mac,
1393
+		#     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1394
+		$st->xorNonce($mac);
1395
+
1396
+		# sodium_increment(STATE_COUNTER(state),
1397
+		#     crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
1398
+		$st->incrementCounter();
1399
+		// Overwrite by reference:
1400
+		$state = $st->toString();
1401
+
1402
+		/** @var bool $rekey */
1403
+		$rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
1404
+		# if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
1405
+		#     sodium_is_zero(STATE_COUNTER(state),
1406
+		#         crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
1407
+		#     crypto_secretstream_xchacha20poly1305_rekey(state);
1408
+		# }
1409
+		if ($rekey || $st->needsRekey()) {
1410
+			// DO REKEY
1411
+			self::secretstream_xchacha20poly1305_rekey($state);
1412
+		}
1413
+		# if (outlen_p != NULL) {
1414
+		#     *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen;
1415
+		# }
1416
+		return $out;
1417
+	}
1418
+
1419
+	/**
1420
+	 * @param string $state
1421
+	 * @param string $cipher
1422
+	 * @param string $aad
1423
+	 * @return bool|array{0: string, 1: int}
1424
+	 * @throws SodiumException
1425
+	 */
1426
+	public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '')
1427
+	{
1428
+		$st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
1429
+
1430
+		$cipherlen = ParagonIE_Sodium_Core32_Util::strlen($cipher);
1431
+		#     mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES;
1432
+		$msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES;
1433
+		$aadlen = ParagonIE_Sodium_Core32_Util::strlen($aad);
1434
+
1435
+		#     if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
1436
+		#         sodium_misuse();
1437
+		#     }
1438
+		if ((($msglen + 63) >> 6) > 0xfffffffe) {
1439
+			throw new SodiumException(
1440
+				'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
1441
+			);
1442
+		}
1443
+
1444
+		#     crypto_stream_chacha20_ietf(block, sizeof block, state->nonce, state->k);
1445
+		#     crypto_onetimeauth_poly1305_init(&poly1305_state, block);
1446
+		#     sodium_memzero(block, sizeof block);
1447
+		$auth = new ParagonIE_Sodium_Core32_Poly1305_State(
1448
+			ParagonIE_Sodium_Core32_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
1449
+		);
1450
+
1451
+		#     crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
1452
+		$auth->update($aad);
1453
+
1454
+		#     crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
1455
+		#         (0x10 - adlen) & 0xf);
1456
+		$auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
1457
+
1458
+
1459
+		#     memset(block, 0, sizeof block);
1460
+		#     block[0] = in[0];
1461
+		#     crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
1462
+		#                                        state->nonce, 1U, state->k);
1463
+		$block = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1464
+			$cipher[0] . str_repeat("\0", 63),
1465
+			$st->getCombinedNonce(),
1466
+			$st->getKey(),
1467
+			ParagonIE_Sodium_Core32_Util::store64_le(1)
1468
+		);
1469
+		#     tag = block[0];
1470
+		#     block[0] = in[0];
1471
+		#     crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
1472
+		$tag = ParagonIE_Sodium_Core32_Util::chrToInt($block[0]);
1473
+		$block[0] = $cipher[0];
1474
+		$auth->update($block);
1475
+
1476
+
1477
+		#     c = in + (sizeof tag);
1478
+		#     crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
1479
+		$auth->update(ParagonIE_Sodium_Core32_Util::substr($cipher, 1, $msglen));
1480
+
1481
+		#     crypto_onetimeauth_poly1305_update
1482
+		#     (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
1483
+		$auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
1484
+
1485
+		#     STORE64_LE(slen, (uint64_t) adlen);
1486
+		#     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1487
+		$slen = ParagonIE_Sodium_Core32_Util::store64_le($aadlen);
1488
+		$auth->update($slen);
1489
+
1490
+		#     STORE64_LE(slen, (sizeof block) + mlen);
1491
+		#     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1492
+		$slen = ParagonIE_Sodium_Core32_Util::store64_le(64 + $msglen);
1493
+		$auth->update($slen);
1494
+
1495
+		#     crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
1496
+		#     sodium_memzero(&poly1305_state, sizeof poly1305_state);
1497
+		$mac = $auth->finish();
1498
+
1499
+		#     stored_mac = c + mlen;
1500
+		#     if (sodium_memcmp(mac, stored_mac, sizeof mac) != 0) {
1501
+		#     sodium_memzero(mac, sizeof mac);
1502
+		#         return -1;
1503
+		#     }
1504
+
1505
+		$stored = ParagonIE_Sodium_Core32_Util::substr($cipher, $msglen + 1, 16);
1506
+		if (!ParagonIE_Sodium_Core32_Util::hashEquals($mac, $stored)) {
1507
+			return false;
1508
+		}
1509
+
1510
+		#     crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k);
1511
+		$out = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1512
+			ParagonIE_Sodium_Core32_Util::substr($cipher, 1, $msglen),
1513
+			$st->getCombinedNonce(),
1514
+			$st->getKey(),
1515
+			ParagonIE_Sodium_Core32_Util::store64_le(2)
1516
+		);
1517
+
1518
+		#     XOR_BUF(STATE_INONCE(state), mac,
1519
+		#         crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1520
+		$st->xorNonce($mac);
1521
+
1522
+		#     sodium_increment(STATE_COUNTER(state),
1523
+		#         crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
1524
+		$st->incrementCounter();
1525
+
1526
+		#     if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
1527
+		#         sodium_is_zero(STATE_COUNTER(state),
1528
+		#             crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
1529
+		#         crypto_secretstream_xchacha20poly1305_rekey(state);
1530
+		#     }
1531
+
1532
+		// Overwrite by reference:
1533
+		$state = $st->toString();
1534
+
1535
+		/** @var bool $rekey */
1536
+		$rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
1537
+		if ($rekey || $st->needsRekey()) {
1538
+			// DO REKEY
1539
+			self::secretstream_xchacha20poly1305_rekey($state);
1540
+		}
1541
+		return array($out, $tag);
1542
+	}
1543
+
1544
+	/**
1545
+	 * @param string $state
1546
+	 * @return void
1547
+	 * @throws SodiumException
1548
+	 */
1549
+	public static function secretstream_xchacha20poly1305_rekey(&$state)
1550
+	{
1551
+		$st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
1552
+		# unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES +
1553
+		# crypto_secretstream_xchacha20poly1305_INONCEBYTES];
1554
+		# size_t        i;
1555
+		# for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
1556
+		#     new_key_and_inonce[i] = state->k[i];
1557
+		# }
1558
+		$new_key_and_inonce = $st->getKey();
1559
+
1560
+		# for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
1561
+		#     new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] =
1562
+		#         STATE_INONCE(state)[i];
1563
+		# }
1564
+		$new_key_and_inonce .= ParagonIE_Sodium_Core32_Util::substR($st->getNonce(), 0, 8);
1565
+
1566
+		# crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce,
1567
+		#                                 sizeof new_key_and_inonce,
1568
+		#                                 state->nonce, state->k);
1569
+
1570
+		$st->rekey(ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1571
+			$new_key_and_inonce,
1572
+			$st->getCombinedNonce(),
1573
+			$st->getKey(),
1574
+			ParagonIE_Sodium_Core32_Util::store64_le(0)
1575
+		));
1576
+
1577
+		# for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
1578
+		#     state->k[i] = new_key_and_inonce[i];
1579
+		# }
1580
+		# for (i = 0U; i < crypto_secretstream_xchacha20poly1305_INONCEBYTES; i++) {
1581
+		#     STATE_INONCE(state)[i] =
1582
+		#          new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i];
1583
+		# }
1584
+		# _crypto_secretstream_xchacha20poly1305_counter_reset(state);
1585
+		$st->counterReset();
1586
+
1587
+		$state = $st->toString();
1588
+	}
1589
+
1590
+	/**
1591
+	 * Detached Ed25519 signature.
1592
+	 *
1593
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1594
+	 *
1595
+	 * @param string $message
1596
+	 * @param string $sk
1597
+	 * @return string
1598
+	 * @throws SodiumException
1599
+	 * @throws TypeError
1600
+	 */
1601
+	public static function sign_detached($message, $sk)
1602
+	{
1603
+		return ParagonIE_Sodium_Core32_Ed25519::sign_detached($message, $sk);
1604
+	}
1605
+
1606
+	/**
1607
+	 * Attached Ed25519 signature. (Returns a signed message.)
1608
+	 *
1609
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1610
+	 *
1611
+	 * @param string $message
1612
+	 * @param string $sk
1613
+	 * @return string
1614
+	 * @throws SodiumException
1615
+	 * @throws TypeError
1616
+	 */
1617
+	public static function sign($message, $sk)
1618
+	{
1619
+		return ParagonIE_Sodium_Core32_Ed25519::sign($message, $sk);
1620
+	}
1621
+
1622
+	/**
1623
+	 * Opens a signed message. If valid, returns the message.
1624
+	 *
1625
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1626
+	 *
1627
+	 * @param string $signedMessage
1628
+	 * @param string $pk
1629
+	 * @return string
1630
+	 * @throws SodiumException
1631
+	 * @throws TypeError
1632
+	 */
1633
+	public static function sign_open($signedMessage, $pk)
1634
+	{
1635
+		return ParagonIE_Sodium_Core32_Ed25519::sign_open($signedMessage, $pk);
1636
+	}
1637
+
1638
+	/**
1639
+	 * Verify a detached signature of a given message and public key.
1640
+	 *
1641
+	 * @internal Do not use this directly. Use ParagonIE_Sodium_Compat.
1642
+	 *
1643
+	 * @param string $signature
1644
+	 * @param string $message
1645
+	 * @param string $pk
1646
+	 * @return bool
1647
+	 * @throws SodiumException
1648
+	 * @throws TypeError
1649
+	 */
1650
+	public static function sign_verify_detached($signature, $message, $pk)
1651
+	{
1652
+		return ParagonIE_Sodium_Core32_Ed25519::verify_detached($signature, $message, $pk);
1653
+	}
1654 1654
 }
Please login to merge, or discard this patch.
Spacing   +290 added lines, -290 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-if (class_exists('ParagonIE_Sodium_Crypto32', false)) {
3
+if ( class_exists( 'ParagonIE_Sodium_Crypto32', false ) ) {
4 4
     return;
5 5
 }
6 6
 
@@ -75,13 +75,13 @@  discard block
 block discarded – undo
75 75
         $key = ''
76 76
     ) {
77 77
         /** @var int $len - Length of message (ciphertext + MAC) */
78
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
78
+        $len = ParagonIE_Sodium_Core32_Util::strlen( $message );
79 79
 
80 80
         /** @var int  $clen - Length of ciphertext */
81 81
         $clen = $len - self::aead_chacha20poly1305_ABYTES;
82 82
 
83 83
         /** @var int $adlen - Length of associated data */
84
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
84
+        $adlen = ParagonIE_Sodium_Core32_Util::strlen( $ad );
85 85
 
86 86
         /** @var string $mac - Message authentication code */
87 87
         $mac = ParagonIE_Sodium_Core32_Util::substr(
@@ -91,7 +91,7 @@  discard block
 block discarded – undo
91 91
         );
92 92
 
93 93
         /** @var string $ciphertext - The encrypted message (sans MAC) */
94
-        $ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 0, $clen);
94
+        $ciphertext = ParagonIE_Sodium_Core32_Util::substr( $message, 0, $clen );
95 95
 
96 96
         /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
97 97
         $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
@@ -101,21 +101,21 @@  discard block
 block discarded – undo
101 101
         );
102 102
 
103 103
         /* Recalculate the Poly1305 authentication tag (MAC): */
104
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
104
+        $state = new ParagonIE_Sodium_Core32_Poly1305_State( $block0 );
105 105
         try {
106
-            ParagonIE_Sodium_Compat::memzero($block0);
107
-        } catch (SodiumException $ex) {
106
+            ParagonIE_Sodium_Compat::memzero( $block0 );
107
+        } catch ( SodiumException $ex ) {
108 108
             $block0 = null;
109 109
         }
110
-        $state->update($ad);
111
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
112
-        $state->update($ciphertext);
113
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
110
+        $state->update( $ad );
111
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $adlen ) );
112
+        $state->update( $ciphertext );
113
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $clen ) );
114 114
         $computed_mac = $state->finish();
115 115
 
116 116
         /* Compare the given MAC with the recalculated MAC: */
117
-        if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
118
-            throw new SodiumException('Invalid MAC');
117
+        if ( ! ParagonIE_Sodium_Core32_Util::verify_16( $computed_mac, $mac ) ) {
118
+            throw new SodiumException( 'Invalid MAC' );
119 119
         }
120 120
 
121 121
         // Here, we know that the MAC is valid, so we decrypt and return the plaintext
@@ -123,7 +123,7 @@  discard block
 block discarded – undo
123 123
             $ciphertext,
124 124
             $nonce,
125 125
             $key,
126
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
126
+            ParagonIE_Sodium_Core32_Util::store64_le( 1 )
127 127
         );
128 128
     }
129 129
 
@@ -147,10 +147,10 @@  discard block
 block discarded – undo
147 147
         $key = ''
148 148
     ) {
149 149
         /** @var int $len - Length of the plaintext message */
150
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
150
+        $len = ParagonIE_Sodium_Core32_Util::strlen( $message );
151 151
 
152 152
         /** @var int $adlen - Length of the associated data */
153
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
153
+        $adlen = ParagonIE_Sodium_Core32_Util::strlen( $ad );
154 154
 
155 155
         /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
156 156
         $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
@@ -158,10 +158,10 @@  discard block
 block discarded – undo
158 158
             $nonce,
159 159
             $key
160 160
         );
161
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
161
+        $state = new ParagonIE_Sodium_Core32_Poly1305_State( $block0 );
162 162
         try {
163
-            ParagonIE_Sodium_Compat::memzero($block0);
164
-        } catch (SodiumException $ex) {
163
+            ParagonIE_Sodium_Compat::memzero( $block0 );
164
+        } catch ( SodiumException $ex ) {
165 165
             $block0 = null;
166 166
         }
167 167
 
@@ -170,13 +170,13 @@  discard block
 block discarded – undo
170 170
             $message,
171 171
             $nonce,
172 172
             $key,
173
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
173
+            ParagonIE_Sodium_Core32_Util::store64_le( 1 )
174 174
         );
175 175
 
176
-        $state->update($ad);
177
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
178
-        $state->update($ciphertext);
179
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
176
+        $state->update( $ad );
177
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $adlen ) );
178
+        $state->update( $ciphertext );
179
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $len ) );
180 180
         return $ciphertext . $state->finish();
181 181
     }
182 182
 
@@ -200,10 +200,10 @@  discard block
 block discarded – undo
200 200
         $key = ''
201 201
     ) {
202 202
         /** @var int $adlen - Length of associated data */
203
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
203
+        $adlen = ParagonIE_Sodium_Core32_Util::strlen( $ad );
204 204
 
205 205
         /** @var int $len - Length of message (ciphertext + MAC) */
206
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
206
+        $len = ParagonIE_Sodium_Core32_Util::strlen( $message );
207 207
 
208 208
         /** @var int  $clen - Length of ciphertext */
209 209
         $clen = $len - self::aead_chacha20poly1305_IETF_ABYTES;
@@ -230,23 +230,23 @@  discard block
 block discarded – undo
230 230
         );
231 231
 
232 232
         /* Recalculate the Poly1305 authentication tag (MAC): */
233
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
233
+        $state = new ParagonIE_Sodium_Core32_Poly1305_State( $block0 );
234 234
         try {
235
-            ParagonIE_Sodium_Compat::memzero($block0);
236
-        } catch (SodiumException $ex) {
235
+            ParagonIE_Sodium_Compat::memzero( $block0 );
236
+        } catch ( SodiumException $ex ) {
237 237
             $block0 = null;
238 238
         }
239
-        $state->update($ad);
240
-        $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
241
-        $state->update($ciphertext);
242
-        $state->update(str_repeat("\x00", (0x10 - $clen) & 0xf));
243
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
244
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($clen));
239
+        $state->update( $ad );
240
+        $state->update( str_repeat( "\x00", ( ( 0x10 - $adlen ) & 0xf ) ) );
241
+        $state->update( $ciphertext );
242
+        $state->update( str_repeat( "\x00", ( 0x10 - $clen ) & 0xf ) );
243
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $adlen ) );
244
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $clen ) );
245 245
         $computed_mac = $state->finish();
246 246
 
247 247
         /* Compare the given MAC with the recalculated MAC: */
248
-        if (!ParagonIE_Sodium_Core32_Util::verify_16($computed_mac, $mac)) {
249
-            throw new SodiumException('Invalid MAC');
248
+        if ( ! ParagonIE_Sodium_Core32_Util::verify_16( $computed_mac, $mac ) ) {
249
+            throw new SodiumException( 'Invalid MAC' );
250 250
         }
251 251
 
252 252
         // Here, we know that the MAC is valid, so we decrypt and return the plaintext
@@ -254,7 +254,7 @@  discard block
 block discarded – undo
254 254
             $ciphertext,
255 255
             $nonce,
256 256
             $key,
257
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
257
+            ParagonIE_Sodium_Core32_Util::store64_le( 1 )
258 258
         );
259 259
     }
260 260
 
@@ -278,10 +278,10 @@  discard block
 block discarded – undo
278 278
         $key = ''
279 279
     ) {
280 280
         /** @var int $len - Length of the plaintext message */
281
-        $len = ParagonIE_Sodium_Core32_Util::strlen($message);
281
+        $len = ParagonIE_Sodium_Core32_Util::strlen( $message );
282 282
 
283 283
         /** @var int $adlen - Length of the associated data */
284
-        $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad);
284
+        $adlen = ParagonIE_Sodium_Core32_Util::strlen( $ad );
285 285
 
286 286
         /** @var string The first block of the chacha20 keystream, used as a poly1305 key */
287 287
         $block0 = ParagonIE_Sodium_Core32_ChaCha20::ietfStream(
@@ -289,10 +289,10 @@  discard block
 block discarded – undo
289 289
             $nonce,
290 290
             $key
291 291
         );
292
-        $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0);
292
+        $state = new ParagonIE_Sodium_Core32_Poly1305_State( $block0 );
293 293
         try {
294
-            ParagonIE_Sodium_Compat::memzero($block0);
295
-        } catch (SodiumException $ex) {
294
+            ParagonIE_Sodium_Compat::memzero( $block0 );
295
+        } catch ( SodiumException $ex ) {
296 296
             $block0 = null;
297 297
         }
298 298
 
@@ -301,15 +301,15 @@  discard block
 block discarded – undo
301 301
             $message,
302 302
             $nonce,
303 303
             $key,
304
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
304
+            ParagonIE_Sodium_Core32_Util::store64_le( 1 )
305 305
         );
306 306
 
307
-        $state->update($ad);
308
-        $state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf)));
309
-        $state->update($ciphertext);
310
-        $state->update(str_repeat("\x00", ((0x10 - $len) & 0xf)));
311
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen));
312
-        $state->update(ParagonIE_Sodium_Core32_Util::store64_le($len));
307
+        $state->update( $ad );
308
+        $state->update( str_repeat( "\x00", ( ( 0x10 - $adlen ) & 0xf ) ) );
309
+        $state->update( $ciphertext );
310
+        $state->update( str_repeat( "\x00", ( ( 0x10 - $len ) & 0xf ) ) );
311
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $adlen ) );
312
+        $state->update( ParagonIE_Sodium_Core32_Util::store64_le( $len ) );
313 313
         return $ciphertext . $state->finish();
314 314
     }
315 315
 
@@ -333,13 +333,13 @@  discard block
 block discarded – undo
333 333
         $key = ''
334 334
     ) {
335 335
         $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
336
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
336
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 0, 16 ),
337 337
             $key
338 338
         );
339 339
         $nonceLast = "\x00\x00\x00\x00" .
340
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
340
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 );
341 341
 
342
-        return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey);
342
+        return self::aead_chacha20poly1305_ietf_decrypt( $message, $ad, $nonceLast, $subkey );
343 343
     }
344 344
 
345 345
     /**
@@ -362,13 +362,13 @@  discard block
 block discarded – undo
362 362
         $key = ''
363 363
     ) {
364 364
         $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
365
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
365
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 0, 16 ),
366 366
             $key
367 367
         );
368 368
         $nonceLast = "\x00\x00\x00\x00" .
369
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
369
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 );
370 370
 
371
-        return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey);
371
+        return self::aead_chacha20poly1305_ietf_encrypt( $message, $ad, $nonceLast, $subkey );
372 372
     }
373 373
 
374 374
     /**
@@ -381,10 +381,10 @@  discard block
 block discarded – undo
381 381
      * @return string
382 382
      * @throws TypeError
383 383
      */
384
-    public static function auth($message, $key)
384
+    public static function auth( $message, $key )
385 385
     {
386 386
         return ParagonIE_Sodium_Core32_Util::substr(
387
-            hash_hmac('sha512', $message, $key, true),
387
+            hash_hmac( 'sha512', $message, $key, true ),
388 388
             0,
389 389
             32
390 390
         );
@@ -402,11 +402,11 @@  discard block
 block discarded – undo
402 402
      * @throws SodiumException
403 403
      * @throws TypeError
404 404
      */
405
-    public static function auth_verify($mac, $message, $key)
405
+    public static function auth_verify( $mac, $message, $key )
406 406
     {
407 407
         return ParagonIE_Sodium_Core32_Util::hashEquals(
408 408
             $mac,
409
-            self::auth($message, $key)
409
+            self::auth( $message, $key )
410 410
         );
411 411
     }
412 412
 
@@ -422,14 +422,14 @@  discard block
 block discarded – undo
422 422
      * @throws SodiumException
423 423
      * @throws TypeError
424 424
      */
425
-    public static function box($plaintext, $nonce, $keypair)
425
+    public static function box( $plaintext, $nonce, $keypair )
426 426
     {
427 427
         return self::secretbox(
428 428
             $plaintext,
429 429
             $nonce,
430 430
             self::box_beforenm(
431
-                self::box_secretkey($keypair),
432
-                self::box_publickey($keypair)
431
+                self::box_secretkey( $keypair ),
432
+                self::box_publickey( $keypair )
433 433
             )
434 434
         );
435 435
     }
@@ -445,16 +445,16 @@  discard block
 block discarded – undo
445 445
      * @throws SodiumException
446 446
      * @throws TypeError
447 447
      */
448
-    public static function box_seal($message, $publicKey)
448
+    public static function box_seal( $message, $publicKey )
449 449
     {
450 450
         /** @var string $ephemeralKeypair */
451 451
         $ephemeralKeypair = self::box_keypair();
452 452
 
453 453
         /** @var string $ephemeralSK */
454
-        $ephemeralSK = self::box_secretkey($ephemeralKeypair);
454
+        $ephemeralSK = self::box_secretkey( $ephemeralKeypair );
455 455
 
456 456
         /** @var string $ephemeralPK */
457
-        $ephemeralPK = self::box_publickey($ephemeralKeypair);
457
+        $ephemeralPK = self::box_publickey( $ephemeralKeypair );
458 458
 
459 459
         /** @var string $nonce */
460 460
         $nonce = self::generichash(
@@ -464,15 +464,15 @@  discard block
 block discarded – undo
464 464
         );
465 465
 
466 466
         /** @var string $keypair - The combined keypair used in crypto_box() */
467
-        $keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey);
467
+        $keypair = self::box_keypair_from_secretkey_and_publickey( $ephemeralSK, $publicKey );
468 468
 
469 469
         /** @var string $ciphertext Ciphertext + MAC from crypto_box */
470
-        $ciphertext = self::box($message, $nonce, $keypair);
470
+        $ciphertext = self::box( $message, $nonce, $keypair );
471 471
         try {
472
-            ParagonIE_Sodium_Compat::memzero($ephemeralKeypair);
473
-            ParagonIE_Sodium_Compat::memzero($ephemeralSK);
474
-            ParagonIE_Sodium_Compat::memzero($nonce);
475
-        } catch (SodiumException $ex) {
472
+            ParagonIE_Sodium_Compat::memzero( $ephemeralKeypair );
473
+            ParagonIE_Sodium_Compat::memzero( $ephemeralSK );
474
+            ParagonIE_Sodium_Compat::memzero( $nonce );
475
+        } catch ( SodiumException $ex ) {
476 476
             $ephemeralKeypair = null;
477 477
             $ephemeralSK = null;
478 478
             $nonce = null;
@@ -491,19 +491,19 @@  discard block
 block discarded – undo
491 491
      * @throws SodiumException
492 492
      * @throws TypeError
493 493
      */
494
-    public static function box_seal_open($message, $keypair)
494
+    public static function box_seal_open( $message, $keypair )
495 495
     {
496 496
         /** @var string $ephemeralPK */
497
-        $ephemeralPK = ParagonIE_Sodium_Core32_Util::substr($message, 0, 32);
497
+        $ephemeralPK = ParagonIE_Sodium_Core32_Util::substr( $message, 0, 32 );
498 498
 
499 499
         /** @var string $ciphertext (ciphertext + MAC) */
500
-        $ciphertext = ParagonIE_Sodium_Core32_Util::substr($message, 32);
500
+        $ciphertext = ParagonIE_Sodium_Core32_Util::substr( $message, 32 );
501 501
 
502 502
         /** @var string $secretKey */
503
-        $secretKey = self::box_secretkey($keypair);
503
+        $secretKey = self::box_secretkey( $keypair );
504 504
 
505 505
         /** @var string $publicKey */
506
-        $publicKey = self::box_publickey($keypair);
506
+        $publicKey = self::box_publickey( $keypair );
507 507
 
508 508
         /** @var string $nonce */
509 509
         $nonce = self::generichash(
@@ -513,15 +513,15 @@  discard block
 block discarded – undo
513 513
         );
514 514
 
515 515
         /** @var string $keypair */
516
-        $keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK);
516
+        $keypair = self::box_keypair_from_secretkey_and_publickey( $secretKey, $ephemeralPK );
517 517
 
518 518
         /** @var string $m */
519
-        $m = self::box_open($ciphertext, $nonce, $keypair);
519
+        $m = self::box_open( $ciphertext, $nonce, $keypair );
520 520
         try {
521
-            ParagonIE_Sodium_Compat::memzero($secretKey);
522
-            ParagonIE_Sodium_Compat::memzero($ephemeralPK);
523
-            ParagonIE_Sodium_Compat::memzero($nonce);
524
-        } catch (SodiumException $ex) {
521
+            ParagonIE_Sodium_Compat::memzero( $secretKey );
522
+            ParagonIE_Sodium_Compat::memzero( $ephemeralPK );
523
+            ParagonIE_Sodium_Compat::memzero( $nonce );
524
+        } catch ( SodiumException $ex ) {
525 525
             $secretKey = null;
526 526
             $ephemeralPK = null;
527 527
             $nonce = null;
@@ -540,11 +540,11 @@  discard block
 block discarded – undo
540 540
      * @throws SodiumException
541 541
      * @throws TypeError
542 542
      */
543
-    public static function box_beforenm($sk, $pk)
543
+    public static function box_beforenm( $sk, $pk )
544 544
     {
545 545
         return ParagonIE_Sodium_Core32_HSalsa20::hsalsa20(
546
-            str_repeat("\x00", 16),
547
-            self::scalarmult($sk, $pk)
546
+            str_repeat( "\x00", 16 ),
547
+            self::scalarmult( $sk, $pk )
548 548
         );
549 549
     }
550 550
 
@@ -558,8 +558,8 @@  discard block
 block discarded – undo
558 558
      */
559 559
     public static function box_keypair()
560 560
     {
561
-        $sKey = random_bytes(32);
562
-        $pKey = self::scalarmult_base($sKey);
561
+        $sKey = random_bytes( 32 );
562
+        $pKey = self::scalarmult_base( $sKey );
563 563
         return $sKey . $pKey;
564 564
     }
565 565
 
@@ -569,14 +569,14 @@  discard block
 block discarded – undo
569 569
      * @throws SodiumException
570 570
      * @throws TypeError
571 571
      */
572
-    public static function box_seed_keypair($seed)
572
+    public static function box_seed_keypair( $seed )
573 573
     {
574 574
         $sKey = ParagonIE_Sodium_Core32_Util::substr(
575
-            hash('sha512', $seed, true),
575
+            hash( 'sha512', $seed, true ),
576 576
             0,
577 577
             32
578 578
         );
579
-        $pKey = self::scalarmult_base($sKey);
579
+        $pKey = self::scalarmult_base( $sKey );
580 580
         return $sKey . $pKey;
581 581
     }
582 582
 
@@ -588,10 +588,10 @@  discard block
 block discarded – undo
588 588
      * @return string
589 589
      * @throws TypeError
590 590
      */
591
-    public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey)
591
+    public static function box_keypair_from_secretkey_and_publickey( $sKey, $pKey )
592 592
     {
593
-        return ParagonIE_Sodium_Core32_Util::substr($sKey, 0, 32) .
594
-            ParagonIE_Sodium_Core32_Util::substr($pKey, 0, 32);
593
+        return ParagonIE_Sodium_Core32_Util::substr( $sKey, 0, 32 ) .
594
+            ParagonIE_Sodium_Core32_Util::substr( $pKey, 0, 32 );
595 595
     }
596 596
 
597 597
     /**
@@ -602,14 +602,14 @@  discard block
 block discarded – undo
602 602
      * @throws RangeException
603 603
      * @throws TypeError
604 604
      */
605
-    public static function box_secretkey($keypair)
605
+    public static function box_secretkey( $keypair )
606 606
     {
607
-        if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== 64) {
607
+        if ( ParagonIE_Sodium_Core32_Util::strlen( $keypair ) !== 64 ) {
608 608
             throw new RangeException(
609 609
                 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
610 610
             );
611 611
         }
612
-        return ParagonIE_Sodium_Core32_Util::substr($keypair, 0, 32);
612
+        return ParagonIE_Sodium_Core32_Util::substr( $keypair, 0, 32 );
613 613
     }
614 614
 
615 615
     /**
@@ -620,14 +620,14 @@  discard block
 block discarded – undo
620 620
      * @throws RangeException
621 621
      * @throws TypeError
622 622
      */
623
-    public static function box_publickey($keypair)
623
+    public static function box_publickey( $keypair )
624 624
     {
625
-        if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
625
+        if ( ParagonIE_Sodium_Core32_Util::strlen( $keypair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) {
626 626
             throw new RangeException(
627 627
                 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
628 628
             );
629 629
         }
630
-        return ParagonIE_Sodium_Core32_Util::substr($keypair, 32, 32);
630
+        return ParagonIE_Sodium_Core32_Util::substr( $keypair, 32, 32 );
631 631
     }
632 632
 
633 633
     /**
@@ -639,14 +639,14 @@  discard block
 block discarded – undo
639 639
      * @throws SodiumException
640 640
      * @throws TypeError
641 641
      */
642
-    public static function box_publickey_from_secretkey($sKey)
642
+    public static function box_publickey_from_secretkey( $sKey )
643 643
     {
644
-        if (ParagonIE_Sodium_Core32_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) {
644
+        if ( ParagonIE_Sodium_Core32_Util::strlen( $sKey ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES ) {
645 645
             throw new RangeException(
646 646
                 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.'
647 647
             );
648 648
         }
649
-        return self::scalarmult_base($sKey);
649
+        return self::scalarmult_base( $sKey );
650 650
     }
651 651
 
652 652
     /**
@@ -661,14 +661,14 @@  discard block
 block discarded – undo
661 661
      * @throws SodiumException
662 662
      * @throws TypeError
663 663
      */
664
-    public static function box_open($ciphertext, $nonce, $keypair)
664
+    public static function box_open( $ciphertext, $nonce, $keypair )
665 665
     {
666 666
         return self::secretbox_open(
667 667
             $ciphertext,
668 668
             $nonce,
669 669
             self::box_beforenm(
670
-                self::box_secretkey($keypair),
671
-                self::box_publickey($keypair)
670
+                self::box_secretkey( $keypair ),
671
+                self::box_publickey( $keypair )
672 672
             )
673 673
         );
674 674
     }
@@ -686,34 +686,34 @@  discard block
 block discarded – undo
686 686
      * @throws SodiumException
687 687
      * @throws TypeError
688 688
      */
689
-    public static function generichash($message, $key = '', $outlen = 32)
689
+    public static function generichash( $message, $key = '', $outlen = 32 )
690 690
     {
691 691
         // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
692 692
         ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
693 693
 
694 694
         $k = null;
695
-        if (!empty($key)) {
695
+        if ( ! empty( $key ) ) {
696 696
             /** @var SplFixedArray $k */
697
-            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
698
-            if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
699
-                throw new RangeException('Invalid key size');
697
+            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray( $key );
698
+            if ( $k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES ) {
699
+                throw new RangeException( 'Invalid key size' );
700 700
             }
701 701
         }
702 702
 
703 703
         /** @var SplFixedArray $in */
704
-        $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message);
704
+        $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray( $message );
705 705
 
706 706
         /** @var SplFixedArray $ctx */
707
-        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outlen);
708
-        ParagonIE_Sodium_Core32_BLAKE2b::update($ctx, $in, $in->count());
707
+        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init( $k, $outlen );
708
+        ParagonIE_Sodium_Core32_BLAKE2b::update( $ctx, $in, $in->count() );
709 709
 
710 710
         /** @var SplFixedArray $out */
711
-        $out = new SplFixedArray($outlen);
712
-        $out = ParagonIE_Sodium_Core32_BLAKE2b::finish($ctx, $out);
711
+        $out = new SplFixedArray( $outlen );
712
+        $out = ParagonIE_Sodium_Core32_BLAKE2b::finish( $ctx, $out );
713 713
 
714 714
         /** @var array<int, int> */
715 715
         $outArray = $out->toArray();
716
-        return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray);
716
+        return ParagonIE_Sodium_Core32_Util::intArrayToString( $outArray );
717 717
     }
718 718
 
719 719
     /**
@@ -727,22 +727,22 @@  discard block
 block discarded – undo
727 727
      * @throws SodiumException
728 728
      * @throws TypeError
729 729
      */
730
-    public static function generichash_final($ctx, $outlen = 32)
730
+    public static function generichash_final( $ctx, $outlen = 32 )
731 731
     {
732
-        if (!is_string($ctx)) {
733
-            throw new TypeError('Context must be a string');
732
+        if ( ! is_string( $ctx ) ) {
733
+            throw new TypeError( 'Context must be a string' );
734 734
         }
735
-        $out = new SplFixedArray($outlen);
735
+        $out = new SplFixedArray( $outlen );
736 736
 
737 737
         /** @var SplFixedArray $context */
738
-        $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx);
738
+        $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext( $ctx );
739 739
 
740 740
         /** @var SplFixedArray $out */
741
-        $out = ParagonIE_Sodium_Core32_BLAKE2b::finish($context, $out);
741
+        $out = ParagonIE_Sodium_Core32_BLAKE2b::finish( $context, $out );
742 742
 
743 743
         /** @var array<int, int> */
744 744
         $outArray = $out->toArray();
745
-        return ParagonIE_Sodium_Core32_Util::intArrayToString($outArray);
745
+        return ParagonIE_Sodium_Core32_Util::intArrayToString( $outArray );
746 746
     }
747 747
 
748 748
     /**
@@ -757,23 +757,23 @@  discard block
 block discarded – undo
757 757
      * @throws SodiumException
758 758
      * @throws TypeError
759 759
      */
760
-    public static function generichash_init($key = '', $outputLength = 32)
760
+    public static function generichash_init( $key = '', $outputLength = 32 )
761 761
     {
762 762
         // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
763 763
         ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
764 764
 
765 765
         $k = null;
766
-        if (!empty($key)) {
767
-            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
768
-            if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
769
-                throw new RangeException('Invalid key size');
766
+        if ( ! empty( $key ) ) {
767
+            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray( $key );
768
+            if ( $k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES ) {
769
+                throw new RangeException( 'Invalid key size' );
770 770
             }
771 771
         }
772 772
 
773 773
         /** @var SplFixedArray $ctx */
774
-        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outputLength);
774
+        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init( $k, $outputLength );
775 775
 
776
-        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($ctx);
776
+        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString( $ctx );
777 777
     }
778 778
 
779 779
     /**
@@ -800,27 +800,27 @@  discard block
 block discarded – undo
800 800
         ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
801 801
 
802 802
         $k = null;
803
-        if (!empty($key)) {
804
-            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($key);
805
-            if ($k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES) {
806
-                throw new RangeException('Invalid key size');
803
+        if ( ! empty( $key ) ) {
804
+            $k = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray( $key );
805
+            if ( $k->count() > ParagonIE_Sodium_Core32_BLAKE2b::KEYBYTES ) {
806
+                throw new RangeException( 'Invalid key size' );
807 807
             }
808 808
         }
809
-        if (!empty($salt)) {
810
-            $s = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($salt);
809
+        if ( ! empty( $salt ) ) {
810
+            $s = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray( $salt );
811 811
         } else {
812 812
             $s = null;
813 813
         }
814
-        if (!empty($salt)) {
815
-            $p = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($personal);
814
+        if ( ! empty( $salt ) ) {
815
+            $p = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray( $personal );
816 816
         } else {
817 817
             $p = null;
818 818
         }
819 819
 
820 820
         /** @var SplFixedArray $ctx */
821
-        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init($k, $outputLength, $s, $p);
821
+        $ctx = ParagonIE_Sodium_Core32_BLAKE2b::init( $k, $outputLength, $s, $p );
822 822
 
823
-        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($ctx);
823
+        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString( $ctx );
824 824
     }
825 825
 
826 826
     /**
@@ -834,20 +834,20 @@  discard block
 block discarded – undo
834 834
      * @throws SodiumException
835 835
      * @throws TypeError
836 836
      */
837
-    public static function generichash_update($ctx, $message)
837
+    public static function generichash_update( $ctx, $message )
838 838
     {
839 839
         // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
840 840
         ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
841 841
 
842 842
         /** @var SplFixedArray $context */
843
-        $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext($ctx);
843
+        $context = ParagonIE_Sodium_Core32_BLAKE2b::stringToContext( $ctx );
844 844
 
845 845
         /** @var SplFixedArray $in */
846
-        $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray($message);
846
+        $in = ParagonIE_Sodium_Core32_BLAKE2b::stringToSplFixedArray( $message );
847 847
 
848
-        ParagonIE_Sodium_Core32_BLAKE2b::update($context, $in, $in->count());
848
+        ParagonIE_Sodium_Core32_BLAKE2b::update( $context, $in, $in->count() );
849 849
 
850
-        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString($context);
850
+        return ParagonIE_Sodium_Core32_BLAKE2b::contextToString( $context );
851 851
     }
852 852
 
853 853
     /**
@@ -863,10 +863,10 @@  discard block
 block discarded – undo
863 863
      * @throws SodiumException
864 864
      * @throws TypeError
865 865
      */
866
-    public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk)
866
+    public static function keyExchange( $my_sk, $their_pk, $client_pk, $server_pk )
867 867
     {
868 868
         return self::generichash(
869
-            self::scalarmult($my_sk, $their_pk) .
869
+            self::scalarmult( $my_sk, $their_pk ) .
870 870
             $client_pk .
871 871
             $server_pk
872 872
         );
@@ -884,10 +884,10 @@  discard block
 block discarded – undo
884 884
      * @throws SodiumException
885 885
      * @throws TypeError
886 886
      */
887
-    public static function scalarmult($sKey, $pKey)
887
+    public static function scalarmult( $sKey, $pKey )
888 888
     {
889
-        $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey);
890
-        self::scalarmult_throw_if_zero($q);
889
+        $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10( $sKey, $pKey );
890
+        self::scalarmult_throw_if_zero( $q );
891 891
         return $q;
892 892
     }
893 893
 
@@ -901,10 +901,10 @@  discard block
 block discarded – undo
901 901
      * @throws SodiumException
902 902
      * @throws TypeError
903 903
      */
904
-    public static function scalarmult_base($secret)
904
+    public static function scalarmult_base( $secret )
905 905
     {
906
-        $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10_base($secret);
907
-        self::scalarmult_throw_if_zero($q);
906
+        $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10_base( $secret );
907
+        self::scalarmult_throw_if_zero( $q );
908 908
         return $q;
909 909
     }
910 910
 
@@ -916,16 +916,16 @@  discard block
 block discarded – undo
916 916
      * @throws SodiumException
917 917
      * @throws TypeError
918 918
      */
919
-    protected static function scalarmult_throw_if_zero($q)
919
+    protected static function scalarmult_throw_if_zero( $q )
920 920
     {
921 921
         $d = 0;
922
-        for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) {
923
-            $d |= ParagonIE_Sodium_Core32_Util::chrToInt($q[$i]);
922
+        for ( $i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i ) {
923
+            $d |= ParagonIE_Sodium_Core32_Util::chrToInt( $q[ $i ] );
924 924
         }
925 925
 
926 926
         /* branch-free variant of === 0 */
927
-        if (-(1 & (($d - 1) >> 8))) {
928
-            throw new SodiumException('Zero public key is not allowed');
927
+        if (-( 1 & ( ( $d - 1 ) >> 8 ) )) {
928
+            throw new SodiumException( 'Zero public key is not allowed' );
929 929
         }
930 930
     }
931 931
 
@@ -941,26 +941,26 @@  discard block
 block discarded – undo
941 941
      * @throws SodiumException
942 942
      * @throws TypeError
943 943
      */
944
-    public static function secretbox($plaintext, $nonce, $key)
944
+    public static function secretbox( $plaintext, $nonce, $key )
945 945
     {
946 946
         /** @var string $subkey */
947
-        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
947
+        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20( $nonce, $key );
948 948
 
949 949
         /** @var string $block0 */
950
-        $block0 = str_repeat("\x00", 32);
950
+        $block0 = str_repeat( "\x00", 32 );
951 951
 
952 952
         /** @var int $mlen - Length of the plaintext message */
953
-        $mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext);
953
+        $mlen = ParagonIE_Sodium_Core32_Util::strlen( $plaintext );
954 954
         $mlen0 = $mlen;
955
-        if ($mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES) {
955
+        if ( $mlen0 > 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES ) {
956 956
             $mlen0 = 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES;
957 957
         }
958
-        $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
958
+        $block0 .= ParagonIE_Sodium_Core32_Util::substr( $plaintext, 0, $mlen0 );
959 959
 
960 960
         /** @var string $block0 */
961 961
         $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20_xor(
962 962
             $block0,
963
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
963
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ),
964 964
             $subkey
965 965
         );
966 966
 
@@ -969,13 +969,13 @@  discard block
 block discarded – undo
969 969
             $block0,
970 970
             self::secretbox_xsalsa20poly1305_ZEROBYTES
971 971
         );
972
-        if ($mlen > $mlen0) {
972
+        if ( $mlen > $mlen0 ) {
973 973
             $c .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
974 974
                 ParagonIE_Sodium_Core32_Util::substr(
975 975
                     $plaintext,
976 976
                     self::secretbox_xsalsa20poly1305_ZEROBYTES
977 977
                 ),
978
-                ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
978
+                ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ),
979 979
                 1,
980 980
                 $subkey
981 981
             );
@@ -988,18 +988,18 @@  discard block
 block discarded – undo
988 988
             )
989 989
         );
990 990
         try {
991
-            ParagonIE_Sodium_Compat::memzero($block0);
992
-            ParagonIE_Sodium_Compat::memzero($subkey);
993
-        } catch (SodiumException $ex) {
991
+            ParagonIE_Sodium_Compat::memzero( $block0 );
992
+            ParagonIE_Sodium_Compat::memzero( $subkey );
993
+        } catch ( SodiumException $ex ) {
994 994
             $block0 = null;
995 995
             $subkey = null;
996 996
         }
997 997
 
998
-        $state->update($c);
998
+        $state->update( $c );
999 999
 
1000 1000
         /** @var string $c - MAC || ciphertext */
1001 1001
         $c = $state->finish() . $c;
1002
-        unset($state);
1002
+        unset( $state );
1003 1003
 
1004 1004
         return $c;
1005 1005
     }
@@ -1016,7 +1016,7 @@  discard block
 block discarded – undo
1016 1016
      * @throws SodiumException
1017 1017
      * @throws TypeError
1018 1018
      */
1019
-    public static function secretbox_open($ciphertext, $nonce, $key)
1019
+    public static function secretbox_open( $ciphertext, $nonce, $key )
1020 1020
     {
1021 1021
         /** @var string $mac */
1022 1022
         $mac = ParagonIE_Sodium_Core32_Util::substr(
@@ -1032,46 +1032,46 @@  discard block
 block discarded – undo
1032 1032
         );
1033 1033
 
1034 1034
         /** @var int $clen */
1035
-        $clen = ParagonIE_Sodium_Core32_Util::strlen($c);
1035
+        $clen = ParagonIE_Sodium_Core32_Util::strlen( $c );
1036 1036
 
1037 1037
         /** @var string $subkey */
1038
-        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
1038
+        $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20( $nonce, $key );
1039 1039
 
1040 1040
         /** @var string $block0 */
1041 1041
         $block0 = ParagonIE_Sodium_Core32_Salsa20::salsa20(
1042 1042
             64,
1043
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1043
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ),
1044 1044
             $subkey
1045 1045
         );
1046 1046
         $verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify(
1047 1047
             $mac,
1048 1048
             $c,
1049
-            ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32)
1049
+            ParagonIE_Sodium_Core32_Util::substr( $block0, 0, 32 )
1050 1050
         );
1051
-        if (!$verified) {
1051
+        if ( ! $verified ) {
1052 1052
             try {
1053
-                ParagonIE_Sodium_Compat::memzero($subkey);
1054
-            } catch (SodiumException $ex) {
1053
+                ParagonIE_Sodium_Compat::memzero( $subkey );
1054
+            } catch ( SodiumException $ex ) {
1055 1055
                 $subkey = null;
1056 1056
             }
1057
-            throw new SodiumException('Invalid MAC');
1057
+            throw new SodiumException( 'Invalid MAC' );
1058 1058
         }
1059 1059
 
1060 1060
         /** @var string $m - Decrypted message */
1061 1061
         $m = ParagonIE_Sodium_Core32_Util::xorStrings(
1062
-            ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES),
1063
-            ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES)
1062
+            ParagonIE_Sodium_Core32_Util::substr( $block0, self::secretbox_xsalsa20poly1305_ZEROBYTES ),
1063
+            ParagonIE_Sodium_Core32_Util::substr( $c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES )
1064 1064
         );
1065
-        if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) {
1065
+        if ( $clen > self::secretbox_xsalsa20poly1305_ZEROBYTES ) {
1066 1066
             // We had more than 1 block, so let's continue to decrypt the rest.
1067 1067
             $m .= ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic(
1068 1068
                 ParagonIE_Sodium_Core32_Util::substr(
1069 1069
                     $c,
1070 1070
                     self::secretbox_xsalsa20poly1305_ZEROBYTES
1071 1071
                 ),
1072
-                ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1072
+                ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ),
1073 1073
                 1,
1074
-                (string) $subkey
1074
+                (string)$subkey
1075 1075
             );
1076 1076
         }
1077 1077
         return $m;
@@ -1089,25 +1089,25 @@  discard block
 block discarded – undo
1089 1089
      * @throws SodiumException
1090 1090
      * @throws TypeError
1091 1091
      */
1092
-    public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key)
1092
+    public static function secretbox_xchacha20poly1305( $plaintext, $nonce, $key )
1093 1093
     {
1094 1094
         /** @var string $subkey */
1095 1095
         $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
1096
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
1096
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 0, 16 ),
1097 1097
             $key
1098 1098
         );
1099
-        $nonceLast = ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8);
1099
+        $nonceLast = ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 );
1100 1100
 
1101 1101
         /** @var string $block0 */
1102
-        $block0 = str_repeat("\x00", 32);
1102
+        $block0 = str_repeat( "\x00", 32 );
1103 1103
 
1104 1104
         /** @var int $mlen - Length of the plaintext message */
1105
-        $mlen = ParagonIE_Sodium_Core32_Util::strlen($plaintext);
1105
+        $mlen = ParagonIE_Sodium_Core32_Util::strlen( $plaintext );
1106 1106
         $mlen0 = $mlen;
1107
-        if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) {
1107
+        if ( $mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES ) {
1108 1108
             $mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES;
1109 1109
         }
1110
-        $block0 .= ParagonIE_Sodium_Core32_Util::substr($plaintext, 0, $mlen0);
1110
+        $block0 .= ParagonIE_Sodium_Core32_Util::substr( $plaintext, 0, $mlen0 );
1111 1111
 
1112 1112
         /** @var string $block0 */
1113 1113
         $block0 = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
@@ -1121,7 +1121,7 @@  discard block
 block discarded – undo
1121 1121
             $block0,
1122 1122
             self::secretbox_xchacha20poly1305_ZEROBYTES
1123 1123
         );
1124
-        if ($mlen > $mlen0) {
1124
+        if ( $mlen > $mlen0 ) {
1125 1125
             $c .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1126 1126
                 ParagonIE_Sodium_Core32_Util::substr(
1127 1127
                     $plaintext,
@@ -1129,7 +1129,7 @@  discard block
 block discarded – undo
1129 1129
                 ),
1130 1130
                 $nonceLast,
1131 1131
                 $subkey,
1132
-                ParagonIE_Sodium_Core32_Util::store64_le(1)
1132
+                ParagonIE_Sodium_Core32_Util::store64_le( 1 )
1133 1133
             );
1134 1134
         }
1135 1135
         $state = new ParagonIE_Sodium_Core32_Poly1305_State(
@@ -1140,18 +1140,18 @@  discard block
 block discarded – undo
1140 1140
             )
1141 1141
         );
1142 1142
         try {
1143
-            ParagonIE_Sodium_Compat::memzero($block0);
1144
-            ParagonIE_Sodium_Compat::memzero($subkey);
1145
-        } catch (SodiumException $ex) {
1143
+            ParagonIE_Sodium_Compat::memzero( $block0 );
1144
+            ParagonIE_Sodium_Compat::memzero( $subkey );
1145
+        } catch ( SodiumException $ex ) {
1146 1146
             $block0 = null;
1147 1147
             $subkey = null;
1148 1148
         }
1149 1149
 
1150
-        $state->update($c);
1150
+        $state->update( $c );
1151 1151
 
1152 1152
         /** @var string $c - MAC || ciphertext */
1153 1153
         $c = $state->finish() . $c;
1154
-        unset($state);
1154
+        unset( $state );
1155 1155
 
1156 1156
         return $c;
1157 1157
     }
@@ -1168,7 +1168,7 @@  discard block
 block discarded – undo
1168 1168
      * @throws SodiumException
1169 1169
      * @throws TypeError
1170 1170
      */
1171
-    public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
1171
+    public static function secretbox_xchacha20poly1305_open( $ciphertext, $nonce, $key )
1172 1172
     {
1173 1173
         /** @var string $mac */
1174 1174
         $mac = ParagonIE_Sodium_Core32_Util::substr(
@@ -1184,48 +1184,48 @@  discard block
 block discarded – undo
1184 1184
         );
1185 1185
 
1186 1186
         /** @var int $clen */
1187
-        $clen = ParagonIE_Sodium_Core32_Util::strlen($c);
1187
+        $clen = ParagonIE_Sodium_Core32_Util::strlen( $c );
1188 1188
 
1189 1189
         /** @var string $subkey */
1190
-        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hchacha20($nonce, $key);
1190
+        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hchacha20( $nonce, $key );
1191 1191
 
1192 1192
         /** @var string $block0 */
1193 1193
         $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream(
1194 1194
             64,
1195
-            ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1195
+            ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ),
1196 1196
             $subkey
1197 1197
         );
1198 1198
         $verified = ParagonIE_Sodium_Core32_Poly1305::onetimeauth_verify(
1199 1199
             $mac,
1200 1200
             $c,
1201
-            ParagonIE_Sodium_Core32_Util::substr($block0, 0, 32)
1201
+            ParagonIE_Sodium_Core32_Util::substr( $block0, 0, 32 )
1202 1202
         );
1203 1203
 
1204
-        if (!$verified) {
1204
+        if ( ! $verified ) {
1205 1205
             try {
1206
-                ParagonIE_Sodium_Compat::memzero($subkey);
1207
-            } catch (SodiumException $ex) {
1206
+                ParagonIE_Sodium_Compat::memzero( $subkey );
1207
+            } catch ( SodiumException $ex ) {
1208 1208
                 $subkey = null;
1209 1209
             }
1210
-            throw new SodiumException('Invalid MAC');
1210
+            throw new SodiumException( 'Invalid MAC' );
1211 1211
         }
1212 1212
 
1213 1213
         /** @var string $m - Decrypted message */
1214 1214
         $m = ParagonIE_Sodium_Core32_Util::xorStrings(
1215
-            ParagonIE_Sodium_Core32_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES),
1216
-            ParagonIE_Sodium_Core32_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES)
1215
+            ParagonIE_Sodium_Core32_Util::substr( $block0, self::secretbox_xchacha20poly1305_ZEROBYTES ),
1216
+            ParagonIE_Sodium_Core32_Util::substr( $c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES )
1217 1217
         );
1218 1218
 
1219
-        if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) {
1219
+        if ( $clen > self::secretbox_xchacha20poly1305_ZEROBYTES ) {
1220 1220
             // We had more than 1 block, so let's continue to decrypt the rest.
1221 1221
             $m .= ParagonIE_Sodium_Core32_ChaCha20::streamXorIc(
1222 1222
                 ParagonIE_Sodium_Core32_Util::substr(
1223 1223
                     $c,
1224 1224
                     self::secretbox_xchacha20poly1305_ZEROBYTES
1225 1225
                 ),
1226
-                ParagonIE_Sodium_Core32_Util::substr($nonce, 16, 8),
1227
-                (string) $subkey,
1228
-                ParagonIE_Sodium_Core32_Util::store64_le(1)
1226
+                ParagonIE_Sodium_Core32_Util::substr( $nonce, 16, 8 ),
1227
+                (string)$subkey,
1228
+                ParagonIE_Sodium_Core32_Util::store64_le( 1 )
1229 1229
             );
1230 1230
         }
1231 1231
         return $m;
@@ -1237,16 +1237,16 @@  discard block
 block discarded – undo
1237 1237
      * @throws Exception
1238 1238
      * @throws SodiumException
1239 1239
      */
1240
-    public static function secretstream_xchacha20poly1305_init_push($key)
1240
+    public static function secretstream_xchacha20poly1305_init_push( $key )
1241 1241
     {
1242 1242
         # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES);
1243
-        $out = random_bytes(24);
1243
+        $out = random_bytes( 24 );
1244 1244
 
1245 1245
         # crypto_core_hchacha20(state->k, out, k, NULL);
1246
-        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20($out, $key);
1246
+        $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20( $out, $key );
1247 1247
         $state = new ParagonIE_Sodium_Core32_SecretStream_State(
1248 1248
             $subkey,
1249
-            ParagonIE_Sodium_Core32_Util::substr($out, 16, 8) . str_repeat("\0", 4)
1249
+            ParagonIE_Sodium_Core32_Util::substr( $out, 16, 8 ) . str_repeat( "\0", 4 )
1250 1250
         );
1251 1251
 
1252 1252
         # _crypto_secretstream_xchacha20poly1305_counter_reset(state);
@@ -1267,16 +1267,16 @@  discard block
 block discarded – undo
1267 1267
      * @return string Returns a state.
1268 1268
      * @throws Exception
1269 1269
      */
1270
-    public static function secretstream_xchacha20poly1305_init_pull($key, $header)
1270
+    public static function secretstream_xchacha20poly1305_init_pull( $key, $header )
1271 1271
     {
1272 1272
         # crypto_core_hchacha20(state->k, in, k, NULL);
1273 1273
         $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
1274
-            ParagonIE_Sodium_Core32_Util::substr($header, 0, 16),
1274
+            ParagonIE_Sodium_Core32_Util::substr( $header, 0, 16 ),
1275 1275
             $key
1276 1276
         );
1277 1277
         $state = new ParagonIE_Sodium_Core32_SecretStream_State(
1278 1278
             $subkey,
1279
-            ParagonIE_Sodium_Core32_Util::substr($header, 16)
1279
+            ParagonIE_Sodium_Core32_Util::substr( $header, 16 )
1280 1280
         );
1281 1281
         $state->counterReset();
1282 1282
         # memcpy(STATE_INONCE(state), in + crypto_core_hchacha20_INPUTBYTES,
@@ -1294,19 +1294,19 @@  discard block
 block discarded – undo
1294 1294
      * @return string
1295 1295
      * @throws SodiumException
1296 1296
      */
1297
-    public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
1297
+    public static function secretstream_xchacha20poly1305_push( &$state, $msg, $aad = '', $tag = 0 )
1298 1298
     {
1299
-        $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
1299
+        $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString( $state );
1300 1300
         # crypto_onetimeauth_poly1305_state poly1305_state;
1301 1301
         # unsigned char                     block[64U];
1302 1302
         # unsigned char                     slen[8U];
1303 1303
         # unsigned char                    *c;
1304 1304
         # unsigned char                    *mac;
1305 1305
 
1306
-        $msglen = ParagonIE_Sodium_Core32_Util::strlen($msg);
1307
-        $aadlen = ParagonIE_Sodium_Core32_Util::strlen($aad);
1306
+        $msglen = ParagonIE_Sodium_Core32_Util::strlen( $msg );
1307
+        $aadlen = ParagonIE_Sodium_Core32_Util::strlen( $aad );
1308 1308
 
1309
-        if ((($msglen + 63) >> 6) > 0xfffffffe) {
1309
+        if ( ( ( $msglen + 63 ) >> 6 ) > 0xfffffffe ) {
1310 1310
             throw new SodiumException(
1311 1311
                 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
1312 1312
             );
@@ -1323,62 +1323,62 @@  discard block
 block discarded – undo
1323 1323
         # crypto_onetimeauth_poly1305_init(&poly1305_state, block);
1324 1324
         # sodium_memzero(block, sizeof block);
1325 1325
         $auth = new ParagonIE_Sodium_Core32_Poly1305_State(
1326
-            ParagonIE_Sodium_Core32_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
1326
+            ParagonIE_Sodium_Core32_ChaCha20::ietfStream( 32, $st->getCombinedNonce(), $st->getKey() )
1327 1327
         );
1328 1328
 
1329 1329
         # crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
1330
-        $auth->update($aad);
1330
+        $auth->update( $aad );
1331 1331
 
1332 1332
         # crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
1333 1333
         #     (0x10 - adlen) & 0xf);
1334
-        $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
1334
+        $auth->update( str_repeat( "\0", ( ( 0x10 - $aadlen ) & 0xf ) ) );
1335 1335
 
1336 1336
         # memset(block, 0, sizeof block);
1337 1337
         # block[0] = tag;
1338 1338
         # crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
1339 1339
         #                                    state->nonce, 1U, state->k);
1340 1340
         $block = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1341
-            ParagonIE_Sodium_Core32_Util::intToChr($tag) . str_repeat("\0", 63),
1341
+            ParagonIE_Sodium_Core32_Util::intToChr( $tag ) . str_repeat( "\0", 63 ),
1342 1342
             $st->getCombinedNonce(),
1343 1343
             $st->getKey(),
1344
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
1344
+            ParagonIE_Sodium_Core32_Util::store64_le( 1 )
1345 1345
         );
1346 1346
 
1347 1347
         # crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
1348
-        $auth->update($block);
1348
+        $auth->update( $block );
1349 1349
 
1350 1350
         # out[0] = block[0];
1351
-        $out = $block[0];
1351
+        $out = $block[ 0 ];
1352 1352
         # c = out + (sizeof tag);
1353 1353
         # crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, state->nonce, 2U, state->k);
1354 1354
         $cipher = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1355 1355
             $msg,
1356 1356
             $st->getCombinedNonce(),
1357 1357
             $st->getKey(),
1358
-            ParagonIE_Sodium_Core32_Util::store64_le(2)
1358
+            ParagonIE_Sodium_Core32_Util::store64_le( 2 )
1359 1359
         );
1360 1360
 
1361 1361
         # crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
1362
-        $auth->update($cipher);
1362
+        $auth->update( $cipher );
1363 1363
 
1364 1364
         $out .= $cipher;
1365
-        unset($cipher);
1365
+        unset( $cipher );
1366 1366
 
1367 1367
         # crypto_onetimeauth_poly1305_update
1368 1368
         # (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
1369
-        $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
1369
+        $auth->update( str_repeat( "\0", ( ( 0x10 - 64 + $msglen ) & 0xf ) ) );
1370 1370
 
1371 1371
         # STORE64_LE(slen, (uint64_t) adlen);
1372
-        $slen = ParagonIE_Sodium_Core32_Util::store64_le($aadlen);
1372
+        $slen = ParagonIE_Sodium_Core32_Util::store64_le( $aadlen );
1373 1373
 
1374 1374
         # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1375
-        $auth->update($slen);
1375
+        $auth->update( $slen );
1376 1376
 
1377 1377
         # STORE64_LE(slen, (sizeof block) + mlen);
1378
-        $slen = ParagonIE_Sodium_Core32_Util::store64_le(64 + $msglen);
1378
+        $slen = ParagonIE_Sodium_Core32_Util::store64_le( 64 + $msglen );
1379 1379
 
1380 1380
         # crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1381
-        $auth->update($slen);
1381
+        $auth->update( $slen );
1382 1382
 
1383 1383
         # mac = c + mlen;
1384 1384
         # crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
@@ -1386,12 +1386,12 @@  discard block
 block discarded – undo
1386 1386
         $out .= $mac;
1387 1387
 
1388 1388
         # sodium_memzero(&poly1305_state, sizeof poly1305_state);
1389
-        unset($auth);
1389
+        unset( $auth );
1390 1390
 
1391 1391
 
1392 1392
         # XOR_BUF(STATE_INONCE(state), mac,
1393 1393
         #     crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1394
-        $st->xorNonce($mac);
1394
+        $st->xorNonce( $mac );
1395 1395
 
1396 1396
         # sodium_increment(STATE_COUNTER(state),
1397 1397
         #     crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
@@ -1400,15 +1400,15 @@  discard block
 block discarded – undo
1400 1400
         $state = $st->toString();
1401 1401
 
1402 1402
         /** @var bool $rekey */
1403
-        $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
1403
+        $rekey = ( $tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY ) !== 0;
1404 1404
         # if ((tag & crypto_secretstream_xchacha20poly1305_TAG_REKEY) != 0 ||
1405 1405
         #     sodium_is_zero(STATE_COUNTER(state),
1406 1406
         #         crypto_secretstream_xchacha20poly1305_COUNTERBYTES)) {
1407 1407
         #     crypto_secretstream_xchacha20poly1305_rekey(state);
1408 1408
         # }
1409
-        if ($rekey || $st->needsRekey()) {
1409
+        if ( $rekey || $st->needsRekey() ) {
1410 1410
             // DO REKEY
1411
-            self::secretstream_xchacha20poly1305_rekey($state);
1411
+            self::secretstream_xchacha20poly1305_rekey( $state );
1412 1412
         }
1413 1413
         # if (outlen_p != NULL) {
1414 1414
         #     *outlen_p = crypto_secretstream_xchacha20poly1305_ABYTES + mlen;
@@ -1423,19 +1423,19 @@  discard block
 block discarded – undo
1423 1423
      * @return bool|array{0: string, 1: int}
1424 1424
      * @throws SodiumException
1425 1425
      */
1426
-    public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '')
1426
+    public static function secretstream_xchacha20poly1305_pull( &$state, $cipher, $aad = '' )
1427 1427
     {
1428
-        $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
1428
+        $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString( $state );
1429 1429
 
1430
-        $cipherlen = ParagonIE_Sodium_Core32_Util::strlen($cipher);
1430
+        $cipherlen = ParagonIE_Sodium_Core32_Util::strlen( $cipher );
1431 1431
         #     mlen = inlen - crypto_secretstream_xchacha20poly1305_ABYTES;
1432 1432
         $msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES;
1433
-        $aadlen = ParagonIE_Sodium_Core32_Util::strlen($aad);
1433
+        $aadlen = ParagonIE_Sodium_Core32_Util::strlen( $aad );
1434 1434
 
1435 1435
         #     if (mlen > crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX) {
1436 1436
         #         sodium_misuse();
1437 1437
         #     }
1438
-        if ((($msglen + 63) >> 6) > 0xfffffffe) {
1438
+        if ( ( ( $msglen + 63 ) >> 6 ) > 0xfffffffe ) {
1439 1439
             throw new SodiumException(
1440 1440
                 'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes'
1441 1441
             );
@@ -1445,15 +1445,15 @@  discard block
 block discarded – undo
1445 1445
         #     crypto_onetimeauth_poly1305_init(&poly1305_state, block);
1446 1446
         #     sodium_memzero(block, sizeof block);
1447 1447
         $auth = new ParagonIE_Sodium_Core32_Poly1305_State(
1448
-            ParagonIE_Sodium_Core32_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey())
1448
+            ParagonIE_Sodium_Core32_ChaCha20::ietfStream( 32, $st->getCombinedNonce(), $st->getKey() )
1449 1449
         );
1450 1450
 
1451 1451
         #     crypto_onetimeauth_poly1305_update(&poly1305_state, ad, adlen);
1452
-        $auth->update($aad);
1452
+        $auth->update( $aad );
1453 1453
 
1454 1454
         #     crypto_onetimeauth_poly1305_update(&poly1305_state, _pad0,
1455 1455
         #         (0x10 - adlen) & 0xf);
1456
-        $auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf)));
1456
+        $auth->update( str_repeat( "\0", ( ( 0x10 - $aadlen ) & 0xf ) ) );
1457 1457
 
1458 1458
 
1459 1459
         #     memset(block, 0, sizeof block);
@@ -1461,36 +1461,36 @@  discard block
 block discarded – undo
1461 1461
         #     crypto_stream_chacha20_ietf_xor_ic(block, block, sizeof block,
1462 1462
         #                                        state->nonce, 1U, state->k);
1463 1463
         $block = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1464
-            $cipher[0] . str_repeat("\0", 63),
1464
+            $cipher[ 0 ] . str_repeat( "\0", 63 ),
1465 1465
             $st->getCombinedNonce(),
1466 1466
             $st->getKey(),
1467
-            ParagonIE_Sodium_Core32_Util::store64_le(1)
1467
+            ParagonIE_Sodium_Core32_Util::store64_le( 1 )
1468 1468
         );
1469 1469
         #     tag = block[0];
1470 1470
         #     block[0] = in[0];
1471 1471
         #     crypto_onetimeauth_poly1305_update(&poly1305_state, block, sizeof block);
1472
-        $tag = ParagonIE_Sodium_Core32_Util::chrToInt($block[0]);
1473
-        $block[0] = $cipher[0];
1474
-        $auth->update($block);
1472
+        $tag = ParagonIE_Sodium_Core32_Util::chrToInt( $block[ 0 ] );
1473
+        $block[ 0 ] = $cipher[ 0 ];
1474
+        $auth->update( $block );
1475 1475
 
1476 1476
 
1477 1477
         #     c = in + (sizeof tag);
1478 1478
         #     crypto_onetimeauth_poly1305_update(&poly1305_state, c, mlen);
1479
-        $auth->update(ParagonIE_Sodium_Core32_Util::substr($cipher, 1, $msglen));
1479
+        $auth->update( ParagonIE_Sodium_Core32_Util::substr( $cipher, 1, $msglen ) );
1480 1480
 
1481 1481
         #     crypto_onetimeauth_poly1305_update
1482 1482
         #     (&poly1305_state, _pad0, (0x10 - (sizeof block) + mlen) & 0xf);
1483
-        $auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf)));
1483
+        $auth->update( str_repeat( "\0", ( ( 0x10 - 64 + $msglen ) & 0xf ) ) );
1484 1484
 
1485 1485
         #     STORE64_LE(slen, (uint64_t) adlen);
1486 1486
         #     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1487
-        $slen = ParagonIE_Sodium_Core32_Util::store64_le($aadlen);
1488
-        $auth->update($slen);
1487
+        $slen = ParagonIE_Sodium_Core32_Util::store64_le( $aadlen );
1488
+        $auth->update( $slen );
1489 1489
 
1490 1490
         #     STORE64_LE(slen, (sizeof block) + mlen);
1491 1491
         #     crypto_onetimeauth_poly1305_update(&poly1305_state, slen, sizeof slen);
1492
-        $slen = ParagonIE_Sodium_Core32_Util::store64_le(64 + $msglen);
1493
-        $auth->update($slen);
1492
+        $slen = ParagonIE_Sodium_Core32_Util::store64_le( 64 + $msglen );
1493
+        $auth->update( $slen );
1494 1494
 
1495 1495
         #     crypto_onetimeauth_poly1305_final(&poly1305_state, mac);
1496 1496
         #     sodium_memzero(&poly1305_state, sizeof poly1305_state);
@@ -1502,22 +1502,22 @@  discard block
 block discarded – undo
1502 1502
         #         return -1;
1503 1503
         #     }
1504 1504
 
1505
-        $stored = ParagonIE_Sodium_Core32_Util::substr($cipher, $msglen + 1, 16);
1506
-        if (!ParagonIE_Sodium_Core32_Util::hashEquals($mac, $stored)) {
1505
+        $stored = ParagonIE_Sodium_Core32_Util::substr( $cipher, $msglen + 1, 16 );
1506
+        if ( ! ParagonIE_Sodium_Core32_Util::hashEquals( $mac, $stored ) ) {
1507 1507
             return false;
1508 1508
         }
1509 1509
 
1510 1510
         #     crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, state->nonce, 2U, state->k);
1511 1511
         $out = ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1512
-            ParagonIE_Sodium_Core32_Util::substr($cipher, 1, $msglen),
1512
+            ParagonIE_Sodium_Core32_Util::substr( $cipher, 1, $msglen ),
1513 1513
             $st->getCombinedNonce(),
1514 1514
             $st->getKey(),
1515
-            ParagonIE_Sodium_Core32_Util::store64_le(2)
1515
+            ParagonIE_Sodium_Core32_Util::store64_le( 2 )
1516 1516
         );
1517 1517
 
1518 1518
         #     XOR_BUF(STATE_INONCE(state), mac,
1519 1519
         #         crypto_secretstream_xchacha20poly1305_INONCEBYTES);
1520
-        $st->xorNonce($mac);
1520
+        $st->xorNonce( $mac );
1521 1521
 
1522 1522
         #     sodium_increment(STATE_COUNTER(state),
1523 1523
         #         crypto_secretstream_xchacha20poly1305_COUNTERBYTES);
@@ -1533,12 +1533,12 @@  discard block
 block discarded – undo
1533 1533
         $state = $st->toString();
1534 1534
 
1535 1535
         /** @var bool $rekey */
1536
-        $rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0;
1537
-        if ($rekey || $st->needsRekey()) {
1536
+        $rekey = ( $tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY ) !== 0;
1537
+        if ( $rekey || $st->needsRekey() ) {
1538 1538
             // DO REKEY
1539
-            self::secretstream_xchacha20poly1305_rekey($state);
1539
+            self::secretstream_xchacha20poly1305_rekey( $state );
1540 1540
         }
1541
-        return array($out, $tag);
1541
+        return array( $out, $tag );
1542 1542
     }
1543 1543
 
1544 1544
     /**
@@ -1546,9 +1546,9 @@  discard block
 block discarded – undo
1546 1546
      * @return void
1547 1547
      * @throws SodiumException
1548 1548
      */
1549
-    public static function secretstream_xchacha20poly1305_rekey(&$state)
1549
+    public static function secretstream_xchacha20poly1305_rekey( &$state )
1550 1550
     {
1551
-        $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
1551
+        $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString( $state );
1552 1552
         # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES +
1553 1553
         # crypto_secretstream_xchacha20poly1305_INONCEBYTES];
1554 1554
         # size_t        i;
@@ -1561,18 +1561,18 @@  discard block
 block discarded – undo
1561 1561
         #     new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES + i] =
1562 1562
         #         STATE_INONCE(state)[i];
1563 1563
         # }
1564
-        $new_key_and_inonce .= ParagonIE_Sodium_Core32_Util::substR($st->getNonce(), 0, 8);
1564
+        $new_key_and_inonce .= ParagonIE_Sodium_Core32_Util::substR( $st->getNonce(), 0, 8 );
1565 1565
 
1566 1566
         # crypto_stream_chacha20_ietf_xor(new_key_and_inonce, new_key_and_inonce,
1567 1567
         #                                 sizeof new_key_and_inonce,
1568 1568
         #                                 state->nonce, state->k);
1569 1569
 
1570
-        $st->rekey(ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1570
+        $st->rekey( ParagonIE_Sodium_Core32_ChaCha20::ietfStreamXorIc(
1571 1571
             $new_key_and_inonce,
1572 1572
             $st->getCombinedNonce(),
1573 1573
             $st->getKey(),
1574
-            ParagonIE_Sodium_Core32_Util::store64_le(0)
1575
-        ));
1574
+            ParagonIE_Sodium_Core32_Util::store64_le( 0 )
1575
+        ) );
1576 1576
 
1577 1577
         # for (i = 0U; i < crypto_stream_chacha20_ietf_KEYBYTES; i++) {
1578 1578
         #     state->k[i] = new_key_and_inonce[i];
@@ -1598,9 +1598,9 @@  discard block
 block discarded – undo
1598 1598
      * @throws SodiumException
1599 1599
      * @throws TypeError
1600 1600
      */
1601
-    public static function sign_detached($message, $sk)
1601
+    public static function sign_detached( $message, $sk )
1602 1602
     {
1603
-        return ParagonIE_Sodium_Core32_Ed25519::sign_detached($message, $sk);
1603
+        return ParagonIE_Sodium_Core32_Ed25519::sign_detached( $message, $sk );
1604 1604
     }
1605 1605
 
1606 1606
     /**
@@ -1614,9 +1614,9 @@  discard block
 block discarded – undo
1614 1614
      * @throws SodiumException
1615 1615
      * @throws TypeError
1616 1616
      */
1617
-    public static function sign($message, $sk)
1617
+    public static function sign( $message, $sk )
1618 1618
     {
1619
-        return ParagonIE_Sodium_Core32_Ed25519::sign($message, $sk);
1619
+        return ParagonIE_Sodium_Core32_Ed25519::sign( $message, $sk );
1620 1620
     }
1621 1621
 
1622 1622
     /**
@@ -1630,9 +1630,9 @@  discard block
 block discarded – undo
1630 1630
      * @throws SodiumException
1631 1631
      * @throws TypeError
1632 1632
      */
1633
-    public static function sign_open($signedMessage, $pk)
1633
+    public static function sign_open( $signedMessage, $pk )
1634 1634
     {
1635
-        return ParagonIE_Sodium_Core32_Ed25519::sign_open($signedMessage, $pk);
1635
+        return ParagonIE_Sodium_Core32_Ed25519::sign_open( $signedMessage, $pk );
1636 1636
     }
1637 1637
 
1638 1638
     /**
@@ -1647,8 +1647,8 @@  discard block
 block discarded – undo
1647 1647
      * @throws SodiumException
1648 1648
      * @throws TypeError
1649 1649
      */
1650
-    public static function sign_verify_detached($signature, $message, $pk)
1650
+    public static function sign_verify_detached( $signature, $message, $pk )
1651 1651
     {
1652
-        return ParagonIE_Sodium_Core32_Ed25519::verify_detached($signature, $message, $pk);
1652
+        return ParagonIE_Sodium_Core32_Ed25519::verify_detached( $signature, $message, $pk );
1653 1653
     }
1654 1654
 }
Please login to merge, or discard this patch.
Braces   +35 added lines, -70 removed lines patch added patch discarded remove patch
@@ -12,8 +12,7 @@  discard block
 block discarded – undo
12 12
  * If you are using this library, you should be using
13 13
  * ParagonIE_Sodium_Compat in your code, not this class.
14 14
  */
15
-abstract class ParagonIE_Sodium_Crypto32
16
-{
15
+abstract class ParagonIE_Sodium_Crypto32 {
17 16
     const aead_chacha20poly1305_KEYBYTES = 32;
18 17
     const aead_chacha20poly1305_NSECBYTES = 0;
19 18
     const aead_chacha20poly1305_NPUBBYTES = 8;
@@ -381,8 +380,7 @@  discard block
 block discarded – undo
381 380
      * @return string
382 381
      * @throws TypeError
383 382
      */
384
-    public static function auth($message, $key)
385
-    {
383
+    public static function auth($message, $key) {
386 384
         return ParagonIE_Sodium_Core32_Util::substr(
387 385
             hash_hmac('sha512', $message, $key, true),
388 386
             0,
@@ -402,8 +400,7 @@  discard block
 block discarded – undo
402 400
      * @throws SodiumException
403 401
      * @throws TypeError
404 402
      */
405
-    public static function auth_verify($mac, $message, $key)
406
-    {
403
+    public static function auth_verify($mac, $message, $key) {
407 404
         return ParagonIE_Sodium_Core32_Util::hashEquals(
408 405
             $mac,
409 406
             self::auth($message, $key)
@@ -422,8 +419,7 @@  discard block
 block discarded – undo
422 419
      * @throws SodiumException
423 420
      * @throws TypeError
424 421
      */
425
-    public static function box($plaintext, $nonce, $keypair)
426
-    {
422
+    public static function box($plaintext, $nonce, $keypair) {
427 423
         return self::secretbox(
428 424
             $plaintext,
429 425
             $nonce,
@@ -445,8 +441,7 @@  discard block
 block discarded – undo
445 441
      * @throws SodiumException
446 442
      * @throws TypeError
447 443
      */
448
-    public static function box_seal($message, $publicKey)
449
-    {
444
+    public static function box_seal($message, $publicKey) {
450 445
         /** @var string $ephemeralKeypair */
451 446
         $ephemeralKeypair = self::box_keypair();
452 447
 
@@ -491,8 +486,7 @@  discard block
 block discarded – undo
491 486
      * @throws SodiumException
492 487
      * @throws TypeError
493 488
      */
494
-    public static function box_seal_open($message, $keypair)
495
-    {
489
+    public static function box_seal_open($message, $keypair) {
496 490
         /** @var string $ephemeralPK */
497 491
         $ephemeralPK = ParagonIE_Sodium_Core32_Util::substr($message, 0, 32);
498 492
 
@@ -540,8 +534,7 @@  discard block
 block discarded – undo
540 534
      * @throws SodiumException
541 535
      * @throws TypeError
542 536
      */
543
-    public static function box_beforenm($sk, $pk)
544
-    {
537
+    public static function box_beforenm($sk, $pk) {
545 538
         return ParagonIE_Sodium_Core32_HSalsa20::hsalsa20(
546 539
             str_repeat("\x00", 16),
547 540
             self::scalarmult($sk, $pk)
@@ -556,8 +549,7 @@  discard block
 block discarded – undo
556 549
      * @throws SodiumException
557 550
      * @throws TypeError
558 551
      */
559
-    public static function box_keypair()
560
-    {
552
+    public static function box_keypair() {
561 553
         $sKey = random_bytes(32);
562 554
         $pKey = self::scalarmult_base($sKey);
563 555
         return $sKey . $pKey;
@@ -569,8 +561,7 @@  discard block
 block discarded – undo
569 561
      * @throws SodiumException
570 562
      * @throws TypeError
571 563
      */
572
-    public static function box_seed_keypair($seed)
573
-    {
564
+    public static function box_seed_keypair($seed) {
574 565
         $sKey = ParagonIE_Sodium_Core32_Util::substr(
575 566
             hash('sha512', $seed, true),
576 567
             0,
@@ -588,8 +579,7 @@  discard block
 block discarded – undo
588 579
      * @return string
589 580
      * @throws TypeError
590 581
      */
591
-    public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey)
592
-    {
582
+    public static function box_keypair_from_secretkey_and_publickey($sKey, $pKey) {
593 583
         return ParagonIE_Sodium_Core32_Util::substr($sKey, 0, 32) .
594 584
             ParagonIE_Sodium_Core32_Util::substr($pKey, 0, 32);
595 585
     }
@@ -602,8 +592,7 @@  discard block
 block discarded – undo
602 592
      * @throws RangeException
603 593
      * @throws TypeError
604 594
      */
605
-    public static function box_secretkey($keypair)
606
-    {
595
+    public static function box_secretkey($keypair) {
607 596
         if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== 64) {
608 597
             throw new RangeException(
609 598
                 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
@@ -620,8 +609,7 @@  discard block
 block discarded – undo
620 609
      * @throws RangeException
621 610
      * @throws TypeError
622 611
      */
623
-    public static function box_publickey($keypair)
624
-    {
612
+    public static function box_publickey($keypair) {
625 613
         if (ParagonIE_Sodium_Core32_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
626 614
             throw new RangeException(
627 615
                 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.'
@@ -639,8 +627,7 @@  discard block
 block discarded – undo
639 627
      * @throws SodiumException
640 628
      * @throws TypeError
641 629
      */
642
-    public static function box_publickey_from_secretkey($sKey)
643
-    {
630
+    public static function box_publickey_from_secretkey($sKey) {
644 631
         if (ParagonIE_Sodium_Core32_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) {
645 632
             throw new RangeException(
646 633
                 'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.'
@@ -661,8 +648,7 @@  discard block
 block discarded – undo
661 648
      * @throws SodiumException
662 649
      * @throws TypeError
663 650
      */
664
-    public static function box_open($ciphertext, $nonce, $keypair)
665
-    {
651
+    public static function box_open($ciphertext, $nonce, $keypair) {
666 652
         return self::secretbox_open(
667 653
             $ciphertext,
668 654
             $nonce,
@@ -686,8 +672,7 @@  discard block
 block discarded – undo
686 672
      * @throws SodiumException
687 673
      * @throws TypeError
688 674
      */
689
-    public static function generichash($message, $key = '', $outlen = 32)
690
-    {
675
+    public static function generichash($message, $key = '', $outlen = 32) {
691 676
         // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
692 677
         ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
693 678
 
@@ -727,8 +712,7 @@  discard block
 block discarded – undo
727 712
      * @throws SodiumException
728 713
      * @throws TypeError
729 714
      */
730
-    public static function generichash_final($ctx, $outlen = 32)
731
-    {
715
+    public static function generichash_final($ctx, $outlen = 32) {
732 716
         if (!is_string($ctx)) {
733 717
             throw new TypeError('Context must be a string');
734 718
         }
@@ -757,8 +741,7 @@  discard block
 block discarded – undo
757 741
      * @throws SodiumException
758 742
      * @throws TypeError
759 743
      */
760
-    public static function generichash_init($key = '', $outputLength = 32)
761
-    {
744
+    public static function generichash_init($key = '', $outputLength = 32) {
762 745
         // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
763 746
         ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
764 747
 
@@ -834,8 +817,7 @@  discard block
 block discarded – undo
834 817
      * @throws SodiumException
835 818
      * @throws TypeError
836 819
      */
837
-    public static function generichash_update($ctx, $message)
838
-    {
820
+    public static function generichash_update($ctx, $message) {
839 821
         // This ensures that ParagonIE_Sodium_Core32_BLAKE2b::$iv is initialized
840 822
         ParagonIE_Sodium_Core32_BLAKE2b::pseudoConstructor();
841 823
 
@@ -863,8 +845,7 @@  discard block
 block discarded – undo
863 845
      * @throws SodiumException
864 846
      * @throws TypeError
865 847
      */
866
-    public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk)
867
-    {
848
+    public static function keyExchange($my_sk, $their_pk, $client_pk, $server_pk) {
868 849
         return self::generichash(
869 850
             self::scalarmult($my_sk, $their_pk) .
870 851
             $client_pk .
@@ -884,8 +865,7 @@  discard block
 block discarded – undo
884 865
      * @throws SodiumException
885 866
      * @throws TypeError
886 867
      */
887
-    public static function scalarmult($sKey, $pKey)
888
-    {
868
+    public static function scalarmult($sKey, $pKey) {
889 869
         $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey);
890 870
         self::scalarmult_throw_if_zero($q);
891 871
         return $q;
@@ -901,8 +881,7 @@  discard block
 block discarded – undo
901 881
      * @throws SodiumException
902 882
      * @throws TypeError
903 883
      */
904
-    public static function scalarmult_base($secret)
905
-    {
884
+    public static function scalarmult_base($secret) {
906 885
         $q = ParagonIE_Sodium_Core32_X25519::crypto_scalarmult_curve25519_ref10_base($secret);
907 886
         self::scalarmult_throw_if_zero($q);
908 887
         return $q;
@@ -916,8 +895,7 @@  discard block
 block discarded – undo
916 895
      * @throws SodiumException
917 896
      * @throws TypeError
918 897
      */
919
-    protected static function scalarmult_throw_if_zero($q)
920
-    {
898
+    protected static function scalarmult_throw_if_zero($q) {
921 899
         $d = 0;
922 900
         for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) {
923 901
             $d |= ParagonIE_Sodium_Core32_Util::chrToInt($q[$i]);
@@ -941,8 +919,7 @@  discard block
 block discarded – undo
941 919
      * @throws SodiumException
942 920
      * @throws TypeError
943 921
      */
944
-    public static function secretbox($plaintext, $nonce, $key)
945
-    {
922
+    public static function secretbox($plaintext, $nonce, $key) {
946 923
         /** @var string $subkey */
947 924
         $subkey = ParagonIE_Sodium_Core32_HSalsa20::hsalsa20($nonce, $key);
948 925
 
@@ -1016,8 +993,7 @@  discard block
 block discarded – undo
1016 993
      * @throws SodiumException
1017 994
      * @throws TypeError
1018 995
      */
1019
-    public static function secretbox_open($ciphertext, $nonce, $key)
1020
-    {
996
+    public static function secretbox_open($ciphertext, $nonce, $key) {
1021 997
         /** @var string $mac */
1022 998
         $mac = ParagonIE_Sodium_Core32_Util::substr(
1023 999
             $ciphertext,
@@ -1089,8 +1065,7 @@  discard block
 block discarded – undo
1089 1065
      * @throws SodiumException
1090 1066
      * @throws TypeError
1091 1067
      */
1092
-    public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key)
1093
-    {
1068
+    public static function secretbox_xchacha20poly1305($plaintext, $nonce, $key) {
1094 1069
         /** @var string $subkey */
1095 1070
         $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
1096 1071
             ParagonIE_Sodium_Core32_Util::substr($nonce, 0, 16),
@@ -1168,8 +1143,7 @@  discard block
 block discarded – undo
1168 1143
      * @throws SodiumException
1169 1144
      * @throws TypeError
1170 1145
      */
1171
-    public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key)
1172
-    {
1146
+    public static function secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) {
1173 1147
         /** @var string $mac */
1174 1148
         $mac = ParagonIE_Sodium_Core32_Util::substr(
1175 1149
             $ciphertext,
@@ -1237,8 +1211,7 @@  discard block
 block discarded – undo
1237 1211
      * @throws Exception
1238 1212
      * @throws SodiumException
1239 1213
      */
1240
-    public static function secretstream_xchacha20poly1305_init_push($key)
1241
-    {
1214
+    public static function secretstream_xchacha20poly1305_init_push($key) {
1242 1215
         # randombytes_buf(out, crypto_secretstream_xchacha20poly1305_HEADERBYTES);
1243 1216
         $out = random_bytes(24);
1244 1217
 
@@ -1267,8 +1240,7 @@  discard block
 block discarded – undo
1267 1240
      * @return string Returns a state.
1268 1241
      * @throws Exception
1269 1242
      */
1270
-    public static function secretstream_xchacha20poly1305_init_pull($key, $header)
1271
-    {
1243
+    public static function secretstream_xchacha20poly1305_init_pull($key, $header) {
1272 1244
         # crypto_core_hchacha20(state->k, in, k, NULL);
1273 1245
         $subkey = ParagonIE_Sodium_Core32_HChaCha20::hChaCha20(
1274 1246
             ParagonIE_Sodium_Core32_Util::substr($header, 0, 16),
@@ -1294,8 +1266,7 @@  discard block
 block discarded – undo
1294 1266
      * @return string
1295 1267
      * @throws SodiumException
1296 1268
      */
1297
-    public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0)
1298
-    {
1269
+    public static function secretstream_xchacha20poly1305_push(&$state, $msg, $aad = '', $tag = 0) {
1299 1270
         $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
1300 1271
         # crypto_onetimeauth_poly1305_state poly1305_state;
1301 1272
         # unsigned char                     block[64U];
@@ -1423,8 +1394,7 @@  discard block
 block discarded – undo
1423 1394
      * @return bool|array{0: string, 1: int}
1424 1395
      * @throws SodiumException
1425 1396
      */
1426
-    public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '')
1427
-    {
1397
+    public static function secretstream_xchacha20poly1305_pull(&$state, $cipher, $aad = '') {
1428 1398
         $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
1429 1399
 
1430 1400
         $cipherlen = ParagonIE_Sodium_Core32_Util::strlen($cipher);
@@ -1546,8 +1516,7 @@  discard block
 block discarded – undo
1546 1516
      * @return void
1547 1517
      * @throws SodiumException
1548 1518
      */
1549
-    public static function secretstream_xchacha20poly1305_rekey(&$state)
1550
-    {
1519
+    public static function secretstream_xchacha20poly1305_rekey(&$state) {
1551 1520
         $st = ParagonIE_Sodium_Core32_SecretStream_State::fromString($state);
1552 1521
         # unsigned char new_key_and_inonce[crypto_stream_chacha20_ietf_KEYBYTES +
1553 1522
         # crypto_secretstream_xchacha20poly1305_INONCEBYTES];
@@ -1598,8 +1567,7 @@  discard block
 block discarded – undo
1598 1567
      * @throws SodiumException
1599 1568
      * @throws TypeError
1600 1569
      */
1601
-    public static function sign_detached($message, $sk)
1602
-    {
1570
+    public static function sign_detached($message, $sk) {
1603 1571
         return ParagonIE_Sodium_Core32_Ed25519::sign_detached($message, $sk);
1604 1572
     }
1605 1573
 
@@ -1614,8 +1582,7 @@  discard block
 block discarded – undo
1614 1582
      * @throws SodiumException
1615 1583
      * @throws TypeError
1616 1584
      */
1617
-    public static function sign($message, $sk)
1618
-    {
1585
+    public static function sign($message, $sk) {
1619 1586
         return ParagonIE_Sodium_Core32_Ed25519::sign($message, $sk);
1620 1587
     }
1621 1588
 
@@ -1630,8 +1597,7 @@  discard block
 block discarded – undo
1630 1597
      * @throws SodiumException
1631 1598
      * @throws TypeError
1632 1599
      */
1633
-    public static function sign_open($signedMessage, $pk)
1634
-    {
1600
+    public static function sign_open($signedMessage, $pk) {
1635 1601
         return ParagonIE_Sodium_Core32_Ed25519::sign_open($signedMessage, $pk);
1636 1602
     }
1637 1603
 
@@ -1647,8 +1613,7 @@  discard block
 block discarded – undo
1647 1613
      * @throws SodiumException
1648 1614
      * @throws TypeError
1649 1615
      */
1650
-    public static function sign_verify_detached($signature, $message, $pk)
1651
-    {
1616
+    public static function sign_verify_detached($signature, $message, $pk) {
1652 1617
         return ParagonIE_Sodium_Core32_Ed25519::verify_detached($signature, $message, $pk);
1653 1618
     }
1654 1619
 }
Please login to merge, or discard this patch.
vendor/paragonie/sodium_compat/src/Core/BLAKE2b.php 3 patches
Indentation   +773 added lines, -773 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) {
4
-    return;
4
+	return;
5 5
 }
6 6
 
7 7
 /**
@@ -11,780 +11,780 @@  discard block
 block discarded – undo
11 11
  */
12 12
 abstract class ParagonIE_Sodium_Core_BLAKE2b extends ParagonIE_Sodium_Core_Util
13 13
 {
14
-    /**
15
-     * @var SplFixedArray
16
-     */
17
-    protected static $iv;
18
-
19
-    /**
20
-     * @var array<int, array<int, int>>
21
-     */
22
-    protected static $sigma = array(
23
-        array(  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15),
24
-        array( 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3),
25
-        array( 11,  8, 12,  0,  5,  2, 15, 13, 10, 14,  3,  6,  7,  1,  9,  4),
26
-        array(  7,  9,  3,  1, 13, 12, 11, 14,  2,  6,  5, 10,  4,  0, 15,  8),
27
-        array(  9,  0,  5,  7,  2,  4, 10, 15, 14,  1, 11, 12,  6,  8,  3, 13),
28
-        array(  2, 12,  6, 10,  0, 11,  8,  3,  4, 13,  7,  5, 15, 14,  1,  9),
29
-        array( 12,  5,  1, 15, 14, 13,  4, 10,  0,  7,  6,  3,  9,  2,  8, 11),
30
-        array( 13, 11,  7, 14, 12,  1,  3,  9,  5,  0, 15,  4,  8,  6,  2, 10),
31
-        array(  6, 15, 14,  9, 11,  3,  0,  8, 12,  2, 13,  7,  1,  4, 10,  5),
32
-        array( 10,  2,  8,  4,  7,  6,  1,  5, 15, 11,  9, 14,  3, 12, 13 , 0),
33
-        array(  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15),
34
-        array( 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3)
35
-    );
36
-
37
-    const BLOCKBYTES = 128;
38
-    const OUTBYTES   = 64;
39
-    const KEYBYTES   = 64;
40
-
41
-    /**
42
-     * Turn two 32-bit integers into a fixed array representing a 64-bit integer.
43
-     *
44
-     * @internal You should not use this directly from another application
45
-     *
46
-     * @param int $high
47
-     * @param int $low
48
-     * @return SplFixedArray
49
-     * @psalm-suppress MixedAssignment
50
-     */
51
-    public static function new64($high, $low)
52
-    {
53
-        $i64 = new SplFixedArray(2);
54
-        $i64[0] = $high & 0xffffffff;
55
-        $i64[1] = $low & 0xffffffff;
56
-        return $i64;
57
-    }
58
-
59
-    /**
60
-     * Convert an arbitrary number into an SplFixedArray of two 32-bit integers
61
-     * that represents a 64-bit integer.
62
-     *
63
-     * @internal You should not use this directly from another application
64
-     *
65
-     * @param int $num
66
-     * @return SplFixedArray
67
-     */
68
-    protected static function to64($num)
69
-    {
70
-        list($hi, $lo) = self::numericTo64BitInteger($num);
71
-        return self::new64($hi, $lo);
72
-    }
73
-
74
-    /**
75
-     * Adds two 64-bit integers together, returning their sum as a SplFixedArray
76
-     * containing two 32-bit integers (representing a 64-bit integer).
77
-     *
78
-     * @internal You should not use this directly from another application
79
-     *
80
-     * @param SplFixedArray $x
81
-     * @param SplFixedArray $y
82
-     * @return SplFixedArray
83
-     * @psalm-suppress MixedArgument
84
-     * @psalm-suppress MixedAssignment
85
-     * @psalm-suppress MixedOperand
86
-     */
87
-    protected static function add64($x, $y)
88
-    {
89
-        $l = ($x[1] + $y[1]) & 0xffffffff;
90
-        return self::new64(
91
-            (int) ($x[0] + $y[0] + (
92
-                ($l < $x[1]) ? 1 : 0
93
-            )),
94
-            (int) $l
95
-        );
96
-    }
97
-
98
-    /**
99
-     * @internal You should not use this directly from another application
100
-     *
101
-     * @param SplFixedArray $x
102
-     * @param SplFixedArray $y
103
-     * @param SplFixedArray $z
104
-     * @return SplFixedArray
105
-     */
106
-    protected static function add364($x, $y, $z)
107
-    {
108
-        return self::add64($x, self::add64($y, $z));
109
-    }
110
-
111
-    /**
112
-     * @internal You should not use this directly from another application
113
-     *
114
-     * @param SplFixedArray $x
115
-     * @param SplFixedArray $y
116
-     * @return SplFixedArray
117
-     * @throws SodiumException
118
-     * @throws TypeError
119
-     */
120
-    protected static function xor64(SplFixedArray $x, SplFixedArray $y)
121
-    {
122
-        if (!is_numeric($x[0])) {
123
-            throw new SodiumException('x[0] is not an integer');
124
-        }
125
-        if (!is_numeric($x[1])) {
126
-            throw new SodiumException('x[1] is not an integer');
127
-        }
128
-        if (!is_numeric($y[0])) {
129
-            throw new SodiumException('y[0] is not an integer');
130
-        }
131
-        if (!is_numeric($y[1])) {
132
-            throw new SodiumException('y[1] is not an integer');
133
-        }
134
-        return self::new64(
135
-            (int) (($x[0] ^ $y[0]) & 0xffffffff),
136
-            (int) (($x[1] ^ $y[1]) & 0xffffffff)
137
-        );
138
-    }
139
-
140
-    /**
141
-     * @internal You should not use this directly from another application
142
-     *
143
-     * @param SplFixedArray $x
144
-     * @param int $c
145
-     * @return SplFixedArray
146
-     * @psalm-suppress MixedAssignment
147
-     */
148
-    public static function rotr64($x, $c)
149
-    {
150
-        if ($c >= 64) {
151
-            $c %= 64;
152
-        }
153
-        if ($c >= 32) {
154
-            /** @var int $tmp */
155
-            $tmp = $x[0];
156
-            $x[0] = $x[1];
157
-            $x[1] = $tmp;
158
-            $c -= 32;
159
-        }
160
-        if ($c === 0) {
161
-            return $x;
162
-        }
163
-
164
-        $l0 = 0;
165
-        $c = 64 - $c;
166
-
167
-        if ($c < 32) {
168
-            /** @var int $h0 */
169
-            $h0 = ((int) ($x[0]) << $c) | (
170
-                (
171
-                    (int) ($x[1]) & ((1 << $c) - 1)
172
-                        <<
173
-                    (32 - $c)
174
-                ) >> (32 - $c)
175
-            );
176
-            /** @var int $l0 */
177
-            $l0 = (int) ($x[1]) << $c;
178
-        } else {
179
-            /** @var int $h0 */
180
-            $h0 = (int) ($x[1]) << ($c - 32);
181
-        }
182
-
183
-        $h1 = 0;
184
-        $c1 = 64 - $c;
185
-
186
-        if ($c1 < 32) {
187
-            /** @var int $h1 */
188
-            $h1 = (int) ($x[0]) >> $c1;
189
-            /** @var int $l1 */
190
-            $l1 = ((int) ($x[1]) >> $c1) | ((int) ($x[0]) & ((1 << $c1) - 1)) << (32 - $c1);
191
-        } else {
192
-            /** @var int $l1 */
193
-            $l1 = (int) ($x[0]) >> ($c1 - 32);
194
-        }
195
-
196
-        return self::new64($h0 | $h1, $l0 | $l1);
197
-    }
198
-
199
-    /**
200
-     * @internal You should not use this directly from another application
201
-     *
202
-     * @param SplFixedArray $x
203
-     * @return int
204
-     * @psalm-suppress MixedOperand
205
-     */
206
-    protected static function flatten64($x)
207
-    {
208
-        return (int) ($x[0] * 4294967296 + $x[1]);
209
-    }
210
-
211
-    /**
212
-     * @internal You should not use this directly from another application
213
-     *
214
-     * @param SplFixedArray $x
215
-     * @param int $i
216
-     * @return SplFixedArray
217
-     * @psalm-suppress MixedArgument
218
-     * @psalm-suppress MixedArrayOffset
219
-     */
220
-    protected static function load64(SplFixedArray $x, $i)
221
-    {
222
-        /** @var int $l */
223
-        $l = (int) ($x[$i])
224
-             | ((int) ($x[$i+1]) << 8)
225
-             | ((int) ($x[$i+2]) << 16)
226
-             | ((int) ($x[$i+3]) << 24);
227
-        /** @var int $h */
228
-        $h = (int) ($x[$i+4])
229
-             | ((int) ($x[$i+5]) << 8)
230
-             | ((int) ($x[$i+6]) << 16)
231
-             | ((int) ($x[$i+7]) << 24);
232
-        return self::new64($h, $l);
233
-    }
234
-
235
-    /**
236
-     * @internal You should not use this directly from another application
237
-     *
238
-     * @param SplFixedArray $x
239
-     * @param int $i
240
-     * @param SplFixedArray $u
241
-     * @return void
242
-     * @psalm-suppress MixedAssignment
243
-     */
244
-    protected static function store64(SplFixedArray $x, $i, SplFixedArray $u)
245
-    {
246
-        $maxLength = $x->getSize() - 1;
247
-        for ($j = 0; $j < 8; ++$j) {
248
-            /*
14
+	/**
15
+	 * @var SplFixedArray
16
+	 */
17
+	protected static $iv;
18
+
19
+	/**
20
+	 * @var array<int, array<int, int>>
21
+	 */
22
+	protected static $sigma = array(
23
+		array(  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15),
24
+		array( 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3),
25
+		array( 11,  8, 12,  0,  5,  2, 15, 13, 10, 14,  3,  6,  7,  1,  9,  4),
26
+		array(  7,  9,  3,  1, 13, 12, 11, 14,  2,  6,  5, 10,  4,  0, 15,  8),
27
+		array(  9,  0,  5,  7,  2,  4, 10, 15, 14,  1, 11, 12,  6,  8,  3, 13),
28
+		array(  2, 12,  6, 10,  0, 11,  8,  3,  4, 13,  7,  5, 15, 14,  1,  9),
29
+		array( 12,  5,  1, 15, 14, 13,  4, 10,  0,  7,  6,  3,  9,  2,  8, 11),
30
+		array( 13, 11,  7, 14, 12,  1,  3,  9,  5,  0, 15,  4,  8,  6,  2, 10),
31
+		array(  6, 15, 14,  9, 11,  3,  0,  8, 12,  2, 13,  7,  1,  4, 10,  5),
32
+		array( 10,  2,  8,  4,  7,  6,  1,  5, 15, 11,  9, 14,  3, 12, 13 , 0),
33
+		array(  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15),
34
+		array( 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3)
35
+	);
36
+
37
+	const BLOCKBYTES = 128;
38
+	const OUTBYTES   = 64;
39
+	const KEYBYTES   = 64;
40
+
41
+	/**
42
+	 * Turn two 32-bit integers into a fixed array representing a 64-bit integer.
43
+	 *
44
+	 * @internal You should not use this directly from another application
45
+	 *
46
+	 * @param int $high
47
+	 * @param int $low
48
+	 * @return SplFixedArray
49
+	 * @psalm-suppress MixedAssignment
50
+	 */
51
+	public static function new64($high, $low)
52
+	{
53
+		$i64 = new SplFixedArray(2);
54
+		$i64[0] = $high & 0xffffffff;
55
+		$i64[1] = $low & 0xffffffff;
56
+		return $i64;
57
+	}
58
+
59
+	/**
60
+	 * Convert an arbitrary number into an SplFixedArray of two 32-bit integers
61
+	 * that represents a 64-bit integer.
62
+	 *
63
+	 * @internal You should not use this directly from another application
64
+	 *
65
+	 * @param int $num
66
+	 * @return SplFixedArray
67
+	 */
68
+	protected static function to64($num)
69
+	{
70
+		list($hi, $lo) = self::numericTo64BitInteger($num);
71
+		return self::new64($hi, $lo);
72
+	}
73
+
74
+	/**
75
+	 * Adds two 64-bit integers together, returning their sum as a SplFixedArray
76
+	 * containing two 32-bit integers (representing a 64-bit integer).
77
+	 *
78
+	 * @internal You should not use this directly from another application
79
+	 *
80
+	 * @param SplFixedArray $x
81
+	 * @param SplFixedArray $y
82
+	 * @return SplFixedArray
83
+	 * @psalm-suppress MixedArgument
84
+	 * @psalm-suppress MixedAssignment
85
+	 * @psalm-suppress MixedOperand
86
+	 */
87
+	protected static function add64($x, $y)
88
+	{
89
+		$l = ($x[1] + $y[1]) & 0xffffffff;
90
+		return self::new64(
91
+			(int) ($x[0] + $y[0] + (
92
+				($l < $x[1]) ? 1 : 0
93
+			)),
94
+			(int) $l
95
+		);
96
+	}
97
+
98
+	/**
99
+	 * @internal You should not use this directly from another application
100
+	 *
101
+	 * @param SplFixedArray $x
102
+	 * @param SplFixedArray $y
103
+	 * @param SplFixedArray $z
104
+	 * @return SplFixedArray
105
+	 */
106
+	protected static function add364($x, $y, $z)
107
+	{
108
+		return self::add64($x, self::add64($y, $z));
109
+	}
110
+
111
+	/**
112
+	 * @internal You should not use this directly from another application
113
+	 *
114
+	 * @param SplFixedArray $x
115
+	 * @param SplFixedArray $y
116
+	 * @return SplFixedArray
117
+	 * @throws SodiumException
118
+	 * @throws TypeError
119
+	 */
120
+	protected static function xor64(SplFixedArray $x, SplFixedArray $y)
121
+	{
122
+		if (!is_numeric($x[0])) {
123
+			throw new SodiumException('x[0] is not an integer');
124
+		}
125
+		if (!is_numeric($x[1])) {
126
+			throw new SodiumException('x[1] is not an integer');
127
+		}
128
+		if (!is_numeric($y[0])) {
129
+			throw new SodiumException('y[0] is not an integer');
130
+		}
131
+		if (!is_numeric($y[1])) {
132
+			throw new SodiumException('y[1] is not an integer');
133
+		}
134
+		return self::new64(
135
+			(int) (($x[0] ^ $y[0]) & 0xffffffff),
136
+			(int) (($x[1] ^ $y[1]) & 0xffffffff)
137
+		);
138
+	}
139
+
140
+	/**
141
+	 * @internal You should not use this directly from another application
142
+	 *
143
+	 * @param SplFixedArray $x
144
+	 * @param int $c
145
+	 * @return SplFixedArray
146
+	 * @psalm-suppress MixedAssignment
147
+	 */
148
+	public static function rotr64($x, $c)
149
+	{
150
+		if ($c >= 64) {
151
+			$c %= 64;
152
+		}
153
+		if ($c >= 32) {
154
+			/** @var int $tmp */
155
+			$tmp = $x[0];
156
+			$x[0] = $x[1];
157
+			$x[1] = $tmp;
158
+			$c -= 32;
159
+		}
160
+		if ($c === 0) {
161
+			return $x;
162
+		}
163
+
164
+		$l0 = 0;
165
+		$c = 64 - $c;
166
+
167
+		if ($c < 32) {
168
+			/** @var int $h0 */
169
+			$h0 = ((int) ($x[0]) << $c) | (
170
+				(
171
+					(int) ($x[1]) & ((1 << $c) - 1)
172
+						<<
173
+					(32 - $c)
174
+				) >> (32 - $c)
175
+			);
176
+			/** @var int $l0 */
177
+			$l0 = (int) ($x[1]) << $c;
178
+		} else {
179
+			/** @var int $h0 */
180
+			$h0 = (int) ($x[1]) << ($c - 32);
181
+		}
182
+
183
+		$h1 = 0;
184
+		$c1 = 64 - $c;
185
+
186
+		if ($c1 < 32) {
187
+			/** @var int $h1 */
188
+			$h1 = (int) ($x[0]) >> $c1;
189
+			/** @var int $l1 */
190
+			$l1 = ((int) ($x[1]) >> $c1) | ((int) ($x[0]) & ((1 << $c1) - 1)) << (32 - $c1);
191
+		} else {
192
+			/** @var int $l1 */
193
+			$l1 = (int) ($x[0]) >> ($c1 - 32);
194
+		}
195
+
196
+		return self::new64($h0 | $h1, $l0 | $l1);
197
+	}
198
+
199
+	/**
200
+	 * @internal You should not use this directly from another application
201
+	 *
202
+	 * @param SplFixedArray $x
203
+	 * @return int
204
+	 * @psalm-suppress MixedOperand
205
+	 */
206
+	protected static function flatten64($x)
207
+	{
208
+		return (int) ($x[0] * 4294967296 + $x[1]);
209
+	}
210
+
211
+	/**
212
+	 * @internal You should not use this directly from another application
213
+	 *
214
+	 * @param SplFixedArray $x
215
+	 * @param int $i
216
+	 * @return SplFixedArray
217
+	 * @psalm-suppress MixedArgument
218
+	 * @psalm-suppress MixedArrayOffset
219
+	 */
220
+	protected static function load64(SplFixedArray $x, $i)
221
+	{
222
+		/** @var int $l */
223
+		$l = (int) ($x[$i])
224
+			 | ((int) ($x[$i+1]) << 8)
225
+			 | ((int) ($x[$i+2]) << 16)
226
+			 | ((int) ($x[$i+3]) << 24);
227
+		/** @var int $h */
228
+		$h = (int) ($x[$i+4])
229
+			 | ((int) ($x[$i+5]) << 8)
230
+			 | ((int) ($x[$i+6]) << 16)
231
+			 | ((int) ($x[$i+7]) << 24);
232
+		return self::new64($h, $l);
233
+	}
234
+
235
+	/**
236
+	 * @internal You should not use this directly from another application
237
+	 *
238
+	 * @param SplFixedArray $x
239
+	 * @param int $i
240
+	 * @param SplFixedArray $u
241
+	 * @return void
242
+	 * @psalm-suppress MixedAssignment
243
+	 */
244
+	protected static function store64(SplFixedArray $x, $i, SplFixedArray $u)
245
+	{
246
+		$maxLength = $x->getSize() - 1;
247
+		for ($j = 0; $j < 8; ++$j) {
248
+			/*
249 249
                [0, 1, 2, 3, 4, 5, 6, 7]
250 250
                     ... becomes ...
251 251
                [0, 0, 0, 0, 1, 1, 1, 1]
252 252
             */
253
-            /** @var int $uIdx */
254
-            $uIdx = ((7 - $j) & 4) >> 2;
255
-            $x[$i]   = ((int) ($u[$uIdx]) & 0xff);
256
-            if (++$i > $maxLength) {
257
-                return;
258
-            }
259
-            /** @psalm-suppress MixedOperand */
260
-            $u[$uIdx] >>= 8;
261
-        }
262
-    }
263
-
264
-    /**
265
-     * This just sets the $iv static variable.
266
-     *
267
-     * @internal You should not use this directly from another application
268
-     *
269
-     * @return void
270
-     */
271
-    public static function pseudoConstructor()
272
-    {
273
-        static $called = false;
274
-        if ($called) {
275
-            return;
276
-        }
277
-        self::$iv = new SplFixedArray(8);
278
-        self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908);
279
-        self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b);
280
-        self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b);
281
-        self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1);
282
-        self::$iv[4] = self::new64(0x510e527f, 0xade682d1);
283
-        self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f);
284
-        self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b);
285
-        self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179);
286
-
287
-        $called = true;
288
-    }
289
-
290
-    /**
291
-     * Returns a fresh BLAKE2 context.
292
-     *
293
-     * @internal You should not use this directly from another application
294
-     *
295
-     * @return SplFixedArray
296
-     * @psalm-suppress MixedAssignment
297
-     * @psalm-suppress MixedArrayAccess
298
-     * @psalm-suppress MixedArrayAssignment
299
-     */
300
-    protected static function context()
301
-    {
302
-        $ctx    = new SplFixedArray(6);
303
-        $ctx[0] = new SplFixedArray(8);   // h
304
-        $ctx[1] = new SplFixedArray(2);   // t
305
-        $ctx[2] = new SplFixedArray(2);   // f
306
-        $ctx[3] = new SplFixedArray(256); // buf
307
-        $ctx[4] = 0;                      // buflen
308
-        $ctx[5] = 0;                      // last_node (uint8_t)
309
-
310
-        for ($i = 8; $i--;) {
311
-            $ctx[0][$i] = self::$iv[$i];
312
-        }
313
-        for ($i = 256; $i--;) {
314
-            $ctx[3][$i] = 0;
315
-        }
316
-
317
-        $zero = self::new64(0, 0);
318
-        $ctx[1][0] = $zero;
319
-        $ctx[1][1] = $zero;
320
-        $ctx[2][0] = $zero;
321
-        $ctx[2][1] = $zero;
322
-
323
-        return $ctx;
324
-    }
325
-
326
-    /**
327
-     * @internal You should not use this directly from another application
328
-     *
329
-     * @param SplFixedArray $ctx
330
-     * @param SplFixedArray $buf
331
-     * @return void
332
-     * @throws SodiumException
333
-     * @throws TypeError
334
-     * @psalm-suppress MixedArgument
335
-     * @psalm-suppress MixedAssignment
336
-     * @psalm-suppress MixedArrayAccess
337
-     * @psalm-suppress MixedArrayAssignment
338
-     * @psalm-suppress MixedArrayOffset
339
-     */
340
-    protected static function compress(SplFixedArray $ctx, SplFixedArray $buf)
341
-    {
342
-        $m = new SplFixedArray(16);
343
-        $v = new SplFixedArray(16);
344
-
345
-        for ($i = 16; $i--;) {
346
-            $m[$i] = self::load64($buf, $i << 3);
347
-        }
348
-
349
-        for ($i = 8; $i--;) {
350
-            $v[$i] = $ctx[0][$i];
351
-        }
352
-
353
-        $v[ 8] = self::$iv[0];
354
-        $v[ 9] = self::$iv[1];
355
-        $v[10] = self::$iv[2];
356
-        $v[11] = self::$iv[3];
357
-
358
-        $v[12] = self::xor64($ctx[1][0], self::$iv[4]);
359
-        $v[13] = self::xor64($ctx[1][1], self::$iv[5]);
360
-        $v[14] = self::xor64($ctx[2][0], self::$iv[6]);
361
-        $v[15] = self::xor64($ctx[2][1], self::$iv[7]);
362
-
363
-        for ($r = 0; $r < 12; ++$r) {
364
-            $v = self::G($r, 0, 0, 4, 8, 12, $v, $m);
365
-            $v = self::G($r, 1, 1, 5, 9, 13, $v, $m);
366
-            $v = self::G($r, 2, 2, 6, 10, 14, $v, $m);
367
-            $v = self::G($r, 3, 3, 7, 11, 15, $v, $m);
368
-            $v = self::G($r, 4, 0, 5, 10, 15, $v, $m);
369
-            $v = self::G($r, 5, 1, 6, 11, 12, $v, $m);
370
-            $v = self::G($r, 6, 2, 7, 8, 13, $v, $m);
371
-            $v = self::G($r, 7, 3, 4, 9, 14, $v, $m);
372
-        }
373
-
374
-        for ($i = 8; $i--;) {
375
-            $ctx[0][$i] = self::xor64(
376
-                $ctx[0][$i], self::xor64($v[$i], $v[$i+8])
377
-            );
378
-        }
379
-    }
380
-
381
-    /**
382
-     * @internal You should not use this directly from another application
383
-     *
384
-     * @param int $r
385
-     * @param int $i
386
-     * @param int $a
387
-     * @param int $b
388
-     * @param int $c
389
-     * @param int $d
390
-     * @param SplFixedArray $v
391
-     * @param SplFixedArray $m
392
-     * @return SplFixedArray
393
-     * @throws SodiumException
394
-     * @throws TypeError
395
-     * @psalm-suppress MixedArgument
396
-     * @psalm-suppress MixedArrayOffset
397
-     */
398
-    public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m)
399
-    {
400
-        $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]);
401
-        $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32);
402
-        $v[$c] = self::add64($v[$c], $v[$d]);
403
-        $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24);
404
-        $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]);
405
-        $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16);
406
-        $v[$c] = self::add64($v[$c], $v[$d]);
407
-        $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63);
408
-        return $v;
409
-    }
410
-
411
-    /**
412
-     * @internal You should not use this directly from another application
413
-     *
414
-     * @param SplFixedArray $ctx
415
-     * @param int $inc
416
-     * @return void
417
-     * @throws SodiumException
418
-     * @psalm-suppress MixedArgument
419
-     * @psalm-suppress MixedArrayAccess
420
-     * @psalm-suppress MixedArrayAssignment
421
-     */
422
-    public static function increment_counter($ctx, $inc)
423
-    {
424
-        if ($inc < 0) {
425
-            throw new SodiumException('Increasing by a negative number makes no sense.');
426
-        }
427
-        $t = self::to64($inc);
428
-        # S->t is $ctx[1] in our implementation
429
-
430
-        # S->t[0] = ( uint64_t )( t >> 0 );
431
-        $ctx[1][0] = self::add64($ctx[1][0], $t);
432
-
433
-        # S->t[1] += ( S->t[0] < inc );
434
-        if (self::flatten64($ctx[1][0]) < $inc) {
435
-            $ctx[1][1] = self::add64($ctx[1][1], self::to64(1));
436
-        }
437
-    }
438
-
439
-    /**
440
-     * @internal You should not use this directly from another application
441
-     *
442
-     * @param SplFixedArray $ctx
443
-     * @param SplFixedArray $p
444
-     * @param int $plen
445
-     * @return void
446
-     * @throws SodiumException
447
-     * @throws TypeError
448
-     * @psalm-suppress MixedArgument
449
-     * @psalm-suppress MixedAssignment
450
-     * @psalm-suppress MixedArrayAccess
451
-     * @psalm-suppress MixedArrayAssignment
452
-     * @psalm-suppress MixedArrayOffset
453
-     * @psalm-suppress MixedOperand
454
-     */
455
-    public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen)
456
-    {
457
-        self::pseudoConstructor();
458
-
459
-        $offset = 0;
460
-        while ($plen > 0) {
461
-            $left = $ctx[4];
462
-            $fill = 256 - $left;
463
-
464
-            if ($plen > $fill) {
465
-                # memcpy( S->buf + left, in, fill ); /* Fill buffer */
466
-                for ($i = $fill; $i--;) {
467
-                    $ctx[3][$i + $left] = $p[$i + $offset];
468
-                }
469
-
470
-                # S->buflen += fill;
471
-                $ctx[4] += $fill;
472
-
473
-                # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
474
-                self::increment_counter($ctx, 128);
475
-
476
-                # blake2b_compress( S, S->buf ); /* Compress */
477
-                self::compress($ctx, $ctx[3]);
478
-
479
-                # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */
480
-                for ($i = 128; $i--;) {
481
-                    $ctx[3][$i] = $ctx[3][$i + 128];
482
-                }
483
-
484
-                # S->buflen -= BLAKE2B_BLOCKBYTES;
485
-                $ctx[4] -= 128;
486
-
487
-                # in += fill;
488
-                $offset += $fill;
489
-
490
-                # inlen -= fill;
491
-                $plen -= $fill;
492
-            } else {
493
-                for ($i = $plen; $i--;) {
494
-                    $ctx[3][$i + $left] = $p[$i + $offset];
495
-                }
496
-                $ctx[4] += $plen;
497
-                $offset += $plen;
498
-                $plen -= $plen;
499
-            }
500
-        }
501
-    }
502
-
503
-    /**
504
-     * @internal You should not use this directly from another application
505
-     *
506
-     * @param SplFixedArray $ctx
507
-     * @param SplFixedArray $out
508
-     * @return SplFixedArray
509
-     * @throws SodiumException
510
-     * @throws TypeError
511
-     * @psalm-suppress MixedArgument
512
-     * @psalm-suppress MixedAssignment
513
-     * @psalm-suppress MixedArrayAccess
514
-     * @psalm-suppress MixedArrayAssignment
515
-     * @psalm-suppress MixedArrayOffset
516
-     * @psalm-suppress MixedOperand
517
-     */
518
-    public static function finish(SplFixedArray $ctx, SplFixedArray $out)
519
-    {
520
-        self::pseudoConstructor();
521
-        if ($ctx[4] > 128) {
522
-            self::increment_counter($ctx, 128);
523
-            self::compress($ctx, $ctx[3]);
524
-            $ctx[4] -= 128;
525
-            if ($ctx[4] > 128) {
526
-                throw new SodiumException('Failed to assert that buflen <= 128 bytes');
527
-            }
528
-            for ($i = $ctx[4]; $i--;) {
529
-                $ctx[3][$i] = $ctx[3][$i + 128];
530
-            }
531
-        }
532
-
533
-        self::increment_counter($ctx, $ctx[4]);
534
-        $ctx[2][0] = self::new64(0xffffffff, 0xffffffff);
535
-
536
-        for ($i = 256 - $ctx[4]; $i--;) {
537
-            $ctx[3][$i+$ctx[4]] = 0;
538
-        }
539
-
540
-        self::compress($ctx, $ctx[3]);
541
-
542
-        $i = (int) (($out->getSize() - 1) / 8);
543
-        for (; $i >= 0; --$i) {
544
-            self::store64($out, $i << 3, $ctx[0][$i]);
545
-        }
546
-        return $out;
547
-    }
548
-
549
-    /**
550
-     * @internal You should not use this directly from another application
551
-     *
552
-     * @param SplFixedArray|null $key
553
-     * @param int $outlen
554
-     * @param SplFixedArray|null $salt
555
-     * @param SplFixedArray|null $personal
556
-     * @return SplFixedArray
557
-     * @throws SodiumException
558
-     * @throws TypeError
559
-     * @psalm-suppress MixedArgument
560
-     * @psalm-suppress MixedAssignment
561
-     * @psalm-suppress MixedArrayAccess
562
-     * @psalm-suppress MixedArrayAssignment
563
-     * @psalm-suppress MixedArrayOffset
564
-     */
565
-    public static function init(
566
-        $key = null,
567
-        $outlen = 64,
568
-        $salt = null,
569
-        $personal = null
570
-    ) {
571
-        self::pseudoConstructor();
572
-        $klen = 0;
573
-
574
-        if ($key !== null) {
575
-            if (count($key) > 64) {
576
-                throw new SodiumException('Invalid key size');
577
-            }
578
-            $klen = count($key);
579
-        }
580
-
581
-        if ($outlen > 64) {
582
-            throw new SodiumException('Invalid output size');
583
-        }
584
-
585
-        $ctx = self::context();
586
-
587
-        $p = new SplFixedArray(64);
588
-        // Zero our param buffer...
589
-        for ($i = 64; --$i;) {
590
-            $p[$i] = 0;
591
-        }
592
-
593
-        $p[0] = $outlen; // digest_length
594
-        $p[1] = $klen;   // key_length
595
-        $p[2] = 1;       // fanout
596
-        $p[3] = 1;       // depth
597
-
598
-        if ($salt instanceof SplFixedArray) {
599
-            // salt: [32] through [47]
600
-            for ($i = 0; $i < 16; ++$i) {
601
-                $p[32 + $i] = (int) $salt[$i];
602
-            }
603
-        }
604
-        if ($personal instanceof SplFixedArray) {
605
-            // personal: [48] through [63]
606
-            for ($i = 0; $i < 16; ++$i) {
607
-                $p[48 + $i] = (int) $personal[$i];
608
-            }
609
-        }
610
-
611
-        $ctx[0][0] = self::xor64(
612
-            $ctx[0][0],
613
-            self::load64($p, 0)
614
-        );
615
-        if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) {
616
-            // We need to do what blake2b_init_param() does:
617
-            for ($i = 1; $i < 8; ++$i) {
618
-                $ctx[0][$i] = self::xor64(
619
-                    $ctx[0][$i],
620
-                    self::load64($p, $i << 3)
621
-                );
622
-            }
623
-        }
624
-
625
-        if ($klen > 0 && $key instanceof SplFixedArray) {
626
-            $block = new SplFixedArray(128);
627
-            for ($i = 128; $i--;) {
628
-                $block[$i] = 0;
629
-            }
630
-            for ($i = $klen; $i--;) {
631
-                $block[$i] = $key[$i];
632
-            }
633
-            self::update($ctx, $block, 128);
634
-            $ctx[4] = 128;
635
-        }
636
-
637
-        return $ctx;
638
-    }
639
-
640
-    /**
641
-     * Convert a string into an SplFixedArray of integers
642
-     *
643
-     * @internal You should not use this directly from another application
644
-     *
645
-     * @param string $str
646
-     * @return SplFixedArray
647
-     * @psalm-suppress MixedArgumentTypeCoercion
648
-     */
649
-    public static function stringToSplFixedArray($str = '')
650
-    {
651
-        $values = unpack('C*', $str);
652
-        return SplFixedArray::fromArray(array_values($values));
653
-    }
654
-
655
-    /**
656
-     * Convert an SplFixedArray of integers into a string
657
-     *
658
-     * @internal You should not use this directly from another application
659
-     *
660
-     * @param SplFixedArray $a
661
-     * @return string
662
-     * @throws TypeError
663
-     */
664
-    public static function SplFixedArrayToString(SplFixedArray $a)
665
-    {
666
-        /**
667
-         * @var array<int, int|string> $arr
668
-         */
669
-        $arr = $a->toArray();
670
-        $c = $a->count();
671
-        array_unshift($arr, str_repeat('C', $c));
672
-        return (string) (call_user_func_array('pack', $arr));
673
-    }
674
-
675
-    /**
676
-     * @internal You should not use this directly from another application
677
-     *
678
-     * @param SplFixedArray $ctx
679
-     * @return string
680
-     * @throws TypeError
681
-     * @psalm-suppress MixedArgument
682
-     * @psalm-suppress MixedAssignment
683
-     * @psalm-suppress MixedArrayAccess
684
-     * @psalm-suppress MixedArrayAssignment
685
-     * @psalm-suppress MixedArrayOffset
686
-     * @psalm-suppress MixedMethodCall
687
-     */
688
-    public static function contextToString(SplFixedArray $ctx)
689
-    {
690
-        $str = '';
691
-        /** @var array<int, array<int, int>> $ctxA */
692
-        $ctxA = $ctx[0]->toArray();
693
-
694
-        # uint64_t h[8];
695
-        for ($i = 0; $i < 8; ++$i) {
696
-            $str .= self::store32_le($ctxA[$i][1]);
697
-            $str .= self::store32_le($ctxA[$i][0]);
698
-        }
699
-
700
-        # uint64_t t[2];
701
-        # uint64_t f[2];
702
-        for ($i = 1; $i < 3; ++$i) {
703
-            $ctxA = $ctx[$i]->toArray();
704
-            $str .= self::store32_le($ctxA[0][1]);
705
-            $str .= self::store32_le($ctxA[0][0]);
706
-            $str .= self::store32_le($ctxA[1][1]);
707
-            $str .= self::store32_le($ctxA[1][0]);
708
-        }
709
-
710
-        # uint8_t buf[2 * 128];
711
-        $str .= self::SplFixedArrayToString($ctx[3]);
712
-
713
-        /** @var int $ctx4 */
714
-        $ctx4 = (int) $ctx[4];
715
-
716
-        # size_t buflen;
717
-        $str .= implode('', array(
718
-            self::intToChr($ctx4 & 0xff),
719
-            self::intToChr(($ctx4 >> 8) & 0xff),
720
-            self::intToChr(($ctx4 >> 16) & 0xff),
721
-            self::intToChr(($ctx4 >> 24) & 0xff),
722
-            self::intToChr(($ctx4 >> 32) & 0xff),
723
-            self::intToChr(($ctx4 >> 40) & 0xff),
724
-            self::intToChr(($ctx4 >> 48) & 0xff),
725
-            self::intToChr(($ctx4 >> 56) & 0xff)
726
-        ));
727
-        # uint8_t last_node;
728
-        return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23);
729
-    }
730
-
731
-    /**
732
-     * Creates an SplFixedArray containing other SplFixedArray elements, from
733
-     * a string (compatible with \Sodium\crypto_generichash_{init, update, final})
734
-     *
735
-     * @internal You should not use this directly from another application
736
-     *
737
-     * @param string $string
738
-     * @return SplFixedArray
739
-     * @throws SodiumException
740
-     * @throws TypeError
741
-     * @psalm-suppress MixedArrayAssignment
742
-     */
743
-    public static function stringToContext($string)
744
-    {
745
-        $ctx = self::context();
746
-
747
-        # uint64_t h[8];
748
-        for ($i = 0; $i < 8; ++$i) {
749
-            $ctx[0][$i] = SplFixedArray::fromArray(
750
-                array(
751
-                    self::load_4(
752
-                        self::substr($string, (($i << 3) + 4), 4)
753
-                    ),
754
-                    self::load_4(
755
-                        self::substr($string, (($i << 3) + 0), 4)
756
-                    )
757
-                )
758
-            );
759
-        }
760
-
761
-        # uint64_t t[2];
762
-        # uint64_t f[2];
763
-        for ($i = 1; $i < 3; ++$i) {
764
-            $ctx[$i][1] = SplFixedArray::fromArray(
765
-                array(
766
-                    self::load_4(self::substr($string, 76 + (($i - 1) << 4), 4)),
767
-                    self::load_4(self::substr($string, 72 + (($i - 1) << 4), 4))
768
-                )
769
-            );
770
-            $ctx[$i][0] = SplFixedArray::fromArray(
771
-                array(
772
-                    self::load_4(self::substr($string, 68 + (($i - 1) << 4), 4)),
773
-                    self::load_4(self::substr($string, 64 + (($i - 1) << 4), 4))
774
-                )
775
-            );
776
-        }
777
-
778
-        # uint8_t buf[2 * 128];
779
-        $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256));
780
-
781
-        # uint8_t buf[2 * 128];
782
-        $int = 0;
783
-        for ($i = 0; $i < 8; ++$i) {
784
-            $int |= self::chrToInt($string[352 + $i]) << ($i << 3);
785
-        }
786
-        $ctx[4] = $int;
787
-
788
-        return $ctx;
789
-    }
253
+			/** @var int $uIdx */
254
+			$uIdx = ((7 - $j) & 4) >> 2;
255
+			$x[$i]   = ((int) ($u[$uIdx]) & 0xff);
256
+			if (++$i > $maxLength) {
257
+				return;
258
+			}
259
+			/** @psalm-suppress MixedOperand */
260
+			$u[$uIdx] >>= 8;
261
+		}
262
+	}
263
+
264
+	/**
265
+	 * This just sets the $iv static variable.
266
+	 *
267
+	 * @internal You should not use this directly from another application
268
+	 *
269
+	 * @return void
270
+	 */
271
+	public static function pseudoConstructor()
272
+	{
273
+		static $called = false;
274
+		if ($called) {
275
+			return;
276
+		}
277
+		self::$iv = new SplFixedArray(8);
278
+		self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908);
279
+		self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b);
280
+		self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b);
281
+		self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1);
282
+		self::$iv[4] = self::new64(0x510e527f, 0xade682d1);
283
+		self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f);
284
+		self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b);
285
+		self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179);
286
+
287
+		$called = true;
288
+	}
289
+
290
+	/**
291
+	 * Returns a fresh BLAKE2 context.
292
+	 *
293
+	 * @internal You should not use this directly from another application
294
+	 *
295
+	 * @return SplFixedArray
296
+	 * @psalm-suppress MixedAssignment
297
+	 * @psalm-suppress MixedArrayAccess
298
+	 * @psalm-suppress MixedArrayAssignment
299
+	 */
300
+	protected static function context()
301
+	{
302
+		$ctx    = new SplFixedArray(6);
303
+		$ctx[0] = new SplFixedArray(8);   // h
304
+		$ctx[1] = new SplFixedArray(2);   // t
305
+		$ctx[2] = new SplFixedArray(2);   // f
306
+		$ctx[3] = new SplFixedArray(256); // buf
307
+		$ctx[4] = 0;                      // buflen
308
+		$ctx[5] = 0;                      // last_node (uint8_t)
309
+
310
+		for ($i = 8; $i--;) {
311
+			$ctx[0][$i] = self::$iv[$i];
312
+		}
313
+		for ($i = 256; $i--;) {
314
+			$ctx[3][$i] = 0;
315
+		}
316
+
317
+		$zero = self::new64(0, 0);
318
+		$ctx[1][0] = $zero;
319
+		$ctx[1][1] = $zero;
320
+		$ctx[2][0] = $zero;
321
+		$ctx[2][1] = $zero;
322
+
323
+		return $ctx;
324
+	}
325
+
326
+	/**
327
+	 * @internal You should not use this directly from another application
328
+	 *
329
+	 * @param SplFixedArray $ctx
330
+	 * @param SplFixedArray $buf
331
+	 * @return void
332
+	 * @throws SodiumException
333
+	 * @throws TypeError
334
+	 * @psalm-suppress MixedArgument
335
+	 * @psalm-suppress MixedAssignment
336
+	 * @psalm-suppress MixedArrayAccess
337
+	 * @psalm-suppress MixedArrayAssignment
338
+	 * @psalm-suppress MixedArrayOffset
339
+	 */
340
+	protected static function compress(SplFixedArray $ctx, SplFixedArray $buf)
341
+	{
342
+		$m = new SplFixedArray(16);
343
+		$v = new SplFixedArray(16);
344
+
345
+		for ($i = 16; $i--;) {
346
+			$m[$i] = self::load64($buf, $i << 3);
347
+		}
348
+
349
+		for ($i = 8; $i--;) {
350
+			$v[$i] = $ctx[0][$i];
351
+		}
352
+
353
+		$v[ 8] = self::$iv[0];
354
+		$v[ 9] = self::$iv[1];
355
+		$v[10] = self::$iv[2];
356
+		$v[11] = self::$iv[3];
357
+
358
+		$v[12] = self::xor64($ctx[1][0], self::$iv[4]);
359
+		$v[13] = self::xor64($ctx[1][1], self::$iv[5]);
360
+		$v[14] = self::xor64($ctx[2][0], self::$iv[6]);
361
+		$v[15] = self::xor64($ctx[2][1], self::$iv[7]);
362
+
363
+		for ($r = 0; $r < 12; ++$r) {
364
+			$v = self::G($r, 0, 0, 4, 8, 12, $v, $m);
365
+			$v = self::G($r, 1, 1, 5, 9, 13, $v, $m);
366
+			$v = self::G($r, 2, 2, 6, 10, 14, $v, $m);
367
+			$v = self::G($r, 3, 3, 7, 11, 15, $v, $m);
368
+			$v = self::G($r, 4, 0, 5, 10, 15, $v, $m);
369
+			$v = self::G($r, 5, 1, 6, 11, 12, $v, $m);
370
+			$v = self::G($r, 6, 2, 7, 8, 13, $v, $m);
371
+			$v = self::G($r, 7, 3, 4, 9, 14, $v, $m);
372
+		}
373
+
374
+		for ($i = 8; $i--;) {
375
+			$ctx[0][$i] = self::xor64(
376
+				$ctx[0][$i], self::xor64($v[$i], $v[$i+8])
377
+			);
378
+		}
379
+	}
380
+
381
+	/**
382
+	 * @internal You should not use this directly from another application
383
+	 *
384
+	 * @param int $r
385
+	 * @param int $i
386
+	 * @param int $a
387
+	 * @param int $b
388
+	 * @param int $c
389
+	 * @param int $d
390
+	 * @param SplFixedArray $v
391
+	 * @param SplFixedArray $m
392
+	 * @return SplFixedArray
393
+	 * @throws SodiumException
394
+	 * @throws TypeError
395
+	 * @psalm-suppress MixedArgument
396
+	 * @psalm-suppress MixedArrayOffset
397
+	 */
398
+	public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m)
399
+	{
400
+		$v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]);
401
+		$v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32);
402
+		$v[$c] = self::add64($v[$c], $v[$d]);
403
+		$v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24);
404
+		$v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]);
405
+		$v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16);
406
+		$v[$c] = self::add64($v[$c], $v[$d]);
407
+		$v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63);
408
+		return $v;
409
+	}
410
+
411
+	/**
412
+	 * @internal You should not use this directly from another application
413
+	 *
414
+	 * @param SplFixedArray $ctx
415
+	 * @param int $inc
416
+	 * @return void
417
+	 * @throws SodiumException
418
+	 * @psalm-suppress MixedArgument
419
+	 * @psalm-suppress MixedArrayAccess
420
+	 * @psalm-suppress MixedArrayAssignment
421
+	 */
422
+	public static function increment_counter($ctx, $inc)
423
+	{
424
+		if ($inc < 0) {
425
+			throw new SodiumException('Increasing by a negative number makes no sense.');
426
+		}
427
+		$t = self::to64($inc);
428
+		# S->t is $ctx[1] in our implementation
429
+
430
+		# S->t[0] = ( uint64_t )( t >> 0 );
431
+		$ctx[1][0] = self::add64($ctx[1][0], $t);
432
+
433
+		# S->t[1] += ( S->t[0] < inc );
434
+		if (self::flatten64($ctx[1][0]) < $inc) {
435
+			$ctx[1][1] = self::add64($ctx[1][1], self::to64(1));
436
+		}
437
+	}
438
+
439
+	/**
440
+	 * @internal You should not use this directly from another application
441
+	 *
442
+	 * @param SplFixedArray $ctx
443
+	 * @param SplFixedArray $p
444
+	 * @param int $plen
445
+	 * @return void
446
+	 * @throws SodiumException
447
+	 * @throws TypeError
448
+	 * @psalm-suppress MixedArgument
449
+	 * @psalm-suppress MixedAssignment
450
+	 * @psalm-suppress MixedArrayAccess
451
+	 * @psalm-suppress MixedArrayAssignment
452
+	 * @psalm-suppress MixedArrayOffset
453
+	 * @psalm-suppress MixedOperand
454
+	 */
455
+	public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen)
456
+	{
457
+		self::pseudoConstructor();
458
+
459
+		$offset = 0;
460
+		while ($plen > 0) {
461
+			$left = $ctx[4];
462
+			$fill = 256 - $left;
463
+
464
+			if ($plen > $fill) {
465
+				# memcpy( S->buf + left, in, fill ); /* Fill buffer */
466
+				for ($i = $fill; $i--;) {
467
+					$ctx[3][$i + $left] = $p[$i + $offset];
468
+				}
469
+
470
+				# S->buflen += fill;
471
+				$ctx[4] += $fill;
472
+
473
+				# blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
474
+				self::increment_counter($ctx, 128);
475
+
476
+				# blake2b_compress( S, S->buf ); /* Compress */
477
+				self::compress($ctx, $ctx[3]);
478
+
479
+				# memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */
480
+				for ($i = 128; $i--;) {
481
+					$ctx[3][$i] = $ctx[3][$i + 128];
482
+				}
483
+
484
+				# S->buflen -= BLAKE2B_BLOCKBYTES;
485
+				$ctx[4] -= 128;
486
+
487
+				# in += fill;
488
+				$offset += $fill;
489
+
490
+				# inlen -= fill;
491
+				$plen -= $fill;
492
+			} else {
493
+				for ($i = $plen; $i--;) {
494
+					$ctx[3][$i + $left] = $p[$i + $offset];
495
+				}
496
+				$ctx[4] += $plen;
497
+				$offset += $plen;
498
+				$plen -= $plen;
499
+			}
500
+		}
501
+	}
502
+
503
+	/**
504
+	 * @internal You should not use this directly from another application
505
+	 *
506
+	 * @param SplFixedArray $ctx
507
+	 * @param SplFixedArray $out
508
+	 * @return SplFixedArray
509
+	 * @throws SodiumException
510
+	 * @throws TypeError
511
+	 * @psalm-suppress MixedArgument
512
+	 * @psalm-suppress MixedAssignment
513
+	 * @psalm-suppress MixedArrayAccess
514
+	 * @psalm-suppress MixedArrayAssignment
515
+	 * @psalm-suppress MixedArrayOffset
516
+	 * @psalm-suppress MixedOperand
517
+	 */
518
+	public static function finish(SplFixedArray $ctx, SplFixedArray $out)
519
+	{
520
+		self::pseudoConstructor();
521
+		if ($ctx[4] > 128) {
522
+			self::increment_counter($ctx, 128);
523
+			self::compress($ctx, $ctx[3]);
524
+			$ctx[4] -= 128;
525
+			if ($ctx[4] > 128) {
526
+				throw new SodiumException('Failed to assert that buflen <= 128 bytes');
527
+			}
528
+			for ($i = $ctx[4]; $i--;) {
529
+				$ctx[3][$i] = $ctx[3][$i + 128];
530
+			}
531
+		}
532
+
533
+		self::increment_counter($ctx, $ctx[4]);
534
+		$ctx[2][0] = self::new64(0xffffffff, 0xffffffff);
535
+
536
+		for ($i = 256 - $ctx[4]; $i--;) {
537
+			$ctx[3][$i+$ctx[4]] = 0;
538
+		}
539
+
540
+		self::compress($ctx, $ctx[3]);
541
+
542
+		$i = (int) (($out->getSize() - 1) / 8);
543
+		for (; $i >= 0; --$i) {
544
+			self::store64($out, $i << 3, $ctx[0][$i]);
545
+		}
546
+		return $out;
547
+	}
548
+
549
+	/**
550
+	 * @internal You should not use this directly from another application
551
+	 *
552
+	 * @param SplFixedArray|null $key
553
+	 * @param int $outlen
554
+	 * @param SplFixedArray|null $salt
555
+	 * @param SplFixedArray|null $personal
556
+	 * @return SplFixedArray
557
+	 * @throws SodiumException
558
+	 * @throws TypeError
559
+	 * @psalm-suppress MixedArgument
560
+	 * @psalm-suppress MixedAssignment
561
+	 * @psalm-suppress MixedArrayAccess
562
+	 * @psalm-suppress MixedArrayAssignment
563
+	 * @psalm-suppress MixedArrayOffset
564
+	 */
565
+	public static function init(
566
+		$key = null,
567
+		$outlen = 64,
568
+		$salt = null,
569
+		$personal = null
570
+	) {
571
+		self::pseudoConstructor();
572
+		$klen = 0;
573
+
574
+		if ($key !== null) {
575
+			if (count($key) > 64) {
576
+				throw new SodiumException('Invalid key size');
577
+			}
578
+			$klen = count($key);
579
+		}
580
+
581
+		if ($outlen > 64) {
582
+			throw new SodiumException('Invalid output size');
583
+		}
584
+
585
+		$ctx = self::context();
586
+
587
+		$p = new SplFixedArray(64);
588
+		// Zero our param buffer...
589
+		for ($i = 64; --$i;) {
590
+			$p[$i] = 0;
591
+		}
592
+
593
+		$p[0] = $outlen; // digest_length
594
+		$p[1] = $klen;   // key_length
595
+		$p[2] = 1;       // fanout
596
+		$p[3] = 1;       // depth
597
+
598
+		if ($salt instanceof SplFixedArray) {
599
+			// salt: [32] through [47]
600
+			for ($i = 0; $i < 16; ++$i) {
601
+				$p[32 + $i] = (int) $salt[$i];
602
+			}
603
+		}
604
+		if ($personal instanceof SplFixedArray) {
605
+			// personal: [48] through [63]
606
+			for ($i = 0; $i < 16; ++$i) {
607
+				$p[48 + $i] = (int) $personal[$i];
608
+			}
609
+		}
610
+
611
+		$ctx[0][0] = self::xor64(
612
+			$ctx[0][0],
613
+			self::load64($p, 0)
614
+		);
615
+		if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) {
616
+			// We need to do what blake2b_init_param() does:
617
+			for ($i = 1; $i < 8; ++$i) {
618
+				$ctx[0][$i] = self::xor64(
619
+					$ctx[0][$i],
620
+					self::load64($p, $i << 3)
621
+				);
622
+			}
623
+		}
624
+
625
+		if ($klen > 0 && $key instanceof SplFixedArray) {
626
+			$block = new SplFixedArray(128);
627
+			for ($i = 128; $i--;) {
628
+				$block[$i] = 0;
629
+			}
630
+			for ($i = $klen; $i--;) {
631
+				$block[$i] = $key[$i];
632
+			}
633
+			self::update($ctx, $block, 128);
634
+			$ctx[4] = 128;
635
+		}
636
+
637
+		return $ctx;
638
+	}
639
+
640
+	/**
641
+	 * Convert a string into an SplFixedArray of integers
642
+	 *
643
+	 * @internal You should not use this directly from another application
644
+	 *
645
+	 * @param string $str
646
+	 * @return SplFixedArray
647
+	 * @psalm-suppress MixedArgumentTypeCoercion
648
+	 */
649
+	public static function stringToSplFixedArray($str = '')
650
+	{
651
+		$values = unpack('C*', $str);
652
+		return SplFixedArray::fromArray(array_values($values));
653
+	}
654
+
655
+	/**
656
+	 * Convert an SplFixedArray of integers into a string
657
+	 *
658
+	 * @internal You should not use this directly from another application
659
+	 *
660
+	 * @param SplFixedArray $a
661
+	 * @return string
662
+	 * @throws TypeError
663
+	 */
664
+	public static function SplFixedArrayToString(SplFixedArray $a)
665
+	{
666
+		/**
667
+		 * @var array<int, int|string> $arr
668
+		 */
669
+		$arr = $a->toArray();
670
+		$c = $a->count();
671
+		array_unshift($arr, str_repeat('C', $c));
672
+		return (string) (call_user_func_array('pack', $arr));
673
+	}
674
+
675
+	/**
676
+	 * @internal You should not use this directly from another application
677
+	 *
678
+	 * @param SplFixedArray $ctx
679
+	 * @return string
680
+	 * @throws TypeError
681
+	 * @psalm-suppress MixedArgument
682
+	 * @psalm-suppress MixedAssignment
683
+	 * @psalm-suppress MixedArrayAccess
684
+	 * @psalm-suppress MixedArrayAssignment
685
+	 * @psalm-suppress MixedArrayOffset
686
+	 * @psalm-suppress MixedMethodCall
687
+	 */
688
+	public static function contextToString(SplFixedArray $ctx)
689
+	{
690
+		$str = '';
691
+		/** @var array<int, array<int, int>> $ctxA */
692
+		$ctxA = $ctx[0]->toArray();
693
+
694
+		# uint64_t h[8];
695
+		for ($i = 0; $i < 8; ++$i) {
696
+			$str .= self::store32_le($ctxA[$i][1]);
697
+			$str .= self::store32_le($ctxA[$i][0]);
698
+		}
699
+
700
+		# uint64_t t[2];
701
+		# uint64_t f[2];
702
+		for ($i = 1; $i < 3; ++$i) {
703
+			$ctxA = $ctx[$i]->toArray();
704
+			$str .= self::store32_le($ctxA[0][1]);
705
+			$str .= self::store32_le($ctxA[0][0]);
706
+			$str .= self::store32_le($ctxA[1][1]);
707
+			$str .= self::store32_le($ctxA[1][0]);
708
+		}
709
+
710
+		# uint8_t buf[2 * 128];
711
+		$str .= self::SplFixedArrayToString($ctx[3]);
712
+
713
+		/** @var int $ctx4 */
714
+		$ctx4 = (int) $ctx[4];
715
+
716
+		# size_t buflen;
717
+		$str .= implode('', array(
718
+			self::intToChr($ctx4 & 0xff),
719
+			self::intToChr(($ctx4 >> 8) & 0xff),
720
+			self::intToChr(($ctx4 >> 16) & 0xff),
721
+			self::intToChr(($ctx4 >> 24) & 0xff),
722
+			self::intToChr(($ctx4 >> 32) & 0xff),
723
+			self::intToChr(($ctx4 >> 40) & 0xff),
724
+			self::intToChr(($ctx4 >> 48) & 0xff),
725
+			self::intToChr(($ctx4 >> 56) & 0xff)
726
+		));
727
+		# uint8_t last_node;
728
+		return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23);
729
+	}
730
+
731
+	/**
732
+	 * Creates an SplFixedArray containing other SplFixedArray elements, from
733
+	 * a string (compatible with \Sodium\crypto_generichash_{init, update, final})
734
+	 *
735
+	 * @internal You should not use this directly from another application
736
+	 *
737
+	 * @param string $string
738
+	 * @return SplFixedArray
739
+	 * @throws SodiumException
740
+	 * @throws TypeError
741
+	 * @psalm-suppress MixedArrayAssignment
742
+	 */
743
+	public static function stringToContext($string)
744
+	{
745
+		$ctx = self::context();
746
+
747
+		# uint64_t h[8];
748
+		for ($i = 0; $i < 8; ++$i) {
749
+			$ctx[0][$i] = SplFixedArray::fromArray(
750
+				array(
751
+					self::load_4(
752
+						self::substr($string, (($i << 3) + 4), 4)
753
+					),
754
+					self::load_4(
755
+						self::substr($string, (($i << 3) + 0), 4)
756
+					)
757
+				)
758
+			);
759
+		}
760
+
761
+		# uint64_t t[2];
762
+		# uint64_t f[2];
763
+		for ($i = 1; $i < 3; ++$i) {
764
+			$ctx[$i][1] = SplFixedArray::fromArray(
765
+				array(
766
+					self::load_4(self::substr($string, 76 + (($i - 1) << 4), 4)),
767
+					self::load_4(self::substr($string, 72 + (($i - 1) << 4), 4))
768
+				)
769
+			);
770
+			$ctx[$i][0] = SplFixedArray::fromArray(
771
+				array(
772
+					self::load_4(self::substr($string, 68 + (($i - 1) << 4), 4)),
773
+					self::load_4(self::substr($string, 64 + (($i - 1) << 4), 4))
774
+				)
775
+			);
776
+		}
777
+
778
+		# uint8_t buf[2 * 128];
779
+		$ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256));
780
+
781
+		# uint8_t buf[2 * 128];
782
+		$int = 0;
783
+		for ($i = 0; $i < 8; ++$i) {
784
+			$int |= self::chrToInt($string[352 + $i]) << ($i << 3);
785
+		}
786
+		$ctx[4] = $int;
787
+
788
+		return $ctx;
789
+	}
790 790
 }
Please login to merge, or discard this patch.
Spacing   +257 added lines, -257 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-if (class_exists('ParagonIE_Sodium_Core_BLAKE2b', false)) {
3
+if ( class_exists( 'ParagonIE_Sodium_Core_BLAKE2b', false ) ) {
4 4
     return;
5 5
 }
6 6
 
@@ -20,18 +20,18 @@  discard block
 block discarded – undo
20 20
      * @var array<int, array<int, int>>
21 21
      */
22 22
     protected static $sigma = array(
23
-        array(  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15),
24
-        array( 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3),
25
-        array( 11,  8, 12,  0,  5,  2, 15, 13, 10, 14,  3,  6,  7,  1,  9,  4),
26
-        array(  7,  9,  3,  1, 13, 12, 11, 14,  2,  6,  5, 10,  4,  0, 15,  8),
27
-        array(  9,  0,  5,  7,  2,  4, 10, 15, 14,  1, 11, 12,  6,  8,  3, 13),
28
-        array(  2, 12,  6, 10,  0, 11,  8,  3,  4, 13,  7,  5, 15, 14,  1,  9),
29
-        array( 12,  5,  1, 15, 14, 13,  4, 10,  0,  7,  6,  3,  9,  2,  8, 11),
30
-        array( 13, 11,  7, 14, 12,  1,  3,  9,  5,  0, 15,  4,  8,  6,  2, 10),
31
-        array(  6, 15, 14,  9, 11,  3,  0,  8, 12,  2, 13,  7,  1,  4, 10,  5),
32
-        array( 10,  2,  8,  4,  7,  6,  1,  5, 15, 11,  9, 14,  3, 12, 13 , 0),
33
-        array(  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15),
34
-        array( 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3)
23
+        array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ),
24
+        array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 ),
25
+        array( 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 ),
26
+        array( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 ),
27
+        array( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 ),
28
+        array( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 ),
29
+        array( 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 ),
30
+        array( 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 ),
31
+        array( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 ),
32
+        array( 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 ),
33
+        array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ),
34
+        array( 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 )
35 35
     );
36 36
 
37 37
     const BLOCKBYTES = 128;
@@ -48,11 +48,11 @@  discard block
 block discarded – undo
48 48
      * @return SplFixedArray
49 49
      * @psalm-suppress MixedAssignment
50 50
      */
51
-    public static function new64($high, $low)
51
+    public static function new64( $high, $low )
52 52
     {
53
-        $i64 = new SplFixedArray(2);
54
-        $i64[0] = $high & 0xffffffff;
55
-        $i64[1] = $low & 0xffffffff;
53
+        $i64 = new SplFixedArray( 2 );
54
+        $i64[ 0 ] = $high & 0xffffffff;
55
+        $i64[ 1 ] = $low & 0xffffffff;
56 56
         return $i64;
57 57
     }
58 58
 
@@ -65,10 +65,10 @@  discard block
 block discarded – undo
65 65
      * @param int $num
66 66
      * @return SplFixedArray
67 67
      */
68
-    protected static function to64($num)
68
+    protected static function to64( $num )
69 69
     {
70
-        list($hi, $lo) = self::numericTo64BitInteger($num);
71
-        return self::new64($hi, $lo);
70
+        list( $hi, $lo ) = self::numericTo64BitInteger( $num );
71
+        return self::new64( $hi, $lo );
72 72
     }
73 73
 
74 74
     /**
@@ -84,14 +84,14 @@  discard block
 block discarded – undo
84 84
      * @psalm-suppress MixedAssignment
85 85
      * @psalm-suppress MixedOperand
86 86
      */
87
-    protected static function add64($x, $y)
87
+    protected static function add64( $x, $y )
88 88
     {
89
-        $l = ($x[1] + $y[1]) & 0xffffffff;
89
+        $l = ( $x[ 1 ] + $y[ 1 ] ) & 0xffffffff;
90 90
         return self::new64(
91
-            (int) ($x[0] + $y[0] + (
92
-                ($l < $x[1]) ? 1 : 0
93
-            )),
94
-            (int) $l
91
+            (int)( $x[ 0 ] + $y[ 0 ] + (
92
+                ( $l < $x[ 1 ] ) ? 1 : 0
93
+            ) ),
94
+            (int)$l
95 95
         );
96 96
     }
97 97
 
@@ -103,9 +103,9 @@  discard block
 block discarded – undo
103 103
      * @param SplFixedArray $z
104 104
      * @return SplFixedArray
105 105
      */
106
-    protected static function add364($x, $y, $z)
106
+    protected static function add364( $x, $y, $z )
107 107
     {
108
-        return self::add64($x, self::add64($y, $z));
108
+        return self::add64( $x, self::add64( $y, $z ) );
109 109
     }
110 110
 
111 111
     /**
@@ -117,23 +117,23 @@  discard block
 block discarded – undo
117 117
      * @throws SodiumException
118 118
      * @throws TypeError
119 119
      */
120
-    protected static function xor64(SplFixedArray $x, SplFixedArray $y)
120
+    protected static function xor64( SplFixedArray $x, SplFixedArray $y )
121 121
     {
122
-        if (!is_numeric($x[0])) {
123
-            throw new SodiumException('x[0] is not an integer');
122
+        if ( ! is_numeric( $x[ 0 ] ) ) {
123
+            throw new SodiumException( 'x[0] is not an integer' );
124 124
         }
125
-        if (!is_numeric($x[1])) {
126
-            throw new SodiumException('x[1] is not an integer');
125
+        if ( ! is_numeric( $x[ 1 ] ) ) {
126
+            throw new SodiumException( 'x[1] is not an integer' );
127 127
         }
128
-        if (!is_numeric($y[0])) {
129
-            throw new SodiumException('y[0] is not an integer');
128
+        if ( ! is_numeric( $y[ 0 ] ) ) {
129
+            throw new SodiumException( 'y[0] is not an integer' );
130 130
         }
131
-        if (!is_numeric($y[1])) {
132
-            throw new SodiumException('y[1] is not an integer');
131
+        if ( ! is_numeric( $y[ 1 ] ) ) {
132
+            throw new SodiumException( 'y[1] is not an integer' );
133 133
         }
134 134
         return self::new64(
135
-            (int) (($x[0] ^ $y[0]) & 0xffffffff),
136
-            (int) (($x[1] ^ $y[1]) & 0xffffffff)
135
+            (int)( ( $x[ 0 ] ^ $y[ 0 ] ) & 0xffffffff ),
136
+            (int)( ( $x[ 1 ] ^ $y[ 1 ] ) & 0xffffffff )
137 137
         );
138 138
     }
139 139
 
@@ -145,55 +145,55 @@  discard block
 block discarded – undo
145 145
      * @return SplFixedArray
146 146
      * @psalm-suppress MixedAssignment
147 147
      */
148
-    public static function rotr64($x, $c)
148
+    public static function rotr64( $x, $c )
149 149
     {
150
-        if ($c >= 64) {
150
+        if ( $c >= 64 ) {
151 151
             $c %= 64;
152 152
         }
153
-        if ($c >= 32) {
153
+        if ( $c >= 32 ) {
154 154
             /** @var int $tmp */
155
-            $tmp = $x[0];
156
-            $x[0] = $x[1];
157
-            $x[1] = $tmp;
155
+            $tmp = $x[ 0 ];
156
+            $x[ 0 ] = $x[ 1 ];
157
+            $x[ 1 ] = $tmp;
158 158
             $c -= 32;
159 159
         }
160
-        if ($c === 0) {
160
+        if ( $c === 0 ) {
161 161
             return $x;
162 162
         }
163 163
 
164 164
         $l0 = 0;
165 165
         $c = 64 - $c;
166 166
 
167
-        if ($c < 32) {
167
+        if ( $c < 32 ) {
168 168
             /** @var int $h0 */
169
-            $h0 = ((int) ($x[0]) << $c) | (
169
+            $h0 = ( (int)( $x[ 0 ] ) << $c ) | (
170 170
                 (
171
-                    (int) ($x[1]) & ((1 << $c) - 1)
171
+                    (int)( $x[ 1 ] ) & ( ( 1 << $c ) - 1 )
172 172
                         <<
173
-                    (32 - $c)
174
-                ) >> (32 - $c)
173
+                    ( 32 - $c )
174
+                ) >> ( 32 - $c )
175 175
             );
176 176
             /** @var int $l0 */
177
-            $l0 = (int) ($x[1]) << $c;
177
+            $l0 = (int)( $x[ 1 ] ) << $c;
178 178
         } else {
179 179
             /** @var int $h0 */
180
-            $h0 = (int) ($x[1]) << ($c - 32);
180
+            $h0 = (int)( $x[ 1 ] ) << ( $c - 32 );
181 181
         }
182 182
 
183 183
         $h1 = 0;
184 184
         $c1 = 64 - $c;
185 185
 
186
-        if ($c1 < 32) {
186
+        if ( $c1 < 32 ) {
187 187
             /** @var int $h1 */
188
-            $h1 = (int) ($x[0]) >> $c1;
188
+            $h1 = (int)( $x[ 0 ] ) >> $c1;
189 189
             /** @var int $l1 */
190
-            $l1 = ((int) ($x[1]) >> $c1) | ((int) ($x[0]) & ((1 << $c1) - 1)) << (32 - $c1);
190
+            $l1 = ( (int)( $x[ 1 ] ) >> $c1 ) | ( (int)( $x[ 0 ] ) & ( ( 1 << $c1 ) - 1 ) ) << ( 32 - $c1 );
191 191
         } else {
192 192
             /** @var int $l1 */
193
-            $l1 = (int) ($x[0]) >> ($c1 - 32);
193
+            $l1 = (int)( $x[ 0 ] ) >> ( $c1 - 32 );
194 194
         }
195 195
 
196
-        return self::new64($h0 | $h1, $l0 | $l1);
196
+        return self::new64( $h0 | $h1, $l0 | $l1 );
197 197
     }
198 198
 
199 199
     /**
@@ -203,9 +203,9 @@  discard block
 block discarded – undo
203 203
      * @return int
204 204
      * @psalm-suppress MixedOperand
205 205
      */
206
-    protected static function flatten64($x)
206
+    protected static function flatten64( $x )
207 207
     {
208
-        return (int) ($x[0] * 4294967296 + $x[1]);
208
+        return (int)( $x[ 0 ] * 4294967296 + $x[ 1 ] );
209 209
     }
210 210
 
211 211
     /**
@@ -217,19 +217,19 @@  discard block
 block discarded – undo
217 217
      * @psalm-suppress MixedArgument
218 218
      * @psalm-suppress MixedArrayOffset
219 219
      */
220
-    protected static function load64(SplFixedArray $x, $i)
220
+    protected static function load64( SplFixedArray $x, $i )
221 221
     {
222 222
         /** @var int $l */
223
-        $l = (int) ($x[$i])
224
-             | ((int) ($x[$i+1]) << 8)
225
-             | ((int) ($x[$i+2]) << 16)
226
-             | ((int) ($x[$i+3]) << 24);
223
+        $l = (int)( $x[ $i ] )
224
+             | ( (int)( $x[ $i + 1 ] ) << 8 )
225
+             | ( (int)( $x[ $i + 2 ] ) << 16 )
226
+             | ( (int)( $x[ $i + 3 ] ) << 24 );
227 227
         /** @var int $h */
228
-        $h = (int) ($x[$i+4])
229
-             | ((int) ($x[$i+5]) << 8)
230
-             | ((int) ($x[$i+6]) << 16)
231
-             | ((int) ($x[$i+7]) << 24);
232
-        return self::new64($h, $l);
228
+        $h = (int)( $x[ $i + 4 ] )
229
+             | ( (int)( $x[ $i + 5 ] ) << 8 )
230
+             | ( (int)( $x[ $i + 6 ] ) << 16 )
231
+             | ( (int)( $x[ $i + 7 ] ) << 24 );
232
+        return self::new64( $h, $l );
233 233
     }
234 234
 
235 235
     /**
@@ -241,23 +241,23 @@  discard block
 block discarded – undo
241 241
      * @return void
242 242
      * @psalm-suppress MixedAssignment
243 243
      */
244
-    protected static function store64(SplFixedArray $x, $i, SplFixedArray $u)
244
+    protected static function store64( SplFixedArray $x, $i, SplFixedArray $u )
245 245
     {
246 246
         $maxLength = $x->getSize() - 1;
247
-        for ($j = 0; $j < 8; ++$j) {
247
+        for ( $j = 0; $j < 8; ++$j ) {
248 248
             /*
249 249
                [0, 1, 2, 3, 4, 5, 6, 7]
250 250
                     ... becomes ...
251 251
                [0, 0, 0, 0, 1, 1, 1, 1]
252 252
             */
253 253
             /** @var int $uIdx */
254
-            $uIdx = ((7 - $j) & 4) >> 2;
255
-            $x[$i]   = ((int) ($u[$uIdx]) & 0xff);
254
+            $uIdx = ( ( 7 - $j ) & 4 ) >> 2;
255
+            $x[ $i ] = ( (int)( $u[ $uIdx ] ) & 0xff );
256 256
             if (++$i > $maxLength) {
257 257
                 return;
258 258
             }
259 259
             /** @psalm-suppress MixedOperand */
260
-            $u[$uIdx] >>= 8;
260
+            $u[ $uIdx ] >>= 8;
261 261
         }
262 262
     }
263 263
 
@@ -271,18 +271,18 @@  discard block
 block discarded – undo
271 271
     public static function pseudoConstructor()
272 272
     {
273 273
         static $called = false;
274
-        if ($called) {
274
+        if ( $called ) {
275 275
             return;
276 276
         }
277
-        self::$iv = new SplFixedArray(8);
278
-        self::$iv[0] = self::new64(0x6a09e667, 0xf3bcc908);
279
-        self::$iv[1] = self::new64(0xbb67ae85, 0x84caa73b);
280
-        self::$iv[2] = self::new64(0x3c6ef372, 0xfe94f82b);
281
-        self::$iv[3] = self::new64(0xa54ff53a, 0x5f1d36f1);
282
-        self::$iv[4] = self::new64(0x510e527f, 0xade682d1);
283
-        self::$iv[5] = self::new64(0x9b05688c, 0x2b3e6c1f);
284
-        self::$iv[6] = self::new64(0x1f83d9ab, 0xfb41bd6b);
285
-        self::$iv[7] = self::new64(0x5be0cd19, 0x137e2179);
277
+        self::$iv = new SplFixedArray( 8 );
278
+        self::$iv[ 0 ] = self::new64( 0x6a09e667, 0xf3bcc908 );
279
+        self::$iv[ 1 ] = self::new64( 0xbb67ae85, 0x84caa73b );
280
+        self::$iv[ 2 ] = self::new64( 0x3c6ef372, 0xfe94f82b );
281
+        self::$iv[ 3 ] = self::new64( 0xa54ff53a, 0x5f1d36f1 );
282
+        self::$iv[ 4 ] = self::new64( 0x510e527f, 0xade682d1 );
283
+        self::$iv[ 5 ] = self::new64( 0x9b05688c, 0x2b3e6c1f );
284
+        self::$iv[ 6 ] = self::new64( 0x1f83d9ab, 0xfb41bd6b );
285
+        self::$iv[ 7 ] = self::new64( 0x5be0cd19, 0x137e2179 );
286 286
 
287 287
         $called = true;
288 288
     }
@@ -299,26 +299,26 @@  discard block
 block discarded – undo
299 299
      */
300 300
     protected static function context()
301 301
     {
302
-        $ctx    = new SplFixedArray(6);
303
-        $ctx[0] = new SplFixedArray(8);   // h
304
-        $ctx[1] = new SplFixedArray(2);   // t
305
-        $ctx[2] = new SplFixedArray(2);   // f
306
-        $ctx[3] = new SplFixedArray(256); // buf
307
-        $ctx[4] = 0;                      // buflen
308
-        $ctx[5] = 0;                      // last_node (uint8_t)
302
+        $ctx    = new SplFixedArray( 6 );
303
+        $ctx[ 0 ] = new SplFixedArray( 8 ); // h
304
+        $ctx[ 1 ] = new SplFixedArray( 2 ); // t
305
+        $ctx[ 2 ] = new SplFixedArray( 2 ); // f
306
+        $ctx[ 3 ] = new SplFixedArray( 256 ); // buf
307
+        $ctx[ 4 ] = 0; // buflen
308
+        $ctx[ 5 ] = 0; // last_node (uint8_t)
309 309
 
310
-        for ($i = 8; $i--;) {
311
-            $ctx[0][$i] = self::$iv[$i];
310
+        for ( $i = 8; $i--; ) {
311
+            $ctx[ 0 ][ $i ] = self::$iv[ $i ];
312 312
         }
313
-        for ($i = 256; $i--;) {
314
-            $ctx[3][$i] = 0;
313
+        for ( $i = 256; $i--; ) {
314
+            $ctx[ 3 ][ $i ] = 0;
315 315
         }
316 316
 
317
-        $zero = self::new64(0, 0);
318
-        $ctx[1][0] = $zero;
319
-        $ctx[1][1] = $zero;
320
-        $ctx[2][0] = $zero;
321
-        $ctx[2][1] = $zero;
317
+        $zero = self::new64( 0, 0 );
318
+        $ctx[ 1 ][ 0 ] = $zero;
319
+        $ctx[ 1 ][ 1 ] = $zero;
320
+        $ctx[ 2 ][ 0 ] = $zero;
321
+        $ctx[ 2 ][ 1 ] = $zero;
322 322
 
323 323
         return $ctx;
324 324
     }
@@ -337,43 +337,43 @@  discard block
 block discarded – undo
337 337
      * @psalm-suppress MixedArrayAssignment
338 338
      * @psalm-suppress MixedArrayOffset
339 339
      */
340
-    protected static function compress(SplFixedArray $ctx, SplFixedArray $buf)
340
+    protected static function compress( SplFixedArray $ctx, SplFixedArray $buf )
341 341
     {
342
-        $m = new SplFixedArray(16);
343
-        $v = new SplFixedArray(16);
342
+        $m = new SplFixedArray( 16 );
343
+        $v = new SplFixedArray( 16 );
344 344
 
345
-        for ($i = 16; $i--;) {
346
-            $m[$i] = self::load64($buf, $i << 3);
345
+        for ( $i = 16; $i--; ) {
346
+            $m[ $i ] = self::load64( $buf, $i << 3 );
347 347
         }
348 348
 
349
-        for ($i = 8; $i--;) {
350
-            $v[$i] = $ctx[0][$i];
349
+        for ( $i = 8; $i--; ) {
350
+            $v[ $i ] = $ctx[ 0 ][ $i ];
351 351
         }
352 352
 
353
-        $v[ 8] = self::$iv[0];
354
-        $v[ 9] = self::$iv[1];
355
-        $v[10] = self::$iv[2];
356
-        $v[11] = self::$iv[3];
353
+        $v[ 8 ] = self::$iv[ 0 ];
354
+        $v[ 9 ] = self::$iv[ 1 ];
355
+        $v[ 10 ] = self::$iv[ 2 ];
356
+        $v[ 11 ] = self::$iv[ 3 ];
357 357
 
358
-        $v[12] = self::xor64($ctx[1][0], self::$iv[4]);
359
-        $v[13] = self::xor64($ctx[1][1], self::$iv[5]);
360
-        $v[14] = self::xor64($ctx[2][0], self::$iv[6]);
361
-        $v[15] = self::xor64($ctx[2][1], self::$iv[7]);
358
+        $v[ 12 ] = self::xor64( $ctx[ 1 ][ 0 ], self::$iv[ 4 ] );
359
+        $v[ 13 ] = self::xor64( $ctx[ 1 ][ 1 ], self::$iv[ 5 ] );
360
+        $v[ 14 ] = self::xor64( $ctx[ 2 ][ 0 ], self::$iv[ 6 ] );
361
+        $v[ 15 ] = self::xor64( $ctx[ 2 ][ 1 ], self::$iv[ 7 ] );
362 362
 
363
-        for ($r = 0; $r < 12; ++$r) {
364
-            $v = self::G($r, 0, 0, 4, 8, 12, $v, $m);
365
-            $v = self::G($r, 1, 1, 5, 9, 13, $v, $m);
366
-            $v = self::G($r, 2, 2, 6, 10, 14, $v, $m);
367
-            $v = self::G($r, 3, 3, 7, 11, 15, $v, $m);
368
-            $v = self::G($r, 4, 0, 5, 10, 15, $v, $m);
369
-            $v = self::G($r, 5, 1, 6, 11, 12, $v, $m);
370
-            $v = self::G($r, 6, 2, 7, 8, 13, $v, $m);
371
-            $v = self::G($r, 7, 3, 4, 9, 14, $v, $m);
363
+        for ( $r = 0; $r < 12; ++$r ) {
364
+            $v = self::G( $r, 0, 0, 4, 8, 12, $v, $m );
365
+            $v = self::G( $r, 1, 1, 5, 9, 13, $v, $m );
366
+            $v = self::G( $r, 2, 2, 6, 10, 14, $v, $m );
367
+            $v = self::G( $r, 3, 3, 7, 11, 15, $v, $m );
368
+            $v = self::G( $r, 4, 0, 5, 10, 15, $v, $m );
369
+            $v = self::G( $r, 5, 1, 6, 11, 12, $v, $m );
370
+            $v = self::G( $r, 6, 2, 7, 8, 13, $v, $m );
371
+            $v = self::G( $r, 7, 3, 4, 9, 14, $v, $m );
372 372
         }
373 373
 
374
-        for ($i = 8; $i--;) {
375
-            $ctx[0][$i] = self::xor64(
376
-                $ctx[0][$i], self::xor64($v[$i], $v[$i+8])
374
+        for ( $i = 8; $i--; ) {
375
+            $ctx[ 0 ][ $i ] = self::xor64(
376
+                $ctx[ 0 ][ $i ], self::xor64( $v[ $i ], $v[ $i + 8 ] )
377 377
             );
378 378
         }
379 379
     }
@@ -395,16 +395,16 @@  discard block
 block discarded – undo
395 395
      * @psalm-suppress MixedArgument
396 396
      * @psalm-suppress MixedArrayOffset
397 397
      */
398
-    public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m)
398
+    public static function G( $r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m )
399 399
     {
400
-        $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]);
401
-        $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32);
402
-        $v[$c] = self::add64($v[$c], $v[$d]);
403
-        $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 24);
404
-        $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][($i << 1) + 1]]);
405
-        $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 16);
406
-        $v[$c] = self::add64($v[$c], $v[$d]);
407
-        $v[$b] = self::rotr64(self::xor64($v[$b], $v[$c]), 63);
400
+        $v[ $a ] = self::add364( $v[ $a ], $v[ $b ], $m[ self::$sigma[ $r ][ $i << 1 ] ] );
401
+        $v[ $d ] = self::rotr64( self::xor64( $v[ $d ], $v[ $a ] ), 32 );
402
+        $v[ $c ] = self::add64( $v[ $c ], $v[ $d ] );
403
+        $v[ $b ] = self::rotr64( self::xor64( $v[ $b ], $v[ $c ] ), 24 );
404
+        $v[ $a ] = self::add364( $v[ $a ], $v[ $b ], $m[ self::$sigma[ $r ][ ( $i << 1 ) + 1 ] ] );
405
+        $v[ $d ] = self::rotr64( self::xor64( $v[ $d ], $v[ $a ] ), 16 );
406
+        $v[ $c ] = self::add64( $v[ $c ], $v[ $d ] );
407
+        $v[ $b ] = self::rotr64( self::xor64( $v[ $b ], $v[ $c ] ), 63 );
408 408
         return $v;
409 409
     }
410 410
 
@@ -419,20 +419,20 @@  discard block
 block discarded – undo
419 419
      * @psalm-suppress MixedArrayAccess
420 420
      * @psalm-suppress MixedArrayAssignment
421 421
      */
422
-    public static function increment_counter($ctx, $inc)
422
+    public static function increment_counter( $ctx, $inc )
423 423
     {
424
-        if ($inc < 0) {
425
-            throw new SodiumException('Increasing by a negative number makes no sense.');
424
+        if ( $inc < 0 ) {
425
+            throw new SodiumException( 'Increasing by a negative number makes no sense.' );
426 426
         }
427
-        $t = self::to64($inc);
427
+        $t = self::to64( $inc );
428 428
         # S->t is $ctx[1] in our implementation
429 429
 
430 430
         # S->t[0] = ( uint64_t )( t >> 0 );
431
-        $ctx[1][0] = self::add64($ctx[1][0], $t);
431
+        $ctx[ 1 ][ 0 ] = self::add64( $ctx[ 1 ][ 0 ], $t );
432 432
 
433 433
         # S->t[1] += ( S->t[0] < inc );
434
-        if (self::flatten64($ctx[1][0]) < $inc) {
435
-            $ctx[1][1] = self::add64($ctx[1][1], self::to64(1));
434
+        if ( self::flatten64( $ctx[ 1 ][ 0 ] ) < $inc ) {
435
+            $ctx[ 1 ][ 1 ] = self::add64( $ctx[ 1 ][ 1 ], self::to64( 1 ) );
436 436
         }
437 437
     }
438 438
 
@@ -452,37 +452,37 @@  discard block
 block discarded – undo
452 452
      * @psalm-suppress MixedArrayOffset
453 453
      * @psalm-suppress MixedOperand
454 454
      */
455
-    public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen)
455
+    public static function update( SplFixedArray $ctx, SplFixedArray $p, $plen )
456 456
     {
457 457
         self::pseudoConstructor();
458 458
 
459 459
         $offset = 0;
460
-        while ($plen > 0) {
461
-            $left = $ctx[4];
460
+        while ( $plen > 0 ) {
461
+            $left = $ctx[ 4 ];
462 462
             $fill = 256 - $left;
463 463
 
464
-            if ($plen > $fill) {
464
+            if ( $plen > $fill ) {
465 465
                 # memcpy( S->buf + left, in, fill ); /* Fill buffer */
466
-                for ($i = $fill; $i--;) {
467
-                    $ctx[3][$i + $left] = $p[$i + $offset];
466
+                for ( $i = $fill; $i--; ) {
467
+                    $ctx[ 3 ][ $i + $left ] = $p[ $i + $offset ];
468 468
                 }
469 469
 
470 470
                 # S->buflen += fill;
471
-                $ctx[4] += $fill;
471
+                $ctx[ 4 ] += $fill;
472 472
 
473 473
                 # blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
474
-                self::increment_counter($ctx, 128);
474
+                self::increment_counter( $ctx, 128 );
475 475
 
476 476
                 # blake2b_compress( S, S->buf ); /* Compress */
477
-                self::compress($ctx, $ctx[3]);
477
+                self::compress( $ctx, $ctx[ 3 ] );
478 478
 
479 479
                 # memcpy( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES ); /* Shift buffer left */
480
-                for ($i = 128; $i--;) {
481
-                    $ctx[3][$i] = $ctx[3][$i + 128];
480
+                for ( $i = 128; $i--; ) {
481
+                    $ctx[ 3 ][ $i ] = $ctx[ 3 ][ $i + 128 ];
482 482
                 }
483 483
 
484 484
                 # S->buflen -= BLAKE2B_BLOCKBYTES;
485
-                $ctx[4] -= 128;
485
+                $ctx[ 4 ] -= 128;
486 486
 
487 487
                 # in += fill;
488 488
                 $offset += $fill;
@@ -490,10 +490,10 @@  discard block
 block discarded – undo
490 490
                 # inlen -= fill;
491 491
                 $plen -= $fill;
492 492
             } else {
493
-                for ($i = $plen; $i--;) {
494
-                    $ctx[3][$i + $left] = $p[$i + $offset];
493
+                for ( $i = $plen; $i--; ) {
494
+                    $ctx[ 3 ][ $i + $left ] = $p[ $i + $offset ];
495 495
                 }
496
-                $ctx[4] += $plen;
496
+                $ctx[ 4 ] += $plen;
497 497
                 $offset += $plen;
498 498
                 $plen -= $plen;
499 499
             }
@@ -515,33 +515,33 @@  discard block
 block discarded – undo
515 515
      * @psalm-suppress MixedArrayOffset
516 516
      * @psalm-suppress MixedOperand
517 517
      */
518
-    public static function finish(SplFixedArray $ctx, SplFixedArray $out)
518
+    public static function finish( SplFixedArray $ctx, SplFixedArray $out )
519 519
     {
520 520
         self::pseudoConstructor();
521
-        if ($ctx[4] > 128) {
522
-            self::increment_counter($ctx, 128);
523
-            self::compress($ctx, $ctx[3]);
524
-            $ctx[4] -= 128;
525
-            if ($ctx[4] > 128) {
526
-                throw new SodiumException('Failed to assert that buflen <= 128 bytes');
521
+        if ( $ctx[ 4 ] > 128 ) {
522
+            self::increment_counter( $ctx, 128 );
523
+            self::compress( $ctx, $ctx[ 3 ] );
524
+            $ctx[ 4 ] -= 128;
525
+            if ( $ctx[ 4 ] > 128 ) {
526
+                throw new SodiumException( 'Failed to assert that buflen <= 128 bytes' );
527 527
             }
528
-            for ($i = $ctx[4]; $i--;) {
529
-                $ctx[3][$i] = $ctx[3][$i + 128];
528
+            for ( $i = $ctx[ 4 ]; $i--; ) {
529
+                $ctx[ 3 ][ $i ] = $ctx[ 3 ][ $i + 128 ];
530 530
             }
531 531
         }
532 532
 
533
-        self::increment_counter($ctx, $ctx[4]);
534
-        $ctx[2][0] = self::new64(0xffffffff, 0xffffffff);
533
+        self::increment_counter( $ctx, $ctx[ 4 ] );
534
+        $ctx[ 2 ][ 0 ] = self::new64( 0xffffffff, 0xffffffff );
535 535
 
536
-        for ($i = 256 - $ctx[4]; $i--;) {
537
-            $ctx[3][$i+$ctx[4]] = 0;
536
+        for ( $i = 256 - $ctx[ 4 ]; $i--; ) {
537
+            $ctx[ 3 ][ $i + $ctx[ 4 ] ] = 0;
538 538
         }
539 539
 
540
-        self::compress($ctx, $ctx[3]);
540
+        self::compress( $ctx, $ctx[ 3 ] );
541 541
 
542
-        $i = (int) (($out->getSize() - 1) / 8);
543
-        for (; $i >= 0; --$i) {
544
-            self::store64($out, $i << 3, $ctx[0][$i]);
542
+        $i = (int)( ( $out->getSize() - 1 ) / 8 );
543
+        for ( ; $i >= 0; --$i ) {
544
+            self::store64( $out, $i << 3, $ctx[ 0 ][ $i ] );
545 545
         }
546 546
         return $out;
547 547
     }
@@ -571,67 +571,67 @@  discard block
 block discarded – undo
571 571
         self::pseudoConstructor();
572 572
         $klen = 0;
573 573
 
574
-        if ($key !== null) {
575
-            if (count($key) > 64) {
576
-                throw new SodiumException('Invalid key size');
574
+        if ( $key !== null ) {
575
+            if ( count( $key ) > 64 ) {
576
+                throw new SodiumException( 'Invalid key size' );
577 577
             }
578
-            $klen = count($key);
578
+            $klen = count( $key );
579 579
         }
580 580
 
581
-        if ($outlen > 64) {
582
-            throw new SodiumException('Invalid output size');
581
+        if ( $outlen > 64 ) {
582
+            throw new SodiumException( 'Invalid output size' );
583 583
         }
584 584
 
585 585
         $ctx = self::context();
586 586
 
587
-        $p = new SplFixedArray(64);
587
+        $p = new SplFixedArray( 64 );
588 588
         // Zero our param buffer...
589
-        for ($i = 64; --$i;) {
590
-            $p[$i] = 0;
589
+        for ( $i = 64; --$i; ) {
590
+            $p[ $i ] = 0;
591 591
         }
592 592
 
593
-        $p[0] = $outlen; // digest_length
594
-        $p[1] = $klen;   // key_length
595
-        $p[2] = 1;       // fanout
596
-        $p[3] = 1;       // depth
593
+        $p[ 0 ] = $outlen; // digest_length
594
+        $p[ 1 ] = $klen; // key_length
595
+        $p[ 2 ] = 1; // fanout
596
+        $p[ 3 ] = 1; // depth
597 597
 
598
-        if ($salt instanceof SplFixedArray) {
598
+        if ( $salt instanceof SplFixedArray ) {
599 599
             // salt: [32] through [47]
600
-            for ($i = 0; $i < 16; ++$i) {
601
-                $p[32 + $i] = (int) $salt[$i];
600
+            for ( $i = 0; $i < 16; ++$i ) {
601
+                $p[ 32 + $i ] = (int)$salt[ $i ];
602 602
             }
603 603
         }
604
-        if ($personal instanceof SplFixedArray) {
604
+        if ( $personal instanceof SplFixedArray ) {
605 605
             // personal: [48] through [63]
606
-            for ($i = 0; $i < 16; ++$i) {
607
-                $p[48 + $i] = (int) $personal[$i];
606
+            for ( $i = 0; $i < 16; ++$i ) {
607
+                $p[ 48 + $i ] = (int)$personal[ $i ];
608 608
             }
609 609
         }
610 610
 
611
-        $ctx[0][0] = self::xor64(
612
-            $ctx[0][0],
613
-            self::load64($p, 0)
611
+        $ctx[ 0 ][ 0 ] = self::xor64(
612
+            $ctx[ 0 ][ 0 ],
613
+            self::load64( $p, 0 )
614 614
         );
615
-        if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) {
615
+        if ( $salt instanceof SplFixedArray || $personal instanceof SplFixedArray ) {
616 616
             // We need to do what blake2b_init_param() does:
617
-            for ($i = 1; $i < 8; ++$i) {
618
-                $ctx[0][$i] = self::xor64(
619
-                    $ctx[0][$i],
620
-                    self::load64($p, $i << 3)
617
+            for ( $i = 1; $i < 8; ++$i ) {
618
+                $ctx[ 0 ][ $i ] = self::xor64(
619
+                    $ctx[ 0 ][ $i ],
620
+                    self::load64( $p, $i << 3 )
621 621
                 );
622 622
             }
623 623
         }
624 624
 
625
-        if ($klen > 0 && $key instanceof SplFixedArray) {
626
-            $block = new SplFixedArray(128);
627
-            for ($i = 128; $i--;) {
628
-                $block[$i] = 0;
625
+        if ( $klen > 0 && $key instanceof SplFixedArray ) {
626
+            $block = new SplFixedArray( 128 );
627
+            for ( $i = 128; $i--; ) {
628
+                $block[ $i ] = 0;
629 629
             }
630
-            for ($i = $klen; $i--;) {
631
-                $block[$i] = $key[$i];
630
+            for ( $i = $klen; $i--; ) {
631
+                $block[ $i ] = $key[ $i ];
632 632
             }
633
-            self::update($ctx, $block, 128);
634
-            $ctx[4] = 128;
633
+            self::update( $ctx, $block, 128 );
634
+            $ctx[ 4 ] = 128;
635 635
         }
636 636
 
637 637
         return $ctx;
@@ -646,10 +646,10 @@  discard block
 block discarded – undo
646 646
      * @return SplFixedArray
647 647
      * @psalm-suppress MixedArgumentTypeCoercion
648 648
      */
649
-    public static function stringToSplFixedArray($str = '')
649
+    public static function stringToSplFixedArray( $str = '' )
650 650
     {
651
-        $values = unpack('C*', $str);
652
-        return SplFixedArray::fromArray(array_values($values));
651
+        $values = unpack( 'C*', $str );
652
+        return SplFixedArray::fromArray( array_values( $values ) );
653 653
     }
654 654
 
655 655
     /**
@@ -661,15 +661,15 @@  discard block
 block discarded – undo
661 661
      * @return string
662 662
      * @throws TypeError
663 663
      */
664
-    public static function SplFixedArrayToString(SplFixedArray $a)
664
+    public static function SplFixedArrayToString( SplFixedArray $a )
665 665
     {
666 666
         /**
667 667
          * @var array<int, int|string> $arr
668 668
          */
669 669
         $arr = $a->toArray();
670 670
         $c = $a->count();
671
-        array_unshift($arr, str_repeat('C', $c));
672
-        return (string) (call_user_func_array('pack', $arr));
671
+        array_unshift( $arr, str_repeat( 'C', $c ) );
672
+        return (string)( call_user_func_array( 'pack', $arr ) );
673 673
     }
674 674
 
675 675
     /**
@@ -685,47 +685,47 @@  discard block
 block discarded – undo
685 685
      * @psalm-suppress MixedArrayOffset
686 686
      * @psalm-suppress MixedMethodCall
687 687
      */
688
-    public static function contextToString(SplFixedArray $ctx)
688
+    public static function contextToString( SplFixedArray $ctx )
689 689
     {
690 690
         $str = '';
691 691
         /** @var array<int, array<int, int>> $ctxA */
692
-        $ctxA = $ctx[0]->toArray();
692
+        $ctxA = $ctx[ 0 ]->toArray();
693 693
 
694 694
         # uint64_t h[8];
695
-        for ($i = 0; $i < 8; ++$i) {
696
-            $str .= self::store32_le($ctxA[$i][1]);
697
-            $str .= self::store32_le($ctxA[$i][0]);
695
+        for ( $i = 0; $i < 8; ++$i ) {
696
+            $str .= self::store32_le( $ctxA[ $i ][ 1 ] );
697
+            $str .= self::store32_le( $ctxA[ $i ][ 0 ] );
698 698
         }
699 699
 
700 700
         # uint64_t t[2];
701 701
         # uint64_t f[2];
702
-        for ($i = 1; $i < 3; ++$i) {
703
-            $ctxA = $ctx[$i]->toArray();
704
-            $str .= self::store32_le($ctxA[0][1]);
705
-            $str .= self::store32_le($ctxA[0][0]);
706
-            $str .= self::store32_le($ctxA[1][1]);
707
-            $str .= self::store32_le($ctxA[1][0]);
702
+        for ( $i = 1; $i < 3; ++$i ) {
703
+            $ctxA = $ctx[ $i ]->toArray();
704
+            $str .= self::store32_le( $ctxA[ 0 ][ 1 ] );
705
+            $str .= self::store32_le( $ctxA[ 0 ][ 0 ] );
706
+            $str .= self::store32_le( $ctxA[ 1 ][ 1 ] );
707
+            $str .= self::store32_le( $ctxA[ 1 ][ 0 ] );
708 708
         }
709 709
 
710 710
         # uint8_t buf[2 * 128];
711
-        $str .= self::SplFixedArrayToString($ctx[3]);
711
+        $str .= self::SplFixedArrayToString( $ctx[ 3 ] );
712 712
 
713 713
         /** @var int $ctx4 */
714
-        $ctx4 = (int) $ctx[4];
714
+        $ctx4 = (int)$ctx[ 4 ];
715 715
 
716 716
         # size_t buflen;
717
-        $str .= implode('', array(
718
-            self::intToChr($ctx4 & 0xff),
719
-            self::intToChr(($ctx4 >> 8) & 0xff),
720
-            self::intToChr(($ctx4 >> 16) & 0xff),
721
-            self::intToChr(($ctx4 >> 24) & 0xff),
722
-            self::intToChr(($ctx4 >> 32) & 0xff),
723
-            self::intToChr(($ctx4 >> 40) & 0xff),
724
-            self::intToChr(($ctx4 >> 48) & 0xff),
725
-            self::intToChr(($ctx4 >> 56) & 0xff)
726
-        ));
717
+        $str .= implode( '', array(
718
+            self::intToChr( $ctx4 & 0xff ),
719
+            self::intToChr( ( $ctx4 >> 8 ) & 0xff ),
720
+            self::intToChr( ( $ctx4 >> 16 ) & 0xff ),
721
+            self::intToChr( ( $ctx4 >> 24 ) & 0xff ),
722
+            self::intToChr( ( $ctx4 >> 32 ) & 0xff ),
723
+            self::intToChr( ( $ctx4 >> 40 ) & 0xff ),
724
+            self::intToChr( ( $ctx4 >> 48 ) & 0xff ),
725
+            self::intToChr( ( $ctx4 >> 56 ) & 0xff )
726
+        ) );
727 727
         # uint8_t last_node;
728
-        return $str . self::intToChr($ctx[5]) . str_repeat("\x00", 23);
728
+        return $str . self::intToChr( $ctx[ 5 ] ) . str_repeat( "\x00", 23 );
729 729
     }
730 730
 
731 731
     /**
@@ -740,19 +740,19 @@  discard block
 block discarded – undo
740 740
      * @throws TypeError
741 741
      * @psalm-suppress MixedArrayAssignment
742 742
      */
743
-    public static function stringToContext($string)
743
+    public static function stringToContext( $string )
744 744
     {
745 745
         $ctx = self::context();
746 746
 
747 747
         # uint64_t h[8];
748
-        for ($i = 0; $i < 8; ++$i) {
749
-            $ctx[0][$i] = SplFixedArray::fromArray(
748
+        for ( $i = 0; $i < 8; ++$i ) {
749
+            $ctx[ 0 ][ $i ] = SplFixedArray::fromArray(
750 750
                 array(
751 751
                     self::load_4(
752
-                        self::substr($string, (($i << 3) + 4), 4)
752
+                        self::substr( $string, ( ( $i << 3 ) + 4 ), 4 )
753 753
                     ),
754 754
                     self::load_4(
755
-                        self::substr($string, (($i << 3) + 0), 4)
755
+                        self::substr( $string, ( ( $i << 3 ) + 0 ), 4 )
756 756
                     )
757 757
                 )
758 758
             );
@@ -760,30 +760,30 @@  discard block
 block discarded – undo
760 760
 
761 761
         # uint64_t t[2];
762 762
         # uint64_t f[2];
763
-        for ($i = 1; $i < 3; ++$i) {
764
-            $ctx[$i][1] = SplFixedArray::fromArray(
763
+        for ( $i = 1; $i < 3; ++$i ) {
764
+            $ctx[ $i ][ 1 ] = SplFixedArray::fromArray(
765 765
                 array(
766
-                    self::load_4(self::substr($string, 76 + (($i - 1) << 4), 4)),
767
-                    self::load_4(self::substr($string, 72 + (($i - 1) << 4), 4))
766
+                    self::load_4( self::substr( $string, 76 + ( ( $i - 1 ) << 4 ), 4 ) ),
767
+                    self::load_4( self::substr( $string, 72 + ( ( $i - 1 ) << 4 ), 4 ) )
768 768
                 )
769 769
             );
770
-            $ctx[$i][0] = SplFixedArray::fromArray(
770
+            $ctx[ $i ][ 0 ] = SplFixedArray::fromArray(
771 771
                 array(
772
-                    self::load_4(self::substr($string, 68 + (($i - 1) << 4), 4)),
773
-                    self::load_4(self::substr($string, 64 + (($i - 1) << 4), 4))
772
+                    self::load_4( self::substr( $string, 68 + ( ( $i - 1 ) << 4 ), 4 ) ),
773
+                    self::load_4( self::substr( $string, 64 + ( ( $i - 1 ) << 4 ), 4 ) )
774 774
                 )
775 775
             );
776 776
         }
777 777
 
778 778
         # uint8_t buf[2 * 128];
779
-        $ctx[3] = self::stringToSplFixedArray(self::substr($string, 96, 256));
779
+        $ctx[ 3 ] = self::stringToSplFixedArray( self::substr( $string, 96, 256 ) );
780 780
 
781 781
         # uint8_t buf[2 * 128];
782 782
         $int = 0;
783
-        for ($i = 0; $i < 8; ++$i) {
784
-            $int |= self::chrToInt($string[352 + $i]) << ($i << 3);
783
+        for ( $i = 0; $i < 8; ++$i ) {
784
+            $int |= self::chrToInt( $string[ 352 + $i ] ) << ( $i << 3 );
785 785
         }
786
-        $ctx[4] = $int;
786
+        $ctx[ 4 ] = $int;
787 787
 
788 788
         return $ctx;
789 789
     }
Please login to merge, or discard this patch.
Braces   +21 added lines, -42 removed lines patch added patch discarded remove patch
@@ -9,8 +9,7 @@  discard block
 block discarded – undo
9 9
  *
10 10
  * Based on the work of Devi Mandiri in devi/salt.
11 11
  */
12
-abstract class ParagonIE_Sodium_Core_BLAKE2b extends ParagonIE_Sodium_Core_Util
13
-{
12
+abstract class ParagonIE_Sodium_Core_BLAKE2b extends ParagonIE_Sodium_Core_Util {
14 13
     /**
15 14
      * @var SplFixedArray
16 15
      */
@@ -48,8 +47,7 @@  discard block
 block discarded – undo
48 47
      * @return SplFixedArray
49 48
      * @psalm-suppress MixedAssignment
50 49
      */
51
-    public static function new64($high, $low)
52
-    {
50
+    public static function new64($high, $low) {
53 51
         $i64 = new SplFixedArray(2);
54 52
         $i64[0] = $high & 0xffffffff;
55 53
         $i64[1] = $low & 0xffffffff;
@@ -65,8 +63,7 @@  discard block
 block discarded – undo
65 63
      * @param int $num
66 64
      * @return SplFixedArray
67 65
      */
68
-    protected static function to64($num)
69
-    {
66
+    protected static function to64($num) {
70 67
         list($hi, $lo) = self::numericTo64BitInteger($num);
71 68
         return self::new64($hi, $lo);
72 69
     }
@@ -84,8 +81,7 @@  discard block
 block discarded – undo
84 81
      * @psalm-suppress MixedAssignment
85 82
      * @psalm-suppress MixedOperand
86 83
      */
87
-    protected static function add64($x, $y)
88
-    {
84
+    protected static function add64($x, $y) {
89 85
         $l = ($x[1] + $y[1]) & 0xffffffff;
90 86
         return self::new64(
91 87
             (int) ($x[0] + $y[0] + (
@@ -103,8 +99,7 @@  discard block
 block discarded – undo
103 99
      * @param SplFixedArray $z
104 100
      * @return SplFixedArray
105 101
      */
106
-    protected static function add364($x, $y, $z)
107
-    {
102
+    protected static function add364($x, $y, $z) {
108 103
         return self::add64($x, self::add64($y, $z));
109 104
     }
110 105
 
@@ -117,8 +112,7 @@  discard block
 block discarded – undo
117 112
      * @throws SodiumException
118 113
      * @throws TypeError
119 114
      */
120
-    protected static function xor64(SplFixedArray $x, SplFixedArray $y)
121
-    {
115
+    protected static function xor64(SplFixedArray $x, SplFixedArray $y) {
122 116
         if (!is_numeric($x[0])) {
123 117
             throw new SodiumException('x[0] is not an integer');
124 118
         }
@@ -145,8 +139,7 @@  discard block
 block discarded – undo
145 139
      * @return SplFixedArray
146 140
      * @psalm-suppress MixedAssignment
147 141
      */
148
-    public static function rotr64($x, $c)
149
-    {
142
+    public static function rotr64($x, $c) {
150 143
         if ($c >= 64) {
151 144
             $c %= 64;
152 145
         }
@@ -203,8 +196,7 @@  discard block
 block discarded – undo
203 196
      * @return int
204 197
      * @psalm-suppress MixedOperand
205 198
      */
206
-    protected static function flatten64($x)
207
-    {
199
+    protected static function flatten64($x) {
208 200
         return (int) ($x[0] * 4294967296 + $x[1]);
209 201
     }
210 202
 
@@ -217,8 +209,7 @@  discard block
 block discarded – undo
217 209
      * @psalm-suppress MixedArgument
218 210
      * @psalm-suppress MixedArrayOffset
219 211
      */
220
-    protected static function load64(SplFixedArray $x, $i)
221
-    {
212
+    protected static function load64(SplFixedArray $x, $i) {
222 213
         /** @var int $l */
223 214
         $l = (int) ($x[$i])
224 215
              | ((int) ($x[$i+1]) << 8)
@@ -241,8 +232,7 @@  discard block
 block discarded – undo
241 232
      * @return void
242 233
      * @psalm-suppress MixedAssignment
243 234
      */
244
-    protected static function store64(SplFixedArray $x, $i, SplFixedArray $u)
245
-    {
235
+    protected static function store64(SplFixedArray $x, $i, SplFixedArray $u) {
246 236
         $maxLength = $x->getSize() - 1;
247 237
         for ($j = 0; $j < 8; ++$j) {
248 238
             /*
@@ -268,8 +258,7 @@  discard block
 block discarded – undo
268 258
      *
269 259
      * @return void
270 260
      */
271
-    public static function pseudoConstructor()
272
-    {
261
+    public static function pseudoConstructor() {
273 262
         static $called = false;
274 263
         if ($called) {
275 264
             return;
@@ -297,8 +286,7 @@  discard block
 block discarded – undo
297 286
      * @psalm-suppress MixedArrayAccess
298 287
      * @psalm-suppress MixedArrayAssignment
299 288
      */
300
-    protected static function context()
301
-    {
289
+    protected static function context() {
302 290
         $ctx    = new SplFixedArray(6);
303 291
         $ctx[0] = new SplFixedArray(8);   // h
304 292
         $ctx[1] = new SplFixedArray(2);   // t
@@ -337,8 +325,7 @@  discard block
 block discarded – undo
337 325
      * @psalm-suppress MixedArrayAssignment
338 326
      * @psalm-suppress MixedArrayOffset
339 327
      */
340
-    protected static function compress(SplFixedArray $ctx, SplFixedArray $buf)
341
-    {
328
+    protected static function compress(SplFixedArray $ctx, SplFixedArray $buf) {
342 329
         $m = new SplFixedArray(16);
343 330
         $v = new SplFixedArray(16);
344 331
 
@@ -395,8 +382,7 @@  discard block
 block discarded – undo
395 382
      * @psalm-suppress MixedArgument
396 383
      * @psalm-suppress MixedArrayOffset
397 384
      */
398
-    public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m)
399
-    {
385
+    public static function G($r, $i, $a, $b, $c, $d, SplFixedArray $v, SplFixedArray $m) {
400 386
         $v[$a] = self::add364($v[$a], $v[$b], $m[self::$sigma[$r][$i << 1]]);
401 387
         $v[$d] = self::rotr64(self::xor64($v[$d], $v[$a]), 32);
402 388
         $v[$c] = self::add64($v[$c], $v[$d]);
@@ -419,8 +405,7 @@  discard block
 block discarded – undo
419 405
      * @psalm-suppress MixedArrayAccess
420 406
      * @psalm-suppress MixedArrayAssignment
421 407
      */
422
-    public static function increment_counter($ctx, $inc)
423
-    {
408
+    public static function increment_counter($ctx, $inc) {
424 409
         if ($inc < 0) {
425 410
             throw new SodiumException('Increasing by a negative number makes no sense.');
426 411
         }
@@ -452,8 +437,7 @@  discard block
 block discarded – undo
452 437
      * @psalm-suppress MixedArrayOffset
453 438
      * @psalm-suppress MixedOperand
454 439
      */
455
-    public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen)
456
-    {
440
+    public static function update(SplFixedArray $ctx, SplFixedArray $p, $plen) {
457 441
         self::pseudoConstructor();
458 442
 
459 443
         $offset = 0;
@@ -515,8 +499,7 @@  discard block
 block discarded – undo
515 499
      * @psalm-suppress MixedArrayOffset
516 500
      * @psalm-suppress MixedOperand
517 501
      */
518
-    public static function finish(SplFixedArray $ctx, SplFixedArray $out)
519
-    {
502
+    public static function finish(SplFixedArray $ctx, SplFixedArray $out) {
520 503
         self::pseudoConstructor();
521 504
         if ($ctx[4] > 128) {
522 505
             self::increment_counter($ctx, 128);
@@ -646,8 +629,7 @@  discard block
 block discarded – undo
646 629
      * @return SplFixedArray
647 630
      * @psalm-suppress MixedArgumentTypeCoercion
648 631
      */
649
-    public static function stringToSplFixedArray($str = '')
650
-    {
632
+    public static function stringToSplFixedArray($str = '') {
651 633
         $values = unpack('C*', $str);
652 634
         return SplFixedArray::fromArray(array_values($values));
653 635
     }
@@ -661,8 +643,7 @@  discard block
 block discarded – undo
661 643
      * @return string
662 644
      * @throws TypeError
663 645
      */
664
-    public static function SplFixedArrayToString(SplFixedArray $a)
665
-    {
646
+    public static function SplFixedArrayToString(SplFixedArray $a) {
666 647
         /**
667 648
          * @var array<int, int|string> $arr
668 649
          */
@@ -685,8 +666,7 @@  discard block
 block discarded – undo
685 666
      * @psalm-suppress MixedArrayOffset
686 667
      * @psalm-suppress MixedMethodCall
687 668
      */
688
-    public static function contextToString(SplFixedArray $ctx)
689
-    {
669
+    public static function contextToString(SplFixedArray $ctx) {
690 670
         $str = '';
691 671
         /** @var array<int, array<int, int>> $ctxA */
692 672
         $ctxA = $ctx[0]->toArray();
@@ -740,8 +720,7 @@  discard block
 block discarded – undo
740 720
      * @throws TypeError
741 721
      * @psalm-suppress MixedArrayAssignment
742 722
      */
743
-    public static function stringToContext($string)
744
-    {
723
+    public static function stringToContext($string) {
745 724
         $ctx = self::context();
746 725
 
747 726
         # uint64_t h[8];
Please login to merge, or discard this patch.
vendor/paragonie/sodium_compat/src/Core/Ed25519.php 3 patches
Indentation   +540 added lines, -540 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 if (class_exists('ParagonIE_Sodium_Core_Ed25519', false)) {
4
-    return;
4
+	return;
5 5
 }
6 6
 
7 7
 /**
@@ -9,543 +9,543 @@  discard block
 block discarded – undo
9 9
  */
10 10
 abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_Curve25519
11 11
 {
12
-    const KEYPAIR_BYTES = 96;
13
-    const SEED_BYTES = 32;
14
-    const SCALAR_BYTES = 32;
15
-
16
-    /**
17
-     * @internal You should not use this directly from another application
18
-     *
19
-     * @return string (96 bytes)
20
-     * @throws Exception
21
-     * @throws SodiumException
22
-     * @throws TypeError
23
-     */
24
-    public static function keypair()
25
-    {
26
-        $seed = random_bytes(self::SEED_BYTES);
27
-        $pk = '';
28
-        $sk = '';
29
-        self::seed_keypair($pk, $sk, $seed);
30
-        return $sk . $pk;
31
-    }
32
-
33
-    /**
34
-     * @internal You should not use this directly from another application
35
-     *
36
-     * @param string $pk
37
-     * @param string $sk
38
-     * @param string $seed
39
-     * @return string
40
-     * @throws SodiumException
41
-     * @throws TypeError
42
-     */
43
-    public static function seed_keypair(&$pk, &$sk, $seed)
44
-    {
45
-        if (self::strlen($seed) !== self::SEED_BYTES) {
46
-            throw new RangeException('crypto_sign keypair seed must be 32 bytes long');
47
-        }
48
-
49
-        /** @var string $pk */
50
-        $pk = self::publickey_from_secretkey($seed);
51
-        $sk = $seed . $pk;
52
-        return $sk;
53
-    }
54
-
55
-    /**
56
-     * @internal You should not use this directly from another application
57
-     *
58
-     * @param string $keypair
59
-     * @return string
60
-     * @throws TypeError
61
-     */
62
-    public static function secretkey($keypair)
63
-    {
64
-        if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
65
-            throw new RangeException('crypto_sign keypair must be 96 bytes long');
66
-        }
67
-        return self::substr($keypair, 0, 64);
68
-    }
69
-
70
-    /**
71
-     * @internal You should not use this directly from another application
72
-     *
73
-     * @param string $keypair
74
-     * @return string
75
-     * @throws TypeError
76
-     */
77
-    public static function publickey($keypair)
78
-    {
79
-        if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
80
-            throw new RangeException('crypto_sign keypair must be 96 bytes long');
81
-        }
82
-        return self::substr($keypair, 64, 32);
83
-    }
84
-
85
-    /**
86
-     * @internal You should not use this directly from another application
87
-     *
88
-     * @param string $sk
89
-     * @return string
90
-     * @throws SodiumException
91
-     * @throws TypeError
92
-     */
93
-    public static function publickey_from_secretkey($sk)
94
-    {
95
-        /** @var string $sk */
96
-        $sk = hash('sha512', self::substr($sk, 0, 32), true);
97
-        $sk[0] = self::intToChr(
98
-            self::chrToInt($sk[0]) & 248
99
-        );
100
-        $sk[31] = self::intToChr(
101
-            (self::chrToInt($sk[31]) & 63) | 64
102
-        );
103
-        return self::sk_to_pk($sk);
104
-    }
105
-
106
-    /**
107
-     * @param string $pk
108
-     * @return string
109
-     * @throws SodiumException
110
-     * @throws TypeError
111
-     */
112
-    public static function pk_to_curve25519($pk)
113
-    {
114
-        if (self::small_order($pk)) {
115
-            throw new SodiumException('Public key is on a small order');
116
-        }
117
-        $A = self::ge_frombytes_negate_vartime(self::substr($pk, 0, 32));
118
-        $p1 = self::ge_mul_l($A);
119
-        if (!self::fe_isnonzero($p1->X)) {
120
-            throw new SodiumException('Unexpected zero result');
121
-        }
122
-
123
-        # fe_1(one_minus_y);
124
-        # fe_sub(one_minus_y, one_minus_y, A.Y);
125
-        # fe_invert(one_minus_y, one_minus_y);
126
-        $one_minux_y = self::fe_invert(
127
-            self::fe_sub(
128
-                self::fe_1(),
129
-                $A->Y
130
-            )
131
-        );
132
-
133
-        # fe_1(x);
134
-        # fe_add(x, x, A.Y);
135
-        # fe_mul(x, x, one_minus_y);
136
-        $x = self::fe_mul(
137
-            self::fe_add(self::fe_1(), $A->Y),
138
-            $one_minux_y
139
-        );
140
-
141
-        # fe_tobytes(curve25519_pk, x);
142
-        return self::fe_tobytes($x);
143
-    }
144
-
145
-    /**
146
-     * @internal You should not use this directly from another application
147
-     *
148
-     * @param string $sk
149
-     * @return string
150
-     * @throws SodiumException
151
-     * @throws TypeError
152
-     */
153
-    public static function sk_to_pk($sk)
154
-    {
155
-        return self::ge_p3_tobytes(
156
-            self::ge_scalarmult_base(
157
-                self::substr($sk, 0, 32)
158
-            )
159
-        );
160
-    }
161
-
162
-    /**
163
-     * @internal You should not use this directly from another application
164
-     *
165
-     * @param string $message
166
-     * @param string $sk
167
-     * @return string
168
-     * @throws SodiumException
169
-     * @throws TypeError
170
-     */
171
-    public static function sign($message, $sk)
172
-    {
173
-        /** @var string $signature */
174
-        $signature = self::sign_detached($message, $sk);
175
-        return $signature . $message;
176
-    }
177
-
178
-    /**
179
-     * @internal You should not use this directly from another application
180
-     *
181
-     * @param string $message A signed message
182
-     * @param string $pk      Public key
183
-     * @return string         Message (without signature)
184
-     * @throws SodiumException
185
-     * @throws TypeError
186
-     */
187
-    public static function sign_open($message, $pk)
188
-    {
189
-        /** @var string $signature */
190
-        $signature = self::substr($message, 0, 64);
191
-
192
-        /** @var string $message */
193
-        $message = self::substr($message, 64);
194
-
195
-        if (self::verify_detached($signature, $message, $pk)) {
196
-            return $message;
197
-        }
198
-        throw new SodiumException('Invalid signature');
199
-    }
200
-
201
-    /**
202
-     * @internal You should not use this directly from another application
203
-     *
204
-     * @param string $message
205
-     * @param string $sk
206
-     * @return string
207
-     * @throws SodiumException
208
-     * @throws TypeError
209
-     */
210
-    public static function sign_detached($message, $sk)
211
-    {
212
-        # crypto_hash_sha512(az, sk, 32);
213
-        $az =  hash('sha512', self::substr($sk, 0, 32), true);
214
-
215
-        # az[0] &= 248;
216
-        # az[31] &= 63;
217
-        # az[31] |= 64;
218
-        $az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
219
-        $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
220
-
221
-        # crypto_hash_sha512_init(&hs);
222
-        # crypto_hash_sha512_update(&hs, az + 32, 32);
223
-        # crypto_hash_sha512_update(&hs, m, mlen);
224
-        # crypto_hash_sha512_final(&hs, nonce);
225
-        $hs = hash_init('sha512');
226
-        hash_update($hs, self::substr($az, 32, 32));
227
-        hash_update($hs, $message);
228
-        $nonceHash = hash_final($hs, true);
229
-
230
-        # memmove(sig + 32, sk + 32, 32);
231
-        $pk = self::substr($sk, 32, 32);
232
-
233
-        # sc_reduce(nonce);
234
-        # ge_scalarmult_base(&R, nonce);
235
-        # ge_p3_tobytes(sig, &R);
236
-        $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
237
-        $sig = self::ge_p3_tobytes(
238
-            self::ge_scalarmult_base($nonce)
239
-        );
240
-
241
-        # crypto_hash_sha512_init(&hs);
242
-        # crypto_hash_sha512_update(&hs, sig, 64);
243
-        # crypto_hash_sha512_update(&hs, m, mlen);
244
-        # crypto_hash_sha512_final(&hs, hram);
245
-        $hs = hash_init('sha512');
246
-        hash_update($hs, self::substr($sig, 0, 32));
247
-        hash_update($hs, self::substr($pk, 0, 32));
248
-        hash_update($hs, $message);
249
-        $hramHash = hash_final($hs, true);
250
-
251
-        # sc_reduce(hram);
252
-        # sc_muladd(sig + 32, hram, az, nonce);
253
-        $hram = self::sc_reduce($hramHash);
254
-        $sigAfter = self::sc_muladd($hram, $az, $nonce);
255
-        $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
256
-
257
-        try {
258
-            ParagonIE_Sodium_Compat::memzero($az);
259
-        } catch (SodiumException $ex) {
260
-            $az = null;
261
-        }
262
-        return $sig;
263
-    }
264
-
265
-    /**
266
-     * @internal You should not use this directly from another application
267
-     *
268
-     * @param string $sig
269
-     * @param string $message
270
-     * @param string $pk
271
-     * @return bool
272
-     * @throws SodiumException
273
-     * @throws TypeError
274
-     */
275
-    public static function verify_detached($sig, $message, $pk)
276
-    {
277
-        if (self::strlen($sig) < 64) {
278
-            throw new SodiumException('Signature is too short');
279
-        }
280
-        if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) {
281
-            throw new SodiumException('S < L - Invalid signature');
282
-        }
283
-        if (self::small_order($sig)) {
284
-            throw new SodiumException('Signature is on too small of an order');
285
-        }
286
-        if ((self::chrToInt($sig[63]) & 224) !== 0) {
287
-            throw new SodiumException('Invalid signature');
288
-        }
289
-        $d = 0;
290
-        for ($i = 0; $i < 32; ++$i) {
291
-            $d |= self::chrToInt($pk[$i]);
292
-        }
293
-        if ($d === 0) {
294
-            throw new SodiumException('All zero public key');
295
-        }
296
-
297
-        /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
298
-        $orig = ParagonIE_Sodium_Compat::$fastMult;
299
-
300
-        // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification.
301
-        ParagonIE_Sodium_Compat::$fastMult = true;
302
-
303
-        /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */
304
-        $A = self::ge_frombytes_negate_vartime($pk);
305
-
306
-        /** @var string $hDigest */
307
-        $hDigest = hash(
308
-            'sha512',
309
-            self::substr($sig, 0, 32) .
310
-                self::substr($pk, 0, 32) .
311
-                $message,
312
-            true
313
-        );
314
-
315
-        /** @var string $h */
316
-        $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32);
317
-
318
-        /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */
319
-        $R = self::ge_double_scalarmult_vartime(
320
-            $h,
321
-            $A,
322
-            self::substr($sig, 32)
323
-        );
324
-
325
-        /** @var string $rcheck */
326
-        $rcheck = self::ge_tobytes($R);
327
-
328
-        // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
329
-        ParagonIE_Sodium_Compat::$fastMult = $orig;
330
-
331
-        return self::verify_32($rcheck, self::substr($sig, 0, 32));
332
-    }
333
-
334
-    /**
335
-     * @internal You should not use this directly from another application
336
-     *
337
-     * @param string $S
338
-     * @return bool
339
-     * @throws SodiumException
340
-     * @throws TypeError
341
-     */
342
-    public static function check_S_lt_L($S)
343
-    {
344
-        if (self::strlen($S) < 32) {
345
-            throw new SodiumException('Signature must be 32 bytes');
346
-        }
347
-        $L = array(
348
-            0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
349
-            0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
350
-            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
351
-            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
352
-        );
353
-        $c = 0;
354
-        $n = 1;
355
-        $i = 32;
356
-
357
-        /** @var array<int, int> $L */
358
-        do {
359
-            --$i;
360
-            $x = self::chrToInt($S[$i]);
361
-            $c |= (
362
-                (($x - $L[$i]) >> 8) & $n
363
-            );
364
-            $n &= (
365
-                (($x ^ $L[$i]) - 1) >> 8
366
-            );
367
-        } while ($i !== 0);
368
-
369
-        return $c === 0;
370
-    }
371
-
372
-    /**
373
-     * @param string $R
374
-     * @return bool
375
-     * @throws SodiumException
376
-     * @throws TypeError
377
-     */
378
-    public static function small_order($R)
379
-    {
380
-        /** @var array<int, array<int, int>> $blocklist */
381
-        $blocklist = array(
382
-            /* 0 (order 4) */
383
-            array(
384
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
385
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
386
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
387
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
388
-            ),
389
-            /* 1 (order 1) */
390
-            array(
391
-                0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
392
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
393
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
394
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
395
-            ),
396
-            /* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
397
-            array(
398
-                0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
399
-                0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
400
-                0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
401
-                0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05
402
-            ),
403
-            /* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
404
-            array(
405
-                0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
406
-                0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
407
-                0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
408
-                0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a
409
-            ),
410
-            /* p-1 (order 2) */
411
-            array(
412
-                0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
413
-                0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
414
-                0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
415
-                0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85
416
-            ),
417
-            /* p (order 4) */
418
-            array(
419
-                0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
420
-                0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
421
-                0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
422
-                0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa
423
-            ),
424
-            /* p+1 (order 1) */
425
-            array(
426
-                0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
427
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
428
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
429
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
430
-            ),
431
-            /* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
432
-            array(
433
-                0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
434
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
435
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
436
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
437
-            ),
438
-            /* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
439
-            array(
440
-                0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
441
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
442
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
443
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
444
-            ),
445
-            /* 2p-1 (order 2) */
446
-            array(
447
-                0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
448
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
449
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
450
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
451
-            ),
452
-            /* 2p (order 4) */
453
-            array(
454
-                0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
455
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
456
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
457
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
458
-            ),
459
-            /* 2p+1 (order 1) */
460
-            array(
461
-                0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
462
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
463
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
464
-                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
465
-            )
466
-        );
467
-        /** @var int $countBlocklist */
468
-        $countBlocklist = count($blocklist);
469
-
470
-        for ($i = 0; $i < $countBlocklist; ++$i) {
471
-            $c = 0;
472
-            for ($j = 0; $j < 32; ++$j) {
473
-                $c |= self::chrToInt($R[$j]) ^ (int) $blocklist[$i][$j];
474
-            }
475
-            if ($c === 0) {
476
-                return true;
477
-            }
478
-        }
479
-        return false;
480
-    }
481
-
482
-    /**
483
-     * @param string $s
484
-     * @return string
485
-     * @throws SodiumException
486
-     */
487
-    public static function scalar_complement($s)
488
-    {
489
-        $t_ = self::L . str_repeat("\x00", 32);
490
-        sodium_increment($t_);
491
-        $s_ = $s . str_repeat("\x00", 32);
492
-        ParagonIE_Sodium_Compat::sub($t_, $s_);
493
-        return self::sc_reduce($t_);
494
-    }
495
-
496
-    /**
497
-     * @return string
498
-     * @throws SodiumException
499
-     */
500
-    public static function scalar_random()
501
-    {
502
-        do {
503
-            $r = ParagonIE_Sodium_Compat::randombytes_buf(self::SCALAR_BYTES);
504
-            $r[self::SCALAR_BYTES - 1] = self::intToChr(
505
-                self::chrToInt($r[self::SCALAR_BYTES - 1]) & 0x1f
506
-            );
507
-        } while (
508
-            !self::check_S_lt_L($r) || ParagonIE_Sodium_Compat::is_zero($r)
509
-        );
510
-        return $r;
511
-    }
512
-
513
-    /**
514
-     * @param string $s
515
-     * @return string
516
-     * @throws SodiumException
517
-     */
518
-    public static function scalar_negate($s)
519
-    {
520
-        $t_ = self::L . str_repeat("\x00", 32) ;
521
-        $s_ = $s . str_repeat("\x00", 32) ;
522
-        ParagonIE_Sodium_Compat::sub($t_, $s_);
523
-        return self::sc_reduce($t_);
524
-    }
525
-
526
-    /**
527
-     * @param string $a
528
-     * @param string $b
529
-     * @return string
530
-     * @throws SodiumException
531
-     */
532
-    public static function scalar_add($a, $b)
533
-    {
534
-        $a_ = $a . str_repeat("\x00", 32);
535
-        $b_ = $b . str_repeat("\x00", 32);
536
-        ParagonIE_Sodium_Compat::add($a_, $b_);
537
-        return self::sc_reduce($a_);
538
-    }
539
-
540
-    /**
541
-     * @param string $x
542
-     * @param string $y
543
-     * @return string
544
-     * @throws SodiumException
545
-     */
546
-    public static function scalar_sub($x, $y)
547
-    {
548
-        $yn = self::scalar_negate($y);
549
-        return self::scalar_add($x, $yn);
550
-    }
12
+	const KEYPAIR_BYTES = 96;
13
+	const SEED_BYTES = 32;
14
+	const SCALAR_BYTES = 32;
15
+
16
+	/**
17
+	 * @internal You should not use this directly from another application
18
+	 *
19
+	 * @return string (96 bytes)
20
+	 * @throws Exception
21
+	 * @throws SodiumException
22
+	 * @throws TypeError
23
+	 */
24
+	public static function keypair()
25
+	{
26
+		$seed = random_bytes(self::SEED_BYTES);
27
+		$pk = '';
28
+		$sk = '';
29
+		self::seed_keypair($pk, $sk, $seed);
30
+		return $sk . $pk;
31
+	}
32
+
33
+	/**
34
+	 * @internal You should not use this directly from another application
35
+	 *
36
+	 * @param string $pk
37
+	 * @param string $sk
38
+	 * @param string $seed
39
+	 * @return string
40
+	 * @throws SodiumException
41
+	 * @throws TypeError
42
+	 */
43
+	public static function seed_keypair(&$pk, &$sk, $seed)
44
+	{
45
+		if (self::strlen($seed) !== self::SEED_BYTES) {
46
+			throw new RangeException('crypto_sign keypair seed must be 32 bytes long');
47
+		}
48
+
49
+		/** @var string $pk */
50
+		$pk = self::publickey_from_secretkey($seed);
51
+		$sk = $seed . $pk;
52
+		return $sk;
53
+	}
54
+
55
+	/**
56
+	 * @internal You should not use this directly from another application
57
+	 *
58
+	 * @param string $keypair
59
+	 * @return string
60
+	 * @throws TypeError
61
+	 */
62
+	public static function secretkey($keypair)
63
+	{
64
+		if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
65
+			throw new RangeException('crypto_sign keypair must be 96 bytes long');
66
+		}
67
+		return self::substr($keypair, 0, 64);
68
+	}
69
+
70
+	/**
71
+	 * @internal You should not use this directly from another application
72
+	 *
73
+	 * @param string $keypair
74
+	 * @return string
75
+	 * @throws TypeError
76
+	 */
77
+	public static function publickey($keypair)
78
+	{
79
+		if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
80
+			throw new RangeException('crypto_sign keypair must be 96 bytes long');
81
+		}
82
+		return self::substr($keypair, 64, 32);
83
+	}
84
+
85
+	/**
86
+	 * @internal You should not use this directly from another application
87
+	 *
88
+	 * @param string $sk
89
+	 * @return string
90
+	 * @throws SodiumException
91
+	 * @throws TypeError
92
+	 */
93
+	public static function publickey_from_secretkey($sk)
94
+	{
95
+		/** @var string $sk */
96
+		$sk = hash('sha512', self::substr($sk, 0, 32), true);
97
+		$sk[0] = self::intToChr(
98
+			self::chrToInt($sk[0]) & 248
99
+		);
100
+		$sk[31] = self::intToChr(
101
+			(self::chrToInt($sk[31]) & 63) | 64
102
+		);
103
+		return self::sk_to_pk($sk);
104
+	}
105
+
106
+	/**
107
+	 * @param string $pk
108
+	 * @return string
109
+	 * @throws SodiumException
110
+	 * @throws TypeError
111
+	 */
112
+	public static function pk_to_curve25519($pk)
113
+	{
114
+		if (self::small_order($pk)) {
115
+			throw new SodiumException('Public key is on a small order');
116
+		}
117
+		$A = self::ge_frombytes_negate_vartime(self::substr($pk, 0, 32));
118
+		$p1 = self::ge_mul_l($A);
119
+		if (!self::fe_isnonzero($p1->X)) {
120
+			throw new SodiumException('Unexpected zero result');
121
+		}
122
+
123
+		# fe_1(one_minus_y);
124
+		# fe_sub(one_minus_y, one_minus_y, A.Y);
125
+		# fe_invert(one_minus_y, one_minus_y);
126
+		$one_minux_y = self::fe_invert(
127
+			self::fe_sub(
128
+				self::fe_1(),
129
+				$A->Y
130
+			)
131
+		);
132
+
133
+		# fe_1(x);
134
+		# fe_add(x, x, A.Y);
135
+		# fe_mul(x, x, one_minus_y);
136
+		$x = self::fe_mul(
137
+			self::fe_add(self::fe_1(), $A->Y),
138
+			$one_minux_y
139
+		);
140
+
141
+		# fe_tobytes(curve25519_pk, x);
142
+		return self::fe_tobytes($x);
143
+	}
144
+
145
+	/**
146
+	 * @internal You should not use this directly from another application
147
+	 *
148
+	 * @param string $sk
149
+	 * @return string
150
+	 * @throws SodiumException
151
+	 * @throws TypeError
152
+	 */
153
+	public static function sk_to_pk($sk)
154
+	{
155
+		return self::ge_p3_tobytes(
156
+			self::ge_scalarmult_base(
157
+				self::substr($sk, 0, 32)
158
+			)
159
+		);
160
+	}
161
+
162
+	/**
163
+	 * @internal You should not use this directly from another application
164
+	 *
165
+	 * @param string $message
166
+	 * @param string $sk
167
+	 * @return string
168
+	 * @throws SodiumException
169
+	 * @throws TypeError
170
+	 */
171
+	public static function sign($message, $sk)
172
+	{
173
+		/** @var string $signature */
174
+		$signature = self::sign_detached($message, $sk);
175
+		return $signature . $message;
176
+	}
177
+
178
+	/**
179
+	 * @internal You should not use this directly from another application
180
+	 *
181
+	 * @param string $message A signed message
182
+	 * @param string $pk      Public key
183
+	 * @return string         Message (without signature)
184
+	 * @throws SodiumException
185
+	 * @throws TypeError
186
+	 */
187
+	public static function sign_open($message, $pk)
188
+	{
189
+		/** @var string $signature */
190
+		$signature = self::substr($message, 0, 64);
191
+
192
+		/** @var string $message */
193
+		$message = self::substr($message, 64);
194
+
195
+		if (self::verify_detached($signature, $message, $pk)) {
196
+			return $message;
197
+		}
198
+		throw new SodiumException('Invalid signature');
199
+	}
200
+
201
+	/**
202
+	 * @internal You should not use this directly from another application
203
+	 *
204
+	 * @param string $message
205
+	 * @param string $sk
206
+	 * @return string
207
+	 * @throws SodiumException
208
+	 * @throws TypeError
209
+	 */
210
+	public static function sign_detached($message, $sk)
211
+	{
212
+		# crypto_hash_sha512(az, sk, 32);
213
+		$az =  hash('sha512', self::substr($sk, 0, 32), true);
214
+
215
+		# az[0] &= 248;
216
+		# az[31] &= 63;
217
+		# az[31] |= 64;
218
+		$az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
219
+		$az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
220
+
221
+		# crypto_hash_sha512_init(&hs);
222
+		# crypto_hash_sha512_update(&hs, az + 32, 32);
223
+		# crypto_hash_sha512_update(&hs, m, mlen);
224
+		# crypto_hash_sha512_final(&hs, nonce);
225
+		$hs = hash_init('sha512');
226
+		hash_update($hs, self::substr($az, 32, 32));
227
+		hash_update($hs, $message);
228
+		$nonceHash = hash_final($hs, true);
229
+
230
+		# memmove(sig + 32, sk + 32, 32);
231
+		$pk = self::substr($sk, 32, 32);
232
+
233
+		# sc_reduce(nonce);
234
+		# ge_scalarmult_base(&R, nonce);
235
+		# ge_p3_tobytes(sig, &R);
236
+		$nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
237
+		$sig = self::ge_p3_tobytes(
238
+			self::ge_scalarmult_base($nonce)
239
+		);
240
+
241
+		# crypto_hash_sha512_init(&hs);
242
+		# crypto_hash_sha512_update(&hs, sig, 64);
243
+		# crypto_hash_sha512_update(&hs, m, mlen);
244
+		# crypto_hash_sha512_final(&hs, hram);
245
+		$hs = hash_init('sha512');
246
+		hash_update($hs, self::substr($sig, 0, 32));
247
+		hash_update($hs, self::substr($pk, 0, 32));
248
+		hash_update($hs, $message);
249
+		$hramHash = hash_final($hs, true);
250
+
251
+		# sc_reduce(hram);
252
+		# sc_muladd(sig + 32, hram, az, nonce);
253
+		$hram = self::sc_reduce($hramHash);
254
+		$sigAfter = self::sc_muladd($hram, $az, $nonce);
255
+		$sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
256
+
257
+		try {
258
+			ParagonIE_Sodium_Compat::memzero($az);
259
+		} catch (SodiumException $ex) {
260
+			$az = null;
261
+		}
262
+		return $sig;
263
+	}
264
+
265
+	/**
266
+	 * @internal You should not use this directly from another application
267
+	 *
268
+	 * @param string $sig
269
+	 * @param string $message
270
+	 * @param string $pk
271
+	 * @return bool
272
+	 * @throws SodiumException
273
+	 * @throws TypeError
274
+	 */
275
+	public static function verify_detached($sig, $message, $pk)
276
+	{
277
+		if (self::strlen($sig) < 64) {
278
+			throw new SodiumException('Signature is too short');
279
+		}
280
+		if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) {
281
+			throw new SodiumException('S < L - Invalid signature');
282
+		}
283
+		if (self::small_order($sig)) {
284
+			throw new SodiumException('Signature is on too small of an order');
285
+		}
286
+		if ((self::chrToInt($sig[63]) & 224) !== 0) {
287
+			throw new SodiumException('Invalid signature');
288
+		}
289
+		$d = 0;
290
+		for ($i = 0; $i < 32; ++$i) {
291
+			$d |= self::chrToInt($pk[$i]);
292
+		}
293
+		if ($d === 0) {
294
+			throw new SodiumException('All zero public key');
295
+		}
296
+
297
+		/** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
298
+		$orig = ParagonIE_Sodium_Compat::$fastMult;
299
+
300
+		// Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification.
301
+		ParagonIE_Sodium_Compat::$fastMult = true;
302
+
303
+		/** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */
304
+		$A = self::ge_frombytes_negate_vartime($pk);
305
+
306
+		/** @var string $hDigest */
307
+		$hDigest = hash(
308
+			'sha512',
309
+			self::substr($sig, 0, 32) .
310
+				self::substr($pk, 0, 32) .
311
+				$message,
312
+			true
313
+		);
314
+
315
+		/** @var string $h */
316
+		$h = self::sc_reduce($hDigest) . self::substr($hDigest, 32);
317
+
318
+		/** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */
319
+		$R = self::ge_double_scalarmult_vartime(
320
+			$h,
321
+			$A,
322
+			self::substr($sig, 32)
323
+		);
324
+
325
+		/** @var string $rcheck */
326
+		$rcheck = self::ge_tobytes($R);
327
+
328
+		// Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
329
+		ParagonIE_Sodium_Compat::$fastMult = $orig;
330
+
331
+		return self::verify_32($rcheck, self::substr($sig, 0, 32));
332
+	}
333
+
334
+	/**
335
+	 * @internal You should not use this directly from another application
336
+	 *
337
+	 * @param string $S
338
+	 * @return bool
339
+	 * @throws SodiumException
340
+	 * @throws TypeError
341
+	 */
342
+	public static function check_S_lt_L($S)
343
+	{
344
+		if (self::strlen($S) < 32) {
345
+			throw new SodiumException('Signature must be 32 bytes');
346
+		}
347
+		$L = array(
348
+			0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
349
+			0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
350
+			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
351
+			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
352
+		);
353
+		$c = 0;
354
+		$n = 1;
355
+		$i = 32;
356
+
357
+		/** @var array<int, int> $L */
358
+		do {
359
+			--$i;
360
+			$x = self::chrToInt($S[$i]);
361
+			$c |= (
362
+				(($x - $L[$i]) >> 8) & $n
363
+			);
364
+			$n &= (
365
+				(($x ^ $L[$i]) - 1) >> 8
366
+			);
367
+		} while ($i !== 0);
368
+
369
+		return $c === 0;
370
+	}
371
+
372
+	/**
373
+	 * @param string $R
374
+	 * @return bool
375
+	 * @throws SodiumException
376
+	 * @throws TypeError
377
+	 */
378
+	public static function small_order($R)
379
+	{
380
+		/** @var array<int, array<int, int>> $blocklist */
381
+		$blocklist = array(
382
+			/* 0 (order 4) */
383
+			array(
384
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
385
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
386
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
387
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
388
+			),
389
+			/* 1 (order 1) */
390
+			array(
391
+				0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
392
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
393
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
394
+				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
395
+			),
396
+			/* 2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
397
+			array(
398
+				0x26, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
399
+				0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
400
+				0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
401
+				0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x05
402
+			),
403
+			/* 55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
404
+			array(
405
+				0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
406
+				0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
407
+				0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
408
+				0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0x7a
409
+			),
410
+			/* p-1 (order 2) */
411
+			array(
412
+				0x13, 0xe8, 0x95, 0x8f, 0xc2, 0xb2, 0x27, 0xb0,
413
+				0x45, 0xc3, 0xf4, 0x89, 0xf2, 0xef, 0x98, 0xf0,
414
+				0xd5, 0xdf, 0xac, 0x05, 0xd3, 0xc6, 0x33, 0x39,
415
+				0xb1, 0x38, 0x02, 0x88, 0x6d, 0x53, 0xfc, 0x85
416
+			),
417
+			/* p (order 4) */
418
+			array(
419
+				0xb4, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f,
420
+				0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
421
+				0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6,
422
+				0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa
423
+			),
424
+			/* p+1 (order 1) */
425
+			array(
426
+				0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
427
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
428
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
429
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
430
+			),
431
+			/* p+2707385501144840649318225287225658788936804267575313519463743609750303402022 (order 8) */
432
+			array(
433
+				0xed, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
434
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
435
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
436
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
437
+			),
438
+			/* p+55188659117513257062467267217118295137698188065244968500265048394206261417927 (order 8) */
439
+			array(
440
+				0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
441
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
442
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
443
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f
444
+			),
445
+			/* 2p-1 (order 2) */
446
+			array(
447
+				0xd9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
448
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
449
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
450
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
451
+			),
452
+			/* 2p (order 4) */
453
+			array(
454
+				0xda, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
455
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
456
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
457
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
458
+			),
459
+			/* 2p+1 (order 1) */
460
+			array(
461
+				0xdb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
462
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
463
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
464
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
465
+			)
466
+		);
467
+		/** @var int $countBlocklist */
468
+		$countBlocklist = count($blocklist);
469
+
470
+		for ($i = 0; $i < $countBlocklist; ++$i) {
471
+			$c = 0;
472
+			for ($j = 0; $j < 32; ++$j) {
473
+				$c |= self::chrToInt($R[$j]) ^ (int) $blocklist[$i][$j];
474
+			}
475
+			if ($c === 0) {
476
+				return true;
477
+			}
478
+		}
479
+		return false;
480
+	}
481
+
482
+	/**
483
+	 * @param string $s
484
+	 * @return string
485
+	 * @throws SodiumException
486
+	 */
487
+	public static function scalar_complement($s)
488
+	{
489
+		$t_ = self::L . str_repeat("\x00", 32);
490
+		sodium_increment($t_);
491
+		$s_ = $s . str_repeat("\x00", 32);
492
+		ParagonIE_Sodium_Compat::sub($t_, $s_);
493
+		return self::sc_reduce($t_);
494
+	}
495
+
496
+	/**
497
+	 * @return string
498
+	 * @throws SodiumException
499
+	 */
500
+	public static function scalar_random()
501
+	{
502
+		do {
503
+			$r = ParagonIE_Sodium_Compat::randombytes_buf(self::SCALAR_BYTES);
504
+			$r[self::SCALAR_BYTES - 1] = self::intToChr(
505
+				self::chrToInt($r[self::SCALAR_BYTES - 1]) & 0x1f
506
+			);
507
+		} while (
508
+			!self::check_S_lt_L($r) || ParagonIE_Sodium_Compat::is_zero($r)
509
+		);
510
+		return $r;
511
+	}
512
+
513
+	/**
514
+	 * @param string $s
515
+	 * @return string
516
+	 * @throws SodiumException
517
+	 */
518
+	public static function scalar_negate($s)
519
+	{
520
+		$t_ = self::L . str_repeat("\x00", 32) ;
521
+		$s_ = $s . str_repeat("\x00", 32) ;
522
+		ParagonIE_Sodium_Compat::sub($t_, $s_);
523
+		return self::sc_reduce($t_);
524
+	}
525
+
526
+	/**
527
+	 * @param string $a
528
+	 * @param string $b
529
+	 * @return string
530
+	 * @throws SodiumException
531
+	 */
532
+	public static function scalar_add($a, $b)
533
+	{
534
+		$a_ = $a . str_repeat("\x00", 32);
535
+		$b_ = $b . str_repeat("\x00", 32);
536
+		ParagonIE_Sodium_Compat::add($a_, $b_);
537
+		return self::sc_reduce($a_);
538
+	}
539
+
540
+	/**
541
+	 * @param string $x
542
+	 * @param string $y
543
+	 * @return string
544
+	 * @throws SodiumException
545
+	 */
546
+	public static function scalar_sub($x, $y)
547
+	{
548
+		$yn = self::scalar_negate($y);
549
+		return self::scalar_add($x, $yn);
550
+	}
551 551
 }
Please login to merge, or discard this patch.
Spacing   +117 added lines, -117 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-if (class_exists('ParagonIE_Sodium_Core_Ed25519', false)) {
3
+if ( class_exists( 'ParagonIE_Sodium_Core_Ed25519', false ) ) {
4 4
     return;
5 5
 }
6 6
 
@@ -23,10 +23,10 @@  discard block
 block discarded – undo
23 23
      */
24 24
     public static function keypair()
25 25
     {
26
-        $seed = random_bytes(self::SEED_BYTES);
26
+        $seed = random_bytes( self::SEED_BYTES );
27 27
         $pk = '';
28 28
         $sk = '';
29
-        self::seed_keypair($pk, $sk, $seed);
29
+        self::seed_keypair( $pk, $sk, $seed );
30 30
         return $sk . $pk;
31 31
     }
32 32
 
@@ -40,14 +40,14 @@  discard block
 block discarded – undo
40 40
      * @throws SodiumException
41 41
      * @throws TypeError
42 42
      */
43
-    public static function seed_keypair(&$pk, &$sk, $seed)
43
+    public static function seed_keypair( &$pk, &$sk, $seed )
44 44
     {
45
-        if (self::strlen($seed) !== self::SEED_BYTES) {
46
-            throw new RangeException('crypto_sign keypair seed must be 32 bytes long');
45
+        if ( self::strlen( $seed ) !== self::SEED_BYTES ) {
46
+            throw new RangeException( 'crypto_sign keypair seed must be 32 bytes long' );
47 47
         }
48 48
 
49 49
         /** @var string $pk */
50
-        $pk = self::publickey_from_secretkey($seed);
50
+        $pk = self::publickey_from_secretkey( $seed );
51 51
         $sk = $seed . $pk;
52 52
         return $sk;
53 53
     }
@@ -59,12 +59,12 @@  discard block
 block discarded – undo
59 59
      * @return string
60 60
      * @throws TypeError
61 61
      */
62
-    public static function secretkey($keypair)
62
+    public static function secretkey( $keypair )
63 63
     {
64
-        if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
65
-            throw new RangeException('crypto_sign keypair must be 96 bytes long');
64
+        if ( self::strlen( $keypair ) !== self::KEYPAIR_BYTES ) {
65
+            throw new RangeException( 'crypto_sign keypair must be 96 bytes long' );
66 66
         }
67
-        return self::substr($keypair, 0, 64);
67
+        return self::substr( $keypair, 0, 64 );
68 68
     }
69 69
 
70 70
     /**
@@ -74,12 +74,12 @@  discard block
 block discarded – undo
74 74
      * @return string
75 75
      * @throws TypeError
76 76
      */
77
-    public static function publickey($keypair)
77
+    public static function publickey( $keypair )
78 78
     {
79
-        if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
80
-            throw new RangeException('crypto_sign keypair must be 96 bytes long');
79
+        if ( self::strlen( $keypair ) !== self::KEYPAIR_BYTES ) {
80
+            throw new RangeException( 'crypto_sign keypair must be 96 bytes long' );
81 81
         }
82
-        return self::substr($keypair, 64, 32);
82
+        return self::substr( $keypair, 64, 32 );
83 83
     }
84 84
 
85 85
     /**
@@ -90,17 +90,17 @@  discard block
 block discarded – undo
90 90
      * @throws SodiumException
91 91
      * @throws TypeError
92 92
      */
93
-    public static function publickey_from_secretkey($sk)
93
+    public static function publickey_from_secretkey( $sk )
94 94
     {
95 95
         /** @var string $sk */
96
-        $sk = hash('sha512', self::substr($sk, 0, 32), true);
97
-        $sk[0] = self::intToChr(
98
-            self::chrToInt($sk[0]) & 248
96
+        $sk = hash( 'sha512', self::substr( $sk, 0, 32 ), true );
97
+        $sk[ 0 ] = self::intToChr(
98
+            self::chrToInt( $sk[ 0 ] ) & 248
99 99
         );
100
-        $sk[31] = self::intToChr(
101
-            (self::chrToInt($sk[31]) & 63) | 64
100
+        $sk[ 31 ] = self::intToChr(
101
+            ( self::chrToInt( $sk[ 31 ] ) & 63 ) | 64
102 102
         );
103
-        return self::sk_to_pk($sk);
103
+        return self::sk_to_pk( $sk );
104 104
     }
105 105
 
106 106
     /**
@@ -109,15 +109,15 @@  discard block
 block discarded – undo
109 109
      * @throws SodiumException
110 110
      * @throws TypeError
111 111
      */
112
-    public static function pk_to_curve25519($pk)
112
+    public static function pk_to_curve25519( $pk )
113 113
     {
114
-        if (self::small_order($pk)) {
115
-            throw new SodiumException('Public key is on a small order');
114
+        if ( self::small_order( $pk ) ) {
115
+            throw new SodiumException( 'Public key is on a small order' );
116 116
         }
117
-        $A = self::ge_frombytes_negate_vartime(self::substr($pk, 0, 32));
118
-        $p1 = self::ge_mul_l($A);
119
-        if (!self::fe_isnonzero($p1->X)) {
120
-            throw new SodiumException('Unexpected zero result');
117
+        $A = self::ge_frombytes_negate_vartime( self::substr( $pk, 0, 32 ) );
118
+        $p1 = self::ge_mul_l( $A );
119
+        if ( ! self::fe_isnonzero( $p1->X ) ) {
120
+            throw new SodiumException( 'Unexpected zero result' );
121 121
         }
122 122
 
123 123
         # fe_1(one_minus_y);
@@ -134,12 +134,12 @@  discard block
 block discarded – undo
134 134
         # fe_add(x, x, A.Y);
135 135
         # fe_mul(x, x, one_minus_y);
136 136
         $x = self::fe_mul(
137
-            self::fe_add(self::fe_1(), $A->Y),
137
+            self::fe_add( self::fe_1(), $A->Y ),
138 138
             $one_minux_y
139 139
         );
140 140
 
141 141
         # fe_tobytes(curve25519_pk, x);
142
-        return self::fe_tobytes($x);
142
+        return self::fe_tobytes( $x );
143 143
     }
144 144
 
145 145
     /**
@@ -150,11 +150,11 @@  discard block
 block discarded – undo
150 150
      * @throws SodiumException
151 151
      * @throws TypeError
152 152
      */
153
-    public static function sk_to_pk($sk)
153
+    public static function sk_to_pk( $sk )
154 154
     {
155 155
         return self::ge_p3_tobytes(
156 156
             self::ge_scalarmult_base(
157
-                self::substr($sk, 0, 32)
157
+                self::substr( $sk, 0, 32 )
158 158
             )
159 159
         );
160 160
     }
@@ -168,10 +168,10 @@  discard block
 block discarded – undo
168 168
      * @throws SodiumException
169 169
      * @throws TypeError
170 170
      */
171
-    public static function sign($message, $sk)
171
+    public static function sign( $message, $sk )
172 172
     {
173 173
         /** @var string $signature */
174
-        $signature = self::sign_detached($message, $sk);
174
+        $signature = self::sign_detached( $message, $sk );
175 175
         return $signature . $message;
176 176
     }
177 177
 
@@ -184,18 +184,18 @@  discard block
 block discarded – undo
184 184
      * @throws SodiumException
185 185
      * @throws TypeError
186 186
      */
187
-    public static function sign_open($message, $pk)
187
+    public static function sign_open( $message, $pk )
188 188
     {
189 189
         /** @var string $signature */
190
-        $signature = self::substr($message, 0, 64);
190
+        $signature = self::substr( $message, 0, 64 );
191 191
 
192 192
         /** @var string $message */
193
-        $message = self::substr($message, 64);
193
+        $message = self::substr( $message, 64 );
194 194
 
195
-        if (self::verify_detached($signature, $message, $pk)) {
195
+        if ( self::verify_detached( $signature, $message, $pk ) ) {
196 196
             return $message;
197 197
         }
198
-        throw new SodiumException('Invalid signature');
198
+        throw new SodiumException( 'Invalid signature' );
199 199
     }
200 200
 
201 201
     /**
@@ -207,56 +207,56 @@  discard block
 block discarded – undo
207 207
      * @throws SodiumException
208 208
      * @throws TypeError
209 209
      */
210
-    public static function sign_detached($message, $sk)
210
+    public static function sign_detached( $message, $sk )
211 211
     {
212 212
         # crypto_hash_sha512(az, sk, 32);
213
-        $az =  hash('sha512', self::substr($sk, 0, 32), true);
213
+        $az = hash( 'sha512', self::substr( $sk, 0, 32 ), true );
214 214
 
215 215
         # az[0] &= 248;
216 216
         # az[31] &= 63;
217 217
         # az[31] |= 64;
218
-        $az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
219
-        $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);
218
+        $az[ 0 ] = self::intToChr( self::chrToInt( $az[ 0 ] ) & 248 );
219
+        $az[ 31 ] = self::intToChr( ( self::chrToInt( $az[ 31 ] ) & 63 ) | 64 );
220 220
 
221 221
         # crypto_hash_sha512_init(&hs);
222 222
         # crypto_hash_sha512_update(&hs, az + 32, 32);
223 223
         # crypto_hash_sha512_update(&hs, m, mlen);
224 224
         # crypto_hash_sha512_final(&hs, nonce);
225
-        $hs = hash_init('sha512');
226
-        hash_update($hs, self::substr($az, 32, 32));
227
-        hash_update($hs, $message);
228
-        $nonceHash = hash_final($hs, true);
225
+        $hs = hash_init( 'sha512' );
226
+        hash_update( $hs, self::substr( $az, 32, 32 ) );
227
+        hash_update( $hs, $message );
228
+        $nonceHash = hash_final( $hs, true );
229 229
 
230 230
         # memmove(sig + 32, sk + 32, 32);
231
-        $pk = self::substr($sk, 32, 32);
231
+        $pk = self::substr( $sk, 32, 32 );
232 232
 
233 233
         # sc_reduce(nonce);
234 234
         # ge_scalarmult_base(&R, nonce);
235 235
         # ge_p3_tobytes(sig, &R);
236
-        $nonce = self::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
236
+        $nonce = self::sc_reduce( $nonceHash ) . self::substr( $nonceHash, 32 );
237 237
         $sig = self::ge_p3_tobytes(
238
-            self::ge_scalarmult_base($nonce)
238
+            self::ge_scalarmult_base( $nonce )
239 239
         );
240 240
 
241 241
         # crypto_hash_sha512_init(&hs);
242 242
         # crypto_hash_sha512_update(&hs, sig, 64);
243 243
         # crypto_hash_sha512_update(&hs, m, mlen);
244 244
         # crypto_hash_sha512_final(&hs, hram);
245
-        $hs = hash_init('sha512');
246
-        hash_update($hs, self::substr($sig, 0, 32));
247
-        hash_update($hs, self::substr($pk, 0, 32));
248
-        hash_update($hs, $message);
249
-        $hramHash = hash_final($hs, true);
245
+        $hs = hash_init( 'sha512' );
246
+        hash_update( $hs, self::substr( $sig, 0, 32 ) );
247
+        hash_update( $hs, self::substr( $pk, 0, 32 ) );
248
+        hash_update( $hs, $message );
249
+        $hramHash = hash_final( $hs, true );
250 250
 
251 251
         # sc_reduce(hram);
252 252
         # sc_muladd(sig + 32, hram, az, nonce);
253
-        $hram = self::sc_reduce($hramHash);
254
-        $sigAfter = self::sc_muladd($hram, $az, $nonce);
255
-        $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
253
+        $hram = self::sc_reduce( $hramHash );
254
+        $sigAfter = self::sc_muladd( $hram, $az, $nonce );
255
+        $sig = self::substr( $sig, 0, 32 ) . self::substr( $sigAfter, 0, 32 );
256 256
 
257 257
         try {
258
-            ParagonIE_Sodium_Compat::memzero($az);
259
-        } catch (SodiumException $ex) {
258
+            ParagonIE_Sodium_Compat::memzero( $az );
259
+        } catch ( SodiumException $ex ) {
260 260
             $az = null;
261 261
         }
262 262
         return $sig;
@@ -272,26 +272,26 @@  discard block
 block discarded – undo
272 272
      * @throws SodiumException
273 273
      * @throws TypeError
274 274
      */
275
-    public static function verify_detached($sig, $message, $pk)
275
+    public static function verify_detached( $sig, $message, $pk )
276 276
     {
277
-        if (self::strlen($sig) < 64) {
278
-            throw new SodiumException('Signature is too short');
277
+        if ( self::strlen( $sig ) < 64 ) {
278
+            throw new SodiumException( 'Signature is too short' );
279 279
         }
280
-        if ((self::chrToInt($sig[63]) & 240) && self::check_S_lt_L(self::substr($sig, 32, 32))) {
281
-            throw new SodiumException('S < L - Invalid signature');
280
+        if ( ( self::chrToInt( $sig[ 63 ] ) & 240 ) && self::check_S_lt_L( self::substr( $sig, 32, 32 ) ) ) {
281
+            throw new SodiumException( 'S < L - Invalid signature' );
282 282
         }
283
-        if (self::small_order($sig)) {
284
-            throw new SodiumException('Signature is on too small of an order');
283
+        if ( self::small_order( $sig ) ) {
284
+            throw new SodiumException( 'Signature is on too small of an order' );
285 285
         }
286
-        if ((self::chrToInt($sig[63]) & 224) !== 0) {
287
-            throw new SodiumException('Invalid signature');
286
+        if ( ( self::chrToInt( $sig[ 63 ] ) & 224 ) !== 0 ) {
287
+            throw new SodiumException( 'Invalid signature' );
288 288
         }
289 289
         $d = 0;
290
-        for ($i = 0; $i < 32; ++$i) {
291
-            $d |= self::chrToInt($pk[$i]);
290
+        for ( $i = 0; $i < 32; ++$i ) {
291
+            $d |= self::chrToInt( $pk[ $i ] );
292 292
         }
293
-        if ($d === 0) {
294
-            throw new SodiumException('All zero public key');
293
+        if ( $d === 0 ) {
294
+            throw new SodiumException( 'All zero public key' );
295 295
         }
296 296
 
297 297
         /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */
@@ -301,34 +301,34 @@  discard block
 block discarded – undo
301 301
         ParagonIE_Sodium_Compat::$fastMult = true;
302 302
 
303 303
         /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */
304
-        $A = self::ge_frombytes_negate_vartime($pk);
304
+        $A = self::ge_frombytes_negate_vartime( $pk );
305 305
 
306 306
         /** @var string $hDigest */
307 307
         $hDigest = hash(
308 308
             'sha512',
309
-            self::substr($sig, 0, 32) .
310
-                self::substr($pk, 0, 32) .
309
+            self::substr( $sig, 0, 32 ) .
310
+                self::substr( $pk, 0, 32 ) .
311 311
                 $message,
312 312
             true
313 313
         );
314 314
 
315 315
         /** @var string $h */
316
-        $h = self::sc_reduce($hDigest) . self::substr($hDigest, 32);
316
+        $h = self::sc_reduce( $hDigest ) . self::substr( $hDigest, 32 );
317 317
 
318 318
         /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */
319 319
         $R = self::ge_double_scalarmult_vartime(
320 320
             $h,
321 321
             $A,
322
-            self::substr($sig, 32)
322
+            self::substr( $sig, 32 )
323 323
         );
324 324
 
325 325
         /** @var string $rcheck */
326
-        $rcheck = self::ge_tobytes($R);
326
+        $rcheck = self::ge_tobytes( $R );
327 327
 
328 328
         // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before.
329 329
         ParagonIE_Sodium_Compat::$fastMult = $orig;
330 330
 
331
-        return self::verify_32($rcheck, self::substr($sig, 0, 32));
331
+        return self::verify_32( $rcheck, self::substr( $sig, 0, 32 ) );
332 332
     }
333 333
 
334 334
     /**
@@ -339,10 +339,10 @@  discard block
 block discarded – undo
339 339
      * @throws SodiumException
340 340
      * @throws TypeError
341 341
      */
342
-    public static function check_S_lt_L($S)
342
+    public static function check_S_lt_L( $S )
343 343
     {
344
-        if (self::strlen($S) < 32) {
345
-            throw new SodiumException('Signature must be 32 bytes');
344
+        if ( self::strlen( $S ) < 32 ) {
345
+            throw new SodiumException( 'Signature must be 32 bytes' );
346 346
         }
347 347
         $L = array(
348 348
             0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
@@ -357,14 +357,14 @@  discard block
 block discarded – undo
357 357
         /** @var array<int, int> $L */
358 358
         do {
359 359
             --$i;
360
-            $x = self::chrToInt($S[$i]);
360
+            $x = self::chrToInt( $S[ $i ] );
361 361
             $c |= (
362
-                (($x - $L[$i]) >> 8) & $n
362
+                ( ( $x - $L[ $i ] ) >> 8 ) & $n
363 363
             );
364 364
             $n &= (
365
-                (($x ^ $L[$i]) - 1) >> 8
365
+                ( ( $x ^ $L[ $i ] ) - 1 ) >> 8
366 366
             );
367
-        } while ($i !== 0);
367
+        } while ( $i !== 0 );
368 368
 
369 369
         return $c === 0;
370 370
     }
@@ -375,7 +375,7 @@  discard block
 block discarded – undo
375 375
      * @throws SodiumException
376 376
      * @throws TypeError
377 377
      */
378
-    public static function small_order($R)
378
+    public static function small_order( $R )
379 379
     {
380 380
         /** @var array<int, array<int, int>> $blocklist */
381 381
         $blocklist = array(
@@ -465,14 +465,14 @@  discard block
 block discarded – undo
465 465
             )
466 466
         );
467 467
         /** @var int $countBlocklist */
468
-        $countBlocklist = count($blocklist);
468
+        $countBlocklist = count( $blocklist );
469 469
 
470
-        for ($i = 0; $i < $countBlocklist; ++$i) {
470
+        for ( $i = 0; $i < $countBlocklist; ++$i ) {
471 471
             $c = 0;
472
-            for ($j = 0; $j < 32; ++$j) {
473
-                $c |= self::chrToInt($R[$j]) ^ (int) $blocklist[$i][$j];
472
+            for ( $j = 0; $j < 32; ++$j ) {
473
+                $c |= self::chrToInt( $R[ $j ] ) ^ (int)$blocklist[ $i ][ $j ];
474 474
             }
475
-            if ($c === 0) {
475
+            if ( $c === 0 ) {
476 476
                 return true;
477 477
             }
478 478
         }
@@ -484,13 +484,13 @@  discard block
 block discarded – undo
484 484
      * @return string
485 485
      * @throws SodiumException
486 486
      */
487
-    public static function scalar_complement($s)
487
+    public static function scalar_complement( $s )
488 488
     {
489
-        $t_ = self::L . str_repeat("\x00", 32);
490
-        sodium_increment($t_);
491
-        $s_ = $s . str_repeat("\x00", 32);
492
-        ParagonIE_Sodium_Compat::sub($t_, $s_);
493
-        return self::sc_reduce($t_);
489
+        $t_ = self::L . str_repeat( "\x00", 32 );
490
+        sodium_increment( $t_ );
491
+        $s_ = $s . str_repeat( "\x00", 32 );
492
+        ParagonIE_Sodium_Compat::sub( $t_, $s_ );
493
+        return self::sc_reduce( $t_ );
494 494
     }
495 495
 
496 496
     /**
@@ -500,12 +500,12 @@  discard block
 block discarded – undo
500 500
     public static function scalar_random()
501 501
     {
502 502
         do {
503
-            $r = ParagonIE_Sodium_Compat::randombytes_buf(self::SCALAR_BYTES);
504
-            $r[self::SCALAR_BYTES - 1] = self::intToChr(
505
-                self::chrToInt($r[self::SCALAR_BYTES - 1]) & 0x1f
503
+            $r = ParagonIE_Sodium_Compat::randombytes_buf( self::SCALAR_BYTES );
504
+            $r[ self::SCALAR_BYTES - 1 ] = self::intToChr(
505
+                self::chrToInt( $r[ self::SCALAR_BYTES - 1 ] ) & 0x1f
506 506
             );
507 507
         } while (
508
-            !self::check_S_lt_L($r) || ParagonIE_Sodium_Compat::is_zero($r)
508
+            ! self::check_S_lt_L( $r ) || ParagonIE_Sodium_Compat::is_zero( $r )
509 509
         );
510 510
         return $r;
511 511
     }
@@ -515,12 +515,12 @@  discard block
 block discarded – undo
515 515
      * @return string
516 516
      * @throws SodiumException
517 517
      */
518
-    public static function scalar_negate($s)
518
+    public static function scalar_negate( $s )
519 519
     {
520
-        $t_ = self::L . str_repeat("\x00", 32) ;
521
-        $s_ = $s . str_repeat("\x00", 32) ;
522
-        ParagonIE_Sodium_Compat::sub($t_, $s_);
523
-        return self::sc_reduce($t_);
520
+        $t_ = self::L . str_repeat( "\x00", 32 );
521
+        $s_ = $s . str_repeat( "\x00", 32 );
522
+        ParagonIE_Sodium_Compat::sub( $t_, $s_ );
523
+        return self::sc_reduce( $t_ );
524 524
     }
525 525
 
526 526
     /**
@@ -529,12 +529,12 @@  discard block
 block discarded – undo
529 529
      * @return string
530 530
      * @throws SodiumException
531 531
      */
532
-    public static function scalar_add($a, $b)
532
+    public static function scalar_add( $a, $b )
533 533
     {
534
-        $a_ = $a . str_repeat("\x00", 32);
535
-        $b_ = $b . str_repeat("\x00", 32);
536
-        ParagonIE_Sodium_Compat::add($a_, $b_);
537
-        return self::sc_reduce($a_);
534
+        $a_ = $a . str_repeat( "\x00", 32 );
535
+        $b_ = $b . str_repeat( "\x00", 32 );
536
+        ParagonIE_Sodium_Compat::add( $a_, $b_ );
537
+        return self::sc_reduce( $a_ );
538 538
     }
539 539
 
540 540
     /**
@@ -543,9 +543,9 @@  discard block
 block discarded – undo
543 543
      * @return string
544 544
      * @throws SodiumException
545 545
      */
546
-    public static function scalar_sub($x, $y)
546
+    public static function scalar_sub( $x, $y )
547 547
     {
548
-        $yn = self::scalar_negate($y);
549
-        return self::scalar_add($x, $yn);
548
+        $yn = self::scalar_negate( $y );
549
+        return self::scalar_add( $x, $yn );
550 550
     }
551 551
 }
Please login to merge, or discard this patch.
Braces   +19 added lines, -38 removed lines patch added patch discarded remove patch
@@ -7,8 +7,7 @@  discard block
 block discarded – undo
7 7
 /**
8 8
  * Class ParagonIE_Sodium_Core_Ed25519
9 9
  */
10
-abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_Curve25519
11
-{
10
+abstract class ParagonIE_Sodium_Core_Ed25519 extends ParagonIE_Sodium_Core_Curve25519 {
12 11
     const KEYPAIR_BYTES = 96;
13 12
     const SEED_BYTES = 32;
14 13
     const SCALAR_BYTES = 32;
@@ -21,8 +20,7 @@  discard block
 block discarded – undo
21 20
      * @throws SodiumException
22 21
      * @throws TypeError
23 22
      */
24
-    public static function keypair()
25
-    {
23
+    public static function keypair() {
26 24
         $seed = random_bytes(self::SEED_BYTES);
27 25
         $pk = '';
28 26
         $sk = '';
@@ -40,8 +38,7 @@  discard block
 block discarded – undo
40 38
      * @throws SodiumException
41 39
      * @throws TypeError
42 40
      */
43
-    public static function seed_keypair(&$pk, &$sk, $seed)
44
-    {
41
+    public static function seed_keypair(&$pk, &$sk, $seed) {
45 42
         if (self::strlen($seed) !== self::SEED_BYTES) {
46 43
             throw new RangeException('crypto_sign keypair seed must be 32 bytes long');
47 44
         }
@@ -59,8 +56,7 @@  discard block
 block discarded – undo
59 56
      * @return string
60 57
      * @throws TypeError
61 58
      */
62
-    public static function secretkey($keypair)
63
-    {
59
+    public static function secretkey($keypair) {
64 60
         if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
65 61
             throw new RangeException('crypto_sign keypair must be 96 bytes long');
66 62
         }
@@ -74,8 +70,7 @@  discard block
 block discarded – undo
74 70
      * @return string
75 71
      * @throws TypeError
76 72
      */
77
-    public static function publickey($keypair)
78
-    {
73
+    public static function publickey($keypair) {
79 74
         if (self::strlen($keypair) !== self::KEYPAIR_BYTES) {
80 75
             throw new RangeException('crypto_sign keypair must be 96 bytes long');
81 76
         }
@@ -90,8 +85,7 @@  discard block
 block discarded – undo
90 85
      * @throws SodiumException
91 86
      * @throws TypeError
92 87
      */
93
-    public static function publickey_from_secretkey($sk)
94
-    {
88
+    public static function publickey_from_secretkey($sk) {
95 89
         /** @var string $sk */
96 90
         $sk = hash('sha512', self::substr($sk, 0, 32), true);
97 91
         $sk[0] = self::intToChr(
@@ -109,8 +103,7 @@  discard block
 block discarded – undo
109 103
      * @throws SodiumException
110 104
      * @throws TypeError
111 105
      */
112
-    public static function pk_to_curve25519($pk)
113
-    {
106
+    public static function pk_to_curve25519($pk) {
114 107
         if (self::small_order($pk)) {
115 108
             throw new SodiumException('Public key is on a small order');
116 109
         }
@@ -150,8 +143,7 @@  discard block
 block discarded – undo
150 143
      * @throws SodiumException
151 144
      * @throws TypeError
152 145
      */
153
-    public static function sk_to_pk($sk)
154
-    {
146
+    public static function sk_to_pk($sk) {
155 147
         return self::ge_p3_tobytes(
156 148
             self::ge_scalarmult_base(
157 149
                 self::substr($sk, 0, 32)
@@ -168,8 +160,7 @@  discard block
 block discarded – undo
168 160
      * @throws SodiumException
169 161
      * @throws TypeError
170 162
      */
171
-    public static function sign($message, $sk)
172
-    {
163
+    public static function sign($message, $sk) {
173 164
         /** @var string $signature */
174 165
         $signature = self::sign_detached($message, $sk);
175 166
         return $signature . $message;
@@ -184,8 +175,7 @@  discard block
 block discarded – undo
184 175
      * @throws SodiumException
185 176
      * @throws TypeError
186 177
      */
187
-    public static function sign_open($message, $pk)
188
-    {
178
+    public static function sign_open($message, $pk) {
189 179
         /** @var string $signature */
190 180
         $signature = self::substr($message, 0, 64);
191 181
 
@@ -207,8 +197,7 @@  discard block
 block discarded – undo
207 197
      * @throws SodiumException
208 198
      * @throws TypeError
209 199
      */
210
-    public static function sign_detached($message, $sk)
211
-    {
200
+    public static function sign_detached($message, $sk) {
212 201
         # crypto_hash_sha512(az, sk, 32);
213 202
         $az =  hash('sha512', self::substr($sk, 0, 32), true);
214 203
 
@@ -272,8 +261,7 @@  discard block
 block discarded – undo
272 261
      * @throws SodiumException
273 262
      * @throws TypeError
274 263
      */
275
-    public static function verify_detached($sig, $message, $pk)
276
-    {
264
+    public static function verify_detached($sig, $message, $pk) {
277 265
         if (self::strlen($sig) < 64) {
278 266
             throw new SodiumException('Signature is too short');
279 267
         }
@@ -339,8 +327,7 @@  discard block
 block discarded – undo
339 327
      * @throws SodiumException
340 328
      * @throws TypeError
341 329
      */
342
-    public static function check_S_lt_L($S)
343
-    {
330
+    public static function check_S_lt_L($S) {
344 331
         if (self::strlen($S) < 32) {
345 332
             throw new SodiumException('Signature must be 32 bytes');
346 333
         }
@@ -375,8 +362,7 @@  discard block
 block discarded – undo
375 362
      * @throws SodiumException
376 363
      * @throws TypeError
377 364
      */
378
-    public static function small_order($R)
379
-    {
365
+    public static function small_order($R) {
380 366
         /** @var array<int, array<int, int>> $blocklist */
381 367
         $blocklist = array(
382 368
             /* 0 (order 4) */
@@ -484,8 +470,7 @@  discard block
 block discarded – undo
484 470
      * @return string
485 471
      * @throws SodiumException
486 472
      */
487
-    public static function scalar_complement($s)
488
-    {
473
+    public static function scalar_complement($s) {
489 474
         $t_ = self::L . str_repeat("\x00", 32);
490 475
         sodium_increment($t_);
491 476
         $s_ = $s . str_repeat("\x00", 32);
@@ -497,8 +482,7 @@  discard block
 block discarded – undo
497 482
      * @return string
498 483
      * @throws SodiumException
499 484
      */
500
-    public static function scalar_random()
501
-    {
485
+    public static function scalar_random() {
502 486
         do {
503 487
             $r = ParagonIE_Sodium_Compat::randombytes_buf(self::SCALAR_BYTES);
504 488
             $r[self::SCALAR_BYTES - 1] = self::intToChr(
@@ -515,8 +499,7 @@  discard block
 block discarded – undo
515 499
      * @return string
516 500
      * @throws SodiumException
517 501
      */
518
-    public static function scalar_negate($s)
519
-    {
502
+    public static function scalar_negate($s) {
520 503
         $t_ = self::L . str_repeat("\x00", 32) ;
521 504
         $s_ = $s . str_repeat("\x00", 32) ;
522 505
         ParagonIE_Sodium_Compat::sub($t_, $s_);
@@ -529,8 +512,7 @@  discard block
 block discarded – undo
529 512
      * @return string
530 513
      * @throws SodiumException
531 514
      */
532
-    public static function scalar_add($a, $b)
533
-    {
515
+    public static function scalar_add($a, $b) {
534 516
         $a_ = $a . str_repeat("\x00", 32);
535 517
         $b_ = $b . str_repeat("\x00", 32);
536 518
         ParagonIE_Sodium_Compat::add($a_, $b_);
@@ -543,8 +525,7 @@  discard block
 block discarded – undo
543 525
      * @return string
544 526
      * @throws SodiumException
545 527
      */
546
-    public static function scalar_sub($x, $y)
547
-    {
528
+    public static function scalar_sub($x, $y) {
548 529
         $yn = self::scalar_negate($y);
549 530
         return self::scalar_add($x, $yn);
550 531
     }
Please login to merge, or discard this patch.
vendor/paragonie/sodium_compat/src/Core/Util.php 3 patches
Indentation   +936 added lines, -936 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 if (class_exists('ParagonIE_Sodium_Core_Util', false)) {
4
-    return;
4
+	return;
5 5
 }
6 6
 
7 7
 /**
@@ -9,939 +9,939 @@  discard block
 block discarded – undo
9 9
  */
10 10
 abstract class ParagonIE_Sodium_Core_Util
11 11
 {
12
-    /**
13
-     * @param int $integer
14
-     * @param int $size (16, 32, 64)
15
-     * @return int
16
-     */
17
-    public static function abs($integer, $size = 0)
18
-    {
19
-        /** @var int $realSize */
20
-        $realSize = (PHP_INT_SIZE << 3) - 1;
21
-        if ($size) {
22
-            --$size;
23
-        } else {
24
-            /** @var int $size */
25
-            $size = $realSize;
26
-        }
27
-
28
-        $negative = -(($integer >> $size) & 1);
29
-        return (int) (
30
-            ($integer ^ $negative)
31
-                +
32
-            (($negative >> $realSize) & 1)
33
-        );
34
-    }
35
-
36
-    /**
37
-     * Convert a binary string into a hexadecimal string without cache-timing
38
-     * leaks
39
-     *
40
-     * @internal You should not use this directly from another application
41
-     *
42
-     * @param string $binaryString (raw binary)
43
-     * @return string
44
-     * @throws TypeError
45
-     */
46
-    public static function bin2hex($binaryString)
47
-    {
48
-        /* Type checks: */
49
-        if (!is_string($binaryString)) {
50
-            throw new TypeError('Argument 1 must be a string, ' . gettype($binaryString) . ' given.');
51
-        }
52
-
53
-        $hex = '';
54
-        $len = self::strlen($binaryString);
55
-        for ($i = 0; $i < $len; ++$i) {
56
-            /** @var array<int, int> $chunk */
57
-            $chunk = unpack('C', $binaryString[$i]);
58
-            /** @var int $c */
59
-            $c = $chunk[1] & 0xf;
60
-            /** @var int $b */
61
-            $b = $chunk[1] >> 4;
62
-            $hex .= pack(
63
-                'CC',
64
-                (87 + $b + ((($b - 10) >> 8) & ~38)),
65
-                (87 + $c + ((($c - 10) >> 8) & ~38))
66
-            );
67
-        }
68
-        return $hex;
69
-    }
70
-
71
-    /**
72
-     * Convert a binary string into a hexadecimal string without cache-timing
73
-     * leaks, returning uppercase letters (as per RFC 4648)
74
-     *
75
-     * @internal You should not use this directly from another application
76
-     *
77
-     * @param string $bin_string (raw binary)
78
-     * @return string
79
-     * @throws TypeError
80
-     */
81
-    public static function bin2hexUpper($bin_string)
82
-    {
83
-        $hex = '';
84
-        $len = self::strlen($bin_string);
85
-        for ($i = 0; $i < $len; ++$i) {
86
-            /** @var array<int, int> $chunk */
87
-            $chunk = unpack('C', $bin_string[$i]);
88
-            /**
89
-             * Lower 16 bits
90
-             *
91
-             * @var int $c
92
-             */
93
-            $c = $chunk[1] & 0xf;
94
-
95
-            /**
96
-             * Upper 16 bits
97
-             * @var int $b
98
-             */
99
-            $b = $chunk[1] >> 4;
100
-
101
-            /**
102
-             * Use pack() and binary operators to turn the two integers
103
-             * into hexadecimal characters. We don't use chr() here, because
104
-             * it uses a lookup table internally and we want to avoid
105
-             * cache-timing side-channels.
106
-             */
107
-            $hex .= pack(
108
-                'CC',
109
-                (55 + $b + ((($b - 10) >> 8) & ~6)),
110
-                (55 + $c + ((($c - 10) >> 8) & ~6))
111
-            );
112
-        }
113
-        return $hex;
114
-    }
115
-
116
-    /**
117
-     * Cache-timing-safe variant of ord()
118
-     *
119
-     * @internal You should not use this directly from another application
120
-     *
121
-     * @param string $chr
122
-     * @return int
123
-     * @throws SodiumException
124
-     * @throws TypeError
125
-     */
126
-    public static function chrToInt($chr)
127
-    {
128
-        /* Type checks: */
129
-        if (!is_string($chr)) {
130
-            throw new TypeError('Argument 1 must be a string, ' . gettype($chr) . ' given.');
131
-        }
132
-        if (self::strlen($chr) !== 1) {
133
-            throw new SodiumException('chrToInt() expects a string that is exactly 1 character long');
134
-        }
135
-        /** @var array<int, int> $chunk */
136
-        $chunk = unpack('C', $chr);
137
-        return (int) ($chunk[1]);
138
-    }
139
-
140
-    /**
141
-     * Compares two strings.
142
-     *
143
-     * @internal You should not use this directly from another application
144
-     *
145
-     * @param string $left
146
-     * @param string $right
147
-     * @param int $len
148
-     * @return int
149
-     * @throws SodiumException
150
-     * @throws TypeError
151
-     */
152
-    public static function compare($left, $right, $len = null)
153
-    {
154
-        $leftLen = self::strlen($left);
155
-        $rightLen = self::strlen($right);
156
-        if ($len === null) {
157
-            $len = max($leftLen, $rightLen);
158
-            $left = str_pad($left, $len, "\x00", STR_PAD_RIGHT);
159
-            $right = str_pad($right, $len, "\x00", STR_PAD_RIGHT);
160
-        }
161
-
162
-        $gt = 0;
163
-        $eq = 1;
164
-        $i = $len;
165
-        while ($i !== 0) {
166
-            --$i;
167
-            $gt |= ((self::chrToInt($right[$i]) - self::chrToInt($left[$i])) >> 8) & $eq;
168
-            $eq &= ((self::chrToInt($right[$i]) ^ self::chrToInt($left[$i])) - 1) >> 8;
169
-        }
170
-        return ($gt + $gt + $eq) - 1;
171
-    }
172
-
173
-    /**
174
-     * If a variable does not match a given type, throw a TypeError.
175
-     *
176
-     * @param mixed $mixedVar
177
-     * @param string $type
178
-     * @param int $argumentIndex
179
-     * @throws TypeError
180
-     * @throws SodiumException
181
-     * @return void
182
-     */
183
-    public static function declareScalarType(&$mixedVar = null, $type = 'void', $argumentIndex = 0)
184
-    {
185
-        if (func_num_args() === 0) {
186
-            /* Tautology, by default */
187
-            return;
188
-        }
189
-        if (func_num_args() === 1) {
190
-            throw new TypeError('Declared void, but passed a variable');
191
-        }
192
-        $realType = strtolower(gettype($mixedVar));
193
-        $type = strtolower($type);
194
-        switch ($type) {
195
-            case 'null':
196
-                if ($mixedVar !== null) {
197
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be null, ' . $realType . ' given.');
198
-                }
199
-                break;
200
-            case 'integer':
201
-            case 'int':
202
-                $allow = array('int', 'integer');
203
-                if (!in_array($type, $allow)) {
204
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be an integer, ' . $realType . ' given.');
205
-                }
206
-                $mixedVar = (int) $mixedVar;
207
-                break;
208
-            case 'boolean':
209
-            case 'bool':
210
-                $allow = array('bool', 'boolean');
211
-                if (!in_array($type, $allow)) {
212
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be a boolean, ' . $realType . ' given.');
213
-                }
214
-                $mixedVar = (bool) $mixedVar;
215
-                break;
216
-            case 'string':
217
-                if (!is_string($mixedVar)) {
218
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be a string, ' . $realType . ' given.');
219
-                }
220
-                $mixedVar = (string) $mixedVar;
221
-                break;
222
-            case 'decimal':
223
-            case 'double':
224
-            case 'float':
225
-                $allow = array('decimal', 'double', 'float');
226
-                if (!in_array($type, $allow)) {
227
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be a float, ' . $realType . ' given.');
228
-                }
229
-                $mixedVar = (float) $mixedVar;
230
-                break;
231
-            case 'object':
232
-                if (!is_object($mixedVar)) {
233
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be an object, ' . $realType . ' given.');
234
-                }
235
-                break;
236
-            case 'array':
237
-                if (!is_array($mixedVar)) {
238
-                    if (is_object($mixedVar)) {
239
-                        if ($mixedVar instanceof ArrayAccess) {
240
-                            return;
241
-                        }
242
-                    }
243
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be an array, ' . $realType . ' given.');
244
-                }
245
-                break;
246
-            default:
247
-                throw new SodiumException('Unknown type (' . $realType .') does not match expect type (' . $type . ')');
248
-        }
249
-    }
250
-
251
-    /**
252
-     * Evaluate whether or not two strings are equal (in constant-time)
253
-     *
254
-     * @param string $left
255
-     * @param string $right
256
-     * @return bool
257
-     * @throws SodiumException
258
-     * @throws TypeError
259
-     */
260
-    public static function hashEquals($left, $right)
261
-    {
262
-        /* Type checks: */
263
-        if (!is_string($left)) {
264
-            throw new TypeError('Argument 1 must be a string, ' . gettype($left) . ' given.');
265
-        }
266
-        if (!is_string($right)) {
267
-            throw new TypeError('Argument 2 must be a string, ' . gettype($right) . ' given.');
268
-        }
269
-
270
-        if (is_callable('hash_equals')) {
271
-            return hash_equals($left, $right);
272
-        }
273
-        $d = 0;
274
-        /** @var int $len */
275
-        $len = self::strlen($left);
276
-        if ($len !== self::strlen($right)) {
277
-            return false;
278
-        }
279
-        for ($i = 0; $i < $len; ++$i) {
280
-            $d |= self::chrToInt($left[$i]) ^ self::chrToInt($right[$i]);
281
-        }
282
-
283
-        if ($d !== 0) {
284
-            return false;
285
-        }
286
-        return $left === $right;
287
-    }
288
-
289
-    /**
290
-     * Catch hash_update() failures and throw instead of silently proceeding
291
-     *
292
-     * @param HashContext|resource &$hs
293
-     * @param string $data
294
-     * @return void
295
-     * @throws SodiumException
296
-     * @psalm-suppress PossiblyInvalidArgument
297
-     */
298
-    protected static function hash_update(&$hs, $data)
299
-    {
300
-        if (!hash_update($hs, $data)) {
301
-            throw new SodiumException('hash_update() failed');
302
-        }
303
-    }
304
-
305
-    /**
306
-     * Convert a hexadecimal string into a binary string without cache-timing
307
-     * leaks
308
-     *
309
-     * @internal You should not use this directly from another application
310
-     *
311
-     * @param string $hexString
312
-     * @param bool $strictPadding
313
-     * @return string (raw binary)
314
-     * @throws RangeException
315
-     * @throws TypeError
316
-     */
317
-    public static function hex2bin($hexString, $strictPadding = false)
318
-    {
319
-        /* Type checks: */
320
-        if (!is_string($hexString)) {
321
-            throw new TypeError('Argument 1 must be a string, ' . gettype($hexString) . ' given.');
322
-        }
323
-
324
-        /** @var int $hex_pos */
325
-        $hex_pos = 0;
326
-        /** @var string $bin */
327
-        $bin = '';
328
-        /** @var int $c_acc */
329
-        $c_acc = 0;
330
-        /** @var int $hex_len */
331
-        $hex_len = self::strlen($hexString);
332
-        /** @var int $state */
333
-        $state = 0;
334
-        if (($hex_len & 1) !== 0) {
335
-            if ($strictPadding) {
336
-                throw new RangeException(
337
-                    'Expected an even number of hexadecimal characters'
338
-                );
339
-            } else {
340
-                $hexString = '0' . $hexString;
341
-                ++$hex_len;
342
-            }
343
-        }
344
-
345
-        $chunk = unpack('C*', $hexString);
346
-        while ($hex_pos < $hex_len) {
347
-            ++$hex_pos;
348
-            /** @var int $c */
349
-            $c = $chunk[$hex_pos];
350
-            /** @var int $c_num */
351
-            $c_num = $c ^ 48;
352
-            /** @var int $c_num0 */
353
-            $c_num0 = ($c_num - 10) >> 8;
354
-            /** @var int $c_alpha */
355
-            $c_alpha = ($c & ~32) - 55;
356
-            /** @var int $c_alpha0 */
357
-            $c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8;
358
-            if (($c_num0 | $c_alpha0) === 0) {
359
-                throw new RangeException(
360
-                    'hex2bin() only expects hexadecimal characters'
361
-                );
362
-            }
363
-            /** @var int $c_val */
364
-            $c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0);
365
-            if ($state === 0) {
366
-                $c_acc = $c_val * 16;
367
-            } else {
368
-                $bin .= pack('C', $c_acc | $c_val);
369
-            }
370
-            $state ^= 1;
371
-        }
372
-        return $bin;
373
-    }
374
-
375
-    /**
376
-     * Turn an array of integers into a string
377
-     *
378
-     * @internal You should not use this directly from another application
379
-     *
380
-     * @param array<int, int> $ints
381
-     * @return string
382
-     */
383
-    public static function intArrayToString(array $ints)
384
-    {
385
-        /** @var array<int, int> $args */
386
-        $args = $ints;
387
-        foreach ($args as $i => $v) {
388
-            $args[$i] = (int) ($v & 0xff);
389
-        }
390
-        array_unshift($args, str_repeat('C', count($ints)));
391
-        return (string) (call_user_func_array('pack', $args));
392
-    }
393
-
394
-    /**
395
-     * Cache-timing-safe variant of ord()
396
-     *
397
-     * @internal You should not use this directly from another application
398
-     *
399
-     * @param int $int
400
-     * @return string
401
-     * @throws TypeError
402
-     */
403
-    public static function intToChr($int)
404
-    {
405
-        return pack('C', $int);
406
-    }
407
-
408
-    /**
409
-     * Load a 3 character substring into an integer
410
-     *
411
-     * @internal You should not use this directly from another application
412
-     *
413
-     * @param string $string
414
-     * @return int
415
-     * @throws RangeException
416
-     * @throws TypeError
417
-     */
418
-    public static function load_3($string)
419
-    {
420
-        /* Type checks: */
421
-        if (!is_string($string)) {
422
-            throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
423
-        }
424
-
425
-        /* Input validation: */
426
-        if (self::strlen($string) < 3) {
427
-            throw new RangeException(
428
-                'String must be 3 bytes or more; ' . self::strlen($string) . ' given.'
429
-            );
430
-        }
431
-        /** @var array<int, int> $unpacked */
432
-        $unpacked = unpack('V', $string . "\0");
433
-        return (int) ($unpacked[1] & 0xffffff);
434
-    }
435
-
436
-    /**
437
-     * Load a 4 character substring into an integer
438
-     *
439
-     * @internal You should not use this directly from another application
440
-     *
441
-     * @param string $string
442
-     * @return int
443
-     * @throws RangeException
444
-     * @throws TypeError
445
-     */
446
-    public static function load_4($string)
447
-    {
448
-        /* Type checks: */
449
-        if (!is_string($string)) {
450
-            throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
451
-        }
452
-
453
-        /* Input validation: */
454
-        if (self::strlen($string) < 4) {
455
-            throw new RangeException(
456
-                'String must be 4 bytes or more; ' . self::strlen($string) . ' given.'
457
-            );
458
-        }
459
-        /** @var array<int, int> $unpacked */
460
-        $unpacked = unpack('V', $string);
461
-        return (int) ($unpacked[1] & 0xffffffff);
462
-    }
463
-
464
-    /**
465
-     * Load a 8 character substring into an integer
466
-     *
467
-     * @internal You should not use this directly from another application
468
-     *
469
-     * @param string $string
470
-     * @return int
471
-     * @throws RangeException
472
-     * @throws SodiumException
473
-     * @throws TypeError
474
-     */
475
-    public static function load64_le($string)
476
-    {
477
-        /* Type checks: */
478
-        if (!is_string($string)) {
479
-            throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
480
-        }
481
-
482
-        /* Input validation: */
483
-        if (self::strlen($string) < 4) {
484
-            throw new RangeException(
485
-                'String must be 4 bytes or more; ' . self::strlen($string) . ' given.'
486
-            );
487
-        }
488
-        if (PHP_VERSION_ID >= 50603 && PHP_INT_SIZE === 8) {
489
-            /** @var array<int, int> $unpacked */
490
-            $unpacked = unpack('P', $string);
491
-            return (int) $unpacked[1];
492
-        }
493
-
494
-        /** @var int $result */
495
-        $result  = (self::chrToInt($string[0]) & 0xff);
496
-        $result |= (self::chrToInt($string[1]) & 0xff) <<  8;
497
-        $result |= (self::chrToInt($string[2]) & 0xff) << 16;
498
-        $result |= (self::chrToInt($string[3]) & 0xff) << 24;
499
-        $result |= (self::chrToInt($string[4]) & 0xff) << 32;
500
-        $result |= (self::chrToInt($string[5]) & 0xff) << 40;
501
-        $result |= (self::chrToInt($string[6]) & 0xff) << 48;
502
-        $result |= (self::chrToInt($string[7]) & 0xff) << 56;
503
-        return (int) $result;
504
-    }
505
-
506
-    /**
507
-     * @internal You should not use this directly from another application
508
-     *
509
-     * @param string $left
510
-     * @param string $right
511
-     * @return int
512
-     * @throws SodiumException
513
-     * @throws TypeError
514
-     */
515
-    public static function memcmp($left, $right)
516
-    {
517
-        if (self::hashEquals($left, $right)) {
518
-            return 0;
519
-        }
520
-        return -1;
521
-    }
522
-
523
-    /**
524
-     * Multiply two integers in constant-time
525
-     *
526
-     * Micro-architecture timing side-channels caused by how your CPU
527
-     * implements multiplication are best prevented by never using the
528
-     * multiplication operators and ensuring that our code always takes
529
-     * the same number of operations to complete, regardless of the values
530
-     * of $a and $b.
531
-     *
532
-     * @internal You should not use this directly from another application
533
-     *
534
-     * @param int $a
535
-     * @param int $b
536
-     * @param int $size Limits the number of operations (useful for small,
537
-     *                  constant operands)
538
-     * @return int
539
-     */
540
-    public static function mul($a, $b, $size = 0)
541
-    {
542
-        if (ParagonIE_Sodium_Compat::$fastMult) {
543
-            return (int) ($a * $b);
544
-        }
545
-
546
-        static $defaultSize = null;
547
-        /** @var int $defaultSize */
548
-        if (!$defaultSize) {
549
-            /** @var int $defaultSize */
550
-            $defaultSize = (PHP_INT_SIZE << 3) - 1;
551
-        }
552
-        if ($size < 1) {
553
-            /** @var int $size */
554
-            $size = $defaultSize;
555
-        }
556
-        /** @var int $size */
557
-
558
-        $c = 0;
559
-
560
-        /**
561
-         * Mask is either -1 or 0.
562
-         *
563
-         * -1 in binary looks like 0x1111 ... 1111
564
-         *  0 in binary looks like 0x0000 ... 0000
565
-         *
566
-         * @var int
567
-         */
568
-        $mask = -(($b >> ((int) $defaultSize)) & 1);
569
-
570
-        /**
571
-         * Ensure $b is a positive integer, without creating
572
-         * a branching side-channel
573
-         *
574
-         * @var int $b
575
-         */
576
-        $b = ($b & ~$mask) | ($mask & -$b);
577
-
578
-        /**
579
-         * Unless $size is provided:
580
-         *
581
-         * This loop always runs 32 times when PHP_INT_SIZE is 4.
582
-         * This loop always runs 64 times when PHP_INT_SIZE is 8.
583
-         */
584
-        for ($i = $size; $i >= 0; --$i) {
585
-            $c += (int) ($a & -($b & 1));
586
-            $a <<= 1;
587
-            $b >>= 1;
588
-        }
589
-        $c = (int) @($c & -1);
590
-
591
-        /**
592
-         * If $b was negative, we then apply the same value to $c here.
593
-         * It doesn't matter much if $a was negative; the $c += above would
594
-         * have produced a negative integer to begin with. But a negative $b
595
-         * makes $b >>= 1 never return 0, so we would end up with incorrect
596
-         * results.
597
-         *
598
-         * The end result is what we'd expect from integer multiplication.
599
-         */
600
-        return (int) (($c & ~$mask) | ($mask & -$c));
601
-    }
602
-
603
-    /**
604
-     * Convert any arbitrary numbers into two 32-bit integers that represent
605
-     * a 64-bit integer.
606
-     *
607
-     * @internal You should not use this directly from another application
608
-     *
609
-     * @param int|float $num
610
-     * @return array<int, int>
611
-     */
612
-    public static function numericTo64BitInteger($num)
613
-    {
614
-        $high = 0;
615
-        /** @var int $low */
616
-        $low = $num & 0xffffffff;
617
-
618
-        if ((+(abs($num))) >= 1) {
619
-            if ($num > 0) {
620
-                /** @var int $high */
621
-                $high = min((+(floor($num/4294967296))), 4294967295);
622
-            } else {
623
-                /** @var int $high */
624
-                $high = ~~((+(ceil(($num - (+((~~($num)))))/4294967296))));
625
-            }
626
-        }
627
-        return array((int) $high, (int) $low);
628
-    }
629
-
630
-    /**
631
-     * Store a 24-bit integer into a string, treating it as big-endian.
632
-     *
633
-     * @internal You should not use this directly from another application
634
-     *
635
-     * @param int $int
636
-     * @return string
637
-     * @throws TypeError
638
-     */
639
-    public static function store_3($int)
640
-    {
641
-        /* Type checks: */
642
-        if (!is_int($int)) {
643
-            if (is_numeric($int)) {
644
-                $int = (int) $int;
645
-            } else {
646
-                throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
647
-            }
648
-        }
649
-        /** @var string $packed */
650
-        $packed = pack('N', $int);
651
-        return self::substr($packed, 1, 3);
652
-    }
653
-
654
-    /**
655
-     * Store a 32-bit integer into a string, treating it as little-endian.
656
-     *
657
-     * @internal You should not use this directly from another application
658
-     *
659
-     * @param int $int
660
-     * @return string
661
-     * @throws TypeError
662
-     */
663
-    public static function store32_le($int)
664
-    {
665
-        /* Type checks: */
666
-        if (!is_int($int)) {
667
-            if (is_numeric($int)) {
668
-                $int = (int) $int;
669
-            } else {
670
-                throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
671
-            }
672
-        }
673
-
674
-        /** @var string $packed */
675
-        $packed = pack('V', $int);
676
-        return $packed;
677
-    }
678
-
679
-    /**
680
-     * Store a 32-bit integer into a string, treating it as big-endian.
681
-     *
682
-     * @internal You should not use this directly from another application
683
-     *
684
-     * @param int $int
685
-     * @return string
686
-     * @throws TypeError
687
-     */
688
-    public static function store_4($int)
689
-    {
690
-        /* Type checks: */
691
-        if (!is_int($int)) {
692
-            if (is_numeric($int)) {
693
-                $int = (int) $int;
694
-            } else {
695
-                throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
696
-            }
697
-        }
698
-
699
-        /** @var string $packed */
700
-        $packed = pack('N', $int);
701
-        return $packed;
702
-    }
703
-
704
-    /**
705
-     * Stores a 64-bit integer as an string, treating it as little-endian.
706
-     *
707
-     * @internal You should not use this directly from another application
708
-     *
709
-     * @param int $int
710
-     * @return string
711
-     * @throws TypeError
712
-     */
713
-    public static function store64_le($int)
714
-    {
715
-        /* Type checks: */
716
-        if (!is_int($int)) {
717
-            if (is_numeric($int)) {
718
-                $int = (int) $int;
719
-            } else {
720
-                throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
721
-            }
722
-        }
723
-
724
-        if (PHP_INT_SIZE === 8) {
725
-            if (PHP_VERSION_ID >= 50603) {
726
-                /** @var string $packed */
727
-                $packed = pack('P', $int);
728
-                return $packed;
729
-            }
730
-            return self::intToChr($int & 0xff) .
731
-                self::intToChr(($int >>  8) & 0xff) .
732
-                self::intToChr(($int >> 16) & 0xff) .
733
-                self::intToChr(($int >> 24) & 0xff) .
734
-                self::intToChr(($int >> 32) & 0xff) .
735
-                self::intToChr(($int >> 40) & 0xff) .
736
-                self::intToChr(($int >> 48) & 0xff) .
737
-                self::intToChr(($int >> 56) & 0xff);
738
-        }
739
-        if ($int > PHP_INT_MAX) {
740
-            list($hiB, $int) = self::numericTo64BitInteger($int);
741
-        } else {
742
-            $hiB = 0;
743
-        }
744
-        return
745
-            self::intToChr(($int      ) & 0xff) .
746
-            self::intToChr(($int >>  8) & 0xff) .
747
-            self::intToChr(($int >> 16) & 0xff) .
748
-            self::intToChr(($int >> 24) & 0xff) .
749
-            self::intToChr($hiB & 0xff) .
750
-            self::intToChr(($hiB >>  8) & 0xff) .
751
-            self::intToChr(($hiB >> 16) & 0xff) .
752
-            self::intToChr(($hiB >> 24) & 0xff);
753
-    }
754
-
755
-    /**
756
-     * Safe string length
757
-     *
758
-     * @internal You should not use this directly from another application
759
-     *
760
-     * @ref mbstring.func_overload
761
-     *
762
-     * @param string $str
763
-     * @return int
764
-     * @throws TypeError
765
-     */
766
-    public static function strlen($str)
767
-    {
768
-        /* Type checks: */
769
-        if (!is_string($str)) {
770
-            throw new TypeError('String expected');
771
-        }
772
-
773
-        return (int) (
774
-        self::isMbStringOverride()
775
-            ? mb_strlen($str, '8bit')
776
-            : strlen($str)
777
-        );
778
-    }
779
-
780
-    /**
781
-     * Turn a string into an array of integers
782
-     *
783
-     * @internal You should not use this directly from another application
784
-     *
785
-     * @param string $string
786
-     * @return array<int, int>
787
-     * @throws TypeError
788
-     */
789
-    public static function stringToIntArray($string)
790
-    {
791
-        if (!is_string($string)) {
792
-            throw new TypeError('String expected');
793
-        }
794
-        /**
795
-         * @var array<int, int>
796
-         */
797
-        $values = array_values(
798
-            unpack('C*', $string)
799
-        );
800
-        return $values;
801
-    }
802
-
803
-    /**
804
-     * Safe substring
805
-     *
806
-     * @internal You should not use this directly from another application
807
-     *
808
-     * @ref mbstring.func_overload
809
-     *
810
-     * @param string $str
811
-     * @param int $start
812
-     * @param int $length
813
-     * @return string
814
-     * @throws TypeError
815
-     */
816
-    public static function substr($str, $start = 0, $length = null)
817
-    {
818
-        /* Type checks: */
819
-        if (!is_string($str)) {
820
-            throw new TypeError('String expected');
821
-        }
822
-
823
-        if ($length === 0) {
824
-            return '';
825
-        }
826
-
827
-        if (self::isMbStringOverride()) {
828
-            if (PHP_VERSION_ID < 50400 && $length === null) {
829
-                $length = self::strlen($str);
830
-            }
831
-            $sub = (string) mb_substr($str, $start, $length, '8bit');
832
-        } elseif ($length === null) {
833
-            $sub = (string) substr($str, $start);
834
-        } else {
835
-            $sub = (string) substr($str, $start, $length);
836
-        }
837
-        if ($sub !== '') {
838
-            return $sub;
839
-        }
840
-        return '';
841
-    }
842
-
843
-    /**
844
-     * Compare a 16-character byte string in constant time.
845
-     *
846
-     * @internal You should not use this directly from another application
847
-     *
848
-     * @param string $a
849
-     * @param string $b
850
-     * @return bool
851
-     * @throws SodiumException
852
-     * @throws TypeError
853
-     */
854
-    public static function verify_16($a, $b)
855
-    {
856
-        /* Type checks: */
857
-        if (!is_string($a)) {
858
-            throw new TypeError('String expected');
859
-        }
860
-        if (!is_string($b)) {
861
-            throw new TypeError('String expected');
862
-        }
863
-        return self::hashEquals(
864
-            self::substr($a, 0, 16),
865
-            self::substr($b, 0, 16)
866
-        );
867
-    }
868
-
869
-    /**
870
-     * Compare a 32-character byte string in constant time.
871
-     *
872
-     * @internal You should not use this directly from another application
873
-     *
874
-     * @param string $a
875
-     * @param string $b
876
-     * @return bool
877
-     * @throws SodiumException
878
-     * @throws TypeError
879
-     */
880
-    public static function verify_32($a, $b)
881
-    {
882
-        /* Type checks: */
883
-        if (!is_string($a)) {
884
-            throw new TypeError('String expected');
885
-        }
886
-        if (!is_string($b)) {
887
-            throw new TypeError('String expected');
888
-        }
889
-        return self::hashEquals(
890
-            self::substr($a, 0, 32),
891
-            self::substr($b, 0, 32)
892
-        );
893
-    }
894
-
895
-    /**
896
-     * Calculate $a ^ $b for two strings.
897
-     *
898
-     * @internal You should not use this directly from another application
899
-     *
900
-     * @param string $a
901
-     * @param string $b
902
-     * @return string
903
-     * @throws TypeError
904
-     */
905
-    public static function xorStrings($a, $b)
906
-    {
907
-        /* Type checks: */
908
-        if (!is_string($a)) {
909
-            throw new TypeError('Argument 1 must be a string');
910
-        }
911
-        if (!is_string($b)) {
912
-            throw new TypeError('Argument 2 must be a string');
913
-        }
914
-
915
-        return (string) ($a ^ $b);
916
-    }
917
-
918
-    /**
919
-     * Returns whether or not mbstring.func_overload is in effect.
920
-     *
921
-     * @internal You should not use this directly from another application
922
-     *
923
-     * Note: MB_OVERLOAD_STRING === 2, but we don't reference the constant
924
-     * (for nuisance-free PHP 8 support)
925
-     *
926
-     * @return bool
927
-     */
928
-    protected static function isMbStringOverride()
929
-    {
930
-        static $mbstring = null;
931
-
932
-        if ($mbstring === null) {
933
-            if (!defined('MB_OVERLOAD_STRING')) {
934
-                $mbstring = false;
935
-                return $mbstring;
936
-            }
937
-            $mbstring = extension_loaded('mbstring')
938
-                && defined('MB_OVERLOAD_STRING')
939
-                &&
940
-            ((int) (ini_get('mbstring.func_overload')) & 2);
941
-            // MB_OVERLOAD_STRING === 2
942
-        }
943
-        /** @var bool $mbstring */
944
-
945
-        return $mbstring;
946
-    }
12
+	/**
13
+	 * @param int $integer
14
+	 * @param int $size (16, 32, 64)
15
+	 * @return int
16
+	 */
17
+	public static function abs($integer, $size = 0)
18
+	{
19
+		/** @var int $realSize */
20
+		$realSize = (PHP_INT_SIZE << 3) - 1;
21
+		if ($size) {
22
+			--$size;
23
+		} else {
24
+			/** @var int $size */
25
+			$size = $realSize;
26
+		}
27
+
28
+		$negative = -(($integer >> $size) & 1);
29
+		return (int) (
30
+			($integer ^ $negative)
31
+				+
32
+			(($negative >> $realSize) & 1)
33
+		);
34
+	}
35
+
36
+	/**
37
+	 * Convert a binary string into a hexadecimal string without cache-timing
38
+	 * leaks
39
+	 *
40
+	 * @internal You should not use this directly from another application
41
+	 *
42
+	 * @param string $binaryString (raw binary)
43
+	 * @return string
44
+	 * @throws TypeError
45
+	 */
46
+	public static function bin2hex($binaryString)
47
+	{
48
+		/* Type checks: */
49
+		if (!is_string($binaryString)) {
50
+			throw new TypeError('Argument 1 must be a string, ' . gettype($binaryString) . ' given.');
51
+		}
52
+
53
+		$hex = '';
54
+		$len = self::strlen($binaryString);
55
+		for ($i = 0; $i < $len; ++$i) {
56
+			/** @var array<int, int> $chunk */
57
+			$chunk = unpack('C', $binaryString[$i]);
58
+			/** @var int $c */
59
+			$c = $chunk[1] & 0xf;
60
+			/** @var int $b */
61
+			$b = $chunk[1] >> 4;
62
+			$hex .= pack(
63
+				'CC',
64
+				(87 + $b + ((($b - 10) >> 8) & ~38)),
65
+				(87 + $c + ((($c - 10) >> 8) & ~38))
66
+			);
67
+		}
68
+		return $hex;
69
+	}
70
+
71
+	/**
72
+	 * Convert a binary string into a hexadecimal string without cache-timing
73
+	 * leaks, returning uppercase letters (as per RFC 4648)
74
+	 *
75
+	 * @internal You should not use this directly from another application
76
+	 *
77
+	 * @param string $bin_string (raw binary)
78
+	 * @return string
79
+	 * @throws TypeError
80
+	 */
81
+	public static function bin2hexUpper($bin_string)
82
+	{
83
+		$hex = '';
84
+		$len = self::strlen($bin_string);
85
+		for ($i = 0; $i < $len; ++$i) {
86
+			/** @var array<int, int> $chunk */
87
+			$chunk = unpack('C', $bin_string[$i]);
88
+			/**
89
+			 * Lower 16 bits
90
+			 *
91
+			 * @var int $c
92
+			 */
93
+			$c = $chunk[1] & 0xf;
94
+
95
+			/**
96
+			 * Upper 16 bits
97
+			 * @var int $b
98
+			 */
99
+			$b = $chunk[1] >> 4;
100
+
101
+			/**
102
+			 * Use pack() and binary operators to turn the two integers
103
+			 * into hexadecimal characters. We don't use chr() here, because
104
+			 * it uses a lookup table internally and we want to avoid
105
+			 * cache-timing side-channels.
106
+			 */
107
+			$hex .= pack(
108
+				'CC',
109
+				(55 + $b + ((($b - 10) >> 8) & ~6)),
110
+				(55 + $c + ((($c - 10) >> 8) & ~6))
111
+			);
112
+		}
113
+		return $hex;
114
+	}
115
+
116
+	/**
117
+	 * Cache-timing-safe variant of ord()
118
+	 *
119
+	 * @internal You should not use this directly from another application
120
+	 *
121
+	 * @param string $chr
122
+	 * @return int
123
+	 * @throws SodiumException
124
+	 * @throws TypeError
125
+	 */
126
+	public static function chrToInt($chr)
127
+	{
128
+		/* Type checks: */
129
+		if (!is_string($chr)) {
130
+			throw new TypeError('Argument 1 must be a string, ' . gettype($chr) . ' given.');
131
+		}
132
+		if (self::strlen($chr) !== 1) {
133
+			throw new SodiumException('chrToInt() expects a string that is exactly 1 character long');
134
+		}
135
+		/** @var array<int, int> $chunk */
136
+		$chunk = unpack('C', $chr);
137
+		return (int) ($chunk[1]);
138
+	}
139
+
140
+	/**
141
+	 * Compares two strings.
142
+	 *
143
+	 * @internal You should not use this directly from another application
144
+	 *
145
+	 * @param string $left
146
+	 * @param string $right
147
+	 * @param int $len
148
+	 * @return int
149
+	 * @throws SodiumException
150
+	 * @throws TypeError
151
+	 */
152
+	public static function compare($left, $right, $len = null)
153
+	{
154
+		$leftLen = self::strlen($left);
155
+		$rightLen = self::strlen($right);
156
+		if ($len === null) {
157
+			$len = max($leftLen, $rightLen);
158
+			$left = str_pad($left, $len, "\x00", STR_PAD_RIGHT);
159
+			$right = str_pad($right, $len, "\x00", STR_PAD_RIGHT);
160
+		}
161
+
162
+		$gt = 0;
163
+		$eq = 1;
164
+		$i = $len;
165
+		while ($i !== 0) {
166
+			--$i;
167
+			$gt |= ((self::chrToInt($right[$i]) - self::chrToInt($left[$i])) >> 8) & $eq;
168
+			$eq &= ((self::chrToInt($right[$i]) ^ self::chrToInt($left[$i])) - 1) >> 8;
169
+		}
170
+		return ($gt + $gt + $eq) - 1;
171
+	}
172
+
173
+	/**
174
+	 * If a variable does not match a given type, throw a TypeError.
175
+	 *
176
+	 * @param mixed $mixedVar
177
+	 * @param string $type
178
+	 * @param int $argumentIndex
179
+	 * @throws TypeError
180
+	 * @throws SodiumException
181
+	 * @return void
182
+	 */
183
+	public static function declareScalarType(&$mixedVar = null, $type = 'void', $argumentIndex = 0)
184
+	{
185
+		if (func_num_args() === 0) {
186
+			/* Tautology, by default */
187
+			return;
188
+		}
189
+		if (func_num_args() === 1) {
190
+			throw new TypeError('Declared void, but passed a variable');
191
+		}
192
+		$realType = strtolower(gettype($mixedVar));
193
+		$type = strtolower($type);
194
+		switch ($type) {
195
+			case 'null':
196
+				if ($mixedVar !== null) {
197
+					throw new TypeError('Argument ' . $argumentIndex . ' must be null, ' . $realType . ' given.');
198
+				}
199
+				break;
200
+			case 'integer':
201
+			case 'int':
202
+				$allow = array('int', 'integer');
203
+				if (!in_array($type, $allow)) {
204
+					throw new TypeError('Argument ' . $argumentIndex . ' must be an integer, ' . $realType . ' given.');
205
+				}
206
+				$mixedVar = (int) $mixedVar;
207
+				break;
208
+			case 'boolean':
209
+			case 'bool':
210
+				$allow = array('bool', 'boolean');
211
+				if (!in_array($type, $allow)) {
212
+					throw new TypeError('Argument ' . $argumentIndex . ' must be a boolean, ' . $realType . ' given.');
213
+				}
214
+				$mixedVar = (bool) $mixedVar;
215
+				break;
216
+			case 'string':
217
+				if (!is_string($mixedVar)) {
218
+					throw new TypeError('Argument ' . $argumentIndex . ' must be a string, ' . $realType . ' given.');
219
+				}
220
+				$mixedVar = (string) $mixedVar;
221
+				break;
222
+			case 'decimal':
223
+			case 'double':
224
+			case 'float':
225
+				$allow = array('decimal', 'double', 'float');
226
+				if (!in_array($type, $allow)) {
227
+					throw new TypeError('Argument ' . $argumentIndex . ' must be a float, ' . $realType . ' given.');
228
+				}
229
+				$mixedVar = (float) $mixedVar;
230
+				break;
231
+			case 'object':
232
+				if (!is_object($mixedVar)) {
233
+					throw new TypeError('Argument ' . $argumentIndex . ' must be an object, ' . $realType . ' given.');
234
+				}
235
+				break;
236
+			case 'array':
237
+				if (!is_array($mixedVar)) {
238
+					if (is_object($mixedVar)) {
239
+						if ($mixedVar instanceof ArrayAccess) {
240
+							return;
241
+						}
242
+					}
243
+					throw new TypeError('Argument ' . $argumentIndex . ' must be an array, ' . $realType . ' given.');
244
+				}
245
+				break;
246
+			default:
247
+				throw new SodiumException('Unknown type (' . $realType .') does not match expect type (' . $type . ')');
248
+		}
249
+	}
250
+
251
+	/**
252
+	 * Evaluate whether or not two strings are equal (in constant-time)
253
+	 *
254
+	 * @param string $left
255
+	 * @param string $right
256
+	 * @return bool
257
+	 * @throws SodiumException
258
+	 * @throws TypeError
259
+	 */
260
+	public static function hashEquals($left, $right)
261
+	{
262
+		/* Type checks: */
263
+		if (!is_string($left)) {
264
+			throw new TypeError('Argument 1 must be a string, ' . gettype($left) . ' given.');
265
+		}
266
+		if (!is_string($right)) {
267
+			throw new TypeError('Argument 2 must be a string, ' . gettype($right) . ' given.');
268
+		}
269
+
270
+		if (is_callable('hash_equals')) {
271
+			return hash_equals($left, $right);
272
+		}
273
+		$d = 0;
274
+		/** @var int $len */
275
+		$len = self::strlen($left);
276
+		if ($len !== self::strlen($right)) {
277
+			return false;
278
+		}
279
+		for ($i = 0; $i < $len; ++$i) {
280
+			$d |= self::chrToInt($left[$i]) ^ self::chrToInt($right[$i]);
281
+		}
282
+
283
+		if ($d !== 0) {
284
+			return false;
285
+		}
286
+		return $left === $right;
287
+	}
288
+
289
+	/**
290
+	 * Catch hash_update() failures and throw instead of silently proceeding
291
+	 *
292
+	 * @param HashContext|resource &$hs
293
+	 * @param string $data
294
+	 * @return void
295
+	 * @throws SodiumException
296
+	 * @psalm-suppress PossiblyInvalidArgument
297
+	 */
298
+	protected static function hash_update(&$hs, $data)
299
+	{
300
+		if (!hash_update($hs, $data)) {
301
+			throw new SodiumException('hash_update() failed');
302
+		}
303
+	}
304
+
305
+	/**
306
+	 * Convert a hexadecimal string into a binary string without cache-timing
307
+	 * leaks
308
+	 *
309
+	 * @internal You should not use this directly from another application
310
+	 *
311
+	 * @param string $hexString
312
+	 * @param bool $strictPadding
313
+	 * @return string (raw binary)
314
+	 * @throws RangeException
315
+	 * @throws TypeError
316
+	 */
317
+	public static function hex2bin($hexString, $strictPadding = false)
318
+	{
319
+		/* Type checks: */
320
+		if (!is_string($hexString)) {
321
+			throw new TypeError('Argument 1 must be a string, ' . gettype($hexString) . ' given.');
322
+		}
323
+
324
+		/** @var int $hex_pos */
325
+		$hex_pos = 0;
326
+		/** @var string $bin */
327
+		$bin = '';
328
+		/** @var int $c_acc */
329
+		$c_acc = 0;
330
+		/** @var int $hex_len */
331
+		$hex_len = self::strlen($hexString);
332
+		/** @var int $state */
333
+		$state = 0;
334
+		if (($hex_len & 1) !== 0) {
335
+			if ($strictPadding) {
336
+				throw new RangeException(
337
+					'Expected an even number of hexadecimal characters'
338
+				);
339
+			} else {
340
+				$hexString = '0' . $hexString;
341
+				++$hex_len;
342
+			}
343
+		}
344
+
345
+		$chunk = unpack('C*', $hexString);
346
+		while ($hex_pos < $hex_len) {
347
+			++$hex_pos;
348
+			/** @var int $c */
349
+			$c = $chunk[$hex_pos];
350
+			/** @var int $c_num */
351
+			$c_num = $c ^ 48;
352
+			/** @var int $c_num0 */
353
+			$c_num0 = ($c_num - 10) >> 8;
354
+			/** @var int $c_alpha */
355
+			$c_alpha = ($c & ~32) - 55;
356
+			/** @var int $c_alpha0 */
357
+			$c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8;
358
+			if (($c_num0 | $c_alpha0) === 0) {
359
+				throw new RangeException(
360
+					'hex2bin() only expects hexadecimal characters'
361
+				);
362
+			}
363
+			/** @var int $c_val */
364
+			$c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0);
365
+			if ($state === 0) {
366
+				$c_acc = $c_val * 16;
367
+			} else {
368
+				$bin .= pack('C', $c_acc | $c_val);
369
+			}
370
+			$state ^= 1;
371
+		}
372
+		return $bin;
373
+	}
374
+
375
+	/**
376
+	 * Turn an array of integers into a string
377
+	 *
378
+	 * @internal You should not use this directly from another application
379
+	 *
380
+	 * @param array<int, int> $ints
381
+	 * @return string
382
+	 */
383
+	public static function intArrayToString(array $ints)
384
+	{
385
+		/** @var array<int, int> $args */
386
+		$args = $ints;
387
+		foreach ($args as $i => $v) {
388
+			$args[$i] = (int) ($v & 0xff);
389
+		}
390
+		array_unshift($args, str_repeat('C', count($ints)));
391
+		return (string) (call_user_func_array('pack', $args));
392
+	}
393
+
394
+	/**
395
+	 * Cache-timing-safe variant of ord()
396
+	 *
397
+	 * @internal You should not use this directly from another application
398
+	 *
399
+	 * @param int $int
400
+	 * @return string
401
+	 * @throws TypeError
402
+	 */
403
+	public static function intToChr($int)
404
+	{
405
+		return pack('C', $int);
406
+	}
407
+
408
+	/**
409
+	 * Load a 3 character substring into an integer
410
+	 *
411
+	 * @internal You should not use this directly from another application
412
+	 *
413
+	 * @param string $string
414
+	 * @return int
415
+	 * @throws RangeException
416
+	 * @throws TypeError
417
+	 */
418
+	public static function load_3($string)
419
+	{
420
+		/* Type checks: */
421
+		if (!is_string($string)) {
422
+			throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
423
+		}
424
+
425
+		/* Input validation: */
426
+		if (self::strlen($string) < 3) {
427
+			throw new RangeException(
428
+				'String must be 3 bytes or more; ' . self::strlen($string) . ' given.'
429
+			);
430
+		}
431
+		/** @var array<int, int> $unpacked */
432
+		$unpacked = unpack('V', $string . "\0");
433
+		return (int) ($unpacked[1] & 0xffffff);
434
+	}
435
+
436
+	/**
437
+	 * Load a 4 character substring into an integer
438
+	 *
439
+	 * @internal You should not use this directly from another application
440
+	 *
441
+	 * @param string $string
442
+	 * @return int
443
+	 * @throws RangeException
444
+	 * @throws TypeError
445
+	 */
446
+	public static function load_4($string)
447
+	{
448
+		/* Type checks: */
449
+		if (!is_string($string)) {
450
+			throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
451
+		}
452
+
453
+		/* Input validation: */
454
+		if (self::strlen($string) < 4) {
455
+			throw new RangeException(
456
+				'String must be 4 bytes or more; ' . self::strlen($string) . ' given.'
457
+			);
458
+		}
459
+		/** @var array<int, int> $unpacked */
460
+		$unpacked = unpack('V', $string);
461
+		return (int) ($unpacked[1] & 0xffffffff);
462
+	}
463
+
464
+	/**
465
+	 * Load a 8 character substring into an integer
466
+	 *
467
+	 * @internal You should not use this directly from another application
468
+	 *
469
+	 * @param string $string
470
+	 * @return int
471
+	 * @throws RangeException
472
+	 * @throws SodiumException
473
+	 * @throws TypeError
474
+	 */
475
+	public static function load64_le($string)
476
+	{
477
+		/* Type checks: */
478
+		if (!is_string($string)) {
479
+			throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
480
+		}
481
+
482
+		/* Input validation: */
483
+		if (self::strlen($string) < 4) {
484
+			throw new RangeException(
485
+				'String must be 4 bytes or more; ' . self::strlen($string) . ' given.'
486
+			);
487
+		}
488
+		if (PHP_VERSION_ID >= 50603 && PHP_INT_SIZE === 8) {
489
+			/** @var array<int, int> $unpacked */
490
+			$unpacked = unpack('P', $string);
491
+			return (int) $unpacked[1];
492
+		}
493
+
494
+		/** @var int $result */
495
+		$result  = (self::chrToInt($string[0]) & 0xff);
496
+		$result |= (self::chrToInt($string[1]) & 0xff) <<  8;
497
+		$result |= (self::chrToInt($string[2]) & 0xff) << 16;
498
+		$result |= (self::chrToInt($string[3]) & 0xff) << 24;
499
+		$result |= (self::chrToInt($string[4]) & 0xff) << 32;
500
+		$result |= (self::chrToInt($string[5]) & 0xff) << 40;
501
+		$result |= (self::chrToInt($string[6]) & 0xff) << 48;
502
+		$result |= (self::chrToInt($string[7]) & 0xff) << 56;
503
+		return (int) $result;
504
+	}
505
+
506
+	/**
507
+	 * @internal You should not use this directly from another application
508
+	 *
509
+	 * @param string $left
510
+	 * @param string $right
511
+	 * @return int
512
+	 * @throws SodiumException
513
+	 * @throws TypeError
514
+	 */
515
+	public static function memcmp($left, $right)
516
+	{
517
+		if (self::hashEquals($left, $right)) {
518
+			return 0;
519
+		}
520
+		return -1;
521
+	}
522
+
523
+	/**
524
+	 * Multiply two integers in constant-time
525
+	 *
526
+	 * Micro-architecture timing side-channels caused by how your CPU
527
+	 * implements multiplication are best prevented by never using the
528
+	 * multiplication operators and ensuring that our code always takes
529
+	 * the same number of operations to complete, regardless of the values
530
+	 * of $a and $b.
531
+	 *
532
+	 * @internal You should not use this directly from another application
533
+	 *
534
+	 * @param int $a
535
+	 * @param int $b
536
+	 * @param int $size Limits the number of operations (useful for small,
537
+	 *                  constant operands)
538
+	 * @return int
539
+	 */
540
+	public static function mul($a, $b, $size = 0)
541
+	{
542
+		if (ParagonIE_Sodium_Compat::$fastMult) {
543
+			return (int) ($a * $b);
544
+		}
545
+
546
+		static $defaultSize = null;
547
+		/** @var int $defaultSize */
548
+		if (!$defaultSize) {
549
+			/** @var int $defaultSize */
550
+			$defaultSize = (PHP_INT_SIZE << 3) - 1;
551
+		}
552
+		if ($size < 1) {
553
+			/** @var int $size */
554
+			$size = $defaultSize;
555
+		}
556
+		/** @var int $size */
557
+
558
+		$c = 0;
559
+
560
+		/**
561
+		 * Mask is either -1 or 0.
562
+		 *
563
+		 * -1 in binary looks like 0x1111 ... 1111
564
+		 *  0 in binary looks like 0x0000 ... 0000
565
+		 *
566
+		 * @var int
567
+		 */
568
+		$mask = -(($b >> ((int) $defaultSize)) & 1);
569
+
570
+		/**
571
+		 * Ensure $b is a positive integer, without creating
572
+		 * a branching side-channel
573
+		 *
574
+		 * @var int $b
575
+		 */
576
+		$b = ($b & ~$mask) | ($mask & -$b);
577
+
578
+		/**
579
+		 * Unless $size is provided:
580
+		 *
581
+		 * This loop always runs 32 times when PHP_INT_SIZE is 4.
582
+		 * This loop always runs 64 times when PHP_INT_SIZE is 8.
583
+		 */
584
+		for ($i = $size; $i >= 0; --$i) {
585
+			$c += (int) ($a & -($b & 1));
586
+			$a <<= 1;
587
+			$b >>= 1;
588
+		}
589
+		$c = (int) @($c & -1);
590
+
591
+		/**
592
+		 * If $b was negative, we then apply the same value to $c here.
593
+		 * It doesn't matter much if $a was negative; the $c += above would
594
+		 * have produced a negative integer to begin with. But a negative $b
595
+		 * makes $b >>= 1 never return 0, so we would end up with incorrect
596
+		 * results.
597
+		 *
598
+		 * The end result is what we'd expect from integer multiplication.
599
+		 */
600
+		return (int) (($c & ~$mask) | ($mask & -$c));
601
+	}
602
+
603
+	/**
604
+	 * Convert any arbitrary numbers into two 32-bit integers that represent
605
+	 * a 64-bit integer.
606
+	 *
607
+	 * @internal You should not use this directly from another application
608
+	 *
609
+	 * @param int|float $num
610
+	 * @return array<int, int>
611
+	 */
612
+	public static function numericTo64BitInteger($num)
613
+	{
614
+		$high = 0;
615
+		/** @var int $low */
616
+		$low = $num & 0xffffffff;
617
+
618
+		if ((+(abs($num))) >= 1) {
619
+			if ($num > 0) {
620
+				/** @var int $high */
621
+				$high = min((+(floor($num/4294967296))), 4294967295);
622
+			} else {
623
+				/** @var int $high */
624
+				$high = ~~((+(ceil(($num - (+((~~($num)))))/4294967296))));
625
+			}
626
+		}
627
+		return array((int) $high, (int) $low);
628
+	}
629
+
630
+	/**
631
+	 * Store a 24-bit integer into a string, treating it as big-endian.
632
+	 *
633
+	 * @internal You should not use this directly from another application
634
+	 *
635
+	 * @param int $int
636
+	 * @return string
637
+	 * @throws TypeError
638
+	 */
639
+	public static function store_3($int)
640
+	{
641
+		/* Type checks: */
642
+		if (!is_int($int)) {
643
+			if (is_numeric($int)) {
644
+				$int = (int) $int;
645
+			} else {
646
+				throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
647
+			}
648
+		}
649
+		/** @var string $packed */
650
+		$packed = pack('N', $int);
651
+		return self::substr($packed, 1, 3);
652
+	}
653
+
654
+	/**
655
+	 * Store a 32-bit integer into a string, treating it as little-endian.
656
+	 *
657
+	 * @internal You should not use this directly from another application
658
+	 *
659
+	 * @param int $int
660
+	 * @return string
661
+	 * @throws TypeError
662
+	 */
663
+	public static function store32_le($int)
664
+	{
665
+		/* Type checks: */
666
+		if (!is_int($int)) {
667
+			if (is_numeric($int)) {
668
+				$int = (int) $int;
669
+			} else {
670
+				throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
671
+			}
672
+		}
673
+
674
+		/** @var string $packed */
675
+		$packed = pack('V', $int);
676
+		return $packed;
677
+	}
678
+
679
+	/**
680
+	 * Store a 32-bit integer into a string, treating it as big-endian.
681
+	 *
682
+	 * @internal You should not use this directly from another application
683
+	 *
684
+	 * @param int $int
685
+	 * @return string
686
+	 * @throws TypeError
687
+	 */
688
+	public static function store_4($int)
689
+	{
690
+		/* Type checks: */
691
+		if (!is_int($int)) {
692
+			if (is_numeric($int)) {
693
+				$int = (int) $int;
694
+			} else {
695
+				throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
696
+			}
697
+		}
698
+
699
+		/** @var string $packed */
700
+		$packed = pack('N', $int);
701
+		return $packed;
702
+	}
703
+
704
+	/**
705
+	 * Stores a 64-bit integer as an string, treating it as little-endian.
706
+	 *
707
+	 * @internal You should not use this directly from another application
708
+	 *
709
+	 * @param int $int
710
+	 * @return string
711
+	 * @throws TypeError
712
+	 */
713
+	public static function store64_le($int)
714
+	{
715
+		/* Type checks: */
716
+		if (!is_int($int)) {
717
+			if (is_numeric($int)) {
718
+				$int = (int) $int;
719
+			} else {
720
+				throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
721
+			}
722
+		}
723
+
724
+		if (PHP_INT_SIZE === 8) {
725
+			if (PHP_VERSION_ID >= 50603) {
726
+				/** @var string $packed */
727
+				$packed = pack('P', $int);
728
+				return $packed;
729
+			}
730
+			return self::intToChr($int & 0xff) .
731
+				self::intToChr(($int >>  8) & 0xff) .
732
+				self::intToChr(($int >> 16) & 0xff) .
733
+				self::intToChr(($int >> 24) & 0xff) .
734
+				self::intToChr(($int >> 32) & 0xff) .
735
+				self::intToChr(($int >> 40) & 0xff) .
736
+				self::intToChr(($int >> 48) & 0xff) .
737
+				self::intToChr(($int >> 56) & 0xff);
738
+		}
739
+		if ($int > PHP_INT_MAX) {
740
+			list($hiB, $int) = self::numericTo64BitInteger($int);
741
+		} else {
742
+			$hiB = 0;
743
+		}
744
+		return
745
+			self::intToChr(($int      ) & 0xff) .
746
+			self::intToChr(($int >>  8) & 0xff) .
747
+			self::intToChr(($int >> 16) & 0xff) .
748
+			self::intToChr(($int >> 24) & 0xff) .
749
+			self::intToChr($hiB & 0xff) .
750
+			self::intToChr(($hiB >>  8) & 0xff) .
751
+			self::intToChr(($hiB >> 16) & 0xff) .
752
+			self::intToChr(($hiB >> 24) & 0xff);
753
+	}
754
+
755
+	/**
756
+	 * Safe string length
757
+	 *
758
+	 * @internal You should not use this directly from another application
759
+	 *
760
+	 * @ref mbstring.func_overload
761
+	 *
762
+	 * @param string $str
763
+	 * @return int
764
+	 * @throws TypeError
765
+	 */
766
+	public static function strlen($str)
767
+	{
768
+		/* Type checks: */
769
+		if (!is_string($str)) {
770
+			throw new TypeError('String expected');
771
+		}
772
+
773
+		return (int) (
774
+		self::isMbStringOverride()
775
+			? mb_strlen($str, '8bit')
776
+			: strlen($str)
777
+		);
778
+	}
779
+
780
+	/**
781
+	 * Turn a string into an array of integers
782
+	 *
783
+	 * @internal You should not use this directly from another application
784
+	 *
785
+	 * @param string $string
786
+	 * @return array<int, int>
787
+	 * @throws TypeError
788
+	 */
789
+	public static function stringToIntArray($string)
790
+	{
791
+		if (!is_string($string)) {
792
+			throw new TypeError('String expected');
793
+		}
794
+		/**
795
+		 * @var array<int, int>
796
+		 */
797
+		$values = array_values(
798
+			unpack('C*', $string)
799
+		);
800
+		return $values;
801
+	}
802
+
803
+	/**
804
+	 * Safe substring
805
+	 *
806
+	 * @internal You should not use this directly from another application
807
+	 *
808
+	 * @ref mbstring.func_overload
809
+	 *
810
+	 * @param string $str
811
+	 * @param int $start
812
+	 * @param int $length
813
+	 * @return string
814
+	 * @throws TypeError
815
+	 */
816
+	public static function substr($str, $start = 0, $length = null)
817
+	{
818
+		/* Type checks: */
819
+		if (!is_string($str)) {
820
+			throw new TypeError('String expected');
821
+		}
822
+
823
+		if ($length === 0) {
824
+			return '';
825
+		}
826
+
827
+		if (self::isMbStringOverride()) {
828
+			if (PHP_VERSION_ID < 50400 && $length === null) {
829
+				$length = self::strlen($str);
830
+			}
831
+			$sub = (string) mb_substr($str, $start, $length, '8bit');
832
+		} elseif ($length === null) {
833
+			$sub = (string) substr($str, $start);
834
+		} else {
835
+			$sub = (string) substr($str, $start, $length);
836
+		}
837
+		if ($sub !== '') {
838
+			return $sub;
839
+		}
840
+		return '';
841
+	}
842
+
843
+	/**
844
+	 * Compare a 16-character byte string in constant time.
845
+	 *
846
+	 * @internal You should not use this directly from another application
847
+	 *
848
+	 * @param string $a
849
+	 * @param string $b
850
+	 * @return bool
851
+	 * @throws SodiumException
852
+	 * @throws TypeError
853
+	 */
854
+	public static function verify_16($a, $b)
855
+	{
856
+		/* Type checks: */
857
+		if (!is_string($a)) {
858
+			throw new TypeError('String expected');
859
+		}
860
+		if (!is_string($b)) {
861
+			throw new TypeError('String expected');
862
+		}
863
+		return self::hashEquals(
864
+			self::substr($a, 0, 16),
865
+			self::substr($b, 0, 16)
866
+		);
867
+	}
868
+
869
+	/**
870
+	 * Compare a 32-character byte string in constant time.
871
+	 *
872
+	 * @internal You should not use this directly from another application
873
+	 *
874
+	 * @param string $a
875
+	 * @param string $b
876
+	 * @return bool
877
+	 * @throws SodiumException
878
+	 * @throws TypeError
879
+	 */
880
+	public static function verify_32($a, $b)
881
+	{
882
+		/* Type checks: */
883
+		if (!is_string($a)) {
884
+			throw new TypeError('String expected');
885
+		}
886
+		if (!is_string($b)) {
887
+			throw new TypeError('String expected');
888
+		}
889
+		return self::hashEquals(
890
+			self::substr($a, 0, 32),
891
+			self::substr($b, 0, 32)
892
+		);
893
+	}
894
+
895
+	/**
896
+	 * Calculate $a ^ $b for two strings.
897
+	 *
898
+	 * @internal You should not use this directly from another application
899
+	 *
900
+	 * @param string $a
901
+	 * @param string $b
902
+	 * @return string
903
+	 * @throws TypeError
904
+	 */
905
+	public static function xorStrings($a, $b)
906
+	{
907
+		/* Type checks: */
908
+		if (!is_string($a)) {
909
+			throw new TypeError('Argument 1 must be a string');
910
+		}
911
+		if (!is_string($b)) {
912
+			throw new TypeError('Argument 2 must be a string');
913
+		}
914
+
915
+		return (string) ($a ^ $b);
916
+	}
917
+
918
+	/**
919
+	 * Returns whether or not mbstring.func_overload is in effect.
920
+	 *
921
+	 * @internal You should not use this directly from another application
922
+	 *
923
+	 * Note: MB_OVERLOAD_STRING === 2, but we don't reference the constant
924
+	 * (for nuisance-free PHP 8 support)
925
+	 *
926
+	 * @return bool
927
+	 */
928
+	protected static function isMbStringOverride()
929
+	{
930
+		static $mbstring = null;
931
+
932
+		if ($mbstring === null) {
933
+			if (!defined('MB_OVERLOAD_STRING')) {
934
+				$mbstring = false;
935
+				return $mbstring;
936
+			}
937
+			$mbstring = extension_loaded('mbstring')
938
+				&& defined('MB_OVERLOAD_STRING')
939
+				&&
940
+			((int) (ini_get('mbstring.func_overload')) & 2);
941
+			// MB_OVERLOAD_STRING === 2
942
+		}
943
+		/** @var bool $mbstring */
944
+
945
+		return $mbstring;
946
+	}
947 947
 }
Please login to merge, or discard this patch.
Spacing   +257 added lines, -257 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-if (class_exists('ParagonIE_Sodium_Core_Util', false)) {
3
+if ( class_exists( 'ParagonIE_Sodium_Core_Util', false ) ) {
4 4
     return;
5 5
 }
6 6
 
@@ -14,22 +14,22 @@  discard block
 block discarded – undo
14 14
      * @param int $size (16, 32, 64)
15 15
      * @return int
16 16
      */
17
-    public static function abs($integer, $size = 0)
17
+    public static function abs( $integer, $size = 0 )
18 18
     {
19 19
         /** @var int $realSize */
20
-        $realSize = (PHP_INT_SIZE << 3) - 1;
21
-        if ($size) {
20
+        $realSize = ( PHP_INT_SIZE << 3 ) - 1;
21
+        if ( $size ) {
22 22
             --$size;
23 23
         } else {
24 24
             /** @var int $size */
25 25
             $size = $realSize;
26 26
         }
27 27
 
28
-        $negative = -(($integer >> $size) & 1);
29
-        return (int) (
30
-            ($integer ^ $negative)
28
+        $negative = -( ( $integer >> $size ) & 1 );
29
+        return (int)(
30
+            ( $integer ^ $negative )
31 31
                 +
32
-            (($negative >> $realSize) & 1)
32
+            ( ( $negative >> $realSize ) & 1 )
33 33
         );
34 34
     }
35 35
 
@@ -43,26 +43,26 @@  discard block
 block discarded – undo
43 43
      * @return string
44 44
      * @throws TypeError
45 45
      */
46
-    public static function bin2hex($binaryString)
46
+    public static function bin2hex( $binaryString )
47 47
     {
48 48
         /* Type checks: */
49
-        if (!is_string($binaryString)) {
50
-            throw new TypeError('Argument 1 must be a string, ' . gettype($binaryString) . ' given.');
49
+        if ( ! is_string( $binaryString ) ) {
50
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $binaryString ) . ' given.' );
51 51
         }
52 52
 
53 53
         $hex = '';
54
-        $len = self::strlen($binaryString);
55
-        for ($i = 0; $i < $len; ++$i) {
54
+        $len = self::strlen( $binaryString );
55
+        for ( $i = 0; $i < $len; ++$i ) {
56 56
             /** @var array<int, int> $chunk */
57
-            $chunk = unpack('C', $binaryString[$i]);
57
+            $chunk = unpack( 'C', $binaryString[ $i ] );
58 58
             /** @var int $c */
59
-            $c = $chunk[1] & 0xf;
59
+            $c = $chunk[ 1 ] & 0xf;
60 60
             /** @var int $b */
61
-            $b = $chunk[1] >> 4;
61
+            $b = $chunk[ 1 ] >> 4;
62 62
             $hex .= pack(
63 63
                 'CC',
64
-                (87 + $b + ((($b - 10) >> 8) & ~38)),
65
-                (87 + $c + ((($c - 10) >> 8) & ~38))
64
+                ( 87 + $b + ( ( ( $b - 10 ) >> 8 ) & ~38 ) ),
65
+                ( 87 + $c + ( ( ( $c - 10 ) >> 8 ) & ~38 ) )
66 66
             );
67 67
         }
68 68
         return $hex;
@@ -78,25 +78,25 @@  discard block
 block discarded – undo
78 78
      * @return string
79 79
      * @throws TypeError
80 80
      */
81
-    public static function bin2hexUpper($bin_string)
81
+    public static function bin2hexUpper( $bin_string )
82 82
     {
83 83
         $hex = '';
84
-        $len = self::strlen($bin_string);
85
-        for ($i = 0; $i < $len; ++$i) {
84
+        $len = self::strlen( $bin_string );
85
+        for ( $i = 0; $i < $len; ++$i ) {
86 86
             /** @var array<int, int> $chunk */
87
-            $chunk = unpack('C', $bin_string[$i]);
87
+            $chunk = unpack( 'C', $bin_string[ $i ] );
88 88
             /**
89 89
              * Lower 16 bits
90 90
              *
91 91
              * @var int $c
92 92
              */
93
-            $c = $chunk[1] & 0xf;
93
+            $c = $chunk[ 1 ] & 0xf;
94 94
 
95 95
             /**
96 96
              * Upper 16 bits
97 97
              * @var int $b
98 98
              */
99
-            $b = $chunk[1] >> 4;
99
+            $b = $chunk[ 1 ] >> 4;
100 100
 
101 101
             /**
102 102
              * Use pack() and binary operators to turn the two integers
@@ -106,8 +106,8 @@  discard block
 block discarded – undo
106 106
              */
107 107
             $hex .= pack(
108 108
                 'CC',
109
-                (55 + $b + ((($b - 10) >> 8) & ~6)),
110
-                (55 + $c + ((($c - 10) >> 8) & ~6))
109
+                ( 55 + $b + ( ( ( $b - 10 ) >> 8 ) & ~6 ) ),
110
+                ( 55 + $c + ( ( ( $c - 10 ) >> 8 ) & ~6 ) )
111 111
             );
112 112
         }
113 113
         return $hex;
@@ -123,18 +123,18 @@  discard block
 block discarded – undo
123 123
      * @throws SodiumException
124 124
      * @throws TypeError
125 125
      */
126
-    public static function chrToInt($chr)
126
+    public static function chrToInt( $chr )
127 127
     {
128 128
         /* Type checks: */
129
-        if (!is_string($chr)) {
130
-            throw new TypeError('Argument 1 must be a string, ' . gettype($chr) . ' given.');
129
+        if ( ! is_string( $chr ) ) {
130
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $chr ) . ' given.' );
131 131
         }
132
-        if (self::strlen($chr) !== 1) {
133
-            throw new SodiumException('chrToInt() expects a string that is exactly 1 character long');
132
+        if ( self::strlen( $chr ) !== 1 ) {
133
+            throw new SodiumException( 'chrToInt() expects a string that is exactly 1 character long' );
134 134
         }
135 135
         /** @var array<int, int> $chunk */
136
-        $chunk = unpack('C', $chr);
137
-        return (int) ($chunk[1]);
136
+        $chunk = unpack( 'C', $chr );
137
+        return (int)( $chunk[ 1 ] );
138 138
     }
139 139
 
140 140
     /**
@@ -149,25 +149,25 @@  discard block
 block discarded – undo
149 149
      * @throws SodiumException
150 150
      * @throws TypeError
151 151
      */
152
-    public static function compare($left, $right, $len = null)
152
+    public static function compare( $left, $right, $len = null )
153 153
     {
154
-        $leftLen = self::strlen($left);
155
-        $rightLen = self::strlen($right);
156
-        if ($len === null) {
157
-            $len = max($leftLen, $rightLen);
158
-            $left = str_pad($left, $len, "\x00", STR_PAD_RIGHT);
159
-            $right = str_pad($right, $len, "\x00", STR_PAD_RIGHT);
154
+        $leftLen = self::strlen( $left );
155
+        $rightLen = self::strlen( $right );
156
+        if ( $len === null ) {
157
+            $len = max( $leftLen, $rightLen );
158
+            $left = str_pad( $left, $len, "\x00", STR_PAD_RIGHT );
159
+            $right = str_pad( $right, $len, "\x00", STR_PAD_RIGHT );
160 160
         }
161 161
 
162 162
         $gt = 0;
163 163
         $eq = 1;
164 164
         $i = $len;
165
-        while ($i !== 0) {
165
+        while ( $i !== 0 ) {
166 166
             --$i;
167
-            $gt |= ((self::chrToInt($right[$i]) - self::chrToInt($left[$i])) >> 8) & $eq;
168
-            $eq &= ((self::chrToInt($right[$i]) ^ self::chrToInt($left[$i])) - 1) >> 8;
167
+            $gt |= ( ( self::chrToInt( $right[ $i ] ) - self::chrToInt( $left[ $i ] ) ) >> 8 ) & $eq;
168
+            $eq &= ( ( self::chrToInt( $right[ $i ] ) ^ self::chrToInt( $left[ $i ] ) ) - 1 ) >> 8;
169 169
         }
170
-        return ($gt + $gt + $eq) - 1;
170
+        return ( $gt + $gt + $eq ) - 1;
171 171
     }
172 172
 
173 173
     /**
@@ -180,71 +180,71 @@  discard block
 block discarded – undo
180 180
      * @throws SodiumException
181 181
      * @return void
182 182
      */
183
-    public static function declareScalarType(&$mixedVar = null, $type = 'void', $argumentIndex = 0)
183
+    public static function declareScalarType( &$mixedVar = null, $type = 'void', $argumentIndex = 0 )
184 184
     {
185
-        if (func_num_args() === 0) {
185
+        if ( func_num_args() === 0 ) {
186 186
             /* Tautology, by default */
187 187
             return;
188 188
         }
189
-        if (func_num_args() === 1) {
190
-            throw new TypeError('Declared void, but passed a variable');
189
+        if ( func_num_args() === 1 ) {
190
+            throw new TypeError( 'Declared void, but passed a variable' );
191 191
         }
192
-        $realType = strtolower(gettype($mixedVar));
193
-        $type = strtolower($type);
194
-        switch ($type) {
192
+        $realType = strtolower( gettype( $mixedVar ) );
193
+        $type = strtolower( $type );
194
+        switch ( $type ) {
195 195
             case 'null':
196
-                if ($mixedVar !== null) {
197
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be null, ' . $realType . ' given.');
196
+                if ( $mixedVar !== null ) {
197
+                    throw new TypeError( 'Argument ' . $argumentIndex . ' must be null, ' . $realType . ' given.' );
198 198
                 }
199 199
                 break;
200 200
             case 'integer':
201 201
             case 'int':
202
-                $allow = array('int', 'integer');
203
-                if (!in_array($type, $allow)) {
204
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be an integer, ' . $realType . ' given.');
202
+                $allow = array( 'int', 'integer' );
203
+                if ( ! in_array( $type, $allow ) ) {
204
+                    throw new TypeError( 'Argument ' . $argumentIndex . ' must be an integer, ' . $realType . ' given.' );
205 205
                 }
206
-                $mixedVar = (int) $mixedVar;
206
+                $mixedVar = (int)$mixedVar;
207 207
                 break;
208 208
             case 'boolean':
209 209
             case 'bool':
210
-                $allow = array('bool', 'boolean');
211
-                if (!in_array($type, $allow)) {
212
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be a boolean, ' . $realType . ' given.');
210
+                $allow = array( 'bool', 'boolean' );
211
+                if ( ! in_array( $type, $allow ) ) {
212
+                    throw new TypeError( 'Argument ' . $argumentIndex . ' must be a boolean, ' . $realType . ' given.' );
213 213
                 }
214
-                $mixedVar = (bool) $mixedVar;
214
+                $mixedVar = (bool)$mixedVar;
215 215
                 break;
216 216
             case 'string':
217
-                if (!is_string($mixedVar)) {
218
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be a string, ' . $realType . ' given.');
217
+                if ( ! is_string( $mixedVar ) ) {
218
+                    throw new TypeError( 'Argument ' . $argumentIndex . ' must be a string, ' . $realType . ' given.' );
219 219
                 }
220
-                $mixedVar = (string) $mixedVar;
220
+                $mixedVar = (string)$mixedVar;
221 221
                 break;
222 222
             case 'decimal':
223 223
             case 'double':
224 224
             case 'float':
225
-                $allow = array('decimal', 'double', 'float');
226
-                if (!in_array($type, $allow)) {
227
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be a float, ' . $realType . ' given.');
225
+                $allow = array( 'decimal', 'double', 'float' );
226
+                if ( ! in_array( $type, $allow ) ) {
227
+                    throw new TypeError( 'Argument ' . $argumentIndex . ' must be a float, ' . $realType . ' given.' );
228 228
                 }
229
-                $mixedVar = (float) $mixedVar;
229
+                $mixedVar = (float)$mixedVar;
230 230
                 break;
231 231
             case 'object':
232
-                if (!is_object($mixedVar)) {
233
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be an object, ' . $realType . ' given.');
232
+                if ( ! is_object( $mixedVar ) ) {
233
+                    throw new TypeError( 'Argument ' . $argumentIndex . ' must be an object, ' . $realType . ' given.' );
234 234
                 }
235 235
                 break;
236 236
             case 'array':
237
-                if (!is_array($mixedVar)) {
238
-                    if (is_object($mixedVar)) {
239
-                        if ($mixedVar instanceof ArrayAccess) {
237
+                if ( ! is_array( $mixedVar ) ) {
238
+                    if ( is_object( $mixedVar ) ) {
239
+                        if ( $mixedVar instanceof ArrayAccess ) {
240 240
                             return;
241 241
                         }
242 242
                     }
243
-                    throw new TypeError('Argument ' . $argumentIndex . ' must be an array, ' . $realType . ' given.');
243
+                    throw new TypeError( 'Argument ' . $argumentIndex . ' must be an array, ' . $realType . ' given.' );
244 244
                 }
245 245
                 break;
246 246
             default:
247
-                throw new SodiumException('Unknown type (' . $realType .') does not match expect type (' . $type . ')');
247
+                throw new SodiumException( 'Unknown type (' . $realType . ') does not match expect type (' . $type . ')' );
248 248
         }
249 249
     }
250 250
 
@@ -257,30 +257,30 @@  discard block
 block discarded – undo
257 257
      * @throws SodiumException
258 258
      * @throws TypeError
259 259
      */
260
-    public static function hashEquals($left, $right)
260
+    public static function hashEquals( $left, $right )
261 261
     {
262 262
         /* Type checks: */
263
-        if (!is_string($left)) {
264
-            throw new TypeError('Argument 1 must be a string, ' . gettype($left) . ' given.');
263
+        if ( ! is_string( $left ) ) {
264
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $left ) . ' given.' );
265 265
         }
266
-        if (!is_string($right)) {
267
-            throw new TypeError('Argument 2 must be a string, ' . gettype($right) . ' given.');
266
+        if ( ! is_string( $right ) ) {
267
+            throw new TypeError( 'Argument 2 must be a string, ' . gettype( $right ) . ' given.' );
268 268
         }
269 269
 
270
-        if (is_callable('hash_equals')) {
271
-            return hash_equals($left, $right);
270
+        if ( is_callable( 'hash_equals' ) ) {
271
+            return hash_equals( $left, $right );
272 272
         }
273 273
         $d = 0;
274 274
         /** @var int $len */
275
-        $len = self::strlen($left);
276
-        if ($len !== self::strlen($right)) {
275
+        $len = self::strlen( $left );
276
+        if ( $len !== self::strlen( $right ) ) {
277 277
             return false;
278 278
         }
279
-        for ($i = 0; $i < $len; ++$i) {
280
-            $d |= self::chrToInt($left[$i]) ^ self::chrToInt($right[$i]);
279
+        for ( $i = 0; $i < $len; ++$i ) {
280
+            $d |= self::chrToInt( $left[ $i ] ) ^ self::chrToInt( $right[ $i ] );
281 281
         }
282 282
 
283
-        if ($d !== 0) {
283
+        if ( $d !== 0 ) {
284 284
             return false;
285 285
         }
286 286
         return $left === $right;
@@ -295,10 +295,10 @@  discard block
 block discarded – undo
295 295
      * @throws SodiumException
296 296
      * @psalm-suppress PossiblyInvalidArgument
297 297
      */
298
-    protected static function hash_update(&$hs, $data)
298
+    protected static function hash_update( &$hs, $data )
299 299
     {
300
-        if (!hash_update($hs, $data)) {
301
-            throw new SodiumException('hash_update() failed');
300
+        if ( ! hash_update( $hs, $data ) ) {
301
+            throw new SodiumException( 'hash_update() failed' );
302 302
         }
303 303
     }
304 304
 
@@ -314,11 +314,11 @@  discard block
 block discarded – undo
314 314
      * @throws RangeException
315 315
      * @throws TypeError
316 316
      */
317
-    public static function hex2bin($hexString, $strictPadding = false)
317
+    public static function hex2bin( $hexString, $strictPadding = false )
318 318
     {
319 319
         /* Type checks: */
320
-        if (!is_string($hexString)) {
321
-            throw new TypeError('Argument 1 must be a string, ' . gettype($hexString) . ' given.');
320
+        if ( ! is_string( $hexString ) ) {
321
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $hexString ) . ' given.' );
322 322
         }
323 323
 
324 324
         /** @var int $hex_pos */
@@ -328,11 +328,11 @@  discard block
 block discarded – undo
328 328
         /** @var int $c_acc */
329 329
         $c_acc = 0;
330 330
         /** @var int $hex_len */
331
-        $hex_len = self::strlen($hexString);
331
+        $hex_len = self::strlen( $hexString );
332 332
         /** @var int $state */
333 333
         $state = 0;
334
-        if (($hex_len & 1) !== 0) {
335
-            if ($strictPadding) {
334
+        if ( ( $hex_len & 1 ) !== 0 ) {
335
+            if ( $strictPadding ) {
336 336
                 throw new RangeException(
337 337
                     'Expected an even number of hexadecimal characters'
338 338
                 );
@@ -342,30 +342,30 @@  discard block
 block discarded – undo
342 342
             }
343 343
         }
344 344
 
345
-        $chunk = unpack('C*', $hexString);
346
-        while ($hex_pos < $hex_len) {
345
+        $chunk = unpack( 'C*', $hexString );
346
+        while ( $hex_pos < $hex_len ) {
347 347
             ++$hex_pos;
348 348
             /** @var int $c */
349
-            $c = $chunk[$hex_pos];
349
+            $c = $chunk[ $hex_pos ];
350 350
             /** @var int $c_num */
351 351
             $c_num = $c ^ 48;
352 352
             /** @var int $c_num0 */
353
-            $c_num0 = ($c_num - 10) >> 8;
353
+            $c_num0 = ( $c_num - 10 ) >> 8;
354 354
             /** @var int $c_alpha */
355
-            $c_alpha = ($c & ~32) - 55;
355
+            $c_alpha = ( $c & ~32 ) - 55;
356 356
             /** @var int $c_alpha0 */
357
-            $c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8;
358
-            if (($c_num0 | $c_alpha0) === 0) {
357
+            $c_alpha0 = ( ( $c_alpha - 10 ) ^ ( $c_alpha - 16 ) ) >> 8;
358
+            if ( ( $c_num0 | $c_alpha0 ) === 0 ) {
359 359
                 throw new RangeException(
360 360
                     'hex2bin() only expects hexadecimal characters'
361 361
                 );
362 362
             }
363 363
             /** @var int $c_val */
364
-            $c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0);
365
-            if ($state === 0) {
364
+            $c_val = ( $c_num0 & $c_num ) | ( $c_alpha & $c_alpha0 );
365
+            if ( $state === 0 ) {
366 366
                 $c_acc = $c_val * 16;
367 367
             } else {
368
-                $bin .= pack('C', $c_acc | $c_val);
368
+                $bin .= pack( 'C', $c_acc | $c_val );
369 369
             }
370 370
             $state ^= 1;
371 371
         }
@@ -380,15 +380,15 @@  discard block
 block discarded – undo
380 380
      * @param array<int, int> $ints
381 381
      * @return string
382 382
      */
383
-    public static function intArrayToString(array $ints)
383
+    public static function intArrayToString( array $ints )
384 384
     {
385 385
         /** @var array<int, int> $args */
386 386
         $args = $ints;
387
-        foreach ($args as $i => $v) {
388
-            $args[$i] = (int) ($v & 0xff);
387
+        foreach ( $args as $i => $v ) {
388
+            $args[ $i ] = (int)( $v & 0xff );
389 389
         }
390
-        array_unshift($args, str_repeat('C', count($ints)));
391
-        return (string) (call_user_func_array('pack', $args));
390
+        array_unshift( $args, str_repeat( 'C', count( $ints ) ) );
391
+        return (string)( call_user_func_array( 'pack', $args ) );
392 392
     }
393 393
 
394 394
     /**
@@ -400,9 +400,9 @@  discard block
 block discarded – undo
400 400
      * @return string
401 401
      * @throws TypeError
402 402
      */
403
-    public static function intToChr($int)
403
+    public static function intToChr( $int )
404 404
     {
405
-        return pack('C', $int);
405
+        return pack( 'C', $int );
406 406
     }
407 407
 
408 408
     /**
@@ -415,22 +415,22 @@  discard block
 block discarded – undo
415 415
      * @throws RangeException
416 416
      * @throws TypeError
417 417
      */
418
-    public static function load_3($string)
418
+    public static function load_3( $string )
419 419
     {
420 420
         /* Type checks: */
421
-        if (!is_string($string)) {
422
-            throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
421
+        if ( ! is_string( $string ) ) {
422
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $string ) . ' given.' );
423 423
         }
424 424
 
425 425
         /* Input validation: */
426
-        if (self::strlen($string) < 3) {
426
+        if ( self::strlen( $string ) < 3 ) {
427 427
             throw new RangeException(
428
-                'String must be 3 bytes or more; ' . self::strlen($string) . ' given.'
428
+                'String must be 3 bytes or more; ' . self::strlen( $string ) . ' given.'
429 429
             );
430 430
         }
431 431
         /** @var array<int, int> $unpacked */
432
-        $unpacked = unpack('V', $string . "\0");
433
-        return (int) ($unpacked[1] & 0xffffff);
432
+        $unpacked = unpack( 'V', $string . "\0" );
433
+        return (int)( $unpacked[ 1 ] & 0xffffff );
434 434
     }
435 435
 
436 436
     /**
@@ -443,22 +443,22 @@  discard block
 block discarded – undo
443 443
      * @throws RangeException
444 444
      * @throws TypeError
445 445
      */
446
-    public static function load_4($string)
446
+    public static function load_4( $string )
447 447
     {
448 448
         /* Type checks: */
449
-        if (!is_string($string)) {
450
-            throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
449
+        if ( ! is_string( $string ) ) {
450
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $string ) . ' given.' );
451 451
         }
452 452
 
453 453
         /* Input validation: */
454
-        if (self::strlen($string) < 4) {
454
+        if ( self::strlen( $string ) < 4 ) {
455 455
             throw new RangeException(
456
-                'String must be 4 bytes or more; ' . self::strlen($string) . ' given.'
456
+                'String must be 4 bytes or more; ' . self::strlen( $string ) . ' given.'
457 457
             );
458 458
         }
459 459
         /** @var array<int, int> $unpacked */
460
-        $unpacked = unpack('V', $string);
461
-        return (int) ($unpacked[1] & 0xffffffff);
460
+        $unpacked = unpack( 'V', $string );
461
+        return (int)( $unpacked[ 1 ] & 0xffffffff );
462 462
     }
463 463
 
464 464
     /**
@@ -472,35 +472,35 @@  discard block
 block discarded – undo
472 472
      * @throws SodiumException
473 473
      * @throws TypeError
474 474
      */
475
-    public static function load64_le($string)
475
+    public static function load64_le( $string )
476 476
     {
477 477
         /* Type checks: */
478
-        if (!is_string($string)) {
479
-            throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
478
+        if ( ! is_string( $string ) ) {
479
+            throw new TypeError( 'Argument 1 must be a string, ' . gettype( $string ) . ' given.' );
480 480
         }
481 481
 
482 482
         /* Input validation: */
483
-        if (self::strlen($string) < 4) {
483
+        if ( self::strlen( $string ) < 4 ) {
484 484
             throw new RangeException(
485
-                'String must be 4 bytes or more; ' . self::strlen($string) . ' given.'
485
+                'String must be 4 bytes or more; ' . self::strlen( $string ) . ' given.'
486 486
             );
487 487
         }
488
-        if (PHP_VERSION_ID >= 50603 && PHP_INT_SIZE === 8) {
488
+        if ( PHP_VERSION_ID >= 50603 && PHP_INT_SIZE === 8 ) {
489 489
             /** @var array<int, int> $unpacked */
490
-            $unpacked = unpack('P', $string);
491
-            return (int) $unpacked[1];
490
+            $unpacked = unpack( 'P', $string );
491
+            return (int)$unpacked[ 1 ];
492 492
         }
493 493
 
494 494
         /** @var int $result */
495
-        $result  = (self::chrToInt($string[0]) & 0xff);
496
-        $result |= (self::chrToInt($string[1]) & 0xff) <<  8;
497
-        $result |= (self::chrToInt($string[2]) & 0xff) << 16;
498
-        $result |= (self::chrToInt($string[3]) & 0xff) << 24;
499
-        $result |= (self::chrToInt($string[4]) & 0xff) << 32;
500
-        $result |= (self::chrToInt($string[5]) & 0xff) << 40;
501
-        $result |= (self::chrToInt($string[6]) & 0xff) << 48;
502
-        $result |= (self::chrToInt($string[7]) & 0xff) << 56;
503
-        return (int) $result;
495
+        $result  = ( self::chrToInt( $string[ 0 ] ) & 0xff );
496
+        $result |= ( self::chrToInt( $string[ 1 ] ) & 0xff ) << 8;
497
+        $result |= ( self::chrToInt( $string[ 2 ] ) & 0xff ) << 16;
498
+        $result |= ( self::chrToInt( $string[ 3 ] ) & 0xff ) << 24;
499
+        $result |= ( self::chrToInt( $string[ 4 ] ) & 0xff ) << 32;
500
+        $result |= ( self::chrToInt( $string[ 5 ] ) & 0xff ) << 40;
501
+        $result |= ( self::chrToInt( $string[ 6 ] ) & 0xff ) << 48;
502
+        $result |= ( self::chrToInt( $string[ 7 ] ) & 0xff ) << 56;
503
+        return (int)$result;
504 504
     }
505 505
 
506 506
     /**
@@ -512,9 +512,9 @@  discard block
 block discarded – undo
512 512
      * @throws SodiumException
513 513
      * @throws TypeError
514 514
      */
515
-    public static function memcmp($left, $right)
515
+    public static function memcmp( $left, $right )
516 516
     {
517
-        if (self::hashEquals($left, $right)) {
517
+        if ( self::hashEquals( $left, $right ) ) {
518 518
             return 0;
519 519
         }
520 520
         return -1;
@@ -537,19 +537,19 @@  discard block
 block discarded – undo
537 537
      *                  constant operands)
538 538
      * @return int
539 539
      */
540
-    public static function mul($a, $b, $size = 0)
540
+    public static function mul( $a, $b, $size = 0 )
541 541
     {
542
-        if (ParagonIE_Sodium_Compat::$fastMult) {
543
-            return (int) ($a * $b);
542
+        if ( ParagonIE_Sodium_Compat::$fastMult ) {
543
+            return (int)( $a * $b );
544 544
         }
545 545
 
546 546
         static $defaultSize = null;
547 547
         /** @var int $defaultSize */
548
-        if (!$defaultSize) {
548
+        if ( ! $defaultSize ) {
549 549
             /** @var int $defaultSize */
550
-            $defaultSize = (PHP_INT_SIZE << 3) - 1;
550
+            $defaultSize = ( PHP_INT_SIZE << 3 ) - 1;
551 551
         }
552
-        if ($size < 1) {
552
+        if ( $size < 1 ) {
553 553
             /** @var int $size */
554 554
             $size = $defaultSize;
555 555
         }
@@ -565,7 +565,7 @@  discard block
 block discarded – undo
565 565
          *
566 566
          * @var int
567 567
          */
568
-        $mask = -(($b >> ((int) $defaultSize)) & 1);
568
+        $mask = -( ( $b >> ( (int)$defaultSize ) ) & 1 );
569 569
 
570 570
         /**
571 571
          * Ensure $b is a positive integer, without creating
@@ -573,7 +573,7 @@  discard block
 block discarded – undo
573 573
          *
574 574
          * @var int $b
575 575
          */
576
-        $b = ($b & ~$mask) | ($mask & -$b);
576
+        $b = ( $b & ~$mask ) | ( $mask & -$b );
577 577
 
578 578
         /**
579 579
          * Unless $size is provided:
@@ -581,12 +581,12 @@  discard block
 block discarded – undo
581 581
          * This loop always runs 32 times when PHP_INT_SIZE is 4.
582 582
          * This loop always runs 64 times when PHP_INT_SIZE is 8.
583 583
          */
584
-        for ($i = $size; $i >= 0; --$i) {
585
-            $c += (int) ($a & -($b & 1));
584
+        for ( $i = $size; $i >= 0; --$i ) {
585
+            $c += (int)( $a & -( $b & 1 ) );
586 586
             $a <<= 1;
587 587
             $b >>= 1;
588 588
         }
589
-        $c = (int) @($c & -1);
589
+        $c = (int)@( $c & -1 );
590 590
 
591 591
         /**
592 592
          * If $b was negative, we then apply the same value to $c here.
@@ -597,7 +597,7 @@  discard block
 block discarded – undo
597 597
          *
598 598
          * The end result is what we'd expect from integer multiplication.
599 599
          */
600
-        return (int) (($c & ~$mask) | ($mask & -$c));
600
+        return (int)( ( $c & ~$mask ) | ( $mask & -$c ) );
601 601
     }
602 602
 
603 603
     /**
@@ -609,22 +609,22 @@  discard block
 block discarded – undo
609 609
      * @param int|float $num
610 610
      * @return array<int, int>
611 611
      */
612
-    public static function numericTo64BitInteger($num)
612
+    public static function numericTo64BitInteger( $num )
613 613
     {
614 614
         $high = 0;
615 615
         /** @var int $low */
616 616
         $low = $num & 0xffffffff;
617 617
 
618
-        if ((+(abs($num))) >= 1) {
619
-            if ($num > 0) {
618
+        if ( (+( abs( $num ) )) >= 1 ) {
619
+            if ( $num > 0 ) {
620 620
                 /** @var int $high */
621
-                $high = min((+(floor($num/4294967296))), 4294967295);
621
+                $high = min( (+( floor( $num / 4294967296 ) )), 4294967295 );
622 622
             } else {
623 623
                 /** @var int $high */
624
-                $high = ~~((+(ceil(($num - (+((~~($num)))))/4294967296))));
624
+                $high = ~~( (+( ceil( ( $num - (+( ( ~~( $num ) ) )) ) / 4294967296 ) )) );
625 625
             }
626 626
         }
627
-        return array((int) $high, (int) $low);
627
+        return array( (int)$high, (int)$low );
628 628
     }
629 629
 
630 630
     /**
@@ -636,19 +636,19 @@  discard block
 block discarded – undo
636 636
      * @return string
637 637
      * @throws TypeError
638 638
      */
639
-    public static function store_3($int)
639
+    public static function store_3( $int )
640 640
     {
641 641
         /* Type checks: */
642
-        if (!is_int($int)) {
643
-            if (is_numeric($int)) {
644
-                $int = (int) $int;
642
+        if ( ! is_int( $int ) ) {
643
+            if ( is_numeric( $int ) ) {
644
+                $int = (int)$int;
645 645
             } else {
646
-                throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
646
+                throw new TypeError( 'Argument 1 must be an integer, ' . gettype( $int ) . ' given.' );
647 647
             }
648 648
         }
649 649
         /** @var string $packed */
650
-        $packed = pack('N', $int);
651
-        return self::substr($packed, 1, 3);
650
+        $packed = pack( 'N', $int );
651
+        return self::substr( $packed, 1, 3 );
652 652
     }
653 653
 
654 654
     /**
@@ -660,19 +660,19 @@  discard block
 block discarded – undo
660 660
      * @return string
661 661
      * @throws TypeError
662 662
      */
663
-    public static function store32_le($int)
663
+    public static function store32_le( $int )
664 664
     {
665 665
         /* Type checks: */
666
-        if (!is_int($int)) {
667
-            if (is_numeric($int)) {
668
-                $int = (int) $int;
666
+        if ( ! is_int( $int ) ) {
667
+            if ( is_numeric( $int ) ) {
668
+                $int = (int)$int;
669 669
             } else {
670
-                throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
670
+                throw new TypeError( 'Argument 1 must be an integer, ' . gettype( $int ) . ' given.' );
671 671
             }
672 672
         }
673 673
 
674 674
         /** @var string $packed */
675
-        $packed = pack('V', $int);
675
+        $packed = pack( 'V', $int );
676 676
         return $packed;
677 677
     }
678 678
 
@@ -685,19 +685,19 @@  discard block
 block discarded – undo
685 685
      * @return string
686 686
      * @throws TypeError
687 687
      */
688
-    public static function store_4($int)
688
+    public static function store_4( $int )
689 689
     {
690 690
         /* Type checks: */
691
-        if (!is_int($int)) {
692
-            if (is_numeric($int)) {
693
-                $int = (int) $int;
691
+        if ( ! is_int( $int ) ) {
692
+            if ( is_numeric( $int ) ) {
693
+                $int = (int)$int;
694 694
             } else {
695
-                throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
695
+                throw new TypeError( 'Argument 1 must be an integer, ' . gettype( $int ) . ' given.' );
696 696
             }
697 697
         }
698 698
 
699 699
         /** @var string $packed */
700
-        $packed = pack('N', $int);
700
+        $packed = pack( 'N', $int );
701 701
         return $packed;
702 702
     }
703 703
 
@@ -710,46 +710,46 @@  discard block
 block discarded – undo
710 710
      * @return string
711 711
      * @throws TypeError
712 712
      */
713
-    public static function store64_le($int)
713
+    public static function store64_le( $int )
714 714
     {
715 715
         /* Type checks: */
716
-        if (!is_int($int)) {
717
-            if (is_numeric($int)) {
718
-                $int = (int) $int;
716
+        if ( ! is_int( $int ) ) {
717
+            if ( is_numeric( $int ) ) {
718
+                $int = (int)$int;
719 719
             } else {
720
-                throw new TypeError('Argument 1 must be an integer, ' . gettype($int) . ' given.');
720
+                throw new TypeError( 'Argument 1 must be an integer, ' . gettype( $int ) . ' given.' );
721 721
             }
722 722
         }
723 723
 
724
-        if (PHP_INT_SIZE === 8) {
725
-            if (PHP_VERSION_ID >= 50603) {
724
+        if ( PHP_INT_SIZE === 8 ) {
725
+            if ( PHP_VERSION_ID >= 50603 ) {
726 726
                 /** @var string $packed */
727
-                $packed = pack('P', $int);
727
+                $packed = pack( 'P', $int );
728 728
                 return $packed;
729 729
             }
730
-            return self::intToChr($int & 0xff) .
731
-                self::intToChr(($int >>  8) & 0xff) .
732
-                self::intToChr(($int >> 16) & 0xff) .
733
-                self::intToChr(($int >> 24) & 0xff) .
734
-                self::intToChr(($int >> 32) & 0xff) .
735
-                self::intToChr(($int >> 40) & 0xff) .
736
-                self::intToChr(($int >> 48) & 0xff) .
737
-                self::intToChr(($int >> 56) & 0xff);
738
-        }
739
-        if ($int > PHP_INT_MAX) {
740
-            list($hiB, $int) = self::numericTo64BitInteger($int);
730
+            return self::intToChr( $int & 0xff ) .
731
+                self::intToChr( ( $int >> 8 ) & 0xff ) .
732
+                self::intToChr( ( $int >> 16 ) & 0xff ) .
733
+                self::intToChr( ( $int >> 24 ) & 0xff ) .
734
+                self::intToChr( ( $int >> 32 ) & 0xff ) .
735
+                self::intToChr( ( $int >> 40 ) & 0xff ) .
736
+                self::intToChr( ( $int >> 48 ) & 0xff ) .
737
+                self::intToChr( ( $int >> 56 ) & 0xff );
738
+        }
739
+        if ( $int > PHP_INT_MAX ) {
740
+            list( $hiB, $int ) = self::numericTo64BitInteger( $int );
741 741
         } else {
742 742
             $hiB = 0;
743 743
         }
744 744
         return
745
-            self::intToChr(($int      ) & 0xff) .
746
-            self::intToChr(($int >>  8) & 0xff) .
747
-            self::intToChr(($int >> 16) & 0xff) .
748
-            self::intToChr(($int >> 24) & 0xff) .
749
-            self::intToChr($hiB & 0xff) .
750
-            self::intToChr(($hiB >>  8) & 0xff) .
751
-            self::intToChr(($hiB >> 16) & 0xff) .
752
-            self::intToChr(($hiB >> 24) & 0xff);
745
+            self::intToChr( ( $int ) & 0xff ) .
746
+            self::intToChr( ( $int >> 8 ) & 0xff ) .
747
+            self::intToChr( ( $int >> 16 ) & 0xff ) .
748
+            self::intToChr( ( $int >> 24 ) & 0xff ) .
749
+            self::intToChr( $hiB & 0xff ) .
750
+            self::intToChr( ( $hiB >> 8 ) & 0xff ) .
751
+            self::intToChr( ( $hiB >> 16 ) & 0xff ) .
752
+            self::intToChr( ( $hiB >> 24 ) & 0xff );
753 753
     }
754 754
 
755 755
     /**
@@ -763,17 +763,17 @@  discard block
 block discarded – undo
763 763
      * @return int
764 764
      * @throws TypeError
765 765
      */
766
-    public static function strlen($str)
766
+    public static function strlen( $str )
767 767
     {
768 768
         /* Type checks: */
769
-        if (!is_string($str)) {
770
-            throw new TypeError('String expected');
769
+        if ( ! is_string( $str ) ) {
770
+            throw new TypeError( 'String expected' );
771 771
         }
772 772
 
773
-        return (int) (
773
+        return (int)(
774 774
         self::isMbStringOverride()
775
-            ? mb_strlen($str, '8bit')
776
-            : strlen($str)
775
+            ? mb_strlen( $str, '8bit' )
776
+            : strlen( $str )
777 777
         );
778 778
     }
779 779
 
@@ -786,16 +786,16 @@  discard block
 block discarded – undo
786 786
      * @return array<int, int>
787 787
      * @throws TypeError
788 788
      */
789
-    public static function stringToIntArray($string)
789
+    public static function stringToIntArray( $string )
790 790
     {
791
-        if (!is_string($string)) {
792
-            throw new TypeError('String expected');
791
+        if ( ! is_string( $string ) ) {
792
+            throw new TypeError( 'String expected' );
793 793
         }
794 794
         /**
795 795
          * @var array<int, int>
796 796
          */
797 797
         $values = array_values(
798
-            unpack('C*', $string)
798
+            unpack( 'C*', $string )
799 799
         );
800 800
         return $values;
801 801
     }
@@ -813,28 +813,28 @@  discard block
 block discarded – undo
813 813
      * @return string
814 814
      * @throws TypeError
815 815
      */
816
-    public static function substr($str, $start = 0, $length = null)
816
+    public static function substr( $str, $start = 0, $length = null )
817 817
     {
818 818
         /* Type checks: */
819
-        if (!is_string($str)) {
820
-            throw new TypeError('String expected');
819
+        if ( ! is_string( $str ) ) {
820
+            throw new TypeError( 'String expected' );
821 821
         }
822 822
 
823
-        if ($length === 0) {
823
+        if ( $length === 0 ) {
824 824
             return '';
825 825
         }
826 826
 
827
-        if (self::isMbStringOverride()) {
828
-            if (PHP_VERSION_ID < 50400 && $length === null) {
829
-                $length = self::strlen($str);
827
+        if ( self::isMbStringOverride() ) {
828
+            if ( PHP_VERSION_ID < 50400 && $length === null ) {
829
+                $length = self::strlen( $str );
830 830
             }
831
-            $sub = (string) mb_substr($str, $start, $length, '8bit');
832
-        } elseif ($length === null) {
833
-            $sub = (string) substr($str, $start);
831
+            $sub = (string)mb_substr( $str, $start, $length, '8bit' );
832
+        } elseif ( $length === null ) {
833
+            $sub = (string)substr( $str, $start );
834 834
         } else {
835
-            $sub = (string) substr($str, $start, $length);
835
+            $sub = (string)substr( $str, $start, $length );
836 836
         }
837
-        if ($sub !== '') {
837
+        if ( $sub !== '' ) {
838 838
             return $sub;
839 839
         }
840 840
         return '';
@@ -851,18 +851,18 @@  discard block
 block discarded – undo
851 851
      * @throws SodiumException
852 852
      * @throws TypeError
853 853
      */
854
-    public static function verify_16($a, $b)
854
+    public static function verify_16( $a, $b )
855 855
     {
856 856
         /* Type checks: */
857
-        if (!is_string($a)) {
858
-            throw new TypeError('String expected');
857
+        if ( ! is_string( $a ) ) {
858
+            throw new TypeError( 'String expected' );
859 859
         }
860
-        if (!is_string($b)) {
861
-            throw new TypeError('String expected');
860
+        if ( ! is_string( $b ) ) {
861
+            throw new TypeError( 'String expected' );
862 862
         }
863 863
         return self::hashEquals(
864
-            self::substr($a, 0, 16),
865
-            self::substr($b, 0, 16)
864
+            self::substr( $a, 0, 16 ),
865
+            self::substr( $b, 0, 16 )
866 866
         );
867 867
     }
868 868
 
@@ -877,18 +877,18 @@  discard block
 block discarded – undo
877 877
      * @throws SodiumException
878 878
      * @throws TypeError
879 879
      */
880
-    public static function verify_32($a, $b)
880
+    public static function verify_32( $a, $b )
881 881
     {
882 882
         /* Type checks: */
883
-        if (!is_string($a)) {
884
-            throw new TypeError('String expected');
883
+        if ( ! is_string( $a ) ) {
884
+            throw new TypeError( 'String expected' );
885 885
         }
886
-        if (!is_string($b)) {
887
-            throw new TypeError('String expected');
886
+        if ( ! is_string( $b ) ) {
887
+            throw new TypeError( 'String expected' );
888 888
         }
889 889
         return self::hashEquals(
890
-            self::substr($a, 0, 32),
891
-            self::substr($b, 0, 32)
890
+            self::substr( $a, 0, 32 ),
891
+            self::substr( $b, 0, 32 )
892 892
         );
893 893
     }
894 894
 
@@ -902,17 +902,17 @@  discard block
 block discarded – undo
902 902
      * @return string
903 903
      * @throws TypeError
904 904
      */
905
-    public static function xorStrings($a, $b)
905
+    public static function xorStrings( $a, $b )
906 906
     {
907 907
         /* Type checks: */
908
-        if (!is_string($a)) {
909
-            throw new TypeError('Argument 1 must be a string');
908
+        if ( ! is_string( $a ) ) {
909
+            throw new TypeError( 'Argument 1 must be a string' );
910 910
         }
911
-        if (!is_string($b)) {
912
-            throw new TypeError('Argument 2 must be a string');
911
+        if ( ! is_string( $b ) ) {
912
+            throw new TypeError( 'Argument 2 must be a string' );
913 913
         }
914 914
 
915
-        return (string) ($a ^ $b);
915
+        return (string)( $a ^ $b );
916 916
     }
917 917
 
918 918
     /**
@@ -929,15 +929,15 @@  discard block
 block discarded – undo
929 929
     {
930 930
         static $mbstring = null;
931 931
 
932
-        if ($mbstring === null) {
933
-            if (!defined('MB_OVERLOAD_STRING')) {
932
+        if ( $mbstring === null ) {
933
+            if ( ! defined( 'MB_OVERLOAD_STRING' ) ) {
934 934
                 $mbstring = false;
935 935
                 return $mbstring;
936 936
             }
937
-            $mbstring = extension_loaded('mbstring')
938
-                && defined('MB_OVERLOAD_STRING')
937
+            $mbstring = extension_loaded( 'mbstring' )
938
+                && defined( 'MB_OVERLOAD_STRING' )
939 939
                 &&
940
-            ((int) (ini_get('mbstring.func_overload')) & 2);
940
+            ( (int)( ini_get( 'mbstring.func_overload' ) ) & 2 );
941 941
             // MB_OVERLOAD_STRING === 2
942 942
         }
943 943
         /** @var bool $mbstring */
Please login to merge, or discard this patch.
Braces   +29 added lines, -58 removed lines patch added patch discarded remove patch
@@ -7,15 +7,13 @@  discard block
 block discarded – undo
7 7
 /**
8 8
  * Class ParagonIE_Sodium_Core_Util
9 9
  */
10
-abstract class ParagonIE_Sodium_Core_Util
11
-{
10
+abstract class ParagonIE_Sodium_Core_Util {
12 11
     /**
13 12
      * @param int $integer
14 13
      * @param int $size (16, 32, 64)
15 14
      * @return int
16 15
      */
17
-    public static function abs($integer, $size = 0)
18
-    {
16
+    public static function abs($integer, $size = 0) {
19 17
         /** @var int $realSize */
20 18
         $realSize = (PHP_INT_SIZE << 3) - 1;
21 19
         if ($size) {
@@ -43,8 +41,7 @@  discard block
 block discarded – undo
43 41
      * @return string
44 42
      * @throws TypeError
45 43
      */
46
-    public static function bin2hex($binaryString)
47
-    {
44
+    public static function bin2hex($binaryString) {
48 45
         /* Type checks: */
49 46
         if (!is_string($binaryString)) {
50 47
             throw new TypeError('Argument 1 must be a string, ' . gettype($binaryString) . ' given.');
@@ -78,8 +75,7 @@  discard block
 block discarded – undo
78 75
      * @return string
79 76
      * @throws TypeError
80 77
      */
81
-    public static function bin2hexUpper($bin_string)
82
-    {
78
+    public static function bin2hexUpper($bin_string) {
83 79
         $hex = '';
84 80
         $len = self::strlen($bin_string);
85 81
         for ($i = 0; $i < $len; ++$i) {
@@ -123,8 +119,7 @@  discard block
 block discarded – undo
123 119
      * @throws SodiumException
124 120
      * @throws TypeError
125 121
      */
126
-    public static function chrToInt($chr)
127
-    {
122
+    public static function chrToInt($chr) {
128 123
         /* Type checks: */
129 124
         if (!is_string($chr)) {
130 125
             throw new TypeError('Argument 1 must be a string, ' . gettype($chr) . ' given.');
@@ -149,8 +144,7 @@  discard block
 block discarded – undo
149 144
      * @throws SodiumException
150 145
      * @throws TypeError
151 146
      */
152
-    public static function compare($left, $right, $len = null)
153
-    {
147
+    public static function compare($left, $right, $len = null) {
154 148
         $leftLen = self::strlen($left);
155 149
         $rightLen = self::strlen($right);
156 150
         if ($len === null) {
@@ -180,8 +174,7 @@  discard block
 block discarded – undo
180 174
      * @throws SodiumException
181 175
      * @return void
182 176
      */
183
-    public static function declareScalarType(&$mixedVar = null, $type = 'void', $argumentIndex = 0)
184
-    {
177
+    public static function declareScalarType(&$mixedVar = null, $type = 'void', $argumentIndex = 0) {
185 178
         if (func_num_args() === 0) {
186 179
             /* Tautology, by default */
187 180
             return;
@@ -257,8 +250,7 @@  discard block
 block discarded – undo
257 250
      * @throws SodiumException
258 251
      * @throws TypeError
259 252
      */
260
-    public static function hashEquals($left, $right)
261
-    {
253
+    public static function hashEquals($left, $right) {
262 254
         /* Type checks: */
263 255
         if (!is_string($left)) {
264 256
             throw new TypeError('Argument 1 must be a string, ' . gettype($left) . ' given.');
@@ -295,8 +287,7 @@  discard block
 block discarded – undo
295 287
      * @throws SodiumException
296 288
      * @psalm-suppress PossiblyInvalidArgument
297 289
      */
298
-    protected static function hash_update(&$hs, $data)
299
-    {
290
+    protected static function hash_update(&$hs, $data) {
300 291
         if (!hash_update($hs, $data)) {
301 292
             throw new SodiumException('hash_update() failed');
302 293
         }
@@ -314,8 +305,7 @@  discard block
 block discarded – undo
314 305
      * @throws RangeException
315 306
      * @throws TypeError
316 307
      */
317
-    public static function hex2bin($hexString, $strictPadding = false)
318
-    {
308
+    public static function hex2bin($hexString, $strictPadding = false) {
319 309
         /* Type checks: */
320 310
         if (!is_string($hexString)) {
321 311
             throw new TypeError('Argument 1 must be a string, ' . gettype($hexString) . ' given.');
@@ -380,8 +370,7 @@  discard block
 block discarded – undo
380 370
      * @param array<int, int> $ints
381 371
      * @return string
382 372
      */
383
-    public static function intArrayToString(array $ints)
384
-    {
373
+    public static function intArrayToString(array $ints) {
385 374
         /** @var array<int, int> $args */
386 375
         $args = $ints;
387 376
         foreach ($args as $i => $v) {
@@ -400,8 +389,7 @@  discard block
 block discarded – undo
400 389
      * @return string
401 390
      * @throws TypeError
402 391
      */
403
-    public static function intToChr($int)
404
-    {
392
+    public static function intToChr($int) {
405 393
         return pack('C', $int);
406 394
     }
407 395
 
@@ -415,8 +403,7 @@  discard block
 block discarded – undo
415 403
      * @throws RangeException
416 404
      * @throws TypeError
417 405
      */
418
-    public static function load_3($string)
419
-    {
406
+    public static function load_3($string) {
420 407
         /* Type checks: */
421 408
         if (!is_string($string)) {
422 409
             throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
@@ -443,8 +430,7 @@  discard block
 block discarded – undo
443 430
      * @throws RangeException
444 431
      * @throws TypeError
445 432
      */
446
-    public static function load_4($string)
447
-    {
433
+    public static function load_4($string) {
448 434
         /* Type checks: */
449 435
         if (!is_string($string)) {
450 436
             throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
@@ -472,8 +458,7 @@  discard block
 block discarded – undo
472 458
      * @throws SodiumException
473 459
      * @throws TypeError
474 460
      */
475
-    public static function load64_le($string)
476
-    {
461
+    public static function load64_le($string) {
477 462
         /* Type checks: */
478 463
         if (!is_string($string)) {
479 464
             throw new TypeError('Argument 1 must be a string, ' . gettype($string) . ' given.');
@@ -512,8 +497,7 @@  discard block
 block discarded – undo
512 497
      * @throws SodiumException
513 498
      * @throws TypeError
514 499
      */
515
-    public static function memcmp($left, $right)
516
-    {
500
+    public static function memcmp($left, $right) {
517 501
         if (self::hashEquals($left, $right)) {
518 502
             return 0;
519 503
         }
@@ -537,8 +521,7 @@  discard block
 block discarded – undo
537 521
      *                  constant operands)
538 522
      * @return int
539 523
      */
540
-    public static function mul($a, $b, $size = 0)
541
-    {
524
+    public static function mul($a, $b, $size = 0) {
542 525
         if (ParagonIE_Sodium_Compat::$fastMult) {
543 526
             return (int) ($a * $b);
544 527
         }
@@ -609,8 +592,7 @@  discard block
 block discarded – undo
609 592
      * @param int|float $num
610 593
      * @return array<int, int>
611 594
      */
612
-    public static function numericTo64BitInteger($num)
613
-    {
595
+    public static function numericTo64BitInteger($num) {
614 596
         $high = 0;
615 597
         /** @var int $low */
616 598
         $low = $num & 0xffffffff;
@@ -636,8 +618,7 @@  discard block
 block discarded – undo
636 618
      * @return string
637 619
      * @throws TypeError
638 620
      */
639
-    public static function store_3($int)
640
-    {
621
+    public static function store_3($int) {
641 622
         /* Type checks: */
642 623
         if (!is_int($int)) {
643 624
             if (is_numeric($int)) {
@@ -660,8 +641,7 @@  discard block
 block discarded – undo
660 641
      * @return string
661 642
      * @throws TypeError
662 643
      */
663
-    public static function store32_le($int)
664
-    {
644
+    public static function store32_le($int) {
665 645
         /* Type checks: */
666 646
         if (!is_int($int)) {
667 647
             if (is_numeric($int)) {
@@ -685,8 +665,7 @@  discard block
 block discarded – undo
685 665
      * @return string
686 666
      * @throws TypeError
687 667
      */
688
-    public static function store_4($int)
689
-    {
668
+    public static function store_4($int) {
690 669
         /* Type checks: */
691 670
         if (!is_int($int)) {
692 671
             if (is_numeric($int)) {
@@ -710,8 +689,7 @@  discard block
 block discarded – undo
710 689
      * @return string
711 690
      * @throws TypeError
712 691
      */
713
-    public static function store64_le($int)
714
-    {
692
+    public static function store64_le($int) {
715 693
         /* Type checks: */
716 694
         if (!is_int($int)) {
717 695
             if (is_numeric($int)) {
@@ -763,8 +741,7 @@  discard block
 block discarded – undo
763 741
      * @return int
764 742
      * @throws TypeError
765 743
      */
766
-    public static function strlen($str)
767
-    {
744
+    public static function strlen($str) {
768 745
         /* Type checks: */
769 746
         if (!is_string($str)) {
770 747
             throw new TypeError('String expected');
@@ -786,8 +763,7 @@  discard block
 block discarded – undo
786 763
      * @return array<int, int>
787 764
      * @throws TypeError
788 765
      */
789
-    public static function stringToIntArray($string)
790
-    {
766
+    public static function stringToIntArray($string) {
791 767
         if (!is_string($string)) {
792 768
             throw new TypeError('String expected');
793 769
         }
@@ -813,8 +789,7 @@  discard block
 block discarded – undo
813 789
      * @return string
814 790
      * @throws TypeError
815 791
      */
816
-    public static function substr($str, $start = 0, $length = null)
817
-    {
792
+    public static function substr($str, $start = 0, $length = null) {
818 793
         /* Type checks: */
819 794
         if (!is_string($str)) {
820 795
             throw new TypeError('String expected');
@@ -851,8 +826,7 @@  discard block
 block discarded – undo
851 826
      * @throws SodiumException
852 827
      * @throws TypeError
853 828
      */
854
-    public static function verify_16($a, $b)
855
-    {
829
+    public static function verify_16($a, $b) {
856 830
         /* Type checks: */
857 831
         if (!is_string($a)) {
858 832
             throw new TypeError('String expected');
@@ -877,8 +851,7 @@  discard block
 block discarded – undo
877 851
      * @throws SodiumException
878 852
      * @throws TypeError
879 853
      */
880
-    public static function verify_32($a, $b)
881
-    {
854
+    public static function verify_32($a, $b) {
882 855
         /* Type checks: */
883 856
         if (!is_string($a)) {
884 857
             throw new TypeError('String expected');
@@ -902,8 +875,7 @@  discard block
 block discarded – undo
902 875
      * @return string
903 876
      * @throws TypeError
904 877
      */
905
-    public static function xorStrings($a, $b)
906
-    {
878
+    public static function xorStrings($a, $b) {
907 879
         /* Type checks: */
908 880
         if (!is_string($a)) {
909 881
             throw new TypeError('Argument 1 must be a string');
@@ -925,8 +897,7 @@  discard block
 block discarded – undo
925 897
      *
926 898
      * @return bool
927 899
      */
928
-    protected static function isMbStringOverride()
929
-    {
900
+    protected static function isMbStringOverride() {
930 901
         static $mbstring = null;
931 902
 
932 903
         if ($mbstring === null) {
Please login to merge, or discard this patch.
vendor/paragonie/sodium_compat/src/Core/SipHash.php 3 patches
Indentation   +293 added lines, -293 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 if (class_exists('ParagonIE_Sodium_Core_SipHash', false)) {
4
-    return;
4
+	return;
5 5
 }
6 6
 
7 7
 /**
@@ -11,296 +11,296 @@  discard block
 block discarded – undo
11 11
  */
12 12
 class ParagonIE_Sodium_Core_SipHash extends ParagonIE_Sodium_Core_Util
13 13
 {
14
-    /**
15
-     * @internal You should not use this directly from another application
16
-     *
17
-     * @param int[] $v
18
-     * @return int[]
19
-     *
20
-     */
21
-    public static function sipRound(array $v)
22
-    {
23
-        # v0 += v1;
24
-        list($v[0], $v[1]) = self::add(
25
-            array($v[0], $v[1]),
26
-            array($v[2], $v[3])
27
-        );
28
-
29
-        #  v1=ROTL(v1,13);
30
-        list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 13);
31
-
32
-        #  v1 ^= v0;
33
-        $v[2] = (int) $v[2] ^ (int) $v[0];
34
-        $v[3] = (int) $v[3] ^ (int) $v[1];
35
-
36
-        #  v0=ROTL(v0,32);
37
-        list($v[0], $v[1]) = self::rotl_64((int) $v[0], (int) $v[1], 32);
38
-
39
-        # v2 += v3;
40
-        list($v[4], $v[5]) = self::add(
41
-            array((int) $v[4], (int) $v[5]),
42
-            array((int) $v[6], (int) $v[7])
43
-        );
44
-
45
-        # v3=ROTL(v3,16);
46
-        list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 16);
47
-
48
-        #  v3 ^= v2;
49
-        $v[6] = (int) $v[6] ^ (int) $v[4];
50
-        $v[7] = (int) $v[7] ^ (int) $v[5];
51
-
52
-        # v0 += v3;
53
-        list($v[0], $v[1]) = self::add(
54
-            array((int) $v[0], (int) $v[1]),
55
-            array((int) $v[6], (int) $v[7])
56
-        );
57
-
58
-        # v3=ROTL(v3,21);
59
-        list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 21);
60
-
61
-        # v3 ^= v0;
62
-        $v[6] = (int) $v[6] ^ (int) $v[0];
63
-        $v[7] = (int) $v[7] ^ (int) $v[1];
64
-
65
-        # v2 += v1;
66
-        list($v[4], $v[5]) = self::add(
67
-            array((int) $v[4], (int) $v[5]),
68
-            array((int) $v[2], (int) $v[3])
69
-        );
70
-
71
-        # v1=ROTL(v1,17);
72
-        list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 17);
73
-
74
-        #  v1 ^= v2;;
75
-        $v[2] = (int) $v[2] ^ (int) $v[4];
76
-        $v[3] = (int) $v[3] ^ (int) $v[5];
77
-
78
-        # v2=ROTL(v2,32)
79
-        list($v[4], $v[5]) = self::rotl_64((int) $v[4], (int) $v[5], 32);
80
-
81
-        return $v;
82
-    }
83
-
84
-    /**
85
-     * Add two 32 bit integers representing a 64-bit integer.
86
-     *
87
-     * @internal You should not use this directly from another application
88
-     *
89
-     * @param int[] $a
90
-     * @param int[] $b
91
-     * @return array<int, mixed>
92
-     */
93
-    public static function add(array $a, array $b)
94
-    {
95
-        /** @var int $x1 */
96
-        $x1 = $a[1] + $b[1];
97
-        /** @var int $c */
98
-        $c = $x1 >> 32; // Carry if ($a + $b) > 0xffffffff
99
-        /** @var int $x0 */
100
-        $x0 = $a[0] + $b[0] + $c;
101
-        return array(
102
-            $x0 & 0xffffffff,
103
-            $x1 & 0xffffffff
104
-        );
105
-    }
106
-
107
-    /**
108
-     * @internal You should not use this directly from another application
109
-     *
110
-     * @param int $int0
111
-     * @param int $int1
112
-     * @param int $c
113
-     * @return array<int, mixed>
114
-     */
115
-    public static function rotl_64($int0, $int1, $c)
116
-    {
117
-        $int0 &= 0xffffffff;
118
-        $int1 &= 0xffffffff;
119
-        $c &= 63;
120
-        if ($c === 32) {
121
-            return array($int1, $int0);
122
-        }
123
-        if ($c > 31) {
124
-            $tmp = $int1;
125
-            $int1 = $int0;
126
-            $int0 = $tmp;
127
-            $c &= 31;
128
-        }
129
-        if ($c === 0) {
130
-            return array($int0, $int1);
131
-        }
132
-        return array(
133
-            0xffffffff & (
134
-                ($int0 << $c)
135
-                    |
136
-                ($int1 >> (32 - $c))
137
-            ),
138
-            0xffffffff & (
139
-                ($int1 << $c)
140
-                    |
141
-                ($int0 >> (32 - $c))
142
-            ),
143
-        );
144
-    }
145
-
146
-    /**
147
-     * Implements Siphash-2-4 using only 32-bit numbers.
148
-     *
149
-     * When we split an int into two, the higher bits go to the lower index.
150
-     * e.g. 0xDEADBEEFAB10C92D becomes [
151
-     *     0 => 0xDEADBEEF,
152
-     *     1 => 0xAB10C92D
153
-     * ].
154
-     *
155
-     * @internal You should not use this directly from another application
156
-     *
157
-     * @param string $in
158
-     * @param string $key
159
-     * @return string
160
-     * @throws SodiumException
161
-     * @throws TypeError
162
-     */
163
-    public static function sipHash24($in, $key)
164
-    {
165
-        $inlen = self::strlen($in);
166
-
167
-        # /* "somepseudorandomlygeneratedbytes" */
168
-        # u64 v0 = 0x736f6d6570736575ULL;
169
-        # u64 v1 = 0x646f72616e646f6dULL;
170
-        # u64 v2 = 0x6c7967656e657261ULL;
171
-        # u64 v3 = 0x7465646279746573ULL;
172
-        $v = array(
173
-            0x736f6d65, // 0
174
-            0x70736575, // 1
175
-            0x646f7261, // 2
176
-            0x6e646f6d, // 3
177
-            0x6c796765, // 4
178
-            0x6e657261, // 5
179
-            0x74656462, // 6
180
-            0x79746573  // 7
181
-        );
182
-        // v0 => $v[0], $v[1]
183
-        // v1 => $v[2], $v[3]
184
-        // v2 => $v[4], $v[5]
185
-        // v3 => $v[6], $v[7]
186
-
187
-        # u64 k0 = LOAD64_LE( k );
188
-        # u64 k1 = LOAD64_LE( k + 8 );
189
-        $k = array(
190
-            self::load_4(self::substr($key, 4, 4)),
191
-            self::load_4(self::substr($key, 0, 4)),
192
-            self::load_4(self::substr($key, 12, 4)),
193
-            self::load_4(self::substr($key, 8, 4))
194
-        );
195
-        // k0 => $k[0], $k[1]
196
-        // k1 => $k[2], $k[3]
197
-
198
-        # b = ( ( u64 )inlen ) << 56;
199
-        $b = array(
200
-            $inlen << 24,
201
-            0
202
-        );
203
-        // See docblock for why the 0th index gets the higher bits.
204
-
205
-        # v3 ^= k1;
206
-        $v[6] ^= $k[2];
207
-        $v[7] ^= $k[3];
208
-        # v2 ^= k0;
209
-        $v[4] ^= $k[0];
210
-        $v[5] ^= $k[1];
211
-        # v1 ^= k1;
212
-        $v[2] ^= $k[2];
213
-        $v[3] ^= $k[3];
214
-        # v0 ^= k0;
215
-        $v[0] ^= $k[0];
216
-        $v[1] ^= $k[1];
217
-
218
-        $left = $inlen;
219
-        # for ( ; in != end; in += 8 )
220
-        while ($left >= 8) {
221
-            # m = LOAD64_LE( in );
222
-            $m = array(
223
-                self::load_4(self::substr($in, 4, 4)),
224
-                self::load_4(self::substr($in, 0, 4))
225
-            );
226
-
227
-            # v3 ^= m;
228
-            $v[6] ^= $m[0];
229
-            $v[7] ^= $m[1];
230
-
231
-            # SIPROUND;
232
-            # SIPROUND;
233
-            $v = self::sipRound($v);
234
-            $v = self::sipRound($v);
235
-
236
-            # v0 ^= m;
237
-            $v[0] ^= $m[0];
238
-            $v[1] ^= $m[1];
239
-
240
-            $in = self::substr($in, 8);
241
-            $left -= 8;
242
-        }
243
-
244
-        # switch( left )
245
-        #  {
246
-        #     case 7: b |= ( ( u64 )in[ 6] )  << 48;
247
-        #     case 6: b |= ( ( u64 )in[ 5] )  << 40;
248
-        #     case 5: b |= ( ( u64 )in[ 4] )  << 32;
249
-        #     case 4: b |= ( ( u64 )in[ 3] )  << 24;
250
-        #     case 3: b |= ( ( u64 )in[ 2] )  << 16;
251
-        #     case 2: b |= ( ( u64 )in[ 1] )  <<  8;
252
-        #     case 1: b |= ( ( u64 )in[ 0] ); break;
253
-        #     case 0: break;
254
-        # }
255
-        switch ($left) {
256
-            case 7:
257
-                $b[0] |= self::chrToInt($in[6]) << 16;
258
-            case 6:
259
-                $b[0] |= self::chrToInt($in[5]) << 8;
260
-            case 5:
261
-                $b[0] |= self::chrToInt($in[4]);
262
-            case 4:
263
-                $b[1] |= self::chrToInt($in[3]) << 24;
264
-            case 3:
265
-                $b[1] |= self::chrToInt($in[2]) << 16;
266
-            case 2:
267
-                $b[1] |= self::chrToInt($in[1]) << 8;
268
-            case 1:
269
-                $b[1] |= self::chrToInt($in[0]);
270
-            case 0:
271
-                break;
272
-        }
273
-        // See docblock for why the 0th index gets the higher bits.
274
-
275
-        # v3 ^= b;
276
-        $v[6] ^= $b[0];
277
-        $v[7] ^= $b[1];
278
-
279
-        # SIPROUND;
280
-        # SIPROUND;
281
-        $v = self::sipRound($v);
282
-        $v = self::sipRound($v);
283
-
284
-        # v0 ^= b;
285
-        $v[0] ^= $b[0];
286
-        $v[1] ^= $b[1];
287
-
288
-        // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation
289
-        # v2 ^= 0xff;
290
-        $v[5] ^= 0xff;
291
-
292
-        # SIPROUND;
293
-        # SIPROUND;
294
-        # SIPROUND;
295
-        # SIPROUND;
296
-        $v = self::sipRound($v);
297
-        $v = self::sipRound($v);
298
-        $v = self::sipRound($v);
299
-        $v = self::sipRound($v);
300
-
301
-        # b = v0 ^ v1 ^ v2 ^ v3;
302
-        # STORE64_LE( out, b );
303
-        return  self::store32_le($v[1] ^ $v[3] ^ $v[5] ^ $v[7]) .
304
-            self::store32_le($v[0] ^ $v[2] ^ $v[4] ^ $v[6]);
305
-    }
14
+	/**
15
+	 * @internal You should not use this directly from another application
16
+	 *
17
+	 * @param int[] $v
18
+	 * @return int[]
19
+	 *
20
+	 */
21
+	public static function sipRound(array $v)
22
+	{
23
+		# v0 += v1;
24
+		list($v[0], $v[1]) = self::add(
25
+			array($v[0], $v[1]),
26
+			array($v[2], $v[3])
27
+		);
28
+
29
+		#  v1=ROTL(v1,13);
30
+		list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 13);
31
+
32
+		#  v1 ^= v0;
33
+		$v[2] = (int) $v[2] ^ (int) $v[0];
34
+		$v[3] = (int) $v[3] ^ (int) $v[1];
35
+
36
+		#  v0=ROTL(v0,32);
37
+		list($v[0], $v[1]) = self::rotl_64((int) $v[0], (int) $v[1], 32);
38
+
39
+		# v2 += v3;
40
+		list($v[4], $v[5]) = self::add(
41
+			array((int) $v[4], (int) $v[5]),
42
+			array((int) $v[6], (int) $v[7])
43
+		);
44
+
45
+		# v3=ROTL(v3,16);
46
+		list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 16);
47
+
48
+		#  v3 ^= v2;
49
+		$v[6] = (int) $v[6] ^ (int) $v[4];
50
+		$v[7] = (int) $v[7] ^ (int) $v[5];
51
+
52
+		# v0 += v3;
53
+		list($v[0], $v[1]) = self::add(
54
+			array((int) $v[0], (int) $v[1]),
55
+			array((int) $v[6], (int) $v[7])
56
+		);
57
+
58
+		# v3=ROTL(v3,21);
59
+		list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 21);
60
+
61
+		# v3 ^= v0;
62
+		$v[6] = (int) $v[6] ^ (int) $v[0];
63
+		$v[7] = (int) $v[7] ^ (int) $v[1];
64
+
65
+		# v2 += v1;
66
+		list($v[4], $v[5]) = self::add(
67
+			array((int) $v[4], (int) $v[5]),
68
+			array((int) $v[2], (int) $v[3])
69
+		);
70
+
71
+		# v1=ROTL(v1,17);
72
+		list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 17);
73
+
74
+		#  v1 ^= v2;;
75
+		$v[2] = (int) $v[2] ^ (int) $v[4];
76
+		$v[3] = (int) $v[3] ^ (int) $v[5];
77
+
78
+		# v2=ROTL(v2,32)
79
+		list($v[4], $v[5]) = self::rotl_64((int) $v[4], (int) $v[5], 32);
80
+
81
+		return $v;
82
+	}
83
+
84
+	/**
85
+	 * Add two 32 bit integers representing a 64-bit integer.
86
+	 *
87
+	 * @internal You should not use this directly from another application
88
+	 *
89
+	 * @param int[] $a
90
+	 * @param int[] $b
91
+	 * @return array<int, mixed>
92
+	 */
93
+	public static function add(array $a, array $b)
94
+	{
95
+		/** @var int $x1 */
96
+		$x1 = $a[1] + $b[1];
97
+		/** @var int $c */
98
+		$c = $x1 >> 32; // Carry if ($a + $b) > 0xffffffff
99
+		/** @var int $x0 */
100
+		$x0 = $a[0] + $b[0] + $c;
101
+		return array(
102
+			$x0 & 0xffffffff,
103
+			$x1 & 0xffffffff
104
+		);
105
+	}
106
+
107
+	/**
108
+	 * @internal You should not use this directly from another application
109
+	 *
110
+	 * @param int $int0
111
+	 * @param int $int1
112
+	 * @param int $c
113
+	 * @return array<int, mixed>
114
+	 */
115
+	public static function rotl_64($int0, $int1, $c)
116
+	{
117
+		$int0 &= 0xffffffff;
118
+		$int1 &= 0xffffffff;
119
+		$c &= 63;
120
+		if ($c === 32) {
121
+			return array($int1, $int0);
122
+		}
123
+		if ($c > 31) {
124
+			$tmp = $int1;
125
+			$int1 = $int0;
126
+			$int0 = $tmp;
127
+			$c &= 31;
128
+		}
129
+		if ($c === 0) {
130
+			return array($int0, $int1);
131
+		}
132
+		return array(
133
+			0xffffffff & (
134
+				($int0 << $c)
135
+					|
136
+				($int1 >> (32 - $c))
137
+			),
138
+			0xffffffff & (
139
+				($int1 << $c)
140
+					|
141
+				($int0 >> (32 - $c))
142
+			),
143
+		);
144
+	}
145
+
146
+	/**
147
+	 * Implements Siphash-2-4 using only 32-bit numbers.
148
+	 *
149
+	 * When we split an int into two, the higher bits go to the lower index.
150
+	 * e.g. 0xDEADBEEFAB10C92D becomes [
151
+	 *     0 => 0xDEADBEEF,
152
+	 *     1 => 0xAB10C92D
153
+	 * ].
154
+	 *
155
+	 * @internal You should not use this directly from another application
156
+	 *
157
+	 * @param string $in
158
+	 * @param string $key
159
+	 * @return string
160
+	 * @throws SodiumException
161
+	 * @throws TypeError
162
+	 */
163
+	public static function sipHash24($in, $key)
164
+	{
165
+		$inlen = self::strlen($in);
166
+
167
+		# /* "somepseudorandomlygeneratedbytes" */
168
+		# u64 v0 = 0x736f6d6570736575ULL;
169
+		# u64 v1 = 0x646f72616e646f6dULL;
170
+		# u64 v2 = 0x6c7967656e657261ULL;
171
+		# u64 v3 = 0x7465646279746573ULL;
172
+		$v = array(
173
+			0x736f6d65, // 0
174
+			0x70736575, // 1
175
+			0x646f7261, // 2
176
+			0x6e646f6d, // 3
177
+			0x6c796765, // 4
178
+			0x6e657261, // 5
179
+			0x74656462, // 6
180
+			0x79746573  // 7
181
+		);
182
+		// v0 => $v[0], $v[1]
183
+		// v1 => $v[2], $v[3]
184
+		// v2 => $v[4], $v[5]
185
+		// v3 => $v[6], $v[7]
186
+
187
+		# u64 k0 = LOAD64_LE( k );
188
+		# u64 k1 = LOAD64_LE( k + 8 );
189
+		$k = array(
190
+			self::load_4(self::substr($key, 4, 4)),
191
+			self::load_4(self::substr($key, 0, 4)),
192
+			self::load_4(self::substr($key, 12, 4)),
193
+			self::load_4(self::substr($key, 8, 4))
194
+		);
195
+		// k0 => $k[0], $k[1]
196
+		// k1 => $k[2], $k[3]
197
+
198
+		# b = ( ( u64 )inlen ) << 56;
199
+		$b = array(
200
+			$inlen << 24,
201
+			0
202
+		);
203
+		// See docblock for why the 0th index gets the higher bits.
204
+
205
+		# v3 ^= k1;
206
+		$v[6] ^= $k[2];
207
+		$v[7] ^= $k[3];
208
+		# v2 ^= k0;
209
+		$v[4] ^= $k[0];
210
+		$v[5] ^= $k[1];
211
+		# v1 ^= k1;
212
+		$v[2] ^= $k[2];
213
+		$v[3] ^= $k[3];
214
+		# v0 ^= k0;
215
+		$v[0] ^= $k[0];
216
+		$v[1] ^= $k[1];
217
+
218
+		$left = $inlen;
219
+		# for ( ; in != end; in += 8 )
220
+		while ($left >= 8) {
221
+			# m = LOAD64_LE( in );
222
+			$m = array(
223
+				self::load_4(self::substr($in, 4, 4)),
224
+				self::load_4(self::substr($in, 0, 4))
225
+			);
226
+
227
+			# v3 ^= m;
228
+			$v[6] ^= $m[0];
229
+			$v[7] ^= $m[1];
230
+
231
+			# SIPROUND;
232
+			# SIPROUND;
233
+			$v = self::sipRound($v);
234
+			$v = self::sipRound($v);
235
+
236
+			# v0 ^= m;
237
+			$v[0] ^= $m[0];
238
+			$v[1] ^= $m[1];
239
+
240
+			$in = self::substr($in, 8);
241
+			$left -= 8;
242
+		}
243
+
244
+		# switch( left )
245
+		#  {
246
+		#     case 7: b |= ( ( u64 )in[ 6] )  << 48;
247
+		#     case 6: b |= ( ( u64 )in[ 5] )  << 40;
248
+		#     case 5: b |= ( ( u64 )in[ 4] )  << 32;
249
+		#     case 4: b |= ( ( u64 )in[ 3] )  << 24;
250
+		#     case 3: b |= ( ( u64 )in[ 2] )  << 16;
251
+		#     case 2: b |= ( ( u64 )in[ 1] )  <<  8;
252
+		#     case 1: b |= ( ( u64 )in[ 0] ); break;
253
+		#     case 0: break;
254
+		# }
255
+		switch ($left) {
256
+			case 7:
257
+				$b[0] |= self::chrToInt($in[6]) << 16;
258
+			case 6:
259
+				$b[0] |= self::chrToInt($in[5]) << 8;
260
+			case 5:
261
+				$b[0] |= self::chrToInt($in[4]);
262
+			case 4:
263
+				$b[1] |= self::chrToInt($in[3]) << 24;
264
+			case 3:
265
+				$b[1] |= self::chrToInt($in[2]) << 16;
266
+			case 2:
267
+				$b[1] |= self::chrToInt($in[1]) << 8;
268
+			case 1:
269
+				$b[1] |= self::chrToInt($in[0]);
270
+			case 0:
271
+				break;
272
+		}
273
+		// See docblock for why the 0th index gets the higher bits.
274
+
275
+		# v3 ^= b;
276
+		$v[6] ^= $b[0];
277
+		$v[7] ^= $b[1];
278
+
279
+		# SIPROUND;
280
+		# SIPROUND;
281
+		$v = self::sipRound($v);
282
+		$v = self::sipRound($v);
283
+
284
+		# v0 ^= b;
285
+		$v[0] ^= $b[0];
286
+		$v[1] ^= $b[1];
287
+
288
+		// Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation
289
+		# v2 ^= 0xff;
290
+		$v[5] ^= 0xff;
291
+
292
+		# SIPROUND;
293
+		# SIPROUND;
294
+		# SIPROUND;
295
+		# SIPROUND;
296
+		$v = self::sipRound($v);
297
+		$v = self::sipRound($v);
298
+		$v = self::sipRound($v);
299
+		$v = self::sipRound($v);
300
+
301
+		# b = v0 ^ v1 ^ v2 ^ v3;
302
+		# STORE64_LE( out, b );
303
+		return  self::store32_le($v[1] ^ $v[3] ^ $v[5] ^ $v[7]) .
304
+			self::store32_le($v[0] ^ $v[2] ^ $v[4] ^ $v[6]);
305
+	}
306 306
 }
Please login to merge, or discard this patch.
Spacing   +86 added lines, -86 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-if (class_exists('ParagonIE_Sodium_Core_SipHash', false)) {
3
+if ( class_exists( 'ParagonIE_Sodium_Core_SipHash', false ) ) {
4 4
     return;
5 5
 }
6 6
 
@@ -18,65 +18,65 @@  discard block
 block discarded – undo
18 18
      * @return int[]
19 19
      *
20 20
      */
21
-    public static function sipRound(array $v)
21
+    public static function sipRound( array $v )
22 22
     {
23 23
         # v0 += v1;
24
-        list($v[0], $v[1]) = self::add(
25
-            array($v[0], $v[1]),
26
-            array($v[2], $v[3])
24
+        list( $v[ 0 ], $v[ 1 ] ) = self::add(
25
+            array( $v[ 0 ], $v[ 1 ] ),
26
+            array( $v[ 2 ], $v[ 3 ] )
27 27
         );
28 28
 
29 29
         #  v1=ROTL(v1,13);
30
-        list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 13);
30
+        list( $v[ 2 ], $v[ 3 ] ) = self::rotl_64( (int)$v[ 2 ], (int)$v[ 3 ], 13 );
31 31
 
32 32
         #  v1 ^= v0;
33
-        $v[2] = (int) $v[2] ^ (int) $v[0];
34
-        $v[3] = (int) $v[3] ^ (int) $v[1];
33
+        $v[ 2 ] = (int)$v[ 2 ] ^ (int)$v[ 0 ];
34
+        $v[ 3 ] = (int)$v[ 3 ] ^ (int)$v[ 1 ];
35 35
 
36 36
         #  v0=ROTL(v0,32);
37
-        list($v[0], $v[1]) = self::rotl_64((int) $v[0], (int) $v[1], 32);
37
+        list( $v[ 0 ], $v[ 1 ] ) = self::rotl_64( (int)$v[ 0 ], (int)$v[ 1 ], 32 );
38 38
 
39 39
         # v2 += v3;
40
-        list($v[4], $v[5]) = self::add(
41
-            array((int) $v[4], (int) $v[5]),
42
-            array((int) $v[6], (int) $v[7])
40
+        list( $v[ 4 ], $v[ 5 ] ) = self::add(
41
+            array( (int)$v[ 4 ], (int)$v[ 5 ] ),
42
+            array( (int)$v[ 6 ], (int)$v[ 7 ] )
43 43
         );
44 44
 
45 45
         # v3=ROTL(v3,16);
46
-        list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 16);
46
+        list( $v[ 6 ], $v[ 7 ] ) = self::rotl_64( (int)$v[ 6 ], (int)$v[ 7 ], 16 );
47 47
 
48 48
         #  v3 ^= v2;
49
-        $v[6] = (int) $v[6] ^ (int) $v[4];
50
-        $v[7] = (int) $v[7] ^ (int) $v[5];
49
+        $v[ 6 ] = (int)$v[ 6 ] ^ (int)$v[ 4 ];
50
+        $v[ 7 ] = (int)$v[ 7 ] ^ (int)$v[ 5 ];
51 51
 
52 52
         # v0 += v3;
53
-        list($v[0], $v[1]) = self::add(
54
-            array((int) $v[0], (int) $v[1]),
55
-            array((int) $v[6], (int) $v[7])
53
+        list( $v[ 0 ], $v[ 1 ] ) = self::add(
54
+            array( (int)$v[ 0 ], (int)$v[ 1 ] ),
55
+            array( (int)$v[ 6 ], (int)$v[ 7 ] )
56 56
         );
57 57
 
58 58
         # v3=ROTL(v3,21);
59
-        list($v[6], $v[7]) = self::rotl_64((int) $v[6], (int) $v[7], 21);
59
+        list( $v[ 6 ], $v[ 7 ] ) = self::rotl_64( (int)$v[ 6 ], (int)$v[ 7 ], 21 );
60 60
 
61 61
         # v3 ^= v0;
62
-        $v[6] = (int) $v[6] ^ (int) $v[0];
63
-        $v[7] = (int) $v[7] ^ (int) $v[1];
62
+        $v[ 6 ] = (int)$v[ 6 ] ^ (int)$v[ 0 ];
63
+        $v[ 7 ] = (int)$v[ 7 ] ^ (int)$v[ 1 ];
64 64
 
65 65
         # v2 += v1;
66
-        list($v[4], $v[5]) = self::add(
67
-            array((int) $v[4], (int) $v[5]),
68
-            array((int) $v[2], (int) $v[3])
66
+        list( $v[ 4 ], $v[ 5 ] ) = self::add(
67
+            array( (int)$v[ 4 ], (int)$v[ 5 ] ),
68
+            array( (int)$v[ 2 ], (int)$v[ 3 ] )
69 69
         );
70 70
 
71 71
         # v1=ROTL(v1,17);
72
-        list($v[2], $v[3]) = self::rotl_64((int) $v[2], (int) $v[3], 17);
72
+        list( $v[ 2 ], $v[ 3 ] ) = self::rotl_64( (int)$v[ 2 ], (int)$v[ 3 ], 17 );
73 73
 
74 74
         #  v1 ^= v2;;
75
-        $v[2] = (int) $v[2] ^ (int) $v[4];
76
-        $v[3] = (int) $v[3] ^ (int) $v[5];
75
+        $v[ 2 ] = (int)$v[ 2 ] ^ (int)$v[ 4 ];
76
+        $v[ 3 ] = (int)$v[ 3 ] ^ (int)$v[ 5 ];
77 77
 
78 78
         # v2=ROTL(v2,32)
79
-        list($v[4], $v[5]) = self::rotl_64((int) $v[4], (int) $v[5], 32);
79
+        list( $v[ 4 ], $v[ 5 ] ) = self::rotl_64( (int)$v[ 4 ], (int)$v[ 5 ], 32 );
80 80
 
81 81
         return $v;
82 82
     }
@@ -90,14 +90,14 @@  discard block
 block discarded – undo
90 90
      * @param int[] $b
91 91
      * @return array<int, mixed>
92 92
      */
93
-    public static function add(array $a, array $b)
93
+    public static function add( array $a, array $b )
94 94
     {
95 95
         /** @var int $x1 */
96
-        $x1 = $a[1] + $b[1];
96
+        $x1 = $a[ 1 ] + $b[ 1 ];
97 97
         /** @var int $c */
98 98
         $c = $x1 >> 32; // Carry if ($a + $b) > 0xffffffff
99 99
         /** @var int $x0 */
100
-        $x0 = $a[0] + $b[0] + $c;
100
+        $x0 = $a[ 0 ] + $b[ 0 ] + $c;
101 101
         return array(
102 102
             $x0 & 0xffffffff,
103 103
             $x1 & 0xffffffff
@@ -112,33 +112,33 @@  discard block
 block discarded – undo
112 112
      * @param int $c
113 113
      * @return array<int, mixed>
114 114
      */
115
-    public static function rotl_64($int0, $int1, $c)
115
+    public static function rotl_64( $int0, $int1, $c )
116 116
     {
117 117
         $int0 &= 0xffffffff;
118 118
         $int1 &= 0xffffffff;
119 119
         $c &= 63;
120
-        if ($c === 32) {
121
-            return array($int1, $int0);
120
+        if ( $c === 32 ) {
121
+            return array( $int1, $int0 );
122 122
         }
123
-        if ($c > 31) {
123
+        if ( $c > 31 ) {
124 124
             $tmp = $int1;
125 125
             $int1 = $int0;
126 126
             $int0 = $tmp;
127 127
             $c &= 31;
128 128
         }
129
-        if ($c === 0) {
130
-            return array($int0, $int1);
129
+        if ( $c === 0 ) {
130
+            return array( $int0, $int1 );
131 131
         }
132 132
         return array(
133 133
             0xffffffff & (
134
-                ($int0 << $c)
134
+                ( $int0 << $c )
135 135
                     |
136
-                ($int1 >> (32 - $c))
136
+                ( $int1 >> ( 32 - $c ) )
137 137
             ),
138 138
             0xffffffff & (
139
-                ($int1 << $c)
139
+                ( $int1 << $c )
140 140
                     |
141
-                ($int0 >> (32 - $c))
141
+                ( $int0 >> ( 32 - $c ) )
142 142
             ),
143 143
         );
144 144
     }
@@ -160,9 +160,9 @@  discard block
 block discarded – undo
160 160
      * @throws SodiumException
161 161
      * @throws TypeError
162 162
      */
163
-    public static function sipHash24($in, $key)
163
+    public static function sipHash24( $in, $key )
164 164
     {
165
-        $inlen = self::strlen($in);
165
+        $inlen = self::strlen( $in );
166 166
 
167 167
         # /* "somepseudorandomlygeneratedbytes" */
168 168
         # u64 v0 = 0x736f6d6570736575ULL;
@@ -187,10 +187,10 @@  discard block
 block discarded – undo
187 187
         # u64 k0 = LOAD64_LE( k );
188 188
         # u64 k1 = LOAD64_LE( k + 8 );
189 189
         $k = array(
190
-            self::load_4(self::substr($key, 4, 4)),
191
-            self::load_4(self::substr($key, 0, 4)),
192
-            self::load_4(self::substr($key, 12, 4)),
193
-            self::load_4(self::substr($key, 8, 4))
190
+            self::load_4( self::substr( $key, 4, 4 ) ),
191
+            self::load_4( self::substr( $key, 0, 4 ) ),
192
+            self::load_4( self::substr( $key, 12, 4 ) ),
193
+            self::load_4( self::substr( $key, 8, 4 ) )
194 194
         );
195 195
         // k0 => $k[0], $k[1]
196 196
         // k1 => $k[2], $k[3]
@@ -203,41 +203,41 @@  discard block
 block discarded – undo
203 203
         // See docblock for why the 0th index gets the higher bits.
204 204
 
205 205
         # v3 ^= k1;
206
-        $v[6] ^= $k[2];
207
-        $v[7] ^= $k[3];
206
+        $v[ 6 ] ^= $k[ 2 ];
207
+        $v[ 7 ] ^= $k[ 3 ];
208 208
         # v2 ^= k0;
209
-        $v[4] ^= $k[0];
210
-        $v[5] ^= $k[1];
209
+        $v[ 4 ] ^= $k[ 0 ];
210
+        $v[ 5 ] ^= $k[ 1 ];
211 211
         # v1 ^= k1;
212
-        $v[2] ^= $k[2];
213
-        $v[3] ^= $k[3];
212
+        $v[ 2 ] ^= $k[ 2 ];
213
+        $v[ 3 ] ^= $k[ 3 ];
214 214
         # v0 ^= k0;
215
-        $v[0] ^= $k[0];
216
-        $v[1] ^= $k[1];
215
+        $v[ 0 ] ^= $k[ 0 ];
216
+        $v[ 1 ] ^= $k[ 1 ];
217 217
 
218 218
         $left = $inlen;
219 219
         # for ( ; in != end; in += 8 )
220
-        while ($left >= 8) {
220
+        while ( $left >= 8 ) {
221 221
             # m = LOAD64_LE( in );
222 222
             $m = array(
223
-                self::load_4(self::substr($in, 4, 4)),
224
-                self::load_4(self::substr($in, 0, 4))
223
+                self::load_4( self::substr( $in, 4, 4 ) ),
224
+                self::load_4( self::substr( $in, 0, 4 ) )
225 225
             );
226 226
 
227 227
             # v3 ^= m;
228
-            $v[6] ^= $m[0];
229
-            $v[7] ^= $m[1];
228
+            $v[ 6 ] ^= $m[ 0 ];
229
+            $v[ 7 ] ^= $m[ 1 ];
230 230
 
231 231
             # SIPROUND;
232 232
             # SIPROUND;
233
-            $v = self::sipRound($v);
234
-            $v = self::sipRound($v);
233
+            $v = self::sipRound( $v );
234
+            $v = self::sipRound( $v );
235 235
 
236 236
             # v0 ^= m;
237
-            $v[0] ^= $m[0];
238
-            $v[1] ^= $m[1];
237
+            $v[ 0 ] ^= $m[ 0 ];
238
+            $v[ 1 ] ^= $m[ 1 ];
239 239
 
240
-            $in = self::substr($in, 8);
240
+            $in = self::substr( $in, 8 );
241 241
             $left -= 8;
242 242
         }
243 243
 
@@ -252,55 +252,55 @@  discard block
 block discarded – undo
252 252
         #     case 1: b |= ( ( u64 )in[ 0] ); break;
253 253
         #     case 0: break;
254 254
         # }
255
-        switch ($left) {
255
+        switch ( $left ) {
256 256
             case 7:
257
-                $b[0] |= self::chrToInt($in[6]) << 16;
257
+                $b[ 0 ] |= self::chrToInt( $in[ 6 ] ) << 16;
258 258
             case 6:
259
-                $b[0] |= self::chrToInt($in[5]) << 8;
259
+                $b[ 0 ] |= self::chrToInt( $in[ 5 ] ) << 8;
260 260
             case 5:
261
-                $b[0] |= self::chrToInt($in[4]);
261
+                $b[ 0 ] |= self::chrToInt( $in[ 4 ] );
262 262
             case 4:
263
-                $b[1] |= self::chrToInt($in[3]) << 24;
263
+                $b[ 1 ] |= self::chrToInt( $in[ 3 ] ) << 24;
264 264
             case 3:
265
-                $b[1] |= self::chrToInt($in[2]) << 16;
265
+                $b[ 1 ] |= self::chrToInt( $in[ 2 ] ) << 16;
266 266
             case 2:
267
-                $b[1] |= self::chrToInt($in[1]) << 8;
267
+                $b[ 1 ] |= self::chrToInt( $in[ 1 ] ) << 8;
268 268
             case 1:
269
-                $b[1] |= self::chrToInt($in[0]);
269
+                $b[ 1 ] |= self::chrToInt( $in[ 0 ] );
270 270
             case 0:
271 271
                 break;
272 272
         }
273 273
         // See docblock for why the 0th index gets the higher bits.
274 274
 
275 275
         # v3 ^= b;
276
-        $v[6] ^= $b[0];
277
-        $v[7] ^= $b[1];
276
+        $v[ 6 ] ^= $b[ 0 ];
277
+        $v[ 7 ] ^= $b[ 1 ];
278 278
 
279 279
         # SIPROUND;
280 280
         # SIPROUND;
281
-        $v = self::sipRound($v);
282
-        $v = self::sipRound($v);
281
+        $v = self::sipRound( $v );
282
+        $v = self::sipRound( $v );
283 283
 
284 284
         # v0 ^= b;
285
-        $v[0] ^= $b[0];
286
-        $v[1] ^= $b[1];
285
+        $v[ 0 ] ^= $b[ 0 ];
286
+        $v[ 1 ] ^= $b[ 1 ];
287 287
 
288 288
         // Flip the lower 8 bits of v2 which is ($v[4], $v[5]) in our implementation
289 289
         # v2 ^= 0xff;
290
-        $v[5] ^= 0xff;
290
+        $v[ 5 ] ^= 0xff;
291 291
 
292 292
         # SIPROUND;
293 293
         # SIPROUND;
294 294
         # SIPROUND;
295 295
         # SIPROUND;
296
-        $v = self::sipRound($v);
297
-        $v = self::sipRound($v);
298
-        $v = self::sipRound($v);
299
-        $v = self::sipRound($v);
296
+        $v = self::sipRound( $v );
297
+        $v = self::sipRound( $v );
298
+        $v = self::sipRound( $v );
299
+        $v = self::sipRound( $v );
300 300
 
301 301
         # b = v0 ^ v1 ^ v2 ^ v3;
302 302
         # STORE64_LE( out, b );
303
-        return  self::store32_le($v[1] ^ $v[3] ^ $v[5] ^ $v[7]) .
304
-            self::store32_le($v[0] ^ $v[2] ^ $v[4] ^ $v[6]);
303
+        return  self::store32_le( $v[ 1 ] ^ $v[ 3 ] ^ $v[ 5 ] ^ $v[ 7 ] ) .
304
+            self::store32_le( $v[ 0 ] ^ $v[ 2 ] ^ $v[ 4 ] ^ $v[ 6 ] );
305 305
     }
306 306
 }
Please login to merge, or discard this patch.
Braces   +5 added lines, -10 removed lines patch added patch discarded remove patch
@@ -9,8 +9,7 @@  discard block
 block discarded – undo
9 9
  *
10 10
  * Only uses 32-bit arithmetic, while the original SipHash used 64-bit integers
11 11
  */
12
-class ParagonIE_Sodium_Core_SipHash extends ParagonIE_Sodium_Core_Util
13
-{
12
+class ParagonIE_Sodium_Core_SipHash extends ParagonIE_Sodium_Core_Util {
14 13
     /**
15 14
      * @internal You should not use this directly from another application
16 15
      *
@@ -18,8 +17,7 @@  discard block
 block discarded – undo
18 17
      * @return int[]
19 18
      *
20 19
      */
21
-    public static function sipRound(array $v)
22
-    {
20
+    public static function sipRound(array $v) {
23 21
         # v0 += v1;
24 22
         list($v[0], $v[1]) = self::add(
25 23
             array($v[0], $v[1]),
@@ -90,8 +88,7 @@  discard block
 block discarded – undo
90 88
      * @param int[] $b
91 89
      * @return array<int, mixed>
92 90
      */
93
-    public static function add(array $a, array $b)
94
-    {
91
+    public static function add(array $a, array $b) {
95 92
         /** @var int $x1 */
96 93
         $x1 = $a[1] + $b[1];
97 94
         /** @var int $c */
@@ -112,8 +109,7 @@  discard block
 block discarded – undo
112 109
      * @param int $c
113 110
      * @return array<int, mixed>
114 111
      */
115
-    public static function rotl_64($int0, $int1, $c)
116
-    {
112
+    public static function rotl_64($int0, $int1, $c) {
117 113
         $int0 &= 0xffffffff;
118 114
         $int1 &= 0xffffffff;
119 115
         $c &= 63;
@@ -160,8 +156,7 @@  discard block
 block discarded – undo
160 156
      * @throws SodiumException
161 157
      * @throws TypeError
162 158
      */
163
-    public static function sipHash24($in, $key)
164
-    {
159
+    public static function sipHash24($in, $key) {
165 160
         $inlen = self::strlen($in);
166 161
 
167 162
         # /* "somepseudorandomlygeneratedbytes" */
Please login to merge, or discard this patch.