HashContext as Object View as Markdown

Implemented (PHP 7.2)
Authors
Sara Golemon pollita@php.net, with initial implementation from Rouven Weßling me@rouvenwessling.de
Date
2016-12-26
Version
1.0
Source
wiki.php.net/rfc/hash-context.as-resource

Votes

Change HashContext to an object closed
Yes
15
No
0
15 voters
ashnazg Yes 2017-01-18 18:47
auroraeosrose Yes 2017-01-18 14:47
bishop Yes 2017-01-18 13:40
bwoebi Yes 2017-01-17 23:08
guilhermeblanco Yes 2017-01-18 00:54
leigh Yes 2017-01-17 23:57
marcio Yes 2017-01-28 01:03
mariano Yes 2017-01-18 07:51
mike Yes 2017-01-31 10:04
nikic Yes 2017-01-18 20:30
ocramius Yes 2017-01-22 01:08
pollita Yes 2017-01-17 22:46
sammyk Yes 2017-01-27 18:28
trowski Yes 2017-01-18 18:05
zimt Yes 2017-01-25 13:29
Merge one or both diffs closed
Frozen
9
Reentrant
6
15 voters
ashnazg Reentrant 2017-01-18 18:47
auroraeosrose Reentrant 2017-01-18 14:47
bishop Reentrant 2017-01-18 13:40
bwoebi Frozen 2017-01-27 14:33
derick Reentrant 2017-01-30 16:25
guilhermeblanco Reentrant 2017-01-18 00:55
krakjoe Frozen 2017-01-18 06:45
leigh Frozen 2017-01-17 23:57
marcio Frozen 2017-01-28 01:03
mariano Frozen 2017-01-18 07:53
mike Frozen 2017-01-31 10:05
nikic Frozen 2017-01-18 01:23
ocramius Reentrant 2017-01-22 01:08
pollita Frozen 2017-01-30 16:20
trowski Frozen 2017-01-27 20:19

Discussions on the mailing list

[RFC] HashContext as Object
by Sara Golemon · 4 messages
[RFC][Vote] HashContext using Objects
by Sara Golemon · 2 messages
[RFC][Accepted] HashContext as Object
by Sara Golemon · 1 message
Convert ext/xml to use an object instead of resource
by Nikita Popov · 13 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

Since PHP5, objects have been the preferred structure for wrapping internal data, however some clod created the hash extension to use resources. This RFC seeks to rectify that error by migrating the Hash extension to use an object implementation for hash contexts instead of a resource.

Proposal

Convert the opaque resource to an opaque object. This is the lightest touch change to ensure that existing code should continue to function unless it has explicit is_resource() checks. These checks can be easily replaced with is_resource|is_object checks instead.

Future Scope

  • Actual oop interface in the form of a few methods: HashContext::__construct() mirroring hash_init, HashContext::update() mirroring hash_update, etc...
  • String casting to invoke `->final()` - Probably a bad idea as the object becomes immutable once it is finalized.
  • Comparison overloading - Handy, but should explicitly use hash_equals() algorithm to avoid timing attacks.
  • Operator overloading (e.g. `$b = $a + 'foo';` ~= `$b = clone $a; $b->update('foo');` - Probably a bad idea, but included for completeness.
  • Serialization/Deserialization - Potential (but unconfirmed) security issues as the internal hash state becomes visible and previously impossible replays may become available.

Backward Incompatible Changes

Any userspace code which explicitly type checks HashContext values using is_resource()/get_resource_type()

Proposed PHP Version(s)

PHP 7.next

Open Issues

Finalizing a hash context currenly invalidates the resource via `zend_list_close()` making all future operations on that value fail. This patch emulates this by clearing the internal value `context` and verifying it in all function invocations using the value. This isn't typical object behavior, but minimizes BC breakage. We could potentially invoke hash_copy() to save the context prior to finalization, finalize it, then revert to the prior state by restoring the saved context. This makes sense to me, but introduces a change to behavior which needs to be discussed.

Proposed Voting Choices

Include one or both patches at https://github.com/php/php-src/pull/2309 50%+1 required.

This primary vote is to determine, overall, if the “hash context” resource used by hash_init/hash_update/hash_final should be changed to an object.

Vote: Change HashContext to an object — Yes: 15 · No: 0 (closed)

This second vote, contingent on passing the first vote, determines which behavior the new HashContext will adopt (per the “Open Issue” above).

  • “Frozen”: Only apply the original patch which will invalidate the HashContext object upon calling hash_final(). This is consistent with the current behavior of the resource, but inconsistent with most class/object definitions in PHP.
  • “Reentrant”: Apply both diffs in PR#2309 which will allow contexts to continue functioning after calling hash_final() by effectively performing: $saved = hash_copy($ctx); $ret = hash_final($ctx); $ctx = $saved;

Vote: Merge one or both diffs — Frozen: 9 · Reentrant: 6 (closed)

Voting opened: 2017-01-17 22:50:00 UTC Voting closes: 2017-01-31 23:59:59 UTC

Patches and Tests