2021-02-26 16:16:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Safe;
|
|
|
|
|
|
|
|
use Safe\Exceptions\HashException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param string $algo Name of selected hashing algorithm (i.e. "sha256", "sha512", "haval160,4", etc..)
|
|
|
|
* See hash_algos for a list of supported algorithms.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Non-cryptographic hash functions are not allowed.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Non-cryptographic hash functions are not allowed.
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param string $key Input keying material (raw binary). Cannot be empty.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @param int $length Desired output length in bytes.
|
|
|
|
* Cannot be greater than 255 times the chosen hash function size.
|
|
|
|
*
|
|
|
|
* If length is 0, the output length
|
|
|
|
* will default to the chosen hash function size.
|
|
|
|
* @param string $info Application/context-specific info string.
|
|
|
|
* @param string $salt Salt to use during derivation.
|
|
|
|
*
|
|
|
|
* While optional, adding random salt significantly improves the strength of HKDF.
|
|
|
|
* @return string Returns a string containing a raw binary representation of the derived key
|
|
|
|
* (also known as output keying material - OKM);.
|
|
|
|
* @throws HashException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function hash_hkdf(string $algo, string $key, int $length = 0, string $info = "", string $salt = ""): string
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
2022-07-12 19:26:21 +00:00
|
|
|
$result = \hash_hkdf($algo, $key, $length, $info, $salt);
|
2021-02-26 16:16:17 +00:00
|
|
|
if ($result === false) {
|
|
|
|
throw HashException::createFromPhpError();
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param \HashContext $context Hashing context returned by hash_init.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @param string $filename URL describing location of file to be hashed; Supports fopen wrappers.
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param \HashContext|null $stream_context Stream context as returned by stream_context_create.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @throws HashException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function hash_update_file(\HashContext $context, string $filename, ?\HashContext $stream_context = null): void
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
2022-07-12 19:26:21 +00:00
|
|
|
if ($stream_context !== null) {
|
|
|
|
$result = \hash_update_file($context, $filename, $stream_context);
|
|
|
|
} else {
|
|
|
|
$result = \hash_update_file($context, $filename);
|
|
|
|
}
|
2021-02-26 16:16:17 +00:00
|
|
|
if ($result === false) {
|
|
|
|
throw HashException::createFromPhpError();
|
|
|
|
}
|
|
|
|
}
|