Skip to content

cypherbits/php-blake3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP BLAKE3 Extension

BLAKE3 is an improved and faster version of BLAKE2.

This extension uses the official BLAKE3 C implementation, thus is single-threaded, but still faster than SHA256 or SHA512 on benchmark (PHP 7.4).

Installation (manual)

Clone the repository and compile it:

$ git clone https://github.com/cypherbits/php-blake3.git
$ cd php-blake3
$ phpize
$ ./configure --enable-blake3
$ make && sudo make install

Enable the extension by adding the following line to your php.ini file:

extension=blake3.so

You may need to restart your web server to load the extension.

Usage

Global constants:

BLAKE3_OUT_LEN: 32

Functions:

string blake3 ( string $str [, int $outputSize = 32, string $key, bool $rawOutput = false ] )
  • $str: The string to hash

  • $outputSize: The length of the output hash in bytes (must be >= 1). BLAKE3 supports extendable output so larger values are allowed; the default is 32 bytes.

  • $key: Turns the output into a keyed hash using the specified key. It MUST be of 32 bytes long.

  • $rawOutput: If set to true, then the hash is returned in raw binary format

  • Return value: A hex string containing the BLAKE3 hash of the input string. Default output size: 32 bytes.

string blake3_file ( string $filename [, bool $rawOutput = false ] )
  • $filename: The filename of the file to hash
  • $rawOutput: If set to true, then the hash is returned in raw binary format
  • Return value: A hex string containing the BLAKE3 hash of the input file

Examples

echo blake3('');

af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262

echo blake3('Hello world', 20);

e7e6fb7d2869d109b62cdb1227208d4016cdaa0a

echo blake3('Hello world', 32, 'cae8954e7b3415ea18303db548e15207');

75672fafd13480d2325914f0665795eceecad4e668d9ea2a87c40e71232a7d3a

Benchmarks

<?php

$start = microtime(true);

for ($i=0; $i<1000; $i++){
    $str = md5("hello");
}

$end = microtime(true);

printf("Function call %s took %.5f seconds\n", "md5", $end - $start);

$start = microtime(true);

for ($i=0; $i<1000; $i++){
    $str = sha1("hello");
}

$end = microtime(true);

printf("Function call %s took %.5f seconds\n", "sha1", $end - $start);

$start = microtime(true);

for ($i=0; $i<1000; $i++){
    $str = hash("sha256", "hello");
}

$end = microtime(true);

printf("Function call %s took %.5f seconds\n", "sha256", $end - $start);


$start = microtime(true);

for ($i=0; $i<1000; $i++){
    $str = hash("sha512", "hello");
}

$end = microtime(true);

printf("Function call %s took %.5f seconds\n", "sha512", $end - $start);


$start = microtime(true);

for ($i=0; $i<1000; $i++){
    $str = blake3("hello");
}

$end = microtime(true);

printf("Function call %s took %.5f seconds\n", "blake3", $end - $start);

Results PHP 7.4

As fast as SHA1 but safer.

Function call md5 took 0.00022 seconds
Function call sha1 took 0.00030 seconds
Function call sha256 took 0.00070 seconds
Function call sha512 took 0.00093 seconds
Function call blake3 took 0.00030 seconds

Results PHP 8.0

As fast as SHA1 but safer.

Function call md5 took 0.00018 seconds
Function call sha1 took 0.00028 seconds
Function call sha256 took 0.00059 seconds
Function call sha512 took 0.00076 seconds
Function call blake3 took 0.00028 seconds

Results PHP 8.0 for a random 4MB file (loop of 100 iterations)

On file hashing Blake3 wins even over SHA1!

Function call sha512 took 1.69619 seconds
Function call sha256 took 2.51510 seconds
Function call blake3 took 0.60553 seconds
Function call sha1 took 1.03434 seconds

More Info

https://github.com/BLAKE3-team/BLAKE3

Donate

Bitcoin: bc1q9s3rzpva228m8lux2mkmter67u9gm5j0jxjezj

Monero: 4BCveGZaPM7FejGkhFyHgtjVXZw52RrYxKs7znZdmnWLfB3xDKAW6SkYZPpNhqBvJA8crE8Tug8y7hx8U9KAmq83PwLtVLe

Packagist & PIE Installation

This extension ships with a composer.json compatible with PIE. Once the repository is published on Packagist (for example as cypherbits/php-blake3 and tagged, e.g. v0.1.0), you can install it with PIE:

pie install cypherbits/php-blake3 --enable-blake3

PIE will perform the usual non-Windows extension build steps:

  1. phpize
  2. ./configure --enable-blake3
  3. make
  4. make install
  5. Enable the extension via an INI file (for example extension=blake3).

You can still install it manually without PIE using the steps in the "Installation (manual)" section above.

Publishing to Packagist

  1. Create a tag, for example: git tag v0.1.0 && git push --tags.
  2. Go to https://packagist.org/packages/submit and submit https://github.com/cypherbits/php-blake3.
  3. Optionally configure auto-updates for GitHub so new tags are picked up automatically.
  4. Test in a clean project using PIE: pie install cypherbits/php-blake3 --enable-blake3.

About

PHP Blake3 extension

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 5