2017 © Pedro Peláez
 

library xxhash

xxHash implementation in Pure PHP

image

exussum12/xxhash

xxHash implementation in Pure PHP

  • Friday, July 13, 2018
  • by exussum
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

A pure PHP implementation of xxhash, (*1)

Currently only working for the 32 bit version. (Pre PHP 7.4). 32 and 64 bit version both work with PHP 7.4, (*2)

If speed is important use the FFI versions (PHP 7.4+), (*3)

XXHash is a fast hash designed for file integrity checking. Passwords should not be hashed with this, please use Argon2 or BCrypt., (*4)

On PHP 8.1 xxhash is included by default, please use that instead, (*5)

hash('xxh32',  'string');

for string mode or, (*6)

$context = hash_init('xxh3');
hash_update($context, 'String1'); // from fgets or similar
hash_update($context, 'String2'); // from fgets or similar
hash_final($context);

Installing

With composer, (*7)

composer require exussum12/xxhash

Hashing input

xxhash has a seed, this is 0 by default. To make a new instance of xxhash run, (*8)

use exussum12\xxhash\V32;
$seed = 0;
$hash = new V32($seed);

Then to hash input, run, (*9)

$hash->hash('string'); ## to hash a string

or, (*10)

$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, (*11)

V32::hash('string'); ## to hash a 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., (*12)

FFI

Since PHP 7.4 FFI allows PHP to call the native C client. This is much faster, and the preferred way if your running PHP 7.4, (*13)

Speed Comparison

This is hashing a 320mb file using the stream method. The time is the time take (smaller is better), (*14)

Method Time Peak Memory
xxHash Binary 0.081 1604kb
FFI 0.194 27616kb
Pure PHP 49.218 27844kb

Memory measured using /usr/bin/time -v, (*15)

The Versions

13/07 2018

dev-master

9999999-dev

xxHash implementation in Pure PHP

  Sources   Download

BSD-2-Clause

The Requires

 

by Scott Dutton