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
if (!file_exists($inputFile)) {
[127] Fix | Delete
throw new SodiumException('Input file does not exist');
[128] Fix | Delete
}
[129] Fix | Delete
/** @var int $size */
[130] Fix | Delete
$size = filesize($inputFile);
[131] Fix | Delete
if (!is_int($size)) {
[132] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
/** @var resource $ifp */
[136] Fix | Delete
$ifp = fopen($inputFile, 'rb');
[137] Fix | Delete
if (!is_resource($ifp)) {
[138] Fix | Delete
throw new SodiumException('Could not open input file for reading');
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
/** @var resource $ofp */
[142] Fix | Delete
$ofp = @fopen($outputFile, 'wb');
[143] Fix | Delete
if (!is_resource($ofp)) {
[144] Fix | Delete
fclose($ifp);
[145] Fix | Delete
throw new SodiumException('Could not open output file for writing');
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
$res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair);
[149] Fix | Delete
fclose($ifp);
[150] Fix | Delete
fclose($ofp);
[151] Fix | Delete
try {
[152] Fix | Delete
ParagonIE_Sodium_Compat::memzero($nonce);
[153] Fix | Delete
ParagonIE_Sodium_Compat::memzero($ephKeypair);
[154] Fix | Delete
} catch (SodiumException $ex) {
[155] Fix | Delete
if (isset($ephKeypair)) {
[156] Fix | Delete
unset($ephKeypair);
[157] Fix | Delete
}
[158] Fix | Delete
}
[159] Fix | Delete
return $res;
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
/**
[163] Fix | Delete
* Seal a file (rather than a string). Uses less memory than
[164] Fix | Delete
* ParagonIE_Sodium_Compat::crypto_box_seal(), but produces
[165] Fix | Delete
* the same result.
[166] Fix | Delete
*
[167] Fix | Delete
* @param string $inputFile Absolute path to a file on the filesystem
[168] Fix | Delete
* @param string $outputFile Absolute path to a file on the filesystem
[169] Fix | Delete
* @param string $publicKey ECDH public key
[170] Fix | Delete
*
[171] Fix | Delete
* @return bool
[172] Fix | Delete
* @throws SodiumException
[173] Fix | Delete
* @throws TypeError
[174] Fix | Delete
*/
[175] Fix | Delete
public static function box_seal(
[176] Fix | Delete
$inputFile,
[177] Fix | Delete
$outputFile,
[178] Fix | Delete
#[\SensitiveParameter]
[179] Fix | Delete
$publicKey
[180] Fix | Delete
) {
[181] Fix | Delete
/* Type checks: */
[182] Fix | Delete
if (!is_string($inputFile)) {
[183] Fix | Delete
throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
[184] Fix | Delete
}
[185] Fix | Delete
if (!is_string($outputFile)) {
[186] Fix | Delete
throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
[187] Fix | Delete
}
[188] Fix | Delete
if (!is_string($publicKey)) {
[189] Fix | Delete
throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.');
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
/* Input validation: */
[193] Fix | Delete
if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
[194] Fix | Delete
throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes');
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
if (!file_exists($inputFile)) {
[198] Fix | Delete
throw new SodiumException('Input file does not exist');
[199] Fix | Delete
}
[200] Fix | Delete
/** @var int $size */
[201] Fix | Delete
$size = filesize($inputFile);
[202] Fix | Delete
if (!is_int($size)) {
[203] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
/** @var resource $ifp */
[207] Fix | Delete
$ifp = fopen($inputFile, 'rb');
[208] Fix | Delete
if (!is_resource($ifp)) {
[209] Fix | Delete
throw new SodiumException('Could not open input file for reading');
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
/** @var resource $ofp */
[213] Fix | Delete
$ofp = @fopen($outputFile, 'wb');
[214] Fix | Delete
if (!is_resource($ofp)) {
[215] Fix | Delete
fclose($ifp);
[216] Fix | Delete
throw new SodiumException('Could not open output file for writing');
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
/** @var string $ephKeypair */
[220] Fix | Delete
$ephKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair();
[221] Fix | Delete
[222] Fix | Delete
/** @var string $msgKeypair */
[223] Fix | Delete
$msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey(
[224] Fix | Delete
ParagonIE_Sodium_Compat::crypto_box_secretkey($ephKeypair),
[225] Fix | Delete
$publicKey
[226] Fix | Delete
);
[227] Fix | Delete
[228] Fix | Delete
/** @var string $ephemeralPK */
[229] Fix | Delete
$ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey($ephKeypair);
[230] Fix | Delete
[231] Fix | Delete
/** @var string $nonce */
[232] Fix | Delete
$nonce = ParagonIE_Sodium_Compat::crypto_generichash(
[233] Fix | Delete
$ephemeralPK . $publicKey,
[234] Fix | Delete
'',
[235] Fix | Delete
24
[236] Fix | Delete
);
[237] Fix | Delete
[238] Fix | Delete
/** @var int $firstWrite */
[239] Fix | Delete
$firstWrite = fwrite(
[240] Fix | Delete
$ofp,
[241] Fix | Delete
$ephemeralPK,
[242] Fix | Delete
ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES
[243] Fix | Delete
);
[244] Fix | Delete
if (!is_int($firstWrite)) {
[245] Fix | Delete
fclose($ifp);
[246] Fix | Delete
fclose($ofp);
[247] Fix | Delete
ParagonIE_Sodium_Compat::memzero($ephKeypair);
[248] Fix | Delete
throw new SodiumException('Could not write to output file');
[249] Fix | Delete
}
[250] Fix | Delete
if ($firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
[251] Fix | Delete
ParagonIE_Sodium_Compat::memzero($ephKeypair);
[252] Fix | Delete
fclose($ifp);
[253] Fix | Delete
fclose($ofp);
[254] Fix | Delete
throw new SodiumException('Error writing public key to output file');
[255] Fix | Delete
}
[256] Fix | Delete
[257] Fix | Delete
$res = self::box_encrypt($ifp, $ofp, $size, $nonce, $msgKeypair);
[258] Fix | Delete
fclose($ifp);
[259] Fix | Delete
fclose($ofp);
[260] Fix | Delete
try {
[261] Fix | Delete
ParagonIE_Sodium_Compat::memzero($nonce);
[262] Fix | Delete
ParagonIE_Sodium_Compat::memzero($ephKeypair);
[263] Fix | Delete
} catch (SodiumException $ex) {
[264] Fix | Delete
/** @psalm-suppress PossiblyUndefinedVariable */
[265] Fix | Delete
unset($ephKeypair);
[266] Fix | Delete
}
[267] Fix | Delete
return $res;
[268] Fix | Delete
}
[269] Fix | Delete
[270] Fix | Delete
/**
[271] Fix | Delete
* Open a sealed file (rather than a string). Uses less memory than
[272] Fix | Delete
* ParagonIE_Sodium_Compat::crypto_box_seal_open(), but produces
[273] Fix | Delete
* the same result.
[274] Fix | Delete
*
[275] Fix | Delete
* Warning: Does not protect against TOCTOU attacks. You should
[276] Fix | Delete
* just load the file into memory and use crypto_box_seal_open() if
[277] Fix | Delete
* you are worried about those.
[278] Fix | Delete
*
[279] Fix | Delete
* @param string $inputFile
[280] Fix | Delete
* @param string $outputFile
[281] Fix | Delete
* @param string $ecdhKeypair
[282] Fix | Delete
* @return bool
[283] Fix | Delete
* @throws SodiumException
[284] Fix | Delete
* @throws TypeError
[285] Fix | Delete
*/
[286] Fix | Delete
public static function box_seal_open(
[287] Fix | Delete
$inputFile,
[288] Fix | Delete
$outputFile,
[289] Fix | Delete
#[\SensitiveParameter]
[290] Fix | Delete
$ecdhKeypair
[291] Fix | Delete
) {
[292] Fix | Delete
/* Type checks: */
[293] Fix | Delete
if (!is_string($inputFile)) {
[294] Fix | Delete
throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.');
[295] Fix | Delete
}
[296] Fix | Delete
if (!is_string($outputFile)) {
[297] Fix | Delete
throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
[298] Fix | Delete
}
[299] Fix | Delete
if (!is_string($ecdhKeypair)) {
[300] Fix | Delete
throw new TypeError('Argument 3 must be a string, ' . gettype($ecdhKeypair) . ' given.');
[301] Fix | Delete
}
[302] Fix | Delete
[303] Fix | Delete
/* Input validation: */
[304] Fix | Delete
if (self::strlen($ecdhKeypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) {
[305] Fix | Delete
throw new TypeError('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes');
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
$publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair);
[309] Fix | Delete
[310] Fix | Delete
if (!file_exists($inputFile)) {
[311] Fix | Delete
throw new SodiumException('Input file does not exist');
[312] Fix | Delete
}
[313] Fix | Delete
/** @var int $size */
[314] Fix | Delete
$size = filesize($inputFile);
[315] Fix | Delete
if (!is_int($size)) {
[316] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[317] Fix | Delete
}
[318] Fix | Delete
[319] Fix | Delete
/** @var resource $ifp */
[320] Fix | Delete
$ifp = fopen($inputFile, 'rb');
[321] Fix | Delete
if (!is_resource($ifp)) {
[322] Fix | Delete
throw new SodiumException('Could not open input file for reading');
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
/** @var resource $ofp */
[326] Fix | Delete
$ofp = @fopen($outputFile, 'wb');
[327] Fix | Delete
if (!is_resource($ofp)) {
[328] Fix | Delete
fclose($ifp);
[329] Fix | Delete
throw new SodiumException('Could not open output file for writing');
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
$ephemeralPK = fread($ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES);
[333] Fix | Delete
if (!is_string($ephemeralPK)) {
[334] Fix | Delete
throw new SodiumException('Could not read input file');
[335] Fix | Delete
}
[336] Fix | Delete
if (self::strlen($ephemeralPK) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) {
[337] Fix | Delete
fclose($ifp);
[338] Fix | Delete
fclose($ofp);
[339] Fix | Delete
throw new SodiumException('Could not read public key from sealed file');
[340] Fix | Delete
}
[341] Fix | Delete
[342] Fix | Delete
$nonce = ParagonIE_Sodium_Compat::crypto_generichash(
[343] Fix | Delete
$ephemeralPK . $publicKey,
[344] Fix | Delete
'',
[345] Fix | Delete
24
[346] Fix | Delete
);
[347] Fix | Delete
$msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey(
[348] Fix | Delete
ParagonIE_Sodium_Compat::crypto_box_secretkey($ecdhKeypair),
[349] Fix | Delete
$ephemeralPK
[350] Fix | Delete
);
[351] Fix | Delete
[352] Fix | Delete
$res = self::box_decrypt($ifp, $ofp, $size, $nonce, $msgKeypair);
[353] Fix | Delete
fclose($ifp);
[354] Fix | Delete
fclose($ofp);
[355] Fix | Delete
try {
[356] Fix | Delete
ParagonIE_Sodium_Compat::memzero($nonce);
[357] Fix | Delete
ParagonIE_Sodium_Compat::memzero($ephKeypair);
[358] Fix | Delete
} catch (SodiumException $ex) {
[359] Fix | Delete
if (isset($ephKeypair)) {
[360] Fix | Delete
unset($ephKeypair);
[361] Fix | Delete
}
[362] Fix | Delete
}
[363] Fix | Delete
return $res;
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
/**
[367] Fix | Delete
* Calculate the BLAKE2b hash of a file.
[368] Fix | Delete
*
[369] Fix | Delete
* @param string $filePath Absolute path to a file on the filesystem
[370] Fix | Delete
* @param string|null $key BLAKE2b key
[371] Fix | Delete
* @param int $outputLength Length of hash output
[372] Fix | Delete
*
[373] Fix | Delete
* @return string BLAKE2b hash
[374] Fix | Delete
* @throws SodiumException
[375] Fix | Delete
* @throws TypeError
[376] Fix | Delete
* @psalm-suppress FailedTypeResolution
[377] Fix | Delete
*/
[378] Fix | Delete
public static function generichash(
[379] Fix | Delete
$filePath,
[380] Fix | Delete
#[\SensitiveParameter]
[381] Fix | Delete
$key = '',
[382] Fix | Delete
$outputLength = 32
[383] Fix | Delete
) {
[384] Fix | Delete
/* Type checks: */
[385] Fix | Delete
if (!is_string($filePath)) {
[386] Fix | Delete
throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.');
[387] Fix | Delete
}
[388] Fix | Delete
if (!is_string($key)) {
[389] Fix | Delete
if (is_null($key)) {
[390] Fix | Delete
$key = '';
[391] Fix | Delete
} else {
[392] Fix | Delete
throw new TypeError('Argument 2 must be a string, ' . gettype($key) . ' given.');
[393] Fix | Delete
}
[394] Fix | Delete
}
[395] Fix | Delete
if (!is_int($outputLength)) {
[396] Fix | Delete
if (!is_numeric($outputLength)) {
[397] Fix | Delete
throw new TypeError('Argument 3 must be an integer, ' . gettype($outputLength) . ' given.');
[398] Fix | Delete
}
[399] Fix | Delete
$outputLength = (int) $outputLength;
[400] Fix | Delete
}
[401] Fix | Delete
[402] Fix | Delete
/* Input validation: */
[403] Fix | Delete
if (!empty($key)) {
[404] Fix | Delete
if (self::strlen($key) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN) {
[405] Fix | Delete
throw new TypeError('Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes');
[406] Fix | Delete
}
[407] Fix | Delete
if (self::strlen($key) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX) {
[408] Fix | Delete
throw new TypeError('Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes');
[409] Fix | Delete
}
[410] Fix | Delete
}
[411] Fix | Delete
if ($outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN) {
[412] Fix | Delete
throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN');
[413] Fix | Delete
}
[414] Fix | Delete
if ($outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX) {
[415] Fix | Delete
throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX');
[416] Fix | Delete
}
[417] Fix | Delete
[418] Fix | Delete
if (!file_exists($filePath)) {
[419] Fix | Delete
throw new SodiumException('File does not exist');
[420] Fix | Delete
}
[421] Fix | Delete
/** @var int $size */
[422] Fix | Delete
$size = filesize($filePath);
[423] Fix | Delete
if (!is_int($size)) {
[424] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[425] Fix | Delete
}
[426] Fix | Delete
[427] Fix | Delete
/** @var resource $fp */
[428] Fix | Delete
$fp = fopen($filePath, 'rb');
[429] Fix | Delete
if (!is_resource($fp)) {
[430] Fix | Delete
throw new SodiumException('Could not open input file for reading');
[431] Fix | Delete
}
[432] Fix | Delete
$ctx = ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outputLength);
[433] Fix | Delete
while ($size > 0) {
[434] Fix | Delete
$blockSize = $size > 64
[435] Fix | Delete
? 64
[436] Fix | Delete
: $size;
[437] Fix | Delete
$read = fread($fp, $blockSize);
[438] Fix | Delete
if (!is_string($read)) {
[439] Fix | Delete
throw new SodiumException('Could not read input file');
[440] Fix | Delete
}
[441] Fix | Delete
ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $read);
[442] Fix | Delete
$size -= $blockSize;
[443] Fix | Delete
}
[444] Fix | Delete
[445] Fix | Delete
fclose($fp);
[446] Fix | Delete
return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength);
[447] Fix | Delete
}
[448] Fix | Delete
[449] Fix | Delete
/**
[450] Fix | Delete
* Encrypt a file (rather than a string). Uses less memory than
[451] Fix | Delete
* ParagonIE_Sodium_Compat::crypto_secretbox(), but produces
[452] Fix | Delete
* the same result.
[453] Fix | Delete
*
[454] Fix | Delete
* @param string $inputFile Absolute path to a file on the filesystem
[455] Fix | Delete
* @param string $outputFile Absolute path to a file on the filesystem
[456] Fix | Delete
* @param string $nonce Number to be used only once
[457] Fix | Delete
* @param string $key Encryption key
[458] Fix | Delete
*
[459] Fix | Delete
* @return bool
[460] Fix | Delete
* @throws SodiumException
[461] Fix | Delete
* @throws TypeError
[462] Fix | Delete
*/
[463] Fix | Delete
public static function secretbox(
[464] Fix | Delete
$inputFile,
[465] Fix | Delete
$outputFile,
[466] Fix | Delete
$nonce,
[467] Fix | Delete
#[\SensitiveParameter]
[468] Fix | Delete
$key
[469] Fix | Delete
) {
[470] Fix | Delete
/* Type checks: */
[471] Fix | Delete
if (!is_string($inputFile)) {
[472] Fix | Delete
throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..');
[473] Fix | Delete
}
[474] Fix | Delete
if (!is_string($outputFile)) {
[475] Fix | Delete
throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');
[476] Fix | Delete
}
[477] Fix | Delete
if (!is_string($nonce)) {
[478] Fix | Delete
throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');
[479] Fix | Delete
}
[480] Fix | Delete
[481] Fix | Delete
/* Input validation: */
[482] Fix | Delete
if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) {
[483] Fix | Delete
throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes');
[484] Fix | Delete
}
[485] Fix | Delete
if (!is_string($key)) {
[486] Fix | Delete
throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.');
[487] Fix | Delete
}
[488] Fix | Delete
if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) {
[489] Fix | Delete
throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes');
[490] Fix | Delete
}
[491] Fix | Delete
[492] Fix | Delete
if (!file_exists($inputFile)) {
[493] Fix | Delete
throw new SodiumException('Input file does not exist');
[494] Fix | Delete
}
[495] Fix | Delete
/** @var int $size */
[496] Fix | Delete
$size = filesize($inputFile);
[497] Fix | Delete
if (!is_int($size)) {
[498] Fix | Delete
throw new SodiumException('Could not obtain the file size');
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function