You are here

public static function ParagonIE_Sodium_File::updateHashWithFile in Automatic Updates 8

Same name and namespace in other branches
  1. 7 vendor/paragonie/sodium_compat/src/File.php \ParagonIE_Sodium_File::updateHashWithFile()

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

@psalm-suppress PossiblyInvalidArgument PHP 7.2 changes from a resource to an object, which causes Psalm to complain about an error. @psalm-suppress TypeCoercion Ditto.

Parameters

resource|object $hash:

resource $fp:

int $size:

Return value

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

Throws

SodiumException

TypeError

4 calls to ParagonIE_Sodium_File::updateHashWithFile()
ParagonIE_Sodium_File::sign in vendor/paragonie/sodium_compat/src/File.php
Sign a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces the same result.
ParagonIE_Sodium_File::sign_core32 in vendor/paragonie/sodium_compat/src/File.php
Sign a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces the same result. (32-bit)
ParagonIE_Sodium_File::verify in vendor/paragonie/sodium_compat/src/File.php
Verify a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but produces the same result.
ParagonIE_Sodium_File::verify_core32 in vendor/paragonie/sodium_compat/src/File.php
Verify a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but produces the same result. (32-bit)

File

vendor/paragonie/sodium_compat/src/File.php, line 1096

Class

ParagonIE_Sodium_File
Class ParagonIE_Sodium_File

Code

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 */
    hash_update($hash, $message);
  }

  // Reset file pointer's position
  fseek($fp, $originalPosition, SEEK_SET);
  return $hash;
}