PHP hash_init() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

Definition

In PHP, hash_init() starts an incremental hash and returns a context object.

Add data with hash_update() as many times as you like, then finish with hash_final(). It suits large or streamed data.

Syntax

Call hash_init() with the values you need. Start with this line.

PHP
$ctx = hash_init('sha256');
CallWhat it does
hash_init('sha256')Starts a plain hash.
hash_init('sha256', HASH_HMAC, 'key')Starts a keyed (HMAC) hash.

In PHP 8 the context is a HashContext object. In PHP 7 it was a resource.

Library declarations
PHP
hash_init(string $algo, int $flags = 0, string $key = "", array $options = []): HashContext

Parameters

ParameterDescription
algoRequired. The algorithm name, such as "sha256".
flagsOptional. 0 (default) or HASH_HMAC for a keyed hash.
keyOptional. The secret key. Required when HASH_HMAC is set.
optionsOptional. PHP 8.1+. Extra settings for some algorithms.

Return Value

Return ValueDescription
HashContextA context to pass to hash_update() and hash_final(). An unknown algorithm or a missing HMAC key throws ValueError.

PHP Examples

Example 1 — Hash in Two Chunks

Add data in pieces and compare with one-shot hash().

PHP
<?php
$ctx = hash_init('sha256');

echo get_class($ctx), PHP_EOL;

hash_update($ctx, 'Hello, ');
hash_update($ctx, 'World');

echo "Matches hash(): ", hash_final($ctx) === hash('sha256', 'Hello, World') ? "yes" : "no", PHP_EOL;

How It Works

  1. Start the context. PHP 8 returns a HashContext.
  2. Add two chunks. The order matters.
  3. Finish with `hash_final()`. It returns the hash.
  4. Compare with `hash()`. The results are equal.

Example 2 — HMAC Mode

Pass HASH_HMAC and a key.

PHP
<?php
$ctx = hash_init('sha256', HASH_HMAC, 'secret-key');
hash_update($ctx, 'Hello, World');

$mac = hash_final($ctx);

echo "Matches hash_hmac(): ", $mac === hash_hmac('sha256', 'Hello, World', 'secret-key') ? "yes" : "no", PHP_EOL;

How It Works

  1. Set the HMAC flag. Add the secret key.
  2. Add data as usual. hash_update() works the same.
  3. Finish. The result is an HMAC.
  4. Compare. It equals the one-shot hash_hmac().

Example 3 — Hash Lines One by One

Feed a list of lines without joining them first.

PHP
<?php
$lines = ["one\n", "two\n", "three\n"];

$ctx = hash_init('md5');
foreach ($lines as $line) {
    hash_update($ctx, $line);
}

echo hash_final($ctx), PHP_EOL;

How It Works

  1. Prepare the lines. They could come from a file.
  2. Start an md5 context. md5 is fine for a checksum.
  3. Update in a loop. Each line is added.
  4. Finish. The result covers all lines in order.

Frequently Asked Questions

It starts an incremental hashing context. You then add data in parts with hash_update() and finish with hash_final().
hash_init(string $algo, int $flags = 0, string $key = "", array $options = []): HashContext.
When the data is large, arrives in chunks, or comes from several sources. hash() needs the whole string at once.
Pass HASH_HMAC as the flags and the secret as the key. The final result equals hash_hmac().
A HashContext object in PHP 8. In PHP 7 it returned a resource.

Next: hash_pbkdf2()

hash_pbkdf2() derives a key from a password with many rounds.

hash_pbkdf2() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful