dynamophp/hash
Generate a deterministic float hash for any string mapped to the range 0–360. Uses a primary hash (e.g., SHA-256), then converts selected leading/trailing characters into a numeric value within the interval.
Hash::generate()) reduces boilerplate, though customization (e.g., hash algorithm or character subsets) requires extending the class.X/Y parameters), but these require manual setup. No built-in validation for edge cases (e.g., empty strings, Unicode normalization).Hashids for arbitrary ranges) suffice?crc32).Str::random() + scaling (e.g., mod 360) achieve similar results with less risk?vlucas/phpcolor) be more maintainable?null, empty strings, Unicode)? Will normalization be required?AppServiceProvider for dependency injection:
$this->app->singleton(HashService::class, function ($app) {
return new \BaptisteContreras\DynamoPHPHash\Hash(
algorithm: $app['config']['hash.algorithm'],
x: $app['config']['hash.x'],
y: $app['config']['hash.y']
);
});
Hash facade for concise usage in Blade, controllers, and services:
// app/Facades/Hash.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Hash extends Facade { public static function generate(string $string): float { ... } }
composer.json autoload for global access:
"autoload": {
"files": ["app/Helpers/hash.php"]
}
config/app.php:
'hash' => [
'algorithm' => env('HASH_ALGORITHM', 'sha256'),
'x' => env('HASH_X', 5),
'y' => env('HASH_Y', 5),
],
$hash = $this->createMock(HashService::class);
$hash->method('generate')->willReturn(180.0);
$this->app->instance(HashService::class, $hash);
$hashes = collect(range(1, 1000))->map(fn($i) => Hash::generate("user_$i"));
$histogram = $hashes->chunk(36)->map(fn($chunk) => $chunk->avg());
rand(0, 360)) with Hash::generate() using Laravel’s replaceInFile() in deploy scripts:
php artisan replace-in-file --file=app/Http/Controllers/ProductController.php --from="rand(0, 360)" --to="Hash::generate($product->id)"
trigger_deprecation('laravel', '1.0', 'Use Hash::generate() instead of rand(0, 360).');
hash_init() polyfills if needed.strict_types=1 enabled.md5), extend the class:
class CustomHash extends Hash {
public function __construct() {
parent::__construct('md5', 4, 4);
}
}
md5 or sha1 for production due to collision risks.Hash::generate(mb_strtolower($string, 'UTF-8'));
X/Y parameters based on precision/entropy needs (e.g., X=8, Y=4 for high entropy).Hash::generate("") → 0.0).'hash' => [
'algorithm' => 'sha256',
'x' => 6, // First 6 characters of hash
'y' => 4, // Last 4 characters of hash
'normalize' => true, // Enable UTF-8 normalization
],
config/app.php and environment variables:
HASH_ALGORITHM=sha256
HASH_X=6
HASH_Y=4
// app/Services/HashService.php
namespace App\Services;
use BaptisteContreras\DynamoPHPHash\Hash as DynamoHash;
class HashService extends DynamoHash {
public function __construct() {
parent::__construct(
config('hash.algorithm'),
config('hash.x'),
How can I help you explore Laravel packages today?