Streamable SHA-3 implementation in pure PHP! Native 64-bit optimized
nord-stream a845ba5ca1 Made sample scripts to use optimized implementation, added some benchmarks tests, updated copyright year | il y a 6 ans | |
---|---|---|
namespaced | il y a 6 ans | |
COPYING | il y a 8 ans | |
COPYING.GPL | il y a 8 ans | |
README.markdown | il y a 8 ans | |
SHA3.php | il y a 6 ans | |
bench-224.php | il y a 6 ans | |
bench-256.php | il y a 6 ans | |
bench-384.php | il y a 6 ans | |
bench-512.php | il y a 6 ans | |
bench-php5_2.php | il y a 8 ans | |
bench-shake128-squeeze.php | il y a 6 ans | |
bench-shake256-squeeze.php | il y a 6 ans | |
bench.php | il y a 8 ans | |
example.php | il y a 8 ans | |
sha3-224sum.php | il y a 6 ans | |
sha3-256sum.php | il y a 6 ans | |
sha3-384sum.php | il y a 6 ans | |
sha3-512sum.php | il y a 6 ans | |
tests-namespaced.php | il y a 8 ans | |
tests-sha3-224.php | il y a 8 ans | |
tests-sha3-256.php | il y a 8 ans | |
tests-sha3-384.php | il y a 8 ans | |
tests-sha3-512.php | il y a 8 ans | |
tests-shake128.php | il y a 8 ans | |
tests-shake256.php | il y a 8 ans | |
tests.php | il y a 8 ans |
https://github.com/0xbb/php-sha3
(PHP 5.6.3 or PHP 7.0)Benchmarking result:
PHP 7.0 (64 bit) > PHP 7.0 (32 bit) > PHP 5.6.3 (64 bit) >> PHP 5.6 (32 bit) > PHP 5.2
NEW: Use 64-bit PHP for best performance, because native 64-bit integers will be used. (Only supported on PHP 5.6.3 or later) 64-bit mode is more than 4 times faster in PHP 5.6.
Note that SHA-3 is considably slower than SHA-2 in software, not to mention PHP's notable slowness. PHP is only fast when your program is trivial or I/O-bound.
Namespaced class desktopd\SHA3\Sponge
is available at the namespaced/
directory of this repository. This is the optimized version.
<?php
use desktopd\SHA3\Sponge as SHA3;
require __DIR__ . '/namespaced/desktopd/SHA3/Sponge.php';
$sponge = SHA3::init (SHA3::SHA3_512);
$sponge->absorb ('');
// fixed size (512 bits) output
echo bin2hex ($sponge->squeeze ()), PHP_EOL;
// a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26
static public desktopd\SHA3\Sponge::init ($type) : desktopd\SHA3\Sponge
where $type
is one of:
squeeze()
)squeeze()
)public desktopd\SHA3\Sponge::absorb (string $data) : desktopd\SHA3\Sponge
Add data to hash. You can call this as many times as you need before calling squeeze ()
.
public desktopd\SHA3\Sponge::squeeze ([int $length]) : string
Get hash data. Only specify $length
with SHAKE128 or SHAKE256. With SHAKE128 or SHAKE256, you can call this as many times as you need.
Use SHA3.php
(slower):
<?php
require dirname (__FILE__) . '/SHA3.php';
$shake128 = SHA3::init (SHA3::SHAKE128);
$shake128->absorb ('The quick brown fox ');
$shake128->absorb ('jumps over the lazy dog');
// Squeeze 256 bits of hash output!
echo bin2hex ($shake128->squeeze (32)) . PHP_EOL;
// f4202e3c5852f9182a0430fd8144f0a74b95e7417ecae17db0f8cfeed0e3e66e
// more output possible
This is nearly the fastest in pure PHP. (Without depending on an extension) Long answer: PHP is not good at processing large binary data.
The terms stem from the fact that SHA-3 is based on sponge construction. Read more here.
Please report if you find a problem.
README.markdown: Copyright (C) 2016 Desktopd Developers
Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved.
PHP-SHA3-Streamable is free software. The library (the files SHA3.php
and
namespaced/desktopd/SHA3/Sponge.php
) is under GNU LGPL version 3 or later.
Other codes are under GNU GPL version 3 or later.
See the files COPYING.LGPL (GNU LGPL) and COPYING (GNU GPL) for copying
conditions.