Skip to content

Commit db00812

Browse files
authored
Add missing hash_file() and hash_hmac_file() functions (#694)
1 parent c40d0f2 commit db00812

12 files changed

+80
-28
lines changed

generated/8.1/functionsList.php

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.1/rector-migrate.php

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.2/functionsList.php

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.2/rector-migrate.php

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.3/functionsList.php

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.3/rector-migrate.php

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.4/functionsList.php

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.4/rector-migrate.php

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.5/functionsList.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.5/hash.php

Lines changed: 0 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generated/8.5/rector-migrate.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/special_cases.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Safe\Exceptions\PcreException;
1919
use Safe\Exceptions\SimplexmlException;
2020
use Safe\Exceptions\FilesystemException;
21+
use Safe\Exceptions\HashException;
2122

2223
use const PREG_NO_ERROR;
2324

@@ -429,3 +430,64 @@ function passthru(string $command, ?int &$result_code = null): void
429430
throw ExecException::createFromPhpError();
430431
}
431432
}
433+
434+
/**
435+
*
436+
*
437+
* @param string $algo Name of selected hashing algorithm (e.g. "sha256").
438+
* For a list of supported algorithms see hash_algos.
439+
* @param string $filename URL describing location of file to be hashed; Supports fopen wrappers.
440+
* @param bool $binary When set to TRUE, outputs raw binary data.
441+
* FALSE outputs lowercase hexits.
442+
* @phpstan-param array<string, mixed> $options
443+
* @param array $options An array of options for the various hashing algorithms.
444+
* Currently, only the "seed" parameter is
445+
* supported by the MurmurHash variants.
446+
* @return non-falsy-string Returns a string containing the calculated message digest as lowercase hexits
447+
* unless binary is set to true in which case the raw
448+
* binary representation of the message digest is returned.
449+
* @throws HashException
450+
*
451+
*/
452+
function hash_file(string $algo, string $filename, bool $binary = false, array $options = []): string
453+
{
454+
error_clear_last();
455+
$safeResult = \hash_file($algo, $filename, $binary, $options);
456+
if ($safeResult === false) {
457+
throw HashException::createFromPhpError();
458+
}
459+
return $safeResult;
460+
}
461+
462+
/**
463+
*
464+
*
465+
* @param string $algo Name of selected hashing algorithm (e.g. "sha256").
466+
* For a list of supported algorithms see hash_hmac_algos.
467+
*
468+
*
469+
* Non-cryptographic hash functions are not allowed.
470+
*
471+
*
472+
*
473+
* Non-cryptographic hash functions are not allowed.
474+
* @param string $filename URL describing location of file to be hashed; Supports fopen wrappers.
475+
* @param string $key Shared secret key used for generating the HMAC variant of the message digest.
476+
* @param bool $binary When set to TRUE, outputs raw binary data.
477+
* FALSE outputs lowercase hexits.
478+
* @return non-falsy-string Returns a string containing the calculated message digest as lowercase hexits
479+
* unless binary is set to true in which case the raw
480+
* binary representation of the message digest is returned.
481+
* Returns FALSE if the file filename cannot be read.
482+
* @throws HashException
483+
*
484+
*/
485+
function hash_hmac_file(string $algo, string $filename, string $key, bool $binary = false): string
486+
{
487+
error_clear_last();
488+
$safeResult = \hash_hmac_file($algo, $filename, $key, $binary);
489+
if ($safeResult === false) {
490+
throw HashException::createFromPhpError();
491+
}
492+
return $safeResult;
493+
}

0 commit comments

Comments
 (0)