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