Code Duplication    Length = 48-48 lines in 2 locations

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

@@ 28-75 (lines=48) @@
25
     * @throws Error
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 Error('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 Error('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 Error('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
@@ 426-473 (lines=48) @@
423
     * @throws Error
424
     * @throws TypeError
425
     */
426
    public static function secretbox($inputFile, $outputFile, $nonce, $key)
427
    {
428
        /* Type checks: */
429
        if (!is_string($inputFile)) {
430
            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..');
431
        }
432
        if (!is_string($outputFile)) {
433
            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
434
        }
435
        if (!is_string($nonce)) {
436
            throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
437
        }
438
439
        /* Input validation: */
440
        if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) {
441
            throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes');
442
        }
443
        if (!is_string($key)) {
444
            throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.');
445
        }
446
        if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) {
447
            throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes');
448
        }
449
450
        /** @var int $size */
451
        $size = filesize($inputFile);
452
        if (!is_int($size)) {
453
            throw new Error('Could not obtain the file size');
454
        }
455
456
        /** @var resource $ifp */
457
        $ifp = fopen($inputFile, 'rb');
458
        if (!is_resource($ifp)) {
459
            throw new Error('Could not open input file for reading');
460
        }
461
462
        /** @var resource $ofp */
463
        $ofp = fopen($outputFile, 'wb');
464
        if (!is_resource($ofp)) {
465
            fclose($ifp);
466
            throw new Error('Could not open output file for writing');
467
        }
468
469
        $res = self::secretbox_encrypt($ifp, $ofp, $size, $nonce, $key);
470
        fclose($ifp);
471
        fclose($ofp);
472
        return $res;
473
    }
474
    /**
475
     * Seal a file (rather than a string). Uses less memory than
476
     * ParagonIE_Sodium_Compat::crypto_secretbox_open(), but produces