Sara Golemon

with initial implementation from Rouven Wessling

with initial implementation from Rouven Wessling


PHP RFC: Migration Hash Context from Resource to Object


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.


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.

Patches and Tests


An option needs 50%+1 votes to win

Change HashContext to an object (100% approved)
User Vote
ashnazg Yes
auroraeosrose Yes
bishop Yes
bwoebi Yes
guilhermeblanco Yes
leigh Yes
marcio Yes
mariano Yes
mike Yes
nikic Yes
ocramius Yes
pollita Yes
sammyk Yes
trowski Yes
zimt Yes
Merge one or both diffs (100% approved)
User Vote
ashnazg Reentrant
auroraeosrose Reentrant
bishop Reentrant
bwoebi Frozen
derick Reentrant
guilhermeblanco Reentrant
krakjoe Frozen
leigh Frozen
marcio Frozen
mariano Frozen
mike Frozen
nikic Frozen
ocramius Reentrant
pollita Frozen
trowski Frozen