PHP hash_copy() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

Definition

In PHP, hash_copy() makes a copy of a hashing context that was made with hash_init().

Use it when many hashes share the same start and you do not want to hash that start again.

Syntax

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

PHP
$copy = hash_copy($context);
CallWhat it does
hash_copy($ctx)Returns a new HashContext in the same state.

The copy and the original are independent. Updating one does not change the other.

Library declarations
PHP
hash_copy(HashContext $context): HashContext

Parameters

ParameterDescription
contextRequired. A HashContext from hash_init() that has not been finalized.

Return Value

Return ValueDescription
HashContextA new context with the same data and settings as the original.

PHP Examples

Example 1 — Branch One Context Two Ways

Share the start, then add different endings.

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

$copy = hash_copy($ctx);

hash_update($ctx, 'World');
hash_update($copy, 'PHP');

echo hash_final($ctx), PHP_EOL;
echo hash_final($copy), PHP_EOL;

How It Works

  1. Start a context. Add the shared text Hello,.
  2. Copy it. Both now hold the same state.
  3. Add different endings. One gets World, the other PHP.
  4. Finish both. Each result is the hash of its full text.

Example 2 — The Copy Matches the Original

A copy taken now equals a one-shot hash of the same text.

PHP
<?php
$ctx = hash_init('sha256');
hash_update($ctx, 'Hello, ');
$copy = hash_copy($ctx);

echo "Copy matches hash(): ", hash_final($copy) === hash('sha256', 'Hello, ') ? "yes" : "no", PHP_EOL;

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

How It Works

  1. Copy after one update. The state holds Hello,.
  2. Finish the copy. It equals hash() of that text.
  3. Keep using the original. Add World.
  4. Finish it. The copy did not disturb the original.

Example 3 — Reuse a Shared Prefix

Hash a common start once, then finish it per user. Only the first 16 characters are printed.

PHP
<?php
$base = hash_init('sha256');
hash_update($base, 'user:');

foreach ([101, 102] as $id) {
    $ctx = hash_copy($base);
    hash_update($ctx, (string) $id);
    echo $id, ": ", substr(hash_final($ctx), 0, 16), PHP_EOL;
}

How It Works

  1. Hash the prefix once. user: goes into the base context.
  2. Copy for each id. The base stays untouched.
  3. Add the id. Each copy now holds a full string.
  4. Finish and print. Each id gets its own hash.

Frequently Asked Questions

It returns a new hashing context in exactly the same state as the one you pass in, so both can continue separately.
hash_copy(HashContext $context): HashContext. Pass a context from hash_init().
When many hashes share a long common prefix, such as a header. Hash the prefix once, then copy the context for each ending.
No. After hash_final() the context is closed and copying it throws an error. Copy before you finish.
No. They are separate objects. Adding data to one has no effect on the other.

Next: hash_equals()

hash_equals() compares two strings in a timing-safe way.

hash_equals() 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