Debian Codegraph @

lib/3rdParty/composer/paragonie/sodium_compat/src/Core/XChaCha20.php

raw symbols SCIP
1 <?php
2 declare(strict_types=1);
3
4 if (class_exists('ParagonIE_Sodium_Core_XChaCha20', false)) {
5 return;
6 }
7
8 /**
9 * Class ParagonIE_Sodium_Core_XChaCha20
10 */
11 class ParagonIE_Sodium_Core_XChaCha20 extends ParagonIE_Sodium_Core_HChaCha20
12 {
13 /**
14 * @internal You should not use this directly from another application
15 *
16 * @param int $len
17 * @param string $nonce
18 * @param string $key
19 * @return string
20 * @throws SodiumException
21 * @throws TypeError
22 */
23 public static function stream(
24 int $len,
25 string $nonce,
26 #[SensitiveParameter]
27 string $key
28 ): string {
29 if (self::strlen($nonce) !== 24) {
30 throw new SodiumException('Nonce must be 24 bytes long');
31 }
32 return self::encryptBytes(
33 new ParagonIE_Sodium_Core_ChaCha20_Ctx(
34 self::hChaCha20(
35 self::substr($nonce, 0, 16),
36 $key
37 ),
38 self::substr($nonce, 16, 8)
39 ),
40 str_repeat("\x00", $len)
41 );
42 }
43
44 /**
45 * @internal You should not use this directly from another application
46 *
47 * @param int $len
48 * @param string $nonce
49 * @param string $key
50 * @return string
51 * @throws SodiumException
52 * @throws TypeError
53 */
54 public static function ietfStream(
55 int $len,
56 string $nonce,
57 #[SensitiveParameter]
58 string $key
59 ): string {
60 if (self::strlen($nonce) !== 24) {
61 throw new SodiumException('Nonce must be 24 bytes long');
62 }
63 return self::encryptBytes(
64 new ParagonIE_Sodium_Core_ChaCha20_IetfCtx(
65 self::hChaCha20(
66 self::substr($nonce, 0, 16),
67 $key
68 ),
69 "\x00\x00\x00\x00" . self::substr($nonce, 16, 8)
70 ),
71 str_repeat("\x00", $len)
72 );
73 }
74
75 /**
76 * @internal You should not use this directly from another application
77 *
78 * @param string $message
79 * @param string $nonce
80 * @param string $key
81 * @param string $ic
82 * @return string
83 * @throws SodiumException
84 * @throws TypeError
85 */
86 public static function streamXorIc(
87 #[SensitiveParameter]
88 string $message,
89 string $nonce,
90 #[SensitiveParameter]
91 string $key,
92 string $ic = ''
93 ): string {
94 if (self::strlen($nonce) !== 24) {
95 throw new SodiumException('Nonce must be 24 bytes long');
96 }
97 return self::encryptBytes(
98 new ParagonIE_Sodium_Core_ChaCha20_Ctx(
99 self::hChaCha20(self::substr($nonce, 0, 16), $key),
100 self::substr($nonce, 16, 8),
101 $ic
102 ),
103 $message
104 );
105 }
106
107 /**
108 * @internal You should not use this directly from another application
109 *
110 * @param string $message
111 * @param string $nonce
112 * @param string $key
113 * @param string $ic
114 * @return string
115 * @throws SodiumException
116 * @throws TypeError
117 */
118 public static function ietfStreamXorIc(
119 #[SensitiveParameter]
120 string $message,
121 string $nonce,
122 string $key,
123 string $ic = ''
124 ): string {
125 if (self::strlen($nonce) !== 24) {
126 throw new SodiumException('Nonce must be 24 bytes long');
127 }
128 return self::encryptBytes(
129 new ParagonIE_Sodium_Core_ChaCha20_IetfCtx(
130 self::hChaCha20(self::substr($nonce, 0, 16), $key),
131 "\x00\x00\x00\x00" . self::substr($nonce, 16, 8),
132 $ic
133 ),
134 $message
135 );
136 }
137 }