hash_file

Generate a hash value using the contents of a given file

Syntax

hash_file(
    string $algo,
    string $filename,
    bool $binary = false,
    array $options = []
): string|false

Parameters

algo

Name of selected hashing algorithm.

filename

The filename of the file to hash.

binary

When set to true, outputs raw binary data. false outputs lowercase hexits.

options

An array of options for the various hashing algorithms. Currently, only the "seed" parameter is supported by the MurmurHash variants.

Return

Returns a string containing the calculated message digest as lowercase hexits unless binary is set to true in which case the raw binary representation of the message digest is returned.

Examples

1 · algo filename

<?

$algo = 'sha384';
$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/file.txt';

$return = hash_file($algo, $filename);

echo $return;

?>
3519fe5ad2c596efe3e276a6f351b8fc0b03db861782490d45f7598ebd0ab5fd5520ed102f38c4a5ec834e98668035fc

2 · binary

<?

$algo = 'sha384';
$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/file.txt';
$binary = true;

$return = hash_file($algo, $filename, $binary);

echo $return . PHP_EOL . bin2hex($return);

?>
5�Z�Ŗ���v��Q��ۆ�I
E�Y��
��U �/8ĥ�N�f�5�
3519fe5ad2c596efe3e276a6f351b8fc0b03db861782490d45f7598ebd0ab5fd5520ed102f38c4a5ec834e98668035fc

3 · options · empty

<?

$algo = 'sha384';
$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/file.txt';
$binary = false;
$options = [];

$return = hash_file($algo, $filename, $binary, $options);

echo $return;

?>
3519fe5ad2c596efe3e276a6f351b8fc0b03db861782490d45f7598ebd0ab5fd5520ed102f38c4a5ec834e98668035fc

4 · options · seed

<?

$algo = 'murmur3f';
$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/file.txt';
$binary = false;
$options = ['seed' => 42];

$return = hash_file($algo, $filename, $binary, $options);

echo $return;

?>
2365fb86a62c2c3ba0c2a44a316b36e2
HomeMenu