Edit File by line
/home/zeestwma/richards.../wp-inclu.../sodium_c.../src
File: File.php
<?php
[0] Fix | Delete
[1] Fix | Delete
if (class_exists('ParagonIE_Sodium_File', false)) {
[2] Fix | Delete
return;
[3] Fix | Delete
}
[4] Fix | Delete
/**
[5] Fix | Delete
* Class ParagonIE_Sodium_File
[6] Fix | Delete
*/
[7] Fix | Delete
class ParagonIE_Sodium_File extends ParagonIE_Sodium_Core_Util
[8] Fix | Delete
{
[9] Fix | Delete
/* PHP's default buffer size is 8192 for fread()/fwrite(). */
[10] Fix | Delete
const BUFFER_SIZE = 8192;
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Box a file (rather than a string). Uses less memory than
[14] Fix | Delete
* ParagonIE_Sodium_Compat::crypto_box(), but produces
[15] Fix | Delete
* the same result.
[16] Fix | Delete
*
[17] Fix | Delete
* @param string $inputFile Absolute path to a file on the filesystem
[18] Fix | Delete
* @param string $outputFile Absolute path to a file on the filesystem
[19] Fix | Delete
* @param string $nonce Number to be used only once
[20] Fix | Delete
* @param string $keyPair ECDH secret key and ECDH public key concatenated
[21] Fix | Delete
*
[22] Fix | Delete
* @return bool
[23] Fix | Delete
* @throws SodiumException
[24] Fix | Delete
* @throws TypeError
[25] Fix | Delete
*/
[26] Fix | Delete
public static function box(
[27] Fix | Delete
$inputFile,
[28] Fix | Delete
$outputFile,
[29] Fix | Delete
$nonce,
[30] Fix | Delete
#[\SensitiveParameter]
[31] Fix | Delete
$keyPair
[32] Fix | Delete
) {
[33] Fix | Delete
/* Type checks: */
[34] Fix | Delete
if (!is_string($inputFile)) {
[35] Fix | Delete
throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
[36] Fix | Delete
}
[37] Fix | Delete
if (!is_string($outputFile)) {
[38] Fix | Delete
throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
[39] Fix | Delete
}
[40] Fix | Delete
if (!is_string($nonce)) {
[41] Fix | Delete
throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
/* Input validation: */
[45] Fix | Delete
if (!is_string($keyPair)) {
[46] Fix | Delete
throw new TypeError('Argument 4 must be a string, ' . gettype($keyPair) . ' given.');
[47] Fix | Delete
}
[48] Fix | Delete
if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) {
[49] Fix | Delete
throw new TypeError('Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes');
[50] Fix | Delete
}
[51] Fix | Delete
if (self::strlen($keyPair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
[52] Fix | Delete
throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
/** @var int $size */
[56] Fix | Delete
$size = filesize($inputFile);
[57] Fix | Delete
if (!is_int($size)) {
[58] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/** @var resource $ifp */
[62] Fix | Delete
$ifp = fopen($inputFile, 'rb');
[63] Fix | Delete
if (!is_resource($ifp)) {
[64] Fix | Delete
throw new SodiumException('Could not open input file for reading');
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
/** @var resource $ofp */
[68] Fix | Delete
$ofp = fopen($outputFile, 'wb');
[69] Fix | Delete
if (!is_resource($ofp)) {
[70] Fix | Delete
fclose($ifp);
[71] Fix | Delete
throw new SodiumException('Could not open output file for writing');
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
$res = self::box_encrypt($ifp, $ofp, $size, $nonce, $keyPair);
[75] Fix | Delete
fclose($ifp);
[76] Fix | Delete
fclose($ofp);
[77] Fix | Delete
return $res;
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
/**
[81] Fix | Delete
* Open a boxed file (rather than a string). Uses less memory than
[82] Fix | Delete
* ParagonIE_Sodium_Compat::crypto_box_open(), but produces
[83] Fix | Delete
* the same result.
[84] Fix | Delete
*
[85] Fix | Delete
* Warning: Does not protect against TOCTOU attacks. You should
[86] Fix | Delete
* just load the file into memory and use crypto_box_open() if
[87] Fix | Delete
* you are worried about those.
[88] Fix | Delete
*
[89] Fix | Delete
* @param string $inputFile
[90] Fix | Delete
* @param string $outputFile
[91] Fix | Delete
* @param string $nonce
[92] Fix | Delete
* @param string $keypair
[93] Fix | Delete
* @return bool
[94] Fix | Delete
* @throws SodiumException
[95] Fix | Delete
* @throws TypeError
[96] Fix | Delete
*/
[97] Fix | Delete
public static function box_open(
[98] Fix | Delete
$inputFile,
[99] Fix | Delete
$outputFile,
[100] Fix | Delete
$nonce,
[101] Fix | Delete
#[\SensitiveParameter]
[102] Fix | Delete
$keypair
[103] Fix | Delete
) {
[104] Fix | Delete
/* Type checks: */
[105] Fix | Delete
if (!is_string($inputFile)) {
[106] Fix | Delete
throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
[107] Fix | Delete
}
[108] Fix | Delete
if (!is_string($outputFile)) {
[109] Fix | Delete
throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
[110] Fix | Delete
}
[111] Fix | Delete
if (!is_string($nonce)) {
[112] Fix | Delete
throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
[113] Fix | Delete
}
[114] Fix | Delete
if (!is_string($keypair)) {
[115] Fix | Delete
throw new TypeError('Argument 4 must be a string, ' . gettype($keypair) . ' given.');
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
/* Input validation: */
[119] Fix | Delete
if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) {
[120] Fix | Delete
throw new TypeError('Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes');
[121] Fix | Delete
}
[122] Fix | Delete
if (self::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
[123] Fix | Delete
throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
/** @var int $size */
[127] Fix | Delete
$size = filesize($inputFile);
[128] Fix | Delete
if (!is_int($size)) {
[129] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
/** @var resource $ifp */
[133] Fix | Delete
$ifp = fopen($inputFile, 'rb');
[134] Fix | Delete
if (!is_resource($ifp)) {
[135] Fix | Delete
throw new SodiumException('Could not open input file for reading');
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
/** @var resource $ofp */
[139] Fix | Delete
$ofp = fopen($outputFile, 'wb');
[140] Fix | Delete
if (!is_resource($ofp)) {
[141] Fix | Delete
fclose($ifp);
[142] Fix | Delete
throw new SodiumException('Could not open output file for writing');
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
$res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair);
[146] Fix | Delete
fclose($ifp);
[147] Fix | Delete
fclose($ofp);
[148] Fix | Delete
try {
[149] Fix | Delete
ParagonIE_Sodium_Compat::memzero($nonce);
[150] Fix | Delete
ParagonIE_Sodium_Compat::memzero($ephKeypair);
[151] Fix | Delete
} catch (SodiumException $ex) {
[152] Fix | Delete
if (isset($ephKeypair)) {
[153] Fix | Delete
unset($ephKeypair);
[154] Fix | Delete
}
[155] Fix | Delete
}
[156] Fix | Delete
return $res;
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
/**
[160] Fix | Delete
* Seal a file (rather than a string). Uses less memory than
[161] Fix | Delete
* ParagonIE_Sodium_Compat::crypto_box_seal(), but produces
[162] Fix | Delete
* the same result.
[163] Fix | Delete
*
[164] Fix | Delete
* @param string $inputFile Absolute path to a file on the filesystem
[165] Fix | Delete
* @param string $outputFile Absolute path to a file on the filesystem
[166] Fix | Delete
* @param string $publicKey ECDH public key
[167] Fix | Delete
*
[168] Fix | Delete
* @return bool
[169] Fix | Delete
* @throws SodiumException
[170] Fix | Delete
* @throws TypeError
[171] Fix | Delete
*/
[172] Fix | Delete
public static function box_seal(
[173] Fix | Delete
$inputFile,
[174] Fix | Delete
$outputFile,
[175] Fix | Delete
#[\SensitiveParameter]
[176] Fix | Delete
$publicKey
[177] Fix | Delete
) {
[178] Fix | Delete
/* Type checks: */
[179] Fix | Delete
if (!is_string($inputFile)) {
[180] Fix | Delete
throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
[181] Fix | Delete
}
[182] Fix | Delete
if (!is_string($outputFile)) {
[183] Fix | Delete
throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
[184] Fix | Delete
}
[185] Fix | Delete
if (!is_string($publicKey)) {
[186] Fix | Delete
throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.');
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
/* Input validation: */
[190] Fix | Delete
if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
[191] Fix | Delete
throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes');
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
/** @var int $size */
[195] Fix | Delete
$size = filesize($inputFile);
[196] Fix | Delete
if (!is_int($size)) {
[197] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
/** @var resource $ifp */
[201] Fix | Delete
$ifp = fopen($inputFile, 'rb');
[202] Fix | Delete
if (!is_resource($ifp)) {
[203] Fix | Delete
throw new SodiumException('Could not open input file for reading');
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
/** @var resource $ofp */
[207] Fix | Delete
$ofp = fopen($outputFile, 'wb');
[208] Fix | Delete
if (!is_resource($ofp)) {
[209] Fix | Delete
fclose($ifp);
[210] Fix | Delete
throw new SodiumException('Could not open output file for writing');
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
/** @var string $ephKeypair */
[214] Fix | Delete
$ephKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair();
[215] Fix | Delete
[216] Fix | Delete
/** @var string $msgKeypair */
[217] Fix | Delete
$msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey(
[218] Fix | Delete
ParagonIE_Sodium_Compat::crypto_box_secretkey($ephKeypair),
[219] Fix | Delete
$publicKey
[220] Fix | Delete
);
[221] Fix | Delete
[222] Fix | Delete
/** @var string $ephemeralPK */
[223] Fix | Delete
$ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey($ephKeypair);
[224] Fix | Delete
[225] Fix | Delete
/** @var string $nonce */
[226] Fix | Delete
$nonce = ParagonIE_Sodium_Compat::crypto_generichash(
[227] Fix | Delete
$ephemeralPK . $publicKey,
[228] Fix | Delete
'',
[229] Fix | Delete
24
[230] Fix | Delete
);
[231] Fix | Delete
[232] Fix | Delete
/** @var int $firstWrite */
[233] Fix | Delete
$firstWrite = fwrite(
[234] Fix | Delete
$ofp,
[235] Fix | Delete
$ephemeralPK,
[236] Fix | Delete
ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES
[237] Fix | Delete
);
[238] Fix | Delete
if (!is_int($firstWrite)) {
[239] Fix | Delete
fclose($ifp);
[240] Fix | Delete
fclose($ofp);
[241] Fix | Delete
ParagonIE_Sodium_Compat::memzero($ephKeypair);
[242] Fix | Delete
throw new SodiumException('Could not write to output file');
[243] Fix | Delete
}
[244] Fix | Delete
if ($firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
[245] Fix | Delete
ParagonIE_Sodium_Compat::memzero($ephKeypair);
[246] Fix | Delete
fclose($ifp);
[247] Fix | Delete
fclose($ofp);
[248] Fix | Delete
throw new SodiumException('Error writing public key to output file');
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
$res = self::box_encrypt($ifp, $ofp, $size, $nonce, $msgKeypair);
[252] Fix | Delete
fclose($ifp);
[253] Fix | Delete
fclose($ofp);
[254] Fix | Delete
try {
[255] Fix | Delete
ParagonIE_Sodium_Compat::memzero($nonce);
[256] Fix | Delete
ParagonIE_Sodium_Compat::memzero($ephKeypair);
[257] Fix | Delete
} catch (SodiumException $ex) {
[258] Fix | Delete
/** @psalm-suppress PossiblyUndefinedVariable */
[259] Fix | Delete
unset($ephKeypair);
[260] Fix | Delete
}
[261] Fix | Delete
return $res;
[262] Fix | Delete
}
[263] Fix | Delete
[264] Fix | Delete
/**
[265] Fix | Delete
* Open a sealed file (rather than a string). Uses less memory than
[266] Fix | Delete
* ParagonIE_Sodium_Compat::crypto_box_seal_open(), but produces
[267] Fix | Delete
* the same result.
[268] Fix | Delete
*
[269] Fix | Delete
* Warning: Does not protect against TOCTOU attacks. You should
[270] Fix | Delete
* just load the file into memory and use crypto_box_seal_open() if
[271] Fix | Delete
* you are worried about those.
[272] Fix | Delete
*
[273] Fix | Delete
* @param string $inputFile
[274] Fix | Delete
* @param string $outputFile
[275] Fix | Delete
* @param string $ecdhKeypair
[276] Fix | Delete
* @return bool
[277] Fix | Delete
* @throws SodiumException
[278] Fix | Delete
* @throws TypeError
[279] Fix | Delete
*/
[280] Fix | Delete
public static function box_seal_open(
[281] Fix | Delete
$inputFile,
[282] Fix | Delete
$outputFile,
[283] Fix | Delete
#[\SensitiveParameter]
[284] Fix | Delete
$ecdhKeypair
[285] Fix | Delete
) {
[286] Fix | Delete
/* Type checks: */
[287] Fix | Delete
if (!is_string($inputFile)) {
[288] Fix | Delete
throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
[289] Fix | Delete
}
[290] Fix | Delete
if (!is_string($outputFile)) {
[291] Fix | Delete
throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
[292] Fix | Delete
}
[293] Fix | Delete
if (!is_string($ecdhKeypair)) {
[294] Fix | Delete
throw new TypeError('Argument 3 must be a string, ' . gettype($ecdhKeypair) . ' given.');
[295] Fix | Delete
}
[296] Fix | Delete
[297] Fix | Delete
/* Input validation: */
[298] Fix | Delete
if (self::strlen($ecdhKeypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
[299] Fix | Delete
throw new TypeError('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
$publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair);
[303] Fix | Delete
[304] Fix | Delete
/** @var int $size */
[305] Fix | Delete
$size = filesize($inputFile);
[306] Fix | Delete
if (!is_int($size)) {
[307] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[308] Fix | Delete
}
[309] Fix | Delete
[310] Fix | Delete
/** @var resource $ifp */
[311] Fix | Delete
$ifp = fopen($inputFile, 'rb');
[312] Fix | Delete
if (!is_resource($ifp)) {
[313] Fix | Delete
throw new SodiumException('Could not open input file for reading');
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
/** @var resource $ofp */
[317] Fix | Delete
$ofp = fopen($outputFile, 'wb');
[318] Fix | Delete
if (!is_resource($ofp)) {
[319] Fix | Delete
fclose($ifp);
[320] Fix | Delete
throw new SodiumException('Could not open output file for writing');
[321] Fix | Delete
}
[322] Fix | Delete
[323] Fix | Delete
$ephemeralPK = fread($ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES);
[324] Fix | Delete
if (!is_string($ephemeralPK)) {
[325] Fix | Delete
throw new SodiumException('Could not read input file');
[326] Fix | Delete
}
[327] Fix | Delete
if (self::strlen($ephemeralPK) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
[328] Fix | Delete
fclose($ifp);
[329] Fix | Delete
fclose($ofp);
[330] Fix | Delete
throw new SodiumException('Could not read public key from sealed file');
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
$nonce = ParagonIE_Sodium_Compat::crypto_generichash(
[334] Fix | Delete
$ephemeralPK . $publicKey,
[335] Fix | Delete
'',
[336] Fix | Delete
24
[337] Fix | Delete
);
[338] Fix | Delete
$msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey(
[339] Fix | Delete
ParagonIE_Sodium_Compat::crypto_box_secretkey($ecdhKeypair),
[340] Fix | Delete
$ephemeralPK
[341] Fix | Delete
);
[342] Fix | Delete
[343] Fix | Delete
$res = self::box_decrypt($ifp, $ofp, $size, $nonce, $msgKeypair);
[344] Fix | Delete
fclose($ifp);
[345] Fix | Delete
fclose($ofp);
[346] Fix | Delete
try {
[347] Fix | Delete
ParagonIE_Sodium_Compat::memzero($nonce);
[348] Fix | Delete
ParagonIE_Sodium_Compat::memzero($ephKeypair);
[349] Fix | Delete
} catch (SodiumException $ex) {
[350] Fix | Delete
if (isset($ephKeypair)) {
[351] Fix | Delete
unset($ephKeypair);
[352] Fix | Delete
}
[353] Fix | Delete
}
[354] Fix | Delete
return $res;
[355] Fix | Delete
}
[356] Fix | Delete
[357] Fix | Delete
/**
[358] Fix | Delete
* Calculate the BLAKE2b hash of a file.
[359] Fix | Delete
*
[360] Fix | Delete
* @param string $filePath Absolute path to a file on the filesystem
[361] Fix | Delete
* @param string|null $key BLAKE2b key
[362] Fix | Delete
* @param int $outputLength Length of hash output
[363] Fix | Delete
*
[364] Fix | Delete
* @return string BLAKE2b hash
[365] Fix | Delete
* @throws SodiumException
[366] Fix | Delete
* @throws TypeError
[367] Fix | Delete
* @psalm-suppress FailedTypeResolution
[368] Fix | Delete
*/
[369] Fix | Delete
public static function generichash(
[370] Fix | Delete
$filePath,
[371] Fix | Delete
#[\SensitiveParameter]
[372] Fix | Delete
$key = '',
[373] Fix | Delete
$outputLength = 32
[374] Fix | Delete
) {
[375] Fix | Delete
/* Type checks: */
[376] Fix | Delete
if (!is_string($filePath)) {
[377] Fix | Delete
throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.');
[378] Fix | Delete
}
[379] Fix | Delete
if (!is_string($key)) {
[380] Fix | Delete
if (is_null($key)) {
[381] Fix | Delete
$key = '';
[382] Fix | Delete
} else {
[383] Fix | Delete
throw new TypeError('Argument 2 must be a string, ' . gettype($key) . ' given.');
[384] Fix | Delete
}
[385] Fix | Delete
}
[386] Fix | Delete
if (!is_int($outputLength)) {
[387] Fix | Delete
if (!is_numeric($outputLength)) {
[388] Fix | Delete
throw new TypeError('Argument 3 must be an integer, ' . gettype($outputLength) . ' given.');
[389] Fix | Delete
}
[390] Fix | Delete
$outputLength = (int) $outputLength;
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
/* Input validation: */
[394] Fix | Delete
if (!empty($key)) {
[395] Fix | Delete
if (self::strlen($key) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
[396] Fix | Delete
throw new TypeError('Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes');
[397] Fix | Delete
}
[398] Fix | Delete
if (self::strlen($key) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
[399] Fix | Delete
throw new TypeError('Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes');
[400] Fix | Delete
}
[401] Fix | Delete
}
[402] Fix | Delete
if ($outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN) {
[403] Fix | Delete
throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN');
[404] Fix | Delete
}
[405] Fix | Delete
if ($outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX) {
[406] Fix | Delete
throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX');
[407] Fix | Delete
}
[408] Fix | Delete
[409] Fix | Delete
/** @var int $size */
[410] Fix | Delete
$size = filesize($filePath);
[411] Fix | Delete
if (!is_int($size)) {
[412] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[413] Fix | Delete
}
[414] Fix | Delete
[415] Fix | Delete
/** @var resource $fp */
[416] Fix | Delete
$fp = fopen($filePath, 'rb');
[417] Fix | Delete
if (!is_resource($fp)) {
[418] Fix | Delete
throw new SodiumException('Could not open input file for reading');
[419] Fix | Delete
}
[420] Fix | Delete
$ctx = ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outputLength);
[421] Fix | Delete
while ($size > 0) {
[422] Fix | Delete
$blockSize = $size > 64
[423] Fix | Delete
? 64
[424] Fix | Delete
: $size;
[425] Fix | Delete
$read = fread($fp, $blockSize);
[426] Fix | Delete
if (!is_string($read)) {
[427] Fix | Delete
throw new SodiumException('Could not read input file');
[428] Fix | Delete
}
[429] Fix | Delete
ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $read);
[430] Fix | Delete
$size -= $blockSize;
[431] Fix | Delete
}
[432] Fix | Delete
[433] Fix | Delete
fclose($fp);
[434] Fix | Delete
return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength);
[435] Fix | Delete
}
[436] Fix | Delete
[437] Fix | Delete
/**
[438] Fix | Delete
* Encrypt a file (rather than a string). Uses less memory than
[439] Fix | Delete
* ParagonIE_Sodium_Compat::crypto_secretbox(), but produces
[440] Fix | Delete
* the same result.
[441] Fix | Delete
*
[442] Fix | Delete
* @param string $inputFile Absolute path to a file on the filesystem
[443] Fix | Delete
* @param string $outputFile Absolute path to a file on the filesystem
[444] Fix | Delete
* @param string $nonce Number to be used only once
[445] Fix | Delete
* @param string $key Encryption key
[446] Fix | Delete
*
[447] Fix | Delete
* @return bool
[448] Fix | Delete
* @throws SodiumException
[449] Fix | Delete
* @throws TypeError
[450] Fix | Delete
*/
[451] Fix | Delete
public static function secretbox(
[452] Fix | Delete
$inputFile,
[453] Fix | Delete
$outputFile,
[454] Fix | Delete
$nonce,
[455] Fix | Delete
#[\SensitiveParameter]
[456] Fix | Delete
$key
[457] Fix | Delete
) {
[458] Fix | Delete
/* Type checks: */
[459] Fix | Delete
if (!is_string($inputFile)) {
[460] Fix | Delete
throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..');
[461] Fix | Delete
}
[462] Fix | Delete
if (!is_string($outputFile)) {
[463] Fix | Delete
throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
[464] Fix | Delete
}
[465] Fix | Delete
if (!is_string($nonce)) {
[466] Fix | Delete
throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
[467] Fix | Delete
}
[468] Fix | Delete
[469] Fix | Delete
/* Input validation: */
[470] Fix | Delete
if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) {
[471] Fix | Delete
throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes');
[472] Fix | Delete
}
[473] Fix | Delete
if (!is_string($key)) {
[474] Fix | Delete
throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.');
[475] Fix | Delete
}
[476] Fix | Delete
if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) {
[477] Fix | Delete
throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes');
[478] Fix | Delete
}
[479] Fix | Delete
[480] Fix | Delete
/** @var int $size */
[481] Fix | Delete
$size = filesize($inputFile);
[482] Fix | Delete
if (!is_int($size)) {
[483] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[484] Fix | Delete
}
[485] Fix | Delete
[486] Fix | Delete
/** @var resource $ifp */
[487] Fix | Delete
$ifp = fopen($inputFile, 'rb');
[488] Fix | Delete
if (!is_resource($ifp)) {
[489] Fix | Delete
throw new SodiumException('Could not open input file for reading');
[490] Fix | Delete
}
[491] Fix | Delete
[492] Fix | Delete
/** @var resource $ofp */
[493] Fix | Delete
$ofp = fopen($outputFile, 'wb');
[494] Fix | Delete
if (!is_resource($ofp)) {
[495] Fix | Delete
fclose($ifp);
[496] Fix | Delete
throw new SodiumException('Could not open output file for writing');
[497] Fix | Delete
}
[498] Fix | Delete
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function