Skip to content

Stream verification #25

Open
Open
@sagikazarmark

Description

@sagikazarmark
Member

Utilize stream filters to verify the content of stream.

For example an md5 verification:

class MD5 extends \php_user_filter
{
    private $context;

    public function onCreate()
    {
        $this->context = hash_init('md5');

        return true;
    }

    public function onClose()
    {
        $hash = hash_final($this->context);

        if (false === hash_equals($this->params, $hash)) {
            throw new VerificationException('The stream integrity cannot be verified');
        }

        return true;
    }

    public function filter($in, $out, &$consumed, $closing)
    {
        while ($bucket = stream_bucket_make_writeable($in)) {
            hash_update($this->context, $bucket->data);

            $consumed += $bucket->datalen;

            stream_bucket_append($out, $bucket);
        }

        return PSFS_PASS_ON;
    }
}

This would be useful when downloading files (PHARs for example, when a hash is likely available)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sagikazarmark

        Issue actions

          Stream verification · Issue #25 · php-http/message