Installation:
composer require dynamophp/hash-bundle
Ensure DynamoPHP\HashBundle\DynamoPHPHashBundle::class is registered in config/bundles.php.
Basic Configuration:
Add to config/packages/dynamo_php_hash.yaml:
dynamo_php_hash:
start_selection: 3
end_selection: 0
(Default values: start_selection: 3, end_selection: 0 if omitted.)
First Use Case:
Inject DynamoHasherInterface into a controller/service and call hash():
use DynamoPHP\HashBundle\Service\DynamoHasherInterface;
public function demo(DynamoHasherInterface $hasher) {
$result = $hasher->hash('input_string'); // Returns truncated SHA-256 hash
return new Response($result);
}
DynamoHasherInterface (only method: hash(string $input): string).config/packages/dynamo_php_hash.yaml (overridable via environment variables).Hashing User Inputs:
// Truncate SHA-256 to first 3 + last 0 hexits (default)
$hashed = $hasher->hash($userInput);
Use for lightweight hashing (e.g., session tokens, non-sensitive IDs).
Custom Hash Selection: Override config to adjust truncation:
dynamo_php_hash:
start_selection: 2 # First 2 hexits
end_selection: 1 # Last 1 hexit
Re-inject the service to reflect changes.
Dependency Injection:
// Service container alias (if needed)
$this->container->get('dynamo_php_hash.hasher');
Event-Driven Hashing: Dispatch events before/after hashing via Symfony’s event system:
$event = new PreHashEvent($input);
$this->eventDispatcher->dispatch($event, PreHashEvent::NAME);
$hashed = $hasher->hash($event->getInput());
Validator to ensure hash consistency:
$constraint = new Length(['min' => 1, 'max' => 64]); // Adjust based on config
$this->validator->validate($hashed, $constraint);
$cache = $this->cache->get('hash_' . $input, function() use ($hasher, $input) {
return $hasher->hash($input);
});
DynamoHasherInterface in PHPUnit:
$this->mockBuilder->getMockBuilder(DynamoHasherInterface::class)
->disableOriginalConstructor()
->getMock()
->method('hash')
->willReturn('mocked_hash');
Non-Deterministic Output:
start_selection/end_selection) may produce collisions for similar inputs. Avoid using for security-sensitive data (e.g., passwords).Config Overrides:
%env(DYNAMO_HASH_START)%) do not override YAML config by default. Use parameter_bag in services.yaml:
parameters:
dynamo_hash.start_selection: '%env(int:DYNAMO_HASH_START)%'
SHA-256 Dependency:
Service Immutability:
DynamoHasherInterface instance is singleton-scoped. Changes to config require a service restart or re-injection.Unexpected Hash Length:
Verify start_selection + end_selection does not exceed SHA-256’s hex length (64). Example:
# Fails (70 > 64)
dynamo_php_hash:
start_selection: 40
end_selection: 30
Fix: Adjust values or use end_selection: 0 to disable suffix truncation.
Case Sensitivity:
Input strings are case-sensitive. Normalize inputs (e.g., mb_strtolower()) for consistency.
Custom Hasher:
Implement DynamoHasherInterface and register as a service:
services:
App\Service\CustomHasher:
tags: ['dynamo_php_hash.hasher']
Note: Requires modifying the bundle’s DynamoHasher class to support multiple hashers.
Event Listeners: Extend hashing logic via Symfony events:
// src/EventListener/HashListener.php
class HashListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [PreHashEvent::NAME => 'onPreHash'];
}
public function onPreHash(PreHashEvent $event) {
$event->setInput(strtoupper($event->getInput()));
}
}
Configuration Validation:
Add custom validation to config/packages/dynamo_php_hash.yaml:
dynamo_php_hash:
start_selection: 3
end_selection: 0
# Custom constraint (via Symfony Validator)
constraints:
- Symfony\Component\Validator\Constraints\Expression:
expression: "value.start_selection + value.end_selection <= 64"
message: "Total selection must not exceed 64 hexits."
How can I help you explore Laravel packages today?