diff --git a/README.md b/README.md index 48eeca1..881abc9 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,34 @@ A pure PHP implementation of [xxhash](https://github.com/Cyan4973/xxHash) Currently only working for the 32 bit version. If speed is important use the php extension instead + +# Installing + +Ideally use composer + + composer require exussum12/xxhash + +Alternatively require the single file (making sure the path is correct) + + require 'xxhash/V32.php'; + +# Hashing input + +xxhash has a seed, this is 0 by default. To make a new instance of xxhash run + + $seed = 0; + $hash = new V32($seed); + +Then to hash input, run + + $hash->hash('string'); ## to hasha string + $file = fopen('path/to/file.ext', 'r'); + $hash->hashStream($file); # for a stream (better for large files) + +The library can be called statically also, however this removes the ability to change the seed. The default see of 0 will be used + + V32::hash('string'); ## to hasha string + $file = fopen('path/to/file.ext', 'r'); + V32::hashStream($file); # for a stream (better for large files) + +Static functions should in general be avoided so the first method is the preferred method to use. diff --git a/composer.json b/composer.json index 2a2301a..804d665 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "library", "require": { "phpunit/phpunit": "^7.1", - "php": ">=7.0", + "php": ">=7.1", "ext-bcmath": "*" }, "autoload": { diff --git a/src/V32.php b/src/V32.php index 3b16a1b..a77b86d 100644 --- a/src/V32.php +++ b/src/V32.php @@ -18,21 +18,31 @@ class V32 public function __construct(int $seed = 0) { - $this->seed = $seed; } - public function hash(string $input) + + public function hash(string $input): string { $file = tmpfile(); fwrite($file, $input); rewind($file); + if (!isset($this)) { + $hash = new self(); + return $hash->hashStream($file); + } + return $this->hashStream($file); } - public function hashStream($input) + public function hashStream($input): string { - if (get_resource_type($input) !== "stream") { + if (!isset ($this)) { + $hash = new self(); + return $hash->hashStream($input); + } + + if (get_resource_type($input) !== 'stream') { throw new InvalidArgumentException(); } @@ -70,10 +80,10 @@ public function hashStream($input) return $this->smallInput($part, $accumulator); } - private function smallInput($input, $accumulator) + private function smallInput($input, $accumulator): string { - list($accumulator, $i, $length) = $this->handle4ByteGroups($input, $accumulator); + [$accumulator, $i, $length] = $this->handle4ByteGroups($input, $accumulator); $accumulator = $this->handleLeftOverBytes($input, $accumulator, $length, $i); @@ -128,7 +138,7 @@ public function minus($a, $b):int /** * left shift wrap at 32 bits */ - public function leftShift($x, $bits) + public function leftShift($x, $bits): int { //left shift is a circular shift so shifted off bits become lower bits $circle = $x >> (32-$bits); diff --git a/tests/HashTest.php b/tests/HashTest.php index 7489377..5640005 100644 --- a/tests/HashTest.php +++ b/tests/HashTest.php @@ -6,6 +6,11 @@ class HashTest extends TestCase { + /** + * @var V32 + */ + protected $hash; + public function setUp() { $this->hash = new V32(0); @@ -51,4 +56,11 @@ public function testDifferntSeed() $hash->hash('test') ); } + public function testStaticCall() + { + $this->assertSame( + '3e2023cf', + V32::hash('test') + ); + } }