kornrunner/keccak
Pure PHP Keccak (SHA-3) implementation with easy static API. Compute Keccak hashes (224/256/384/512) and SHAKE outputs (XOF) without extensions. Includes test suite and coverage, suitable for Ethereum and other crypto use cases.
Install via Composer:
composer require kornrunner/keccak
First use case: Generate a SHA-3 hash for a blockchain-related feature (e.g., Ethereum address derivation):
use kornrunner\Keccak;
// Keccak-256 (common for Ethereum)
$hash = Keccak::hash('wallet_data', 256);
Where to look first:
Extend Laravel’s Hash facade to support Keccak:
// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Hash;
use kornrunner\Keccak;
Hash::extend('keccak', function() {
return function($value, array $options = []) {
$rounds = $options['rounds'] ?? 256;
return Keccak::hash($value, $rounds);
};
});
Usage:
// In controllers/services
$hash = Hash::driver('keccak')->make('data');
Bind the package to Laravel’s container for dependency injection:
// config/app.php
'aliases' => [
'Keccak' => kornrunner\Keccak::class,
],
Usage:
use Illuminate\Support\Facades\Keccak;
$hash = Keccak::hash('data', 256);
Use Keccak for salt derivation or custom hash rounds:
$salt = Keccak::hash(str_random(32), 128); // 128-bit salt
$passwordHash = Hash::make($password, ['rounds' => 12]);
Generate Ethereum-compatible hashes:
// Keccak-256 for wallet addresses
$addressHash = Keccak::hash($walletData, 256);
$address = '0x' . substr($addressHash, -40);
Use SHAKE for custom-length hashes (e.g., signatures):
$customHash = Keccak::shake('data', 128, 512); // 128-bit output from 512-bit SHAKE
Verify file/database record integrity:
$expectedHash = Keccak::hash($originalData, 256);
$actualHash = Keccak::hash($retrievedData, 256);
if ($expectedHash !== $actualHash) {
throw new \RuntimeException('Data corruption detected!');
}
Cache hashes for static data (e.g., config, assets):
$hash = Cache::remember("keccak_{$data}", 3600, function() use ($data) {
return Keccak::hash($data, 256);
});
Use queues for bulk hashing (e.g., migrating records):
// Dispatch a job
HashBulkJob::dispatch($records);
// Job class
public function handle() {
foreach ($this->records as $record) {
$record->hash = Keccak::hash($record->data, 256);
$record->save();
}
}
Fallback to OpenSSL if available:
function hybridHash($data, $rounds = 256) {
if (extension_loaded('openssl')) {
return hash('sha3-' . $rounds, $data);
}
return Keccak::hash($data, $rounds);
}
Test hash outputs against known values:
public function testKeccak256() {
$hash = Keccak::hash('', 256);
$this->assertEquals(
'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
$hash
);
}
Verify Laravel facade integration:
public function testHashFacade() {
Hash::extend('keccak', function() { ... });
$hash = Hash::driver('keccak')->make('test');
$this->assertEquals(Keccak::hash('test', 256), $hash);
}
Performance Overhead
sha3_*.microtime(true) before production use. Avoid in loops or high-throughput scenarios.Memory Limits
memory_limit.memory_limit in php.ini.No Constant-Time Comparison
hash_equals() for sensitive comparisons:
if (!hash_equals($expected, $actual)) { ... }
Stale Maintenance
SHAKE Misuse
SHAKE is a XOF (eXtendable-Output Function), not a fixed hash. Output length must be specified.shake($data, 128, 256)).Non-String Inputs
$data = is_string($data) ? $data : json_encode($data);
Verify Outputs
$this->assertEquals(
'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
Keccak::hash('', 256)
);
Memory Issues
xdebug.memory_enable=1 in php.ini to track memory usage:
$start = memory_get_usage();
$hash = Keccak::hash(file_get_contents('large_file'), 256);
$end = memory_get_usage();
$used = $end - $start; // Check if >100MB
Performance Profiling
$time = microtime(true);
$hash = Keccak::hash(str_repeat('a', 1000), 256);
$duration = microtime(true) - $time; // Should be >1ms
Edge Cases
Keccak::hash('', 256)).Keccak::hash('🚀', 256)).Keccak::hash(file_get_contents('image.png'), 256)).Laravel Hash Driver
Hash facade, ensure the driver name doesn’t conflict with existing drivers (e.g., bcrypt, argon2).keccak_sha3.PHP Version Compatibility
string return types).Autoloading
composer.json autoloads the package:
"autoload": {
"
How can I help you explore Laravel packages today?