1<?php
2
3if (class_exists('ParagonIE_Sodium_Core32_XChaCha20', false)) {
4 return;
5}
6
7/**
8 * Class ParagonIE_Sodium_Core32_XChaCha20
9 */
10class ParagonIE_Sodium_Core32_XChaCha20 extends ParagonIE_Sodium_Core32_HChaCha20
11{
12 /**
13 * @internal You should not use this directly from another application
14 *
15 * @param int $len
16 * @param string $nonce
17 * @param string $key
18 * @return string
19 * @throws SodiumException
20 * @throws TypeError
21 */
22 public static function stream($len = 64, $nonce = '', $key = '')
23 {
24 if (self::strlen($nonce) !== 24) {
25 throw new SodiumException('Nonce must be 24 bytes long');
26 }
27 return self::encryptBytes(
28 new ParagonIE_Sodium_Core32_ChaCha20_Ctx(
29 self::hChaCha20(
30 self::substr($nonce, 0, 16),
31 $key
32 ),
33 self::substr($nonce, 16, 8)
34 ),
35 str_repeat("\x00", $len)
36 );
37 }
38
39 /**
40 * @internal You should not use this directly from another application
41 *
42 * @param string $message
43 * @param string $nonce
44 * @param string $key
45 * @param string $ic
46 * @return string
47 * @throws SodiumException
48 * @throws TypeError
49 */
50 public static function streamXorIc($message, $nonce = '', $key = '', $ic = '')
51 {
52 if (self::strlen($nonce) !== 24) {
53 throw new SodiumException('Nonce must be 24 bytes long');
54 }
55 return self::encryptBytes(
56 new ParagonIE_Sodium_Core32_ChaCha20_Ctx(
57 self::hChaCha20(self::substr($nonce, 0, 16), $key),
58 self::substr($nonce, 16, 8),
59 $ic
60 ),
61 $message
62 );
63 }
64
65 /**
66 * @internal You should not use this directly from another application
67 *
68 * @param string $message
69 * @param string $nonce
70 * @param string $key
71 * @param string $ic
72 * @return string
73 * @throws SodiumException
74 * @throws TypeError
75 */
76 public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '')
77 {
78 return self::encryptBytes(
79 new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx(
80 self::hChaCha20(self::substr($nonce, 0, 16), $key),
81 "\x00\x00\x00\x00" . self::substr($nonce, 16, 8),
82 $ic
83 ),
84 $message
85 );
86 }
87}
88