WordPress at Your Fingertips
function is not described

ParagonIE_Sodium_File::box_open() public WP 1.0

Open a boxed file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_box_open(), but produces the same result.

Warning: Does not protect against TOCTOU attacks. You should just load the file into memory and use crypto_box_open() if you are worried about those.

{} It's a method of the class: ParagonIE_Sodium_File{}

No Hooks.

Return

true|false.

Usage

$result = ParagonIE_Sodium_File::box_open( $inputFile, $outputFile, $nonce, $keypair );
$inputFile(string) (required)
-
$outputFile(string) (required)
-
$nonce(string) (required)
-
$keypair(string) (required)
-

Code of ParagonIE_Sodium_File::box_open() WP 5.8.2

public static function box_open($inputFile, $outputFile, $nonce, $keypair)
{
    /* Type checks: */
    if (!is_string($inputFile)) {
        throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
    }
    if (!is_string($outputFile)) {
        throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
    }
    if (!is_string($nonce)) {
        throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
    }
    if (!is_string($keypair)) {
        throw new TypeError('Argument 4 must be a string, ' . gettype($keypair) . ' given.');
    }

    /* Input validation: */
    if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) {
        throw new TypeError('Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes');
    }
    if (self::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
        throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
    }

    /** @var int $size */
    $size = filesize($inputFile);
    if (!is_int($size)) {
        throw new SodiumException('Could not obtain the file size');
    }

    /** @var resource $ifp */
    $ifp = fopen($inputFile, 'rb');
    if (!is_resource($ifp)) {
        throw new SodiumException('Could not open input file for reading');
    }

    /** @var resource $ofp */
    $ofp = fopen($outputFile, 'wb');
    if (!is_resource($ofp)) {
        fclose($ifp);
        throw new SodiumException('Could not open output file for writing');
    }

    $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair);
    fclose($ifp);
    fclose($ofp);
    try {
        ParagonIE_Sodium_Compat::memzero($nonce);
        ParagonIE_Sodium_Compat::memzero($ephKeypair);
    } catch (SodiumException $ex) {
        if (isset($ephKeypair)) {
            unset($ephKeypair);
        }
    }
    return $res;
}