ParagonIE_Sodium_File::updateHashWithFile() – Update a hash context with the contents of a file, without loading the entire file into memory.

You appear to be a bot. Output may be restricted

Description

Update a hash context with the contents of a file, without loading the entire file into memory.

Usage

$resource|object = ParagonIE_Sodium_File::updateHashWithFile( $hash, $fp, $size );

Parameters

$hash
( resource|HashContext ) required
$fp
( resource ) required
$size
( int ) optional

Returns

resource|object Resource on PHP < 7.2, HashContext object on PHP >= 7.2

Source

File name: wordpress/wp-includes/sodium_compat/src/File.php
Lines:

1 to 44 of 44
    public static function updateHashWithFile($hash, $fp, $size = 0)
    {
        /* Type checks: */
        if (PHP_VERSION_ID < 70200) {
            if (!is_resource($hash)) {
                throw new TypeError('Argument 1 must be a resource, ' . gettype($hash) . ' given.');
            }
        } else {
            if (!is_object($hash)) {
                throw new TypeError('Argument 1 must be an object (PHP 7.2+), ' . gettype($hash) . ' given.');
            }
        }

        if (!is_resource($fp)) {
            throw new TypeError('Argument 2 must be a resource, ' . gettype($fp) . ' given.');
        }
        if (!is_int($size)) {
            throw new TypeError('Argument 3 must be an integer, ' . gettype($size) . ' given.');
        }

        
/** @var int $originalPosition */
        $originalPosition = self::ftell($fp);

        // Move file pointer to beginning of file
        fseek($fp, 0, SEEK_SET);
        for ($i = 0; $i < $size; $i += self::BUFFER_SIZE) {
            
/** @var string|bool $message */
            $message = fread(
                $fp,
                ($size - $i) > self::BUFFER_SIZE
                    ? $size - $i
                    : self::BUFFER_SIZE
            );
            if (!is_string($message)) {
                throw new SodiumException('Unexpected error reading from file.');
            }
            
/** @var string $message */
            
/** @psalm-suppress InvalidArgument */
            self::hash_update($hash, $message);
        }
        // Reset file pointer's position
        fseek($fp, $originalPosition, SEEK_SET);
        return $hash;
    }
 

 View on GitHub View on Trac