Git Product home page Git Product logo

xxhash's Introduction

A pure PHP implementation of xxhash

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

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

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

On PHP 8.1 xxhash is included by default, please use that instead

hash('xxh32',  'string');

for string mode or

$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

composer require exussum12/xxhash

Hashing input

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

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

Then to hash input, run

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

or

$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 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.

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

Speed Comparison

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

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

xxhash's People

Contributors

exussum12 avatar yurunsoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

xxhash's Issues

xxhash 64 without ffi

hi is there anyway to make xxhash 64 support without the use of ffi? the hosting i use doesn't let me use ffi. i personally don't care if speeds will be increased.

128bit support

Could you implement support for the XXH128 algorithm?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.