php-standard-library/hash
Hash utilities for PHP: cryptographic and non-cryptographic hashing via an Algorithm enum, HMAC helpers, and timing-safe string comparison. Lightweight package from PHP Standard Library for consistent, secure hashing across projects.
Install via Composer:
composer require php-standard-library/hash
Basic Usage:
use PhpStandardLibrary\Hash\HashGenerator;
$generator = new HashGenerator();
$hash = $generator->generate('my-secret-string');
// Outputs: e.g., "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
First Use Case:
bcrypt or argon2i).
$hash = $generator->generate('user_password', 'argon2i');
Hash Generation:
sha256 by default for general-purpose hashing.md5, sha512, bcrypt) to generate().
$hash = $generator->generate($input, 'bcrypt', ['cost' => 12]);
$hash = $generator->generate(file_get_contents('file.bin'), 'sha1');
Secure Comparison:
HashComparator to mitigate timing attacks when verifying hashes (e.g., passwords).
use PhpStandardLibrary\Hash\HashComparator;
$comparator = new HashComparator();
$isMatch = $comparator->compare($storedHash, $input, 'argon2i');
Integration with Laravel:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(HashGenerator::class);
$this->app->singleton(HashComparator::class);
}
// app/Helpers/HashHelper.php
function hashString($string, $algorithm = 'sha256')
{
return app(HashGenerator::class)->generate($string, $algorithm);
}
Configuration:
// config/hash.php
return [
'default_algorithm' => 'argon2i',
'argon2_options' => ['memory_cost' => 65536],
];
HashGenerator constructor or via dependency injection.Algorithm Limitations:
md5, sha1) for security-sensitive data (e.g., passwords). Use bcrypt or argon2i instead.Timing Attacks:
HashComparator for verification, never === or hash_equals() directly. Example of unsafe code:
// UNSAFE: Timing attack risk
if (hash('sha256', $input) === $storedHash) { ... }
HashComparator::compare().Stateful Algorithms:
bcrypt or argon2i require unique salts per input. The library handles this automatically, but ensure you’re not reusing salts manually.Output Formatting:
hex2bin() or raw binary handling.
$binaryHash = hex2bin($generator->generate('data'));
Performance:
xxh3 or crc32.microtime() for your workload:
$start = microtime(true);
$generator->generate('large_input', 'sha256');
echo microtime(true) - $start; // ~0.0001s for 1KB input
Extending the Library:
PhpStandardLibrary\Hash\Contracts\AlgorithmInterface for new hashing methods.
class CustomAlgorithm implements AlgorithmInterface {
public function hash($data): string { ... }
public function verify($data, $hash): bool { ... }
}
HashGenerator:
$generator->addAlgorithm('custom', new CustomAlgorithm());
Testing:
HashGenerator in unit tests to avoid flaky tests due to hash variability.
$mockGenerator = Mockery::mock(HashGenerator::class);
$mockGenerator->shouldReceive('generate')->andReturn('mocked_hash');
sha256) for test data to ensure reproducibility.Laravel-Specific:
$hashedKey = app(HashGenerator::class)->generate(request('api_key'));
remember_token) during migration:
use Illuminate\Support\Facades\Hash as LaravelHash;
use PhpStandardLibrary\Hash\HashGenerator;
$hashedToken = app(HashGenerator::class)->generate(str_random(60));
How can I help you explore Laravel packages today?