PHP hash_init: Initialize Incremental Hashing Context

The article explains PHP's hash_init function, detailing its purpose to initialize an incremental hashing context, describing its parameters (algorithm name, options, key), return value, and provides a complete example demonstrating how to compute an MD5 hash using hash_update and hash_final.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP hash_init: Initialize Incremental Hashing Context

The hash_init function in PHP creates an incremental hashing context, allowing you to feed data in chunks and compute a final hash value.

Syntax:

resource hash_init(string $algo [, int $options = 0 [, string $key = NULL ]])

Parameters

algo : The name of the hash algorithm to use, e.g., "md5", "sha256", "haval160,4". Use hash_algos() to retrieve the list of supported algorithms.

options : Optional settings for the hash operation; currently only HASH_HMAC is supported. When this option is set, the key parameter must also be provided.

key : The shared secret key used when options is HASH_HMAC, enabling HMAC hashing.

Return value

Returns a hash context resource that can be used with hash_update() and hash_final().

Example

The following PHP code demonstrates initializing an MD5 hash context, updating it with two strings, and obtaining the final hash:

<?php
$ctx = hash_init('md5');
hash_update($ctx, 'The quick brown fox ');
hash_update($ctx, 'jumped over the lazy dog.');
echo hash_final($ctx);
?>

Output

5c6ffbdd40d9556b73a21e63c3e0e904
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPHashincremental hashing
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.