You are here

public static function ParagonIE_Sodium_File::box_seal in Automatic Updates 8

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

Seal a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_box_seal(), but produces the same result.

Parameters

string $inputFile Absolute path to a file on the filesystem:

string $outputFile Absolute path to a file on the filesystem:

string $publicKey ECDH public key:

Return value

bool

Throws

SodiumException

TypeError

File

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

Class

ParagonIE_Sodium_File
Class ParagonIE_Sodium_File

Code

public static function box_seal($inputFile, $outputFile, $publicKey) {

  /* 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($publicKey)) {
    throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.');
  }

  /* Input validation: */
  if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
    throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES 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');
  }

  /** @var string $ephKeypair */
  $ephKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair();

  /** @var string $msgKeypair */
  $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey(ParagonIE_Sodium_Compat::crypto_box_secretkey($ephKeypair), $publicKey);

  /** @var string $ephemeralPK */
  $ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey($ephKeypair);

  /** @var string $nonce */
  $nonce = ParagonIE_Sodium_Compat::crypto_generichash($ephemeralPK . $publicKey, '', 24);

  /** @var int $firstWrite */
  $firstWrite = fwrite($ofp, $ephemeralPK, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES);
  if (!is_int($firstWrite)) {
    fclose($ifp);
    fclose($ofp);
    ParagonIE_Sodium_Compat::memzero($ephKeypair);
    throw new SodiumException('Could not write to output file');
  }
  if ($firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
    ParagonIE_Sodium_Compat::memzero($ephKeypair);
    fclose($ifp);
    fclose($ofp);
    throw new SodiumException('Error writing public key to output file');
  }
  $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $msgKeypair);
  fclose($ifp);
  fclose($ofp);
  try {
    ParagonIE_Sodium_Compat::memzero($nonce);
    ParagonIE_Sodium_Compat::memzero($ephKeypair);
  } catch (SodiumException $ex) {
    unset($ephKeypair);
  }
  return $res;
}