Example 1 — Hash in Two Chunks
Add data in pieces and compare with one-shot hash().
<?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
- Start the context. PHP 8 returns a
HashContext. - Add two chunks. The order matters.
- Finish with `hash_final()`. It returns the hash.
- Compare with `hash()`. The results are equal.

