A lightweight PHP extension that provides Ethereum-compatible Keccak-256 hashing (padding suffix 0x01
) with minimal overhead.
This is not the NIST SHA3-256 (0x06
) variant. Use hash('sha3-256', …)
for standard SHA3-256.
- Keccak-256 only (Ethereum style,
0x01
padding) - Raw or hex output: 32-byte binary blob or 64-character hex string
- Blazing fast: C wrapper outperforms pure-PHP implementations by ~17×
Function | Padding | Purpose |
---|---|---|
keccak_hash() (this extension) |
0x01 |
Ethereum, smart contracts, dApps |
hash('sha3-256', …) (PHP built-in) |
0x06 |
NIST SHA3-256 standard |
- PHP 8.1 or later
- PHP development headers and tools (
php-dev
,phpize
,autoconf
,make
,gcc
)
On Debian/Ubuntu:
sudo apt-get update
sudo apt-get install -y php-dev autoconf make gcc
-
Clone the repo:
git clone https://github.com/RandomCoderTinker/php-keccak256.git cd php-keccak256
-
Prepare the build:
phpize ./configure
-
Compile and install:
make sudo make install
-
Enable the extension by adding to your
php.ini
:extension=keccak.so
-
Restart your web server or PHP-FPM:
sudo service apache2 restart # or sudo service php8.x-fpm restart
<?php
$data = "hello world";
// Hexadecimal output (default)
$hex = keccak_hash($data);
echo $hex; // e.g. d4ee...a3f
// Raw binary output
$raw = keccak_hash($data, true);
echo bin2hex($raw); // same hex representation
Implementation | Hex Output | Raw Output |
---|---|---|
C extension (keccak_hash ) |
3.119 ms | 2.902 ms |
KornRunner PHP (Keccak::hash ) |
52.729 ms | 50.877 ms |
Benchmarked over 100 iterations on identical hardware. The C extension delivers a ~17× speedup over the pure-PHP KornRunner package.
Many applications—especially blockchain clients, Ethereum tooling, and cryptographic protocols—rely exclusively on the 256-bit Keccak (SHA-3) digest:
- Standardization: Ethereum and many smart-contract platforms specify Keccak-256 as the sole hash function.
- Simplicity: If you don’t need SHA-3-224, SHA-3-384, SHA-3-512, or other ciphers, a focused extension lowers complexity, binary size, and attack surface.
- Performance: A minimal implementation optimizes for one output size; there’s no overhead handling multiple rates or capacities.
By offering just Keccak-256, this extension provides a drop-in, high-performance hash function tailored to the vast ecosystem of applications that only require the 256-bit variant.
Built with ❤️ using PHP 8.1+