PHP Function Reference

PHP hash_init() Function



The PHP hash_init() function initializes an incremental hashing context which can be used with other hash functions like hash_update(), hash_final() etc. It takes input as a hash algorithm and the output as a Hashing Context.

Syntax

hash_init(algo, flags, key)

Parameters

algo Required. Specify the name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..). A list of supported algorithms can be found using hash_algos() function.
flags Optional. Specify settings for hash generation, currently supports only one option: HASH_HMAC. When specified, the key must be specified.
key Optional. When HASH_HMAC is specified for flags, a shared secret key to be used with the HMAC hashing method must be supplied in this parameter.

Return Value

Returns a Hashing Context for use with hash_update(), hash_update_stream(), hash_update_file(), and hash_final().

Example: hash_init() example

The example below shows the usage of hash_init() function.

<?php
//initializing an incremental hashing context
$ctx = hash_init('md5');

//updating context
hash_update($ctx, 'Hello World!');

//finalizing an incremental hash
//and returning resulting digest
echo hash_final($ctx);
?>

The output of the above code will be:

ed076287532e86365e841e92bfc50d8c

❮ PHP Hash Reference