Merge pull request #11 from rullzer/add_hash_wrapper

Added a simple hash wrapper
This commit is contained in:
Robin Appelman 2019-03-12 10:32:37 +01:00 committed by GitHub
commit 59d8a6ab2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 168 additions and 2 deletions

90
src/HashWrapper.php Normal file
View file

@ -0,0 +1,90 @@
<?php
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace Icewind\Streams;
/**
* Wrapper that calculates the hash on the stream on read
*
* The stream and hash should be passed in when wrapping the stream.
* On close the callback will be called with the calculated checksum.
*
* For supported hashes see: http://php.net/manual/en/function.hash-algos.php
*/
class HashWrapper extends Wrapper {
/**
* @var callable
*/
private $callback;
/**
* @var resource
*/
private $hashContext;
/**
* Wraps a stream to make it seekable
*
* @param resource $source
* @param string $hash
* @param callable $callback
* @return resource
*
* @throws \BadMethodCallException
*/
public static function wrap($source, $hash, $callback) {
$context = array(
'hash' => $hash,
'callback' => $callback,
);
return self::wrapSource($source, $context);
}
public function dir_opendir($path, $options) {
return false;
}
public function stream_open($path, $mode, $options, &$opened_path) {
$context = $this->loadContext();
$this->callback = $context['callback'];
$this->hashContext = hash_init($context['hash']);
return true;
}
public function stream_read($count) {
$data = parent::stream_read($count);
if ($this->hashContext !== false) {
hash_update($this->hashContext, $data);
}
return $data;
}
public function stream_close() {
$hash = hash_final($this->hashContext);
if ($this->hashContext !== false && is_callable($this->callback)) {
call_user_func($this->callback, $hash);
}
return parent::stream_close();
}
}

View file

@ -60,7 +60,10 @@ class WrapperHandler {
$class = static::class; $class = static::class;
} }
$protocol = self::getProtocol($class); if ($protocol === null) {
$protocol = self::getProtocol($class);
}
$context = self::buildContext($protocol, $context, $source); $context = self::buildContext($protocol, $context, $source);
try { try {
stream_wrapper_register($protocol, $class); stream_wrapper_register($protocol, $class);
@ -106,4 +109,4 @@ class WrapperHandler {
} }
return $context; return $context;
} }
} }

73
tests/HashWrapperTest.php Normal file
View file

@ -0,0 +1,73 @@
<?php
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace Icewind\Streams\Tests;
class HashWrapperTest extends \PHPUnit_Framework_TestCase {
/**
* @param resource $source
* @param string $hash
* @param callable $callback
* @return resource
*/
protected function wrapSource($source, $hash, $callback) {
return \Icewind\Streams\HashWrapper::wrap($source, $hash, $callback);
}
protected function getSource() {
$source = fopen('php://temp', 'w+');
fwrite($source, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.');
fseek($source, 0);
return $source;
}
public function hashData() {
return [
['md5', '818c6e601a24f72750da0f6c9b8ebe28'],
['sha1', 'cca0871ecbe200379f0a1e4b46de177e2d62e655'],
['sha256', '973153f86ec2da1748e63f0cf85b89835b42f8ee8018c549868a1308a19f6ca3'],
['sha512', '83cd8866be238eda447cb0ee94a6bfa6248109346b1ce3c75f8a67d35f3d8ab1697b46703065c094fcc7d3a61acc1e8ee85a4f306f13cc1a7aea7651781199b3'],
];
}
/**
* @dataProvider hashData
*
* @param string $algorithm
* @param string $expectedHash
*/
public function testHash($algorithm, $expectedHash) {
$obtainedHash = null;
$callback = function($hash) use (&$obtainedHash) {
$obtainedHash = $hash;
};
$stream = $this->wrapSource($this->getSource(), $algorithm, $callback);
while(feof($stream) === false) {
fread($stream, 20);
}
fclose($stream);
$this->assertSame($expectedHash, $obtainedHash);
}
}