Skip to content

Commit

Permalink
Update docs and allow static calls
Browse files Browse the repository at this point in the history
  • Loading branch information
exussum12 committed Mar 28, 2020
1 parent 66e359d commit 1cd487c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",
"require": {
"phpunit/phpunit": "^7.1",
"php": ">=7.0",
"php": ">=7.1",
"ext-bcmath": "*"
},
"autoload": {
Expand Down
24 changes: 17 additions & 7 deletions src/V32.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions tests/HashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class HashTest extends TestCase
{
/**
* @var V32
*/
protected $hash;

public function setUp()
{
$this->hash = new V32(0);
Expand Down Expand Up @@ -51,4 +56,11 @@ public function testDifferntSeed()
$hash->hash('test')
);
}
public function testStaticCall()
{
$this->assertSame(
'3e2023cf',
V32::hash('test')
);
}
}

0 comments on commit 1cd487c

Please sign in to comment.