Code Duplication    Length = 52-52 lines in 2 locations

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

@@ 251-302 (lines=52) @@
248
     * @throws Error
249
     * @throws TypeError
250
     */
251
    public static function crypto_aead_chacha20poly1305_encrypt(
252
        $plaintext = '',
253
        $assocData = '',
254
        $nonce = '',
255
        $key = ''
256
    ) {
257
        /* Type checks: */
258
        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
259
        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
260
        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
261
        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
262
263
        /* Input validation: */
264
        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES) {
265
            throw new Error('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES long');
266
        }
267
        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
268
            throw new Error('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
269
        }
270
271
        if (self::isPhp72OrGreater()) {
272
            return sodium_crypto_aead_chacha20poly1305_encrypt(
273
                $plaintext,
274
                $assocData,
275
                $nonce,
276
                $key
277
            );
278
        }
279
        if (self::use_fallback('crypto_aead_chacha20poly1305_encrypt')) {
280
            return call_user_func(
281
                '\\Sodium\\crypto_aead_chacha20poly1305_encrypt',
282
                $plaintext,
283
                $assocData,
284
                $nonce,
285
                $key
286
            );
287
        }
288
        if (PHP_INT_SIZE === 4) {
289
            return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_encrypt(
290
                $plaintext,
291
                $assocData,
292
                $nonce,
293
                $key
294
            );
295
        }
296
        return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_encrypt(
297
            $plaintext,
298
            $assocData,
299
            $nonce,
300
            $key
301
        );
302
    }
303
304
    /**
305
     * Authenticated Encryption with Associated Data: Decryption
@@ 397-448 (lines=52) @@
394
     * @throws Error
395
     * @throws TypeError
396
     */
397
    public static function crypto_aead_chacha20poly1305_ietf_encrypt(
398
        $plaintext = '',
399
        $assocData = '',
400
        $nonce = '',
401
        $key = ''
402
    ) {
403
        /* Type checks: */
404
        ParagonIE_Sodium_Core_Util::declareScalarType($plaintext, 'string', 1);
405
        ParagonIE_Sodium_Core_Util::declareScalarType($assocData, 'string', 2);
406
        ParagonIE_Sodium_Core_Util::declareScalarType($nonce, 'string', 3);
407
        ParagonIE_Sodium_Core_Util::declareScalarType($key, 'string', 4);
408
409
        /* Input validation: */
410
        if (ParagonIE_Sodium_Core_Util::strlen($nonce) !== self::CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES) {
411
            throw new Error('Nonce must be CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES long');
412
        }
413
        if (ParagonIE_Sodium_Core_Util::strlen($key) !== self::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES) {
414
            throw new Error('Key must be CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES long');
415
        }
416
417
        if (self::isPhp72OrGreater()) {
418
            return sodium_crypto_aead_chacha20poly1305_ietf_encrypt(
419
                $plaintext,
420
                $assocData,
421
                $nonce,
422
                $key
423
            );
424
        }
425
        if (self::use_fallback('crypto_aead_chacha20poly1305_ietf_encrypt')) {
426
            return call_user_func(
427
                '\\Sodium\\crypto_aead_chacha20poly1305_ietf_encrypt',
428
                $plaintext,
429
                $assocData,
430
                $nonce,
431
                $key
432
            );
433
        }
434
        if (PHP_INT_SIZE === 4) {
435
            return ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_ietf_encrypt(
436
                $plaintext,
437
                $assocData,
438
                $nonce,
439
                $key
440
            );
441
        }
442
        return ParagonIE_Sodium_Crypto::aead_chacha20poly1305_ietf_encrypt(
443
            $plaintext,
444
            $assocData,
445
            $nonce,
446
            $key
447
        );
448
    }
449
450
    /**
451
     * Authenticated Encryption with Associated Data: Decryption