Code Duplication    Length = 55-55 lines in 2 locations

src/library/sodium_compat/src/Compat.php 2 locations

@@ 176-230 (lines=55) @@
173
     * @throws Error
174
     * @throws TypeError
175
     */
176
    public static function crypto_aead_chacha20poly1305_decrypt(
177
        $ciphertext = '',
178
        $assocData = '',
179
        $nonce = '',
180
        $key = ''
181
    ) {
182
        /* Type checks: */
183
        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
184
        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
185
        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
186
        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
187
188
        /* Input validation: */
189
        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES) {
190
            throw new Error('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES long');
191
        }
192
        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
193
            throw new Error('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
194
        }
195
        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES) {
196
            throw new Error('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_ABYTES long');
197
        }
198
199
        if (self::isPhp72OrGreater()) {
200
            return sodium_crypto_aead_chacha20poly1305_decrypt(
201
                $ciphertext,
202
                $assocData,
203
                $nonce,
204
                $key
205
            );
206
        }
207
        if (self::use_fallback('crypto_aead_chacha20poly1305_decrypt')) {
208
            return call_user_func(
209
                '\\Sodium\\crypto_aead_chacha20poly1305_decrypt',
210
                $ciphertext,
211
                $assocData,
212
                $nonce,
213
                $key
214
            );
215
        }
216
        if (PHP_INT_SIZE === 4) {
217
            return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_decrypt(
218
                $ciphertext,
219
                $assocData,
220
                $nonce,
221
                $key
222
            );
223
        }
224
        return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_decrypt(
225
            $ciphertext,
226
            $assocData,
227
            $nonce,
228
            $key
229
        );
230
    }
231
232
    /**
233
     * Authenticated Encryption with Associated Data
@@ 322-376 (lines=55) @@
319
     * @throws Error
320
     * @throws TypeError
321
     */
322
    public static function crypto_aead_chacha20poly1305_ietf_decrypt(
323
        $ciphertext = '',
324
        $assocData = '',
325
        $nonce = '',
326
        $key = ''
327
    ) {
328
        /* Type checks: */
329
        ParagonIE_Sodium_Core_Util::declareScalarType($ciphertext, 'string', 1);
330
        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
331
        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
332
        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
333
334
        /* Input validation: */
335
        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES) {
336
            throw new Error('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long');
337
        }
338
        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
339
            throw new Error('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
340
        }
341
        if (ParagonIE_Sodium_Core_Util::strlen($ciphertext) < self::CRYPTO_AEAD_CHACHA20POLY1305_ABYTES) {
342
            throw new Error('Message must be at least CRYPTO_AEAD_CHACHA20POLY1305_ABYTES long');
343
        }
344
345
        if (self::isPhp72OrGreater()) {
346
            return sodium_crypto_aead_chacha20poly1305_ietf_decrypt(
347
                $ciphertext,
348
                $assocData,
349
                $nonce,
350
                $key
351
            );
352
        }
353
        if (self::use_fallback('crypto_aead_chacha20poly1305_ietf_decrypt')) {
354
            return call_user_func(
355
                '\\Sodium\\crypto_aead_chacha20poly1305_ietf_decrypt',
356
                $ciphertext,
357
                $assocData,
358
                $nonce,
359
                $key
360
            );
361
        }
362
        if (PHP_INT_SIZE === 4) {
363
            return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_ietf_decrypt(
364
                $ciphertext,
365
                $assocData,
366
                $nonce,
367
                $key
368
            );
369
        }
370
        return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_ietf_decrypt(
371
            $ciphertext,
372
            $assocData,
373
            $nonce,
374
            $key
375
        );
376
    }
377
378
    /**
379
     * Authenticated Encryption with Associated Data